git.iko.soy/typeable/pandoc
on master help sign in
Docx writer: move a couple more vars to ReaderT

In general, we want things that are either: 1. unchanging environment variables, or 2. environment variables that will change for a the scope of a function and then pop back to be in the reader monad. This is safer for (1), since we won't accidentally change it, and easier for (2), since we can use `local` instad of setting the old value and then resetting. We keep the StateT monad for values that we will want to accumulate or change and then use later.

commit1893f8fe489332e52aa1913144ef644093604a53 authorJesse Rosenthal <jrosenthal@jhu.edu> dateTue 4 Oct 2016, 08:54 (10 years ago) parent3f9a0a8
1 file changed+29−21
src/Text/Pandoc/Writers/Docx.hs+29−21
src/Text/Pandoc/Writers/Docx.hs+29−21
@@ -97,6 +97,10 @@ data WriterEnv = WriterEnv{ envTextProperties :: [Element]
9797 , envRTL :: Bool
9898 , envListLevel :: Int
9999 , envListNumId :: Int
100+ , envInDel :: Bool
101+ , envChangesAuthor :: String
102+ , envChangesDate :: String
103+
100104 }
101105
102106defaultWriterEnv :: WriterEnv
@@ -105,6 +109,9 @@ defaultWriterEnv = WriterEnv{ envTextProperties = []
105109 , envRTL = False
106110 , envListLevel = -1
107111 , envListNumId = 1
112+ , envInDel = False
113+ , envChangesAuthor = "unknown"
114+ , envChangesDate = "1969-12-31T19:00:00Z"
108115 }
109116
110117data WriterState = WriterState{
@@ -115,9 +122,6 @@ data WriterState = WriterState{
115122 , stLists :: [ListMarker]
116123 , stInsId :: Int
117124 , stDelId :: Int
118− , stInDel :: Bool
119− , stChangesAuthor :: String
120− , stChangesDate :: String
121125 , stPrintWidth :: Integer
122126 , stStyleMaps :: StyleMaps
123127 , stFirstPara :: Bool
@@ -135,9 +139,6 @@ defaultWriterState = WriterState{
135139 , stLists = [NoMarker]
136140 , stInsId = 1
137141 , stDelId = 1
138− , stInDel = False
139− , stChangesAuthor = "unknown"
140− , stChangesDate = "1969-12-31T19:00:00Z"
141142 , stPrintWidth = 1
142143 , stStyleMaps = defaultStyleMaps
143144 , stFirstPara = False
@@ -257,14 +258,23 @@ writeDocx opts doc@(Pandoc meta _) = do
257258 let tocTitle = fromMaybe (stTocTitle defaultWriterState) $
258259 metaValueToInlines <$> lookupMeta "toc-title" meta
259260
261+ let initialSt = defaultWriterState {
262+ stPrintWidth = (maybe 420 (\x -> quot x 20) pgContentWidth)
263+ , stStyleMaps = styleMaps
264+ , stTocTitle = tocTitle
265+ }
266+
267+ let env = defaultWriterEnv {
268+ envChangesAuthor = fromMaybe "unknown" username
269+ , envChangesDate = formatTime defaultTimeLocale "%FT%XZ" utctime
270+ }
271+
272+
260273 ((contents, footnotes), st) <- runStateT
261− (runReaderT (writeOpenXML opts{writerWrapText = WrapNone} doc') defaultWriterEnv)
262− defaultWriterState{ stChangesAuthor = fromMaybe "unknown" username
263− , stChangesDate = formatTime defaultTimeLocale "%FT%XZ" utctime
264− , stPrintWidth = (maybe 420 (\x -> quot x 20) pgContentWidth)
265− , stStyleMaps = styleMaps
266− , stTocTitle = tocTitle
267− }
274+ (runReaderT
275+ (writeOpenXML opts{writerWrapText = WrapNone} doc')
276+ env)
277+ initialSt
268278 let epochtime = floor $ utcTimeToPOSIXSeconds utctime
269279 let imgs = M.elems $ stImages st
270280
@@ -1006,7 +1016,7 @@ withParaPropM = (. flip withParaProp) . (>>=)
10061016formattedString :: String -> WS [Element]
10071017formattedString str = do
10081018 props <- getTextProps
1009− inDel <- gets stInDel
1019+ inDel <- asks envInDel
10101020 return [ mknode "w:r" [] $
10111021 props ++
10121022 [ mknode (if inDel then "w:delText" else "w:t")
@@ -1033,8 +1043,8 @@ inlineToOpenXML opts (Span (ident,classes,kvs) ils)
10331043 let kvs' = filter (("dir", "ltr")/=) kvs
10341044 setLTR $ inlineToOpenXML opts (Span (ident,classes,kvs') ils)
10351045 | "insertion" `elem` classes = do
1036− defaultAuthor <- gets stChangesAuthor
1037− defaultDate <- gets stChangesDate
1046+ defaultAuthor <- asks envChangesAuthor
1047+ defaultDate <- asks envChangesDate
10381048 let author = fromMaybe defaultAuthor (lookup "author" kvs)
10391049 date = fromMaybe defaultDate (lookup "date" kvs)
10401050 insId <- gets stInsId
@@ -1045,15 +1055,13 @@ inlineToOpenXML opts (Span (ident,classes,kvs) ils)
10451055 ("w:date", date)]
10461056 x ]
10471057 | "deletion" `elem` classes = do
1048− defaultAuthor <- gets stChangesAuthor
1049− defaultDate <- gets stChangesDate
1058+ defaultAuthor <- asks envChangesAuthor
1059+ defaultDate <- asks envChangesDate
10501060 let author = fromMaybe defaultAuthor (lookup "author" kvs)
10511061 date = fromMaybe defaultDate (lookup "date" kvs)
10521062 delId <- gets stDelId
10531063 modify $ \s -> s{stDelId = (delId + 1)}
1054− modify $ \s -> s{stInDel = True}
1055− x <- inlinesToOpenXML opts ils
1056− modify $ \s -> s{stInDel = False}
1064+ x <- local (\env -> env {envInDel = True}) (inlinesToOpenXML opts ils)
10571065 return [ mknode "w:del" [("w:id", (show delId)),
10581066 ("w:author", author),
10591067 ("w:date", date)]