git.iko.soy/typeable/pandoc
on master help sign in
Moved fixDisplayMath from Docx writer to Writer.Shared.
e3d48daJohn MacFarlane
src/Text/Pandoc/Writers/Docx.hs+2−25
@@ -30,7 +30,7 @@ Conversion of 'Pandoc' documents to docx.
30-}
30-}
31module Text.Pandoc.Writers.Docx ( writeDocx ) where
31module Text.Pandoc.Writers.Docx ( writeDocx ) where
32import Data.Maybe (fromMaybe)
32import Data.Maybe (fromMaybe)
33−import Data.List ( intercalate, groupBy, isPrefixOf )
33+import Data.List ( intercalate, isPrefixOf )
34import qualified Data.ByteString as B
34import qualified Data.ByteString as B
35import qualified Data.ByteString.Lazy as BL
35import qualified Data.ByteString.Lazy as BL
36import qualified Data.ByteString.Lazy.Char8 as BL8
36import qualified Data.ByteString.Lazy.Char8 as BL8
@@ -43,6 +43,7 @@ import Text.Pandoc.Definition
43import Text.Pandoc.Generic
43import Text.Pandoc.Generic
44import Text.Pandoc.ImageSize
44import Text.Pandoc.ImageSize
45import Text.Pandoc.Shared hiding (Element)
45import Text.Pandoc.Shared hiding (Element)
46+import Text.Pandoc.Writers.Shared (fixDisplayMath)
46import Text.Pandoc.Options
47import Text.Pandoc.Options
47import Text.Pandoc.Readers.TeXMath
48import Text.Pandoc.Readers.TeXMath
48import Text.Pandoc.Highlighting ( highlight )
49import Text.Pandoc.Highlighting ( highlight )
@@ -817,27 +818,3 @@ parseXml refArchive relpath =
817 case (findEntryByPath relpath refArchive >>= parseXMLDoc . UTF8.toStringLazy . fromEntry) of
818 case (findEntryByPath relpath refArchive >>= parseXMLDoc . UTF8.toStringLazy . fromEntry) of
818 Just d -> return d
819 Just d -> return d
819 Nothing -> fail $ relpath ++ " missing in reference docx"
820 Nothing -> fail $ relpath ++ " missing in reference docx"
820−
821−isDisplayMath :: Inline -> Bool
822−isDisplayMath (Math DisplayMath _) = True
823−isDisplayMath _ = False
824−
825−stripLeadingTrailingSpace :: [Inline] -> [Inline]
826−stripLeadingTrailingSpace = go . reverse . go . reverse
827− where go (Space:xs) = xs
828− go xs = xs
829−
830−fixDisplayMath :: Block -> Block
831−fixDisplayMath (Plain lst)
832− | any isDisplayMath lst && not (all isDisplayMath lst) =
833− -- chop into several paragraphs so each displaymath is its own
834− Div ("",["math"],[]) $ map (Plain . stripLeadingTrailingSpace) $
835− groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
836− not (isDisplayMath x || isDisplayMath y)) lst
837−fixDisplayMath (Para lst)
838− | any isDisplayMath lst && not (all isDisplayMath lst) =
839− -- chop into several paragraphs so each displaymath is its own
840− Div ("",["math"],[]) $ map (Para . stripLeadingTrailingSpace) $
841− groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
842− not (isDisplayMath x || isDisplayMath y)) lst
843−fixDisplayMath x = x
src/Text/Pandoc/Writers/Shared.hs+27
@@ -34,6 +34,7 @@ module Text.Pandoc.Writers.Shared (
34 , setField
34 , setField
35 , defField
35 , defField
36 , tagWithAttrs
36 , tagWithAttrs
37+ , fixDisplayMath
37 )
38 )
38where
39where
39import Text.Pandoc.Definition
40import Text.Pandoc.Definition
@@ -46,6 +47,7 @@ import qualified Data.Map as M
46import qualified Data.Text as T
47import qualified Data.Text as T
47import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..))
48import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..))
48import qualified Data.Traversable as Traversable
49import qualified Data.Traversable as Traversable
50+import Data.List ( groupBy )
49
51
50-- | Create JSON value for template from a 'Meta' and an association list
52-- | Create JSON value for template from a 'Meta' and an association list
51-- of variables, specified at the command line or in the writer.
53-- of variables, specified at the command line or in the writer.
@@ -136,3 +138,28 @@ tagWithAttrs tag (ident,classes,kvs) = hsep
136 ,hsep (map (\(k,v) -> text k <> "=" <>
138 ,hsep (map (\(k,v) -> text k <> "=" <>
137 doubleQuotes (text (escapeStringForXML v))) kvs)
139 doubleQuotes (text (escapeStringForXML v))) kvs)
138 ] <> ">"
140 ] <> ">"
141+
142+isDisplayMath :: Inline -> Bool
143+isDisplayMath (Math DisplayMath _) = True
144+isDisplayMath _ = False
145+
146+stripLeadingTrailingSpace :: [Inline] -> [Inline]
147+stripLeadingTrailingSpace = go . reverse . go . reverse
148+ where go (Space:xs) = xs
149+ go xs = xs
150+
151+-- Put display math in its own block (for ODT/DOCX).
152+fixDisplayMath :: Block -> Block
153+fixDisplayMath (Plain lst)
154+ | any isDisplayMath lst && not (all isDisplayMath lst) =
155+ -- chop into several paragraphs so each displaymath is its own
156+ Div ("",["math"],[]) $ map (Plain . stripLeadingTrailingSpace) $
157+ groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
158+ not (isDisplayMath x || isDisplayMath y)) lst
159+fixDisplayMath (Para lst)
160+ | any isDisplayMath lst && not (all isDisplayMath lst) =
161+ -- chop into several paragraphs so each displaymath is its own
162+ Div ("",["math"],[]) $ map (Para . stripLeadingTrailingSpace) $
163+ groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
164+ not (isDisplayMath x || isDisplayMath y)) lst
165+fixDisplayMath x = x