git.iko.soy/typeable/pandoc
on master help sign in
Move more enviroment vars to Reader Monad.
a2d3854Jesse Rosenthal
src/Text/Pandoc/Writers/Docx.hs+52−62
@@ -92,20 +92,28 @@ listMarkerToId (NumberMarker sty delim n) =
92 OneParen -> '2'
92 OneParen -> '2'
93 TwoParens -> '3'
93 TwoParens -> '3'
94
94
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+ }
96
101
97defaultWriterEnv :: WriterEnv
102defaultWriterEnv :: WriterEnv
98−defaultWriterEnv = WriterEnv{ envRTL = False }
103+defaultWriterEnv = WriterEnv{ envTextProperties = []
104+ , envParaProperties = []
105+ , envRTL = False
106+ , envListLevel = -1
107+ , envListNumId = 1
108+ }
99
109
100data WriterState = WriterState{
110data WriterState = WriterState{
101− stTextProperties :: [Element]
102− , stParaProperties :: [Element]
103− , stFootnotes :: [Element]
111+ stFootnotes :: [Element]
104 , stSectionIds :: Set.Set String
112 , stSectionIds :: Set.Set String
105 , stExternalLinks :: M.Map String String
113 , stExternalLinks :: M.Map String String
106 , stImages :: M.Map FilePath (String, String, Maybe MimeType, Element, B.ByteString)
114 , stImages :: M.Map FilePath (String, String, Maybe MimeType, Element, B.ByteString)
107− , stListLevel :: Int
115+ -- , stListLevel :: Int
108− , stListNumId :: Int
116+ -- , stListNumId :: Int
109 , stLists :: [ListMarker]
117 , stLists :: [ListMarker]
110 , stInsId :: Int
118 , stInsId :: Int
111 , stDelId :: Int
119 , stDelId :: Int
@@ -122,14 +130,12 @@ data WriterState = WriterState{
122
130
123defaultWriterState :: WriterState
131defaultWriterState :: WriterState
124defaultWriterState = WriterState{
132defaultWriterState = WriterState{
125− stTextProperties = []
126− , stParaProperties = []
127− , stFootnotes = defaultFootnotes
133+ stFootnotes = defaultFootnotes
128 , stSectionIds = Set.empty
134 , stSectionIds = Set.empty
129 , stExternalLinks = M.empty
135 , stExternalLinks = M.empty
130 , stImages = M.empty
136 , stImages = M.empty
131− , stListLevel = -1
137+ -- , stListLevel = -1
132− , stListNumId = 1
138+ -- , stListNumId = 1
133 , stLists = [NoMarker]
139 , stLists = [NoMarker]
134 , stInsId = 1
140 , stInsId = 1
135 , stDelId = 1
141 , stDelId = 1
@@ -809,12 +815,11 @@ blockToOpenXML opts (Plain lst) = withParaProp (pCustomStyle "Compact")
809-- title beginning with fig: indicates that the image is a figure
815-- title beginning with fig: indicates that the image is a figure
810blockToOpenXML opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) = do
816blockToOpenXML opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) = do
811 setFirstPara
817 setFirstPara
812− pushParaProp $ pCustomStyle $
818+ let prop = pCustomStyle $
813− if null alt
819+ if null alt
814− then "Figure"
820+ then "Figure"
815− else "FigureWithCaption"
821+ else "FigureWithCaption"
816− paraProps <- getParaProps False
822+ paraProps <- local (\env -> env { envParaProperties = prop : envParaProperties env }) (getParaProps False)
817− popParaProp
818 contents <- inlinesToOpenXML opts [Image attr alt (src,tit)]
823 contents <- inlinesToOpenXML opts [Image attr alt (src,tit)]
819 captionNode <- withParaProp (pCustomStyle "ImageCaption")
824 captionNode <- withParaProp (pCustomStyle "ImageCaption")
820 $ blockToOpenXML opts (Para alt)
825 $ blockToOpenXML opts (Para alt)
@@ -952,49 +957,36 @@ inlinesToOpenXML :: WriterOptions -> [Inline] -> WS [Element]
952inlinesToOpenXML opts lst = concat `fmap` mapM (inlineToOpenXML opts) lst
957inlinesToOpenXML opts lst = concat `fmap` mapM (inlineToOpenXML opts) lst
953
958
954withNumId :: Int -> WS a -> WS a
959withNumId :: Int -> WS a -> WS a
955−withNumId numid p = do
960+withNumId numid = local $ \env -> env{ envListNumId = numid }
956− origNumId <- gets stListNumId
957− modify $ \st -> st{ stListNumId = numid }
958− result <- p
959− modify $ \st -> st{ stListNumId = origNumId }
960− return result
961
961
962asList :: WS a -> WS a
962asList :: WS a -> WS a
963−asList p = do
963+asList = local $ \env -> env{ envListLevel = envListLevel env + 1 }
964− origListLevel <- gets stListLevel
965− modify $ \st -> st{ stListLevel = stListLevel st + 1 }
966− result <- p
967− modify $ \st -> st{ stListLevel = origListLevel }
968− return result
969
964
970getTextProps :: WS [Element]
965getTextProps :: WS [Element]
971getTextProps = do
966getTextProps = do
972− props <- gets stTextProperties
967+ props <- asks envTextProperties
973 return $ if null props
968 return $ if null props
974 then []
969 then []
975 else [mknode "w:rPr" [] props]
970 else [mknode "w:rPr" [] props]
976
971
977−pushTextProp :: Element -> WS ()
972+-- pushTextProp :: Element -> WS ()
978−pushTextProp d = modify $ \s -> s{ stTextProperties = d : stTextProperties s }
973+-- pushTextProp d = modify $ \s -> s{ stTextProperties = d : stTextProperties s }
979
974
980−popTextProp :: WS ()
975+-- popTextProp :: WS ()
981−popTextProp = modify $ \s -> s{ stTextProperties = drop 1 $ stTextProperties s }
976+-- popTextProp = modify $ \s -> s{ stTextProperties = drop 1 $ stTextProperties s }
982
977
983withTextProp :: Element -> WS a -> WS a
978withTextProp :: Element -> WS a -> WS a
984−withTextProp d p = do
979+withTextProp d p =
985− pushTextProp d
980+ local (\env -> env {envTextProperties = d : envTextProperties env}) p
986− res <- p
987− popTextProp
988− return res
989
981
990withTextPropM :: WS Element -> WS a -> WS a
982withTextPropM :: WS Element -> WS a -> WS a
991withTextPropM = (. flip withTextProp) . (>>=)
983withTextPropM = (. flip withTextProp) . (>>=)
992
984
993getParaProps :: Bool -> WS [Element]
985getParaProps :: Bool -> WS [Element]
994getParaProps displayMathPara = do
986getParaProps displayMathPara = do
995− props <- gets stParaProperties
987+ props <- asks envParaProperties
996− listLevel <- gets stListLevel
988+ listLevel <- asks envListLevel
997− numid <- gets stListNumId
989+ numid <- asks envListNumId
998 let listPr = if listLevel >= 0 && not displayMathPara
990 let listPr = if listLevel >= 0 && not displayMathPara
999 then [ mknode "w:numPr" []
991 then [ mknode "w:numPr" []
1000 [ mknode "w:numId" [("w:val",show numid)] ()
992 [ mknode "w:numId" [("w:val",show numid)] ()
@@ -1005,18 +997,9 @@ getParaProps displayMathPara = do
1005 [] -> []
997 [] -> []
1006 ps -> [mknode "w:pPr" [] ps]
998 ps -> [mknode "w:pPr" [] ps]
1007
999
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−
1014withParaProp :: Element -> WS a -> WS a
1000withParaProp :: Element -> WS a -> WS a
1015−withParaProp d p = do
1001+withParaProp d p =
1016− pushParaProp d
1002+ local (\env -> env {envParaProperties = d : envParaProperties env}) p
1017− res <- p
1018− popParaProp
1019− return res
1020
1003
1021withParaPropM :: WS Element -> WS a -> WS a
1004withParaPropM :: WS Element -> WS a -> WS a
1022withParaPropM = (. flip withParaProp) . (>>=)
1005withParaPropM = (. flip withParaProp) . (>>=)
@@ -1135,14 +1118,12 @@ inlineToOpenXML opts (Note bs) = do
1135 let insertNoteRef (Plain ils : xs) = Plain (notemarkerXml : Space : ils) : xs
1118 let insertNoteRef (Plain ils : xs) = Plain (notemarkerXml : Space : ils) : xs
1136 insertNoteRef (Para ils : xs) = Para (notemarkerXml : Space : ils) : xs
1119 insertNoteRef (Para ils : xs) = Para (notemarkerXml : Space : ils) : xs
1137 insertNoteRef xs = Para [notemarkerXml] : xs
1120 insertNoteRef xs = Para [notemarkerXml] : xs
1138− oldListLevel <- gets stListLevel
1121+
1139− oldParaProperties <- gets stParaProperties
1122+ contents <- local (\env -> env{ envListLevel = -1
1140− oldTextProperties <- gets stTextProperties
1123+ , envParaProperties = []
1141− modify $ \st -> st{ stListLevel = -1, stParaProperties = [], stTextProperties = [] }
1124+ , envTextProperties = [] })
1142− contents <- withParaPropM (pStyleM "Footnote Text") $ blocksToOpenXML opts
1125+ (withParaPropM (pStyleM "Footnote Text") $ blocksToOpenXML opts
1143− $ insertNoteRef bs
1126+ $ insertNoteRef bs)
1144− modify $ \st -> st{ stListLevel = oldListLevel, stParaProperties = oldParaProperties,
1145− stTextProperties = oldTextProperties }
1146 let newnote = mknode "w:footnote" [("w:id", notenum)] $ contents
1127 let newnote = mknode "w:footnote" [("w:id", notenum)] $ contents
1147 modify $ \s -> s{ stFootnotes = newnote : notes }
1128 modify $ \s -> s{ stFootnotes = newnote : notes }
1148 return [ mknode "w:r" []
1129 return [ mknode "w:r" []
@@ -1274,3 +1255,12 @@ fitToPage (x, y) pageWidth
1274 | x > fromIntegral pageWidth =
1255 | x > fromIntegral pageWidth =
1275 (pageWidth, floor $ ((fromIntegral pageWidth) / x) * y)
1256 (pageWidth, floor $ ((fromIntegral pageWidth) / x) * y)
1276 | otherwise = (floor x, floor y)
1257 | 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