git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1529 from jkr/dunning-fixes

Fixes from contributed document

commit095d9dc6beca2465e00e7e5693098666849b29c6 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 12 Aug 2014, 21:51 (12 years ago) parent5b1d841a1320a7
4 files changed+75−36
data/templates+1−1 src/Text/Pandoc/Readers/Docx.hs+37−15 src/Text/Pandoc/Readers/Docx/Parse.hs+35−20 src/Text/Pandoc/Readers/Docx/Reducible.hs+2
data/templates+1−1

Submodule changed.

src/Text/Pandoc/Readers/Docx.hs+37−15
@@ -202,17 +202,30 @@ runStyleToContainers rPr =
202202 [Container $ (\ils -> Code ("", [], []) (concatMap ilToCode ils))]
203203 spanClassToContainers s | s `elem` spansToKeep =
204204 [Container $ Span ("", [s], [])]
205− spanClassToContainers _ = []
205+ spanClassToContainers _ = []
206206
207207 classContainers = case rStyle rPr of
208208 Nothing -> []
209209 Just s -> spanClassToContainers s
210210
211+ resolveFmt :: Bool -> Maybe Bool -> Bool
212+ resolveFmt _ (Just True) = True
213+ resolveFmt _ (Just False) = False
214+ resolveFmt bool Nothing = bool
215+
211216 formatters = map Container $ mapMaybe id
212− [ if isBold rPr then (Just Strong) else Nothing
213− , if isItalic rPr then (Just Emph) else Nothing
214− , if isSmallCaps rPr then (Just SmallCaps) else Nothing
215− , if isStrike rPr then (Just Strikeout) else Nothing
217+ [ if resolveFmt (rStyle rPr == Just "Bold") (isBold rPr)
218+ then (Just Strong)
219+ else Nothing
220+ , if resolveFmt (rStyle rPr == Just "Italic") (isItalic rPr)
221+ then (Just Emph)
222+ else Nothing
223+ , if resolveFmt False (isSmallCaps rPr)
224+ then (Just SmallCaps)
225+ else Nothing
226+ , if resolveFmt False (isStrike rPr)
227+ then (Just Strikeout)
228+ else Nothing
216229 , if isSuperScript rPr then (Just Superscript) else Nothing
217230 , if isSubScript rPr then (Just Subscript) else Nothing
218231 , rUnderline rPr >>=
@@ -272,7 +285,7 @@ codeSpans :: [String]
272285codeSpans = ["VerbatimChar"]
273286
274287blockQuoteDivs :: [String]
275−blockQuoteDivs = ["Quote", "BlockQuote"]
288+blockQuoteDivs = ["Quote", "BlockQuote", "BlockQuotation"]
276289
277290codeDivs :: [String]
278291codeDivs = ["SourceCode"]
@@ -389,8 +402,7 @@ parPartToInlines (PlainOMath exps) = do
389402
390403
391404isAnchorSpan :: Inline -> Bool
392−isAnchorSpan (Span (ident, classes, kvs) ils) =
393− (not . null) ident &&
405+isAnchorSpan (Span (_, classes, kvs) ils) =
394406 classes == ["anchor"] &&
395407 null kvs &&
396408 null ils
@@ -403,14 +415,16 @@ makeHeaderAnchor :: Block -> DocxContext Block
403415-- If there is an anchor already there (an anchor span in the header,
404416-- to be exact), we rename and associate the new id with the old one.
405417makeHeaderAnchor (Header n (_, classes, kvs) ils)
406− | (x : xs) <- filter isAnchorSpan ils
407− , (Span (ident, _, _) _) <- x
408− , notElem ident dummyAnchors =
418+ | xs <- filter isAnchorSpan ils
419+ , idents <- filter (\i -> notElem i dummyAnchors) $
420+ map (\(Span (ident, _, _) _) -> ident) xs
421+ , not $ null idents =
409422 do
410423 hdrIDMap <- gets docxAnchorMap
411424 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
412− modify $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
413− return $ Header n (newIdent, classes, kvs) (ils \\ (x:xs))
425+ newMap = M.fromList $ map (\i -> (i, newIdent)) idents
426+ modify $ \s -> s {docxAnchorMap = M.union newMap hdrIDMap}
427+ return $ Header n (newIdent, classes, kvs) (ils \\ xs)
414428-- Otherwise we just give it a name, and register that name (associate
415429-- it with itself.)
416430makeHeaderAnchor (Header n (_, classes, kvs) ils) =
@@ -441,6 +455,13 @@ isHeaderContainer :: Container Block -> Bool
441455isHeaderContainer (Container f) | Header _ _ _ <- f [] = True
442456isHeaderContainer _ = False
443457
458+trimLineBreaks :: [Inline] -> [Inline]
459+trimLineBreaks [] = []
460+trimLineBreaks (LineBreak : ils) = trimLineBreaks ils
461+trimLineBreaks ils
462+ | (LineBreak : ils') <- reverse ils = trimLineBreaks (reverse ils')
463+trimLineBreaks ils = ils
464+
444465bodyPartToBlocks :: BodyPart -> DocxContext [Block]
445466bodyPartToBlocks (Paragraph pPr parparts)
446467 | any isBlockCodeContainer (parStyleToContainers pPr) =
@@ -453,8 +474,9 @@ bodyPartToBlocks (Paragraph pPr parparts)
453474 [CodeBlock ("", [], []) (concatMap parPartToString parparts)]
454475bodyPartToBlocks (Paragraph pPr parparts)
455476 | any isHeaderContainer (parStyleToContainers pPr) = do
456− ils <- normalizeSpaces <$> local (\s -> s{docxInHeaderBlock = True})
457− (parPartsToInlines parparts)
477+ ils <- (trimLineBreaks . normalizeSpaces) <$>
478+ local (\s -> s{docxInHeaderBlock = True})
479+ (parPartsToInlines parparts)
458480 let (Container hdrFun) = head $ filter isHeaderContainer (parStyleToContainers pPr)
459481 Header n attr _ = hdrFun []
460482 hdr <- makeHeaderAnchor $ Header n attr ils
src/Text/Pandoc/Readers/Docx/Parse.hs+35−20
@@ -196,10 +196,10 @@ data Run = Run RunStyle [RunElem]
196196data RunElem = TextRun String | LnBrk | Tab
197197 deriving Show
198198
199−data RunStyle = RunStyle { isBold :: Bool
200− , isItalic :: Bool
201− , isSmallCaps :: Bool
202− , isStrike :: Bool
199+data RunStyle = RunStyle { isBold :: Maybe Bool
200+ , isItalic :: Maybe Bool
201+ , isSmallCaps :: Maybe Bool
202+ , isStrike :: Maybe Bool
203203 , isSuperScript :: Bool
204204 , isSubScript :: Bool
205205 , rUnderline :: Maybe String
@@ -207,10 +207,10 @@ data RunStyle = RunStyle { isBold :: Bool
207207 deriving Show
208208
209209defaultRunStyle :: RunStyle
210−defaultRunStyle = RunStyle { isBold = False
211− , isItalic = False
212− , isSmallCaps = False
213− , isStrike = False
210+defaultRunStyle = RunStyle { isBold = Nothing
211+ , isItalic = Nothing
212+ , isSmallCaps = Nothing
213+ , isStrike = Nothing
214214 , isSuperScript = False
215215 , isSubScript = False
216216 , rUnderline = Nothing
@@ -652,30 +652,45 @@ elemToParagraphStyle ns element
652652 }
653653elemToParagraphStyle _ _ = defaultParagraphStyle
654654
655+checkOnOff :: NameSpaces -> Element -> QName -> Maybe Bool
656+checkOnOff ns rPr tag
657+ | Just t <- findChild tag rPr
658+ , Just val <- findAttr (elemName ns "w" "val") t =
659+ Just $ case val of
660+ "true" -> True
661+ "false" -> False
662+ "on" -> True
663+ "off" -> False
664+ "1" -> True
665+ "0" -> False
666+ _ -> False
667+ | Just _ <- findChild tag rPr = Just True
668+checkOnOff _ _ _ = Nothing
669+
655670
656671elemToRunStyle :: NameSpaces -> Element -> RunStyle
657672elemToRunStyle ns element
658673 | Just rPr <- findChild (elemName ns "w" "rPr") element =
659674 RunStyle
660675 {
661− isBold = isJust $ findChild (QName "b" (lookup "w" ns) (Just "w")) rPr
662− , isItalic = isJust $ findChild (QName "i" (lookup "w" ns) (Just "w")) rPr
663− , isSmallCaps = isJust $ findChild (QName "smallCaps" (lookup "w" ns) (Just "w")) rPr
664− , isStrike = isJust $ findChild (QName "strike" (lookup "w" ns) (Just "w")) rPr
676+ isBold = checkOnOff ns rPr (elemName ns "w" "b")
677+ , isItalic = checkOnOff ns rPr (elemName ns "w" "i")
678+ , isSmallCaps = checkOnOff ns rPr (elemName ns "w" "smallCaps")
679+ , isStrike = checkOnOff ns rPr (elemName ns "w" "strike")
665680 , isSuperScript =
666681 (Just "superscript" ==
667− (findChild (QName "vertAlign" (lookup "w" ns) (Just "w")) rPr >>=
668− findAttr (QName "val" (lookup "w" ns) (Just "w"))))
682+ (findChild (elemName ns "w" "vertAlign") rPr >>=
683+ findAttr (elemName ns "w" "val")))
669684 , isSubScript =
670685 (Just "subscript" ==
671− (findChild (QName "vertAlign" (lookup "w" ns) (Just "w")) rPr >>=
672− findAttr (QName "val" (lookup "w" ns) (Just "w"))))
686+ (findChild (elemName ns "w" "vertAlign") rPr >>=
687+ findAttr (elemName ns "w" "val")))
673688 , rUnderline =
674− findChild (QName "u" (lookup "w" ns) (Just "w")) rPr >>=
675− findAttr (QName "val" (lookup "w" ns) (Just "w"))
689+ findChild (elemName ns "w" "u") rPr >>=
690+ findAttr (elemName ns "w" "val")
676691 , rStyle =
677− findChild (QName "rStyle" (lookup "w" ns) (Just "w")) rPr >>=
678− findAttr (QName "val" (lookup "w" ns) (Just "w"))
692+ findChild (elemName ns "w" "rStyle") rPr >>=
693+ findAttr (elemName ns "w" "val")
679694 }
680695elemToRunStyle _ _ = defaultRunStyle
681696
src/Text/Pandoc/Readers/Docx/Reducible.hs+2
@@ -138,6 +138,7 @@ instance Reducible Inline where
138138
139139 container (Emph _) = Container Emph
140140 container (Strong _) = Container Strong
141+ container (SmallCaps _) = Container SmallCaps
141142 container (Strikeout _) = Container Strikeout
142143 container (Subscript _) = Container Subscript
143144 container (Superscript _) = Container Superscript
@@ -147,6 +148,7 @@ instance Reducible Inline where
147148 container _ = NullContainer
148149
149150 innards (Emph ils) = ils
151+ innards (SmallCaps ils) = ils
150152 innards (Strong ils) = ils
151153 innards (Strikeout ils) = ils
152154 innards (Subscript ils) = ils