git.iko.soy/typeable/pandoc
on master help sign in
Docx Reader: Add state to the parser, for warnings

In order to be able to collect warnings during parsing, we add a state monad transformer to the D monad. At the moment, this only includes a list of warning strings (nothing currently triggers them, however). We use StateT instead of WriterT to correspond more closely with the warnings behavior in T.P.Parsing.

commit102ba9ecb869da80fac03480b2dd03a695a4f78c authorJesse Rosenthal <jrosenthal@jhu.edu> dateThu 10 Mar 2016, 15:19 (11 years ago) parenta485c42
1 file changed+19−6
src/Text/Pandoc/Readers/Docx/Parse.hs+19−6
src/Text/Pandoc/Readers/Docx/Parse.hs+19−6
@@ -50,6 +50,7 @@ module Text.Pandoc.Readers.Docx.Parse ( Docx(..)
5050 , Row(..)
5151 , Cell(..)
5252 , archiveToDocx
53+ , archiveToDocxWithWarnings
5354 ) where
5455import Codec.Archive.Zip
5556import Text.XML.Light
@@ -60,6 +61,7 @@ import Data.Bits ((.|.))
6061import qualified Data.ByteString.Lazy as B
6162import qualified Text.Pandoc.UTF8 as UTF8
6263import Control.Monad.Reader
64+import Control.Monad.State
6365import Control.Applicative ((<|>))
6466import qualified Data.Map as M
6567import Text.Pandoc.Compat.Except
@@ -81,16 +83,20 @@ data ReaderEnv = ReaderEnv { envNotes :: Notes
8183 }
8284 deriving Show
8385
86+data ReaderState = ReaderState { stateWarnings :: [String] }
87+ deriving Show
88+
89+
8490data DocxError = DocxError | WrongElem
8591 deriving Show
8692
8793instance Error DocxError where
8894 noMsg = WrongElem
8995
90−type D = ExceptT DocxError (Reader ReaderEnv)
96+type D = ExceptT DocxError (ReaderT ReaderEnv (State ReaderState))
9197
92−runD :: D a -> ReaderEnv -> Either DocxError a
93−runD dx re = runReader (runExceptT dx) re
98+runD :: D a -> ReaderEnv -> ReaderState -> (Either DocxError a, ReaderState)
99+runD dx re rs = runState (runReaderT (runExceptT dx) re) rs
94100
95101maybeToD :: Maybe a -> D a
96102maybeToD (Just a) = return a
@@ -257,7 +263,10 @@ type Author = String
257263type ChangeDate = String
258264
259265archiveToDocx :: Archive -> Either DocxError Docx
260−archiveToDocx archive = do
266+archiveToDocx archive = fst <$> archiveToDocxWithWarnings archive
267+
268+archiveToDocxWithWarnings :: Archive -> Either DocxError (Docx, [String])
269+archiveToDocxWithWarnings archive = do
261270 let notes = archiveToNotes archive
262271 numbering = archiveToNumbering archive
263272 rels = archiveToRelationships archive
@@ -265,8 +274,12 @@ archiveToDocx archive = do
265274 (styles, parstyles) = archiveToStyles archive
266275 rEnv =
267276 ReaderEnv notes numbering rels media Nothing styles parstyles InDocument
268− doc <- runD (archiveToDocument archive) rEnv
269− return $ Docx doc
277+ rState = ReaderState { stateWarnings = [] }
278+ (eitherDoc, st) = runD (archiveToDocument archive) rEnv rState
279+ case eitherDoc of
280+ Right doc -> Right (Docx doc, stateWarnings st)
281+ Left e -> Left e
282+
270283
271284
272285archiveToDocument :: Archive -> D Document