git.iko.soy/typeable/pandoc
on master help sign in
Move more enviroment vars to Reader Monad.

Things that get pushed and then reset are better in ReaderT, because they can be run with `local`.

commita2d3854f232dc0a15fe09c9f4460ed2bc097dc30 authorJesse Rosenthal <jrosenthal@jhu.edu> dateMon 3 Oct 2016, 12:12 (10 years ago) parent6a3d1cf
1 file changed+52−62
src/Text/Pandoc/Writers/Docx.hs+52−62
src/Text/Pandoc/Writers/Docx.hs+52−62
@@ -92,20 +92,28 @@ listMarkerToId (NumberMarker sty delim n) =
9292 OneParen -> '2'
9393 TwoParens -> '3'
9494
95−data WriterEnv = WriterEnv{ envRTL :: Bool }
95+data WriterEnv = WriterEnv{ envTextProperties :: [Element]
96+ , envParaProperties :: [Element]
97+ , envRTL :: Bool
98+ , envListLevel :: Int
99+ , envListNumId :: Int
100+ }
96101
97102defaultWriterEnv :: WriterEnv
98−defaultWriterEnv = WriterEnv{ envRTL = False }
103+defaultWriterEnv = WriterEnv{ envTextProperties = []
104+ , envParaProperties = []
105+ , envRTL = False
106+ , envListLevel = -1
107+ , envListNumId = 1
108+ }
99109
100110data WriterState = WriterState{
101− stTextProperties :: [Element]
102− , stParaProperties :: [Element]
103− , stFootnotes :: [Element]
111+ stFootnotes :: [Element]
104112 , stSectionIds :: Set.Set String
105113 , stExternalLinks :: M.Map String String
106114 , stImages :: M.Map FilePath (String, String, Maybe MimeType, Element, B.ByteString)
107− , stListLevel :: Int
108− , stListNumId :: Int
115+ -- , stListLevel :: Int
116+ -- , stListNumId :: Int
109117 , stLists :: [ListMarker]
110118 , stInsId :: Int
111119 , stDelId :: Int
@@ -122,14 +130,12 @@ data WriterState = WriterState{
122130
123131defaultWriterState :: WriterState
124132defaultWriterState = WriterState{
125− stTextProperties = []
126− , stParaProperties = []
127− , stFootnotes = defaultFootnotes
133+ stFootnotes = defaultFootnotes
128134 , stSectionIds = Set.empty
129135 , stExternalLinks = M.empty
130136 , stImages = M.empty
131− , stListLevel = -1
132− , stListNumId = 1
137+ -- , stListLevel = -1
138+ -- , stListNumId = 1
133139 , stLists = [NoMarker]
134140 , stInsId = 1
135141 , stDelId = 1
@@ -809,12 +815,11 @@ blockToOpenXML opts (Plain lst) = withParaProp (pCustomStyle "Compact")
809815-- title beginning with fig: indicates that the image is a figure
810816blockToOpenXML opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) = do
811817 setFirstPara
812− pushParaProp $ pCustomStyle $
813− if null alt
814− then "Figure"
815− else "FigureWithCaption"
816− paraProps <- getParaProps False
817− popParaProp
818+ let prop = pCustomStyle $
819+ if null alt
820+ then "Figure"
821+ else "FigureWithCaption"
822+ paraProps <- local (\env -> env { envParaProperties = prop : envParaProperties env }) (getParaProps False)
818823 contents <- inlinesToOpenXML opts [Image attr alt (src,tit)]
819824 captionNode <- withParaProp (pCustomStyle "ImageCaption")
820825 $ blockToOpenXML opts (Para alt)
@@ -952,49 +957,36 @@ inlinesToOpenXML :: WriterOptions -> [Inline] -> WS [Element]
952957inlinesToOpenXML opts lst = concat `fmap` mapM (inlineToOpenXML opts) lst
953958
954959withNumId :: Int -> WS a -> WS a
955−withNumId numid p = do
956− origNumId <- gets stListNumId
957− modify $ \st -> st{ stListNumId = numid }
958− result <- p
959− modify $ \st -> st{ stListNumId = origNumId }
960− return result
960+withNumId numid = local $ \env -> env{ envListNumId = numid }
961961
962962asList :: WS a -> WS a
963−asList p = do
964− origListLevel <- gets stListLevel
965− modify $ \st -> st{ stListLevel = stListLevel st + 1 }
966− result <- p
967− modify $ \st -> st{ stListLevel = origListLevel }
968− return result
963+asList = local $ \env -> env{ envListLevel = envListLevel env + 1 }
969964
970965getTextProps :: WS [Element]
971966getTextProps = do
972− props <- gets stTextProperties
967+ props <- asks envTextProperties
973968 return $ if null props
974969 then []
975970 else [mknode "w:rPr" [] props]
976971
977−pushTextProp :: Element -> WS ()
978−pushTextProp d = modify $ \s -> s{ stTextProperties = d : stTextProperties s }
972+-- pushTextProp :: Element -> WS ()
973+-- pushTextProp d = modify $ \s -> s{ stTextProperties = d : stTextProperties s }
979974
980−popTextProp :: WS ()
981−popTextProp = modify $ \s -> s{ stTextProperties = drop 1 $ stTextProperties s }
975+-- popTextProp :: WS ()
976+-- popTextProp = modify $ \s -> s{ stTextProperties = drop 1 $ stTextProperties s }
982977
983978withTextProp :: Element -> WS a -> WS a
984−withTextProp d p = do
985− pushTextProp d
986− res <- p
987− popTextProp
988− return res
979+withTextProp d p =
980+ local (\env -> env {envTextProperties = d : envTextProperties env}) p
989981
990982withTextPropM :: WS Element -> WS a -> WS a
991983withTextPropM = (. flip withTextProp) . (>>=)
992984
993985getParaProps :: Bool -> WS [Element]
994986getParaProps displayMathPara = do
995− props <- gets stParaProperties
996− listLevel <- gets stListLevel
997− numid <- gets stListNumId
987+ props <- asks envParaProperties
988+ listLevel <- asks envListLevel
989+ numid <- asks envListNumId
998990 let listPr = if listLevel >= 0 && not displayMathPara
999991 then [ mknode "w:numPr" []
1000992 [ mknode "w:numId" [("w:val",show numid)] ()
@@ -1005,18 +997,9 @@ getParaProps displayMathPara = do
1005997 [] -> []
1006998 ps -> [mknode "w:pPr" [] ps]
1007999
1008−pushParaProp :: Element -> WS ()
1009−pushParaProp d = modify $ \s -> s{ stParaProperties = d : stParaProperties s }
1010−
1011−popParaProp :: WS ()
1012−popParaProp = modify $ \s -> s{ stParaProperties = drop 1 $ stParaProperties s }
1013−
10141000withParaProp :: Element -> WS a -> WS a
1015−withParaProp d p = do
1016− pushParaProp d
1017− res <- p
1018− popParaProp
1019− return res
1001+withParaProp d p =
1002+ local (\env -> env {envParaProperties = d : envParaProperties env}) p
10201003
10211004withParaPropM :: WS Element -> WS a -> WS a
10221005withParaPropM = (. flip withParaProp) . (>>=)
@@ -1135,14 +1118,12 @@ inlineToOpenXML opts (Note bs) = do
11351118 let insertNoteRef (Plain ils : xs) = Plain (notemarkerXml : Space : ils) : xs
11361119 insertNoteRef (Para ils : xs) = Para (notemarkerXml : Space : ils) : xs
11371120 insertNoteRef xs = Para [notemarkerXml] : xs
1138− oldListLevel <- gets stListLevel
1139− oldParaProperties <- gets stParaProperties
1140− oldTextProperties <- gets stTextProperties
1141− modify $ \st -> st{ stListLevel = -1, stParaProperties = [], stTextProperties = [] }
1142− contents <- withParaPropM (pStyleM "Footnote Text") $ blocksToOpenXML opts
1143− $ insertNoteRef bs
1144− modify $ \st -> st{ stListLevel = oldListLevel, stParaProperties = oldParaProperties,
1145− stTextProperties = oldTextProperties }
1121+
1122+ contents <- local (\env -> env{ envListLevel = -1
1123+ , envParaProperties = []
1124+ , envTextProperties = [] })
1125+ (withParaPropM (pStyleM "Footnote Text") $ blocksToOpenXML opts
1126+ $ insertNoteRef bs)
11461127 let newnote = mknode "w:footnote" [("w:id", notenum)] $ contents
11471128 modify $ \s -> s{ stFootnotes = newnote : notes }
11481129 return [ mknode "w:r" []
@@ -1274,3 +1255,12 @@ fitToPage (x, y) pageWidth
12741255 | x > fromIntegral pageWidth =
12751256 (pageWidth, floor $ ((fromIntegral pageWidth) / x) * y)
12761257 | otherwise = (floor x, floor y)
1258+
1259+-- setRTL :: WS a -> WS a
1260+-- setRTL = do
1261+-- isRTL <- asks envRTL
1262+-- if isRTL
1263+-- then id
1264+-- else (withParaProp (mknode "w:bidi" [] ()) . withTextProp (mknode "w:rtl" [] ()))
1265+
1266+-- setLTR :: WS a -> WS a