git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1503 from jkr/streamlineMath

OMath parser: Change signature of exported function.

commitaae90a8671d4fdb46ede2669e6713a559b9d3fbb authorJohn MacFarlane <jgm@berkeley.edu> dateFri 8 Aug 2014, 13:45 (12 years ago) parentf723a05a426812
3 files changed+24−12
src/Text/Pandoc/Readers/Docx.hs+4−5 src/Text/Pandoc/Readers/Docx/OMath.hs+13−4 src/Text/Pandoc/Readers/Docx/Parse.hs+7−3
src/Text/Pandoc/Readers/Docx.hs+4−5
@@ -321,7 +321,7 @@ runToInlines (InlineDrawing fp bs) = do
321321 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
322322 return [Image [] (fp, "")]
323323
324−
324+
325325
326326
327327parPartToInlines :: ParPart -> DocxContext [Inline]
@@ -507,10 +507,9 @@ bodyPartToBlocks (Tbl cap _ look (r:rs)) = do
507507 widths = replicate size 0 :: [Double]
508508
509509 return [Table caption alignments widths hdrCells cells]
510−bodyPartToBlocks (OMathPara exps) = do
511− return [Para $
512− map (\e -> Math DisplayMath (writeTeX e))
513− exps]
510+bodyPartToBlocks (OMathPara e) = do
511+ return [Para [Math DisplayMath (writeTeX e)]]
512+
514513
515514-- replace targets with generated anchors.
516515rewriteLink :: Inline -> DocxContext Inline
src/Text/Pandoc/Readers/Docx/OMath.hs+13−4
@@ -38,11 +38,20 @@ import Data.Maybe (mapMaybe, fromMaybe)
3838import Data.List (intersperse)
3939import qualified Text.TeXMath.Types as TM
4040
41−readOMML :: Element -> Maybe [TM.Exp]
42−readOMML element | isElem "m" "oMath" element =
41+readOMML :: String -> Either String [TM.Exp]
42+readOMML s | Just e <- parseXMLDoc s =
43+ case elemToOMML e of
44+ Just exs -> Right exs
45+ Nothing -> Left "xml file was not an <m:oMathPara> or <m:oMath> element."
46+readOMML _ = Left "Couldn't parse OMML file"
47+
48+elemToOMML :: Element -> Maybe [TM.Exp]
49+elemToOMML element | isElem "m" "oMathPara" element = do
50+ let expList = mapMaybe elemToOMML (elChildren element)
51+ return $ map (\l -> if length l == 1 then (head l) else TM.EGrouped l) expList
52+elemToOMML element | isElem "m" "oMath" element =
4353 Just $ concat $ mapMaybe (elemToExps') (elChildren element)
44−readOMML _ = Nothing
45−
54+elemToOMML _ = Nothing
4655
4756isElem :: String -> String -> Element -> Bool
4857isElem prefix name element =
src/Text/Pandoc/Readers/Docx/Parse.hs+7−3
@@ -86,6 +86,10 @@ maybeToD :: Maybe a -> D a
8686maybeToD (Just a) = return a
8787maybeToD Nothing = throwError DocxError
8888
89+eitherToD :: Either a b -> D b
90+eitherToD (Right b) = return b
91+eitherToD (Left _) = throwError DocxError
92+
8993concatMapM :: (Monad m) => (a -> m [b]) -> [a] -> m [b]
9094concatMapM f xs = liftM concat (mapM f xs)
9195
@@ -150,7 +154,7 @@ defaultParagraphStyle = ParagraphStyle { pStyle = []
150154data BodyPart = Paragraph ParagraphStyle [ParPart]
151155 | ListItem ParagraphStyle String String Level [ParPart]
152156 | Tbl String TblGrid TblLook [Row]
153− | OMathPara [[Exp]]
157+ | OMathPara [Exp]
154158 deriving Show
155159
156160type TblGrid = [Integer]
@@ -475,7 +479,7 @@ elemToBodyPart ns element
475479 | isElem ns "w" "p" element
476480 , (c:_) <- findChildren (elemName ns "m" "oMathPara") element =
477481 do
478− expsLst <- mapD (\e -> (maybeToD $ readOMML e)) (elChildren c)
482+ expsLst <- eitherToD $ readOMML $ showElement c
479483 return $ OMathPara expsLst
480484elemToBodyPart ns element
481485 | isElem ns "w" "p" element
@@ -575,7 +579,7 @@ elemToParPart ns element
575579 Nothing -> ExternalHyperLink "" runs
576580elemToParPart ns element
577581 | isElem ns "m" "oMath" element =
578− (maybeToD $ readOMML element) >>= (return . PlainOMath)
582+ (eitherToD $ readOMML $ showElement element) >>= (return . PlainOMath)
579583elemToParPart _ _ = throwError WrongElem
580584
581585lookupFootnote :: String -> Notes -> Maybe Element