git.iko.soy/typeable/pandoc
on master help sign in
Writers: Use gets to access MonadState where possible (#3480)
commit39a8359b571ba0aa1e193b0ff9ef6252acc7a754 authorAlexander Krotov <ilabdsf@gmail.com> dateWed 1 Mar 2017, 23:36 (10 years ago) parentea619bf
7 files changed+53−53
src/Text/Pandoc/Writers/AsciiDoc.hs+3−3 src/Text/Pandoc/Writers/DokuWiki.hs+2−2 src/Text/Pandoc/Writers/Man.hs+3−3 src/Text/Pandoc/Writers/Org.hs+5−5 src/Text/Pandoc/Writers/RST.hs+14−14 src/Text/Pandoc/Writers/Textile.hs+10−10 src/Text/Pandoc/Writers/ZimWiki.hs+16−16
src/Text/Pandoc/Writers/AsciiDoc.hs+3−3
@@ -286,7 +286,7 @@ bulletListItemToAsciiDoc opts blocks = do
286286 return $ d <> cr <> chomp x
287287 addBlock d b = do x <- blockToAsciiDoc opts b
288288 return $ d <> cr <> text "+" <> cr <> chomp x
289− lev <- bulletListLevel `fmap` get
289+ lev <- gets bulletListLevel
290290 modify $ \s -> s{ bulletListLevel = lev + 1 }
291291 contents <- foldM addBlock empty blocks
292292 modify $ \s -> s{ bulletListLevel = lev }
@@ -307,7 +307,7 @@ orderedListItemToAsciiDoc opts marker blocks = do
307307 return $ d <> cr <> chomp x
308308 addBlock d b = do x <- blockToAsciiDoc opts b
309309 return $ d <> cr <> text "+" <> cr <> chomp x
310− lev <- orderedListLevel `fmap` get
310+ lev <- gets orderedListLevel
311311 modify $ \s -> s{ orderedListLevel = lev + 1 }
312312 contents <- foldM addBlock empty blocks
313313 modify $ \s -> s{ orderedListLevel = lev }
@@ -320,7 +320,7 @@ definitionListItemToAsciiDoc :: PandocMonad m
320320 -> ADW m Doc
321321definitionListItemToAsciiDoc opts (label, defs) = do
322322 labelText <- inlineListToAsciiDoc opts label
323− marker <- defListMarker `fmap` get
323+ marker <- gets defListMarker
324324 if marker == "::"
325325 then modify (\st -> st{ defListMarker = ";;"})
326326 else modify (\st -> st{ defListMarker = "::"})
src/Text/Pandoc/Writers/DokuWiki.hs+2−2
@@ -53,7 +53,7 @@ import Data.List ( intersect, intercalate, isPrefixOf, transpose )
5353import Data.Default (Default(..))
5454import Network.URI ( isURI )
5555import Control.Monad ( zipWithM )
56−import Control.Monad.State ( modify, State, get, evalState )
56+import Control.Monad.State ( modify, State, gets, evalState )
5757import Control.Monad.Reader ( ReaderT, runReaderT, ask, local )
5858import Text.Pandoc.Class (PandocMonad)
5959
@@ -93,7 +93,7 @@ pandocToDokuWiki opts (Pandoc meta blocks) = do
9393 (inlineListToDokuWiki opts)
9494 meta
9595 body <- blockListToDokuWiki opts blocks
96− notesExist <- stNotes <$> get
96+ notesExist <- gets stNotes
9797 let notes = if notesExist
9898 then "" -- TODO Was "\n<references />" Check whether I can really remove this:
9999 -- if it is definitely to do with footnotes, can remove this whole bit
src/Text/Pandoc/Writers/Man.hs+3−3
@@ -82,10 +82,10 @@ pandocToMan opts (Pandoc meta blocks) = do
8282 (fmap (render colwidth) . inlineListToMan opts)
8383 $ deleteMeta "title" meta
8484 body <- blockListToMan opts blocks
85− notes <- liftM stNotes get
85+ notes <- gets stNotes
8686 notes' <- notesToMan opts (reverse notes)
8787 let main = render' $ body $$ notes' $$ text ""
88− hasTables <- liftM stHasTables get
88+ hasTables <- gets stHasTables
8989 let context = defField "body" main
9090 $ setFieldsFromTitle
9191 $ defField "has-tables" hasTables
@@ -376,6 +376,6 @@ inlineToMan opts (Image attr alternate (source, tit)) = do
376376inlineToMan _ (Note contents) = do
377377 -- add to notes in state
378378 modify $ \st -> st{ stNotes = contents : stNotes st }
379− notes <- liftM stNotes get
379+ notes <- gets stNotes
380380 let ref = show $ (length notes)
381381 return $ char '[' <> text ref <> char ']'
src/Text/Pandoc/Writers/Org.hs+5−5
@@ -61,7 +61,7 @@ writeOrg opts document = return $
6161-- | Return Org representation of document.
6262pandocToOrg :: Pandoc -> State WriterState String
6363pandocToOrg (Pandoc meta blocks) = do
64− opts <- liftM stOptions get
64+ opts <- gets stOptions
6565 let colwidth = if writerWrapText opts == WrapAuto
6666 then Just $ writerColumns opts
6767 else Nothing
@@ -70,8 +70,8 @@ pandocToOrg (Pandoc meta blocks) = do
7070 (fmap (render colwidth) . inlineListToOrg)
7171 meta
7272 body <- blockListToOrg blocks
73− notes <- liftM (reverse . stNotes) get >>= notesToOrg
74− hasMath <- liftM stHasMath get
73+ notes <- gets (reverse . stNotes) >>= notesToOrg
74+ hasMath <- gets stHasMath
7575 let main = render colwidth $ foldl ($+$) empty $ [body, notes]
7676 let context = defField "body" main
7777 $ defField "math" hasMath
@@ -188,7 +188,7 @@ blockToOrg (Header level attr inlines) = do
188188 else cr <> nest (level + 1) (propertiesDrawer attr)
189189 return $ headerStr <> " " <> contents <> drawerStr <> blankline
190190blockToOrg (CodeBlock (_,classes,_) str) = do
191− opts <- stOptions <$> get
191+ opts <- gets stOptions
192192 let tabstop = writerTabStop opts
193193 let at = map pandocLangToOrg classes `intersect` orgLangIdentifiers
194194 let (beg, end) = case at of
@@ -365,7 +365,7 @@ inlineToOrg (Image _ _ (source, _)) = do
365365 return $ "[[" <> text (orgPath source) <> "]]"
366366inlineToOrg (Note contents) = do
367367 -- add to notes in state
368− notes <- get >>= (return . stNotes)
368+ notes <- gets stNotes
369369 modify $ \st -> st { stNotes = contents:notes }
370370 let ref = show $ (length notes) + 1
371371 return $ "[fn:" <> text ref <> "]"
src/Text/Pandoc/Writers/RST.hs+14−14
@@ -70,7 +70,7 @@ writeRST opts document = return $
7070-- | Return RST representation of document.
7171pandocToRST :: Pandoc -> State WriterState String
7272pandocToRST (Pandoc meta blocks) = do
73− opts <- liftM stOptions get
73+ opts <- gets stOptions
7474 let colwidth = if writerWrapText opts == WrapAuto
7575 then Just $ writerColumns opts
7676 else Nothing
@@ -85,12 +85,12 @@ pandocToRST (Pandoc meta blocks) = do
8585 body <- blockListToRST' True $ case writerTemplate opts of
8686 Just _ -> normalizeHeadings 1 blocks
8787 Nothing -> blocks
88− notes <- liftM (reverse . stNotes) get >>= notesToRST
88+ notes <- gets (reverse . stNotes) >>= notesToRST
8989 -- note that the notes may contain refs, so we do them first
90− refs <- liftM (reverse . stLinks) get >>= refsToRST
91− pics <- liftM (reverse . stImages) get >>= pictRefsToRST
92− hasMath <- liftM stHasMath get
93− rawTeX <- liftM stHasRawTeX get
90+ refs <- gets (reverse . stLinks) >>= refsToRST
91+ pics <- gets (reverse . stImages) >>= pictRefsToRST
92+ hasMath <- gets stHasMath
93+ rawTeX <- gets stHasRawTeX
9494 let main = render colwidth $ foldl ($+$) empty $ [body, notes, refs, pics]
9595 let context = defField "body" main
9696 $ defField "toc" (writerTableOfContents opts)
@@ -245,7 +245,7 @@ blockToRST (Header level (name,classes,_) inlines) = do
245245 | otherwise = ":class: " <> text (unwords classes)
246246 return $ nowrap $ hang 3 ".. " (rub $$ name' $$ cls) $$ blankline
247247blockToRST (CodeBlock (_,classes,kvs) str) = do
248− opts <- stOptions <$> get
248+ opts <- gets stOptions
249249 let tabstop = writerTabStop opts
250250 let startnum = maybe "" (\x -> " " <> text x) $ lookup "startFrom" kvs
251251 let numberlines = if "numberLines" `elem` classes
@@ -261,7 +261,7 @@ blockToRST (CodeBlock (_,classes,kvs) str) = do
261261 (lang:_) -> (".. code:: " <> text lang) $$ numberlines)
262262 $+$ nest tabstop (text str) $$ blankline
263263blockToRST (BlockQuote blocks) = do
264− tabstop <- get >>= (return . writerTabStop . stOptions)
264+ tabstop <- gets $ writerTabStop . stOptions
265265 contents <- blockListToRST blocks
266266 return $ (nest tabstop contents) <> blankline
267267blockToRST (Table caption _ widths headers rows) = do
@@ -274,7 +274,7 @@ blockToRST (Table caption _ widths headers rows) = do
274274 -- isSimpleCell _ = False
275275 -- let isSimple = all (==0) widths && all (all isSimpleCell) rows
276276 let numChars = maximum . map offset
277− opts <- get >>= return . stOptions
277+ opts <- gets stOptions
278278 let widthsInChars =
279279 if all (== 0) widths
280280 then map ((+2) . numChars) $ transpose (headers' : rawRows)
@@ -342,7 +342,7 @@ definitionListItemToRST :: ([Inline], [[Block]]) -> State WriterState Doc
342342definitionListItemToRST (label, defs) = do
343343 label' <- inlineListToRST label
344344 contents <- liftM vcat $ mapM blockListToRST defs
345− tabstop <- get >>= (return . writerTabStop . stOptions)
345+ tabstop <- gets $ writerTabStop . stOptions
346346 return $ label' $$ nest tabstop (nestle contents <> cr)
347347
348348-- | Format a list of lines as line block.
@@ -483,7 +483,7 @@ inlineToRST (RawInline f x)
483483inlineToRST (LineBreak) = return cr -- there's no line break in RST (see Para)
484484inlineToRST Space = return space
485485inlineToRST SoftBreak = do
486− wrapText <- gets (writerWrapText . stOptions)
486+ wrapText <- gets $ writerWrapText . stOptions
487487 case wrapText of
488488 WrapPreserve -> return cr
489489 WrapAuto -> return space
@@ -500,10 +500,10 @@ inlineToRST (Link _ [Image attr alt (imgsrc,imgtit)] (src, _tit)) = do
500500 label <- registerImage attr alt (imgsrc,imgtit) (Just src)
501501 return $ "|" <> label <> "|"
502502inlineToRST (Link _ txt (src, tit)) = do
503− useReferenceLinks <- get >>= return . writerReferenceLinks . stOptions
503+ useReferenceLinks <- gets $ writerReferenceLinks . stOptions
504504 linktext <- inlineListToRST $ normalizeSpaces txt
505505 if useReferenceLinks
506− then do refs <- get >>= return . stLinks
506+ then do refs <- gets stLinks
507507 case lookup txt refs of
508508 Just (src',tit') ->
509509 if src == src' && tit == tit'
@@ -526,7 +526,7 @@ inlineToRST (Note contents) = do
526526
527527registerImage :: Attr -> [Inline] -> Target -> Maybe String -> State WriterState Doc
528528registerImage attr alt (src,tit) mbtarget = do
529− pics <- get >>= return . stImages
529+ pics <- gets stImages
530530 txt <- case lookup alt pics of
531531 Just (a,s,t,mbt) | (a,s,t,mbt) == (attr,src,tit,mbtarget)
532532 -> return alt
src/Text/Pandoc/Writers/Textile.hs+10−10
@@ -63,7 +63,7 @@ pandocToTextile opts (Pandoc meta blocks) = do
6363 metadata <- metaToJSON opts (blockListToTextile opts)
6464 (inlineListToTextile opts) meta
6565 body <- blockListToTextile opts blocks
66− notes <- liftM (unlines . reverse . stNotes) get
66+ notes <- gets $ unlines . reverse . stNotes
6767 let main = body ++ if null notes then "" else ("\n\n" ++ notes)
6868 let context = defField "body" main metadata
6969 case writerTemplate opts of
@@ -72,7 +72,7 @@ pandocToTextile opts (Pandoc meta blocks) = do
7272
7373withUseTags :: State WriterState a -> State WriterState a
7474withUseTags action = do
75− oldUseTags <- liftM stUseTags get
75+ oldUseTags <- gets stUseTags
7676 modify $ \s -> s { stUseTags = True }
7777 result <- action
7878 modify $ \s -> s { stUseTags = oldUseTags }
@@ -124,8 +124,8 @@ blockToTextile opts (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
124124 return $ im ++ "\n" ++ capt
125125
126126blockToTextile opts (Para inlines) = do
127− useTags <- liftM stUseTags get
128− listLevel <- liftM stListLevel get
127+ useTags <- gets stUseTags
128+ listLevel <- gets stListLevel
129129 contents <- inlineListToTextile opts inlines
130130 return $ if useTags
131131 then "<p>" ++ contents ++ "</p>"
@@ -212,7 +212,7 @@ blockToTextile opts (Table capt aligns widths headers rows') = do
212212 "<tbody>\n" ++ unlines body' ++ "</tbody>\n</table>\n"
213213
214214blockToTextile opts x@(BulletList items) = do
215− oldUseTags <- liftM stUseTags get
215+ oldUseTags <- gets stUseTags
216216 let useTags = oldUseTags || not (isSimpleList x)
217217 if useTags
218218 then do
@@ -220,13 +220,13 @@ blockToTextile opts x@(BulletList items) = do
220220 return $ "<ul>\n" ++ vcat contents ++ "\n</ul>\n"
221221 else do
222222 modify $ \s -> s { stListLevel = stListLevel s ++ "*" }
223− level <- get >>= return . length . stListLevel
223+ level <- gets $ length . stListLevel
224224 contents <- mapM (listItemToTextile opts) items
225225 modify $ \s -> s { stListLevel = init (stListLevel s) }
226226 return $ vcat contents ++ (if level > 1 then "" else "\n")
227227
228228blockToTextile opts x@(OrderedList attribs@(start, _, _) items) = do
229− oldUseTags <- liftM stUseTags get
229+ oldUseTags <- gets stUseTags
230230 let useTags = oldUseTags || not (isSimpleList x)
231231 if useTags
232232 then do
@@ -238,7 +238,7 @@ blockToTextile opts x@(OrderedList attribs@(start, _, _) items) = do
238238 , stStartNum = if start > 1
239239 then Just start
240240 else Nothing }
241− level <- get >>= return . length . stListLevel
241+ level <- gets $ length . stListLevel
242242 contents <- mapM (listItemToTextile opts) items
243243 modify $ \s -> s { stListLevel = init (stListLevel s),
244244 stStartNum = Nothing }
@@ -265,7 +265,7 @@ listAttribsToString (startnum, numstyle, _) =
265265listItemToTextile :: WriterOptions -> [Block] -> State WriterState String
266266listItemToTextile opts items = do
267267 contents <- blockListToTextile opts items
268− useTags <- get >>= return . stUseTags
268+ useTags <- gets stUseTags
269269 if useTags
270270 then return $ "<li>" ++ contents ++ "</li>"
271271 else do
@@ -477,7 +477,7 @@ inlineToTextile opts (Image attr@(_, cls, _) alt (source, tit)) = do
477477 return $ "!" ++ classes ++ styles ++ source ++ txt ++ "!"
478478
479479inlineToTextile opts (Note contents) = do
480− curNotes <- liftM stNotes get
480+ curNotes <- gets stNotes
481481 let newnum = length curNotes + 1
482482 contents' <- blockListToTextile opts contents
483483 let thisnote = "fn" ++ show newnum ++ ". " ++ contents' ++ "\n"
src/Text/Pandoc/Writers/ZimWiki.hs+16−16
@@ -43,7 +43,7 @@ import Data.Text ( breakOnAll, pack )
4343import Data.Default (Default(..))
4444import Network.URI ( isURI )
4545import Control.Monad ( zipWithM )
46−import Control.Monad.State ( modify, State, get, evalState )
46+import Control.Monad.State ( modify, State, gets, evalState )
4747import Text.Pandoc.Class ( PandocMonad )
4848import qualified Data.Map as Map
4949
@@ -110,8 +110,8 @@ blockToZimWiki opts (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
110110 return $ "{{" ++ prefix ++ src ++ imageDims opts attr ++ opt ++ "}}\n"
111111
112112blockToZimWiki opts (Para inlines) = do
113− indent <- stIndent <$> get
114− -- useTags <- stUseTags <$> get
113+ indent <- gets stIndent
114+ -- useTags <- gets stUseTags
115115 contents <- inlineListToZimWiki opts inlines
116116 return $ contents ++ if null indent then "\n" else ""
117117
@@ -181,14 +181,14 @@ blockToZimWiki opts (Table capt aligns _ headers rows) = do
181181 unlines (map renderRow rows')
182182
183183blockToZimWiki opts (BulletList items) = do
184− indent <- stIndent <$> get
184+ indent <- gets stIndent
185185 modify $ \s -> s { stIndent = stIndent s ++ "\t" }
186186 contents <- (mapM (listItemToZimWiki opts) items)
187187 modify $ \s -> s{ stIndent = indent } -- drop 1 (stIndent s) }
188188 return $ vcat contents ++ if null indent then "\n" else ""
189189
190190blockToZimWiki opts (OrderedList _ items) = do
191− indent <- stIndent <$> get
191+ indent <- gets stIndent
192192 modify $ \s -> s { stIndent = stIndent s ++ "\t", stItemNum = 1 }
193193 contents <- (mapM (orderedListItemToZimWiki opts) items)
194194 modify $ \s -> s{ stIndent = indent } -- drop 1 (stIndent s) }
@@ -202,14 +202,14 @@ definitionListItemToZimWiki :: WriterOptions -> ([Inline],[[Block]]) -> State Wr
202202definitionListItemToZimWiki opts (label, items) = do
203203 labelText <- inlineListToZimWiki opts label
204204 contents <- mapM (blockListToZimWiki opts) items
205− indent <- stIndent <$> get
205+ indent <- gets stIndent
206206 return $ indent ++ "* **" ++ labelText ++ "** " ++ concat contents
207207
208208-- Auxiliary functions for lists:
209209indentFromHTML :: WriterOptions -> String -> State WriterState String
210210indentFromHTML _ str = do
211− indent <- stIndent <$> get
212− itemnum <- stItemNum <$> get
211+ indent <- gets stIndent
212+ itemnum <- gets stItemNum
213213 if isInfixOf "<li>" str then return $ indent ++ show itemnum ++ "."
214214 else if isInfixOf "</li>" str then return "\n"
215215 else if isInfixOf "<li value=" str then do
@@ -242,15 +242,15 @@ vcat = intercalate "\n"
242242listItemToZimWiki :: WriterOptions -> [Block] -> State WriterState String
243243listItemToZimWiki opts items = do
244244 contents <- blockListToZimWiki opts items
245− indent <- stIndent <$> get
245+ indent <- gets stIndent
246246 return $ indent ++ "* " ++ contents
247247
248248-- | Convert ordered list item (list of blocks) to ZimWiki.
249249orderedListItemToZimWiki :: WriterOptions -> [Block] -> State WriterState String
250250orderedListItemToZimWiki opts items = do
251251 contents <- blockListToZimWiki opts items
252− indent <- stIndent <$> get
253− itemnum <- stItemNum <$> get
252+ indent <- gets stIndent
253+ itemnum <- gets stItemNum
254254 --modify $ \s -> s { stItemNum = itemnum + 1 } -- this is not strictly necessary for zim as zim does its own renumbering
255255 return $ indent ++ show itemnum ++ ". " ++ contents
256256
@@ -316,8 +316,8 @@ inlineToZimWiki opts (Cite _ lst) = inlineListToZimWiki opts lst
316316inlineToZimWiki _ (Code _ str) = return $ "''" ++ str ++ "''"
317317
318318inlineToZimWiki _ (Str str) = do
319− inTable <- stInTable <$> get
320− inLink <- stInLink <$> get
319+ inTable <- gets stInTable
320+ inLink <- gets stInLink
321321 if inTable
322322 then return $ substitute "|" "\\|" . escapeString $ str
323323 else
@@ -337,7 +337,7 @@ inlineToZimWiki opts (RawInline f str)
337337 | otherwise = return ""
338338
339339inlineToZimWiki _ LineBreak = do
340− inTable <- stInTable <$> get
340+ inTable <- gets stInTable
341341 if inTable
342342 then return "\\n"
343343 else return "\n"
@@ -351,7 +351,7 @@ inlineToZimWiki opts SoftBreak =
351351inlineToZimWiki _ Space = return " "
352352
353353inlineToZimWiki opts (Link _ txt (src, _)) = do
354− inTable <- stInTable <$> get
354+ inTable <- gets stInTable
355355 modify $ \s -> s { stInLink = True }
356356 label <- inlineListToZimWiki opts $ removeFormatting txt -- zim does not allow formatting in link text, it takes the text verbatim, no need to escape it
357357 modify $ \s -> s { stInLink = False }
@@ -369,7 +369,7 @@ inlineToZimWiki opts (Link _ txt (src, _)) = do
369369 _ -> src -- link to a help page
370370inlineToZimWiki opts (Image attr alt (source, tit)) = do
371371 alt' <- inlineListToZimWiki opts alt
372− inTable <- stInTable <$> get
372+ inTable <- gets stInTable
373373 let txt = case (tit, alt, inTable) of
374374 ("",[], _) -> ""
375375 ("", _, False ) -> "|" ++ alt'