From a426812cccf4ead88de16eb62c8b8f865a3348e3 Mon Sep 17 00:00:00 2001 From: Jesse Rosenthal Date: Fri, 8 Aug 2014 16:17:40 -0400 Subject: OMath parser: Change signature of exported function. This changes the signature of the exported `readOMML` to `String -> Either String [Exp]`, so it can now, in theory, be slotted into TeXMath. It doesn't have any real error reporting yet, but that might make more sense once I put it in a branch, and understand how it works in the other readers. It also now reads strings that parse to either oMath or oMathPara elements. Note that the distinction is lost in the output. It's up to the caller to remember the display type. --- src/Text/Pandoc/Readers/Docx/OMath.hs | 17 +++++++++++++---- src/Text/Pandoc/Readers/Docx/Parse.hs | 10 +++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) (limited to 'src/Text/Pandoc/Readers/Docx') diff --git a/src/Text/Pandoc/Readers/Docx/OMath.hs b/src/Text/Pandoc/Readers/Docx/OMath.hs index 62fc6286c..47f8dd197 100644 --- a/src/Text/Pandoc/Readers/Docx/OMath.hs +++ b/src/Text/Pandoc/Readers/Docx/OMath.hs @@ -38,11 +38,20 @@ import Data.Maybe (mapMaybe, fromMaybe) import Data.List (intersperse) import qualified Text.TeXMath.Types as TM -readOMML :: Element -> Maybe [TM.Exp] -readOMML element | isElem "m" "oMath" element = +readOMML :: String -> Either String [TM.Exp] +readOMML s | Just e <- parseXMLDoc s = + case elemToOMML e of + Just exs -> Right exs + Nothing -> Left "xml file was not an or element." +readOMML _ = Left "Couldn't parse OMML file" + +elemToOMML :: Element -> Maybe [TM.Exp] +elemToOMML element | isElem "m" "oMathPara" element = do + let expList = mapMaybe elemToOMML (elChildren element) + return $ map (\l -> if length l == 1 then (head l) else TM.EGrouped l) expList +elemToOMML element | isElem "m" "oMath" element = Just $ concat $ mapMaybe (elemToExps') (elChildren element) -readOMML _ = Nothing - +elemToOMML _ = Nothing isElem :: String -> String -> Element -> Bool isElem prefix name element = diff --git a/src/Text/Pandoc/Readers/Docx/Parse.hs b/src/Text/Pandoc/Readers/Docx/Parse.hs index 8c9b4d672..beb58fed2 100644 --- a/src/Text/Pandoc/Readers/Docx/Parse.hs +++ b/src/Text/Pandoc/Readers/Docx/Parse.hs @@ -86,6 +86,10 @@ maybeToD :: Maybe a -> D a maybeToD (Just a) = return a maybeToD Nothing = throwError DocxError +eitherToD :: Either a b -> D b +eitherToD (Right b) = return b +eitherToD (Left _) = throwError DocxError + concatMapM :: (Monad m) => (a -> m [b]) -> [a] -> m [b] concatMapM f xs = liftM concat (mapM f xs) @@ -150,7 +154,7 @@ defaultParagraphStyle = ParagraphStyle { pStyle = [] data BodyPart = Paragraph ParagraphStyle [ParPart] | ListItem ParagraphStyle String String Level [ParPart] | Tbl String TblGrid TblLook [Row] - | OMathPara [[Exp]] + | OMathPara [Exp] deriving Show type TblGrid = [Integer] @@ -475,7 +479,7 @@ elemToBodyPart ns element | isElem ns "w" "p" element , (c:_) <- findChildren (elemName ns "m" "oMathPara") element = do - expsLst <- mapD (\e -> (maybeToD $ readOMML e)) (elChildren c) + expsLst <- eitherToD $ readOMML $ showElement c return $ OMathPara expsLst elemToBodyPart ns element | isElem ns "w" "p" element @@ -575,7 +579,7 @@ elemToParPart ns element Nothing -> ExternalHyperLink "" runs elemToParPart ns element | isElem ns "m" "oMath" element = - (maybeToD $ readOMML element) >>= (return . PlainOMath) + (eitherToD $ readOMML $ showElement element) >>= (return . PlainOMath) elemToParPart _ _ = throwError WrongElem lookupFootnote :: String -> Notes -> Maybe Element -- cgit v1.2.3