git.iko.soy/typeable/pandoc
on master help sign in
Docx writer: Fixed rendering of display math in lists.

In 1.11 and 1.11.1, display math in lists rendered as a new list item. Now it always appears centered, just as outside of lists, and in proper display math style, no matter how far indented the containing list item is. Closes #784.

commitd596b0db8321cdb9c018ac8037d301291d0cc63c authorJohn MacFarlane <fiddlosopher@gmail.com> dateMon 18 Mar 2013, 19:31 (14 years ago) parentb3661ad
1 file changed+27−13
src/Text/Pandoc/Writers/Docx.hs+27−13
src/Text/Pandoc/Writers/Docx.hs+27−13
@@ -105,12 +105,13 @@ writeDocx :: WriterOptions -- ^ Writer options
105105 -> IO BL.ByteString
106106writeDocx opts doc@(Pandoc (Meta tit auths date) _) = do
107107 let datadir = writerUserDataDir opts
108+ let doc' = bottomUp (concatMap fixDisplayMath) doc
108109 refArchive <- liftM (toArchive . toLazy) $
109110 case writerReferenceDocx opts of
110111 Just f -> B.readFile f
111112 Nothing -> readDataFile datadir "reference.docx"
112113
113− ((contents, footnotes), st) <- runStateT (writeOpenXML opts{writerWrapText = False} doc)
114+ ((contents, footnotes), st) <- runStateT (writeOpenXML opts{writerWrapText = False} doc')
114115 defaultWriterState
115116 epochtime <- floor `fmap` getPOSIXTime
116117 let imgs = M.elems $ stImages st
@@ -420,19 +421,17 @@ blockToOpenXML opts (Header lev (ident,_,_) lst) = do
420421blockToOpenXML opts (Plain lst) = blockToOpenXML opts (Para lst)
421422-- title beginning with fig: indicates that the image is a figure
422423blockToOpenXML opts (Para [Image alt (src,'f':'i':'g':':':tit)]) = do
423− paraProps <- getParaProps
424+ paraProps <- getParaProps False
424425 contents <- inlinesToOpenXML opts [Image alt (src,tit)]
425426 captionNode <- withParaProp (pStyle "ImageCaption")
426427 $ blockToOpenXML opts (Para alt)
427428 return $ mknode "w:p" [] (paraProps ++ contents) : captionNode
428−blockToOpenXML opts (Para lst)
429− | any isDisplayMath lst && not (all isDisplayMath lst) = do
430− -- chop into several paragraphs so each displaymath is its own
431− let lsts = groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
432− not (isDisplayMath x || isDisplayMath y)) lst
433− blocksToOpenXML opts (map Para lsts)
434− | otherwise = do
435− paraProps <- getParaProps
429+-- fixDisplayMath sometimes produces a Para [] as artifact
430+blockToOpenXML _ (Para []) = return []
431+blockToOpenXML opts (Para lst) = do
432+ paraProps <- getParaProps $ case lst of
433+ [Math DisplayMath _] -> True
434+ _ -> False
436435 contents <- inlinesToOpenXML opts lst
437436 return [mknode "w:p" [] (paraProps ++ contents)]
438437blockToOpenXML _ (RawBlock format str)
@@ -571,12 +570,12 @@ withTextProp d p = do
571570 popTextProp
572571 return res
573572
574−getParaProps :: WS [Element]
575−getParaProps = do
573+getParaProps :: Bool -> WS [Element]
574+getParaProps displayMathPara = do
576575 props <- gets stParaProperties
577576 listLevel <- gets stListLevel
578577 numid <- gets stListNumId
579− let listPr = if listLevel >= 0
578+ let listPr = if listLevel >= 0 && not displayMathPara
580579 then [ mknode "w:numPr" []
581580 [ mknode "w:numId" [("w:val",show numid)] ()
582581 , mknode "w:ilvl" [("w:val",show listLevel)] () ]
@@ -775,3 +774,18 @@ parseXml refArchive relpath =
775774isDisplayMath :: Inline -> Bool
776775isDisplayMath (Math DisplayMath _) = True
777776isDisplayMath _ = False
777+
778+stripLeadingTrailingSpace :: [Inline] -> [Inline]
779+stripLeadingTrailingSpace = go . reverse . go . reverse
780+ where go (Space:xs) = xs
781+ go xs = xs
782+
783+fixDisplayMath :: Block -> [Block]
784+fixDisplayMath (Plain lst) = fixDisplayMath (Para lst)
785+fixDisplayMath (Para lst)
786+ | any isDisplayMath lst && not (all isDisplayMath lst) =
787+ -- chop into several paragraphs so each displaymath is its own
788+ map (Para . stripLeadingTrailingSpace) $
789+ groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
790+ not (isDisplayMath x || isDisplayMath y)) lst
791+fixDisplayMath x = [x]