git.iko.soy/typeable/pandoc
on master help sign in
ConTeXt writer: converted to use PandocMonad.
commitac15b0443f942771003508ccbe891954242fd07f authorJohn MacFarlane <jgm@berkeley.edu> dateFri 10 Mar 2017, 20:52 (10 years ago) parentba78b75
1 file changed+28−19
src/Text/Pandoc/Writers/ConTeXt.hs+28−19
src/Text/Pandoc/Writers/ConTeXt.hs+28−19
@@ -34,7 +34,8 @@ import Data.Char (ord)
3434import Data.List (intercalate, intersperse)
3535import Data.Maybe (catMaybes)
3636import Network.URI (isURI, unEscapeString)
37−import Text.Pandoc.Class (PandocMonad)
37+import Text.Pandoc.Class (PandocMonad, report)
38+import Text.Pandoc.Logging
3839import Text.Pandoc.Definition
3940import Text.Pandoc.ImageSize
4041import Text.Pandoc.Options
@@ -56,14 +57,16 @@ orderedListStyles = cycle "narg"
5657
5758-- | Convert Pandoc to ConTeXt.
5859writeConTeXt :: PandocMonad m => WriterOptions -> Pandoc -> m String
59−writeConTeXt options document = return $
60+writeConTeXt options document =
6061 let defaultWriterState = WriterState { stNextRef = 1
6162 , stOrderedListLevel = 0
6263 , stOptions = options
6364 }
64− in evalState (pandocToConTeXt options document) defaultWriterState
65+ in evalStateT (pandocToConTeXt options document) defaultWriterState
6566
66−pandocToConTeXt :: WriterOptions -> Pandoc -> State WriterState String
67+type WM = StateT WriterState
68+
69+pandocToConTeXt :: PandocMonad m => WriterOptions -> Pandoc -> WM m String
6770pandocToConTeXt options (Pandoc meta blocks) = do
6871 let colwidth = if writerWrapText options == WrapAuto
6972 then Just $ writerColumns options
@@ -142,7 +145,7 @@ toLabel z = concatMap go z
142145 | otherwise = [x]
143146
144147-- | Convert Elements to ConTeXt
145−elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
148+elementToConTeXt :: PandocMonad m => WriterOptions -> Element -> WM m Doc
146149elementToConTeXt _ (Blk block) = blockToConTeXt block
147150elementToConTeXt opts (Sec level _ attr title' elements) = do
148151 header' <- sectionHeader attr level title'
@@ -150,8 +153,7 @@ elementToConTeXt opts (Sec level _ attr title' elements) = do
150153 return $ vcat (header' : innerContents)
151154
152155-- | Convert Pandoc block element to ConTeXt.
153−blockToConTeXt :: Block
154− -> State WriterState Doc
156+blockToConTeXt :: PandocMonad m => Block -> WM m Doc
155157blockToConTeXt Null = return empty
156158blockToConTeXt (Plain lst) = inlineListToConTeXt lst
157159-- title beginning with fig: indicates that the image is a figure
@@ -176,7 +178,9 @@ blockToConTeXt (CodeBlock _ str) =
176178 return $ flush ("\\starttyping" <> cr <> text str <> cr <> "\\stoptyping") $$ blankline
177179 -- blankline because \stoptyping can't have anything after it, inc. '}'
178180blockToConTeXt (RawBlock "context" str) = return $ text str <> blankline
179−blockToConTeXt (RawBlock _ _ ) = return empty
181+blockToConTeXt b@(RawBlock _ _ ) = do
182+ report $ BlockNotRendered b
183+ return empty
180184blockToConTeXt (Div (ident,_,kvs) bs) = do
181185 let align dir txt = "\\startalignment[" <> dir <> "]" $$ txt $$ "\\stopalignment"
182186 let wrapRef txt = if null ident
@@ -262,16 +266,16 @@ blockToConTeXt (Table caption aligns widths heads rows) = do
262266 "\\HL" $$ headers $$
263267 vcat rows' $$ "\\HL" $$ "\\stoptable" <> blankline
264268
265−tableRowToConTeXt :: [[Block]] -> State WriterState Doc
269+tableRowToConTeXt :: PandocMonad m => [[Block]] -> WM m Doc
266270tableRowToConTeXt cols = do
267271 cols' <- mapM blockListToConTeXt cols
268272 return $ (vcat (map ("\\NC " <>) cols')) $$ "\\NC\\AR"
269273
270−listItemToConTeXt :: [Block] -> State WriterState Doc
274+listItemToConTeXt :: PandocMonad m => [Block] -> WM m Doc
271275listItemToConTeXt list = blockListToConTeXt list >>=
272276 return . ("\\item" $$) . (nest 2)
273277
274−defListItemToConTeXt :: ([Inline], [[Block]]) -> State WriterState Doc
278+defListItemToConTeXt :: PandocMonad m => ([Inline], [[Block]]) -> WM m Doc
275279defListItemToConTeXt (term, defs) = do
276280 term' <- inlineListToConTeXt term
277281 def' <- liftM vsep $ mapM blockListToConTeXt defs
@@ -279,12 +283,13 @@ defListItemToConTeXt (term, defs) = do
279283 "\\stopdescription" <> blankline
280284
281285-- | Convert list of block elements to ConTeXt.
282−blockListToConTeXt :: [Block] -> State WriterState Doc
286+blockListToConTeXt :: PandocMonad m => [Block] -> WM m Doc
283287blockListToConTeXt lst = liftM vcat $ mapM blockToConTeXt lst
284288
285289-- | Convert list of inline elements to ConTeXt.
286−inlineListToConTeXt :: [Inline] -- ^ Inlines to convert
287− -> State WriterState Doc
290+inlineListToConTeXt :: PandocMonad m
291+ => [Inline] -- ^ Inlines to convert
292+ -> WM m Doc
288293inlineListToConTeXt lst = liftM hcat $ mapM inlineToConTeXt $ addStruts lst
289294 -- We add a \strut after a line break that precedes a space,
290295 -- or the space gets swallowed
@@ -298,8 +303,9 @@ inlineListToConTeXt lst = liftM hcat $ mapM inlineToConTeXt $ addStruts lst
298303 isSpacey _ = False
299304
300305-- | Convert inline element to ConTeXt
301−inlineToConTeXt :: Inline -- ^ Inline to convert
302− -> State WriterState Doc
306+inlineToConTeXt :: PandocMonad m
307+ => Inline -- ^ Inline to convert
308+ -> WM m Doc
303309inlineToConTeXt (Emph lst) = do
304310 contents <- inlineListToConTeXt lst
305311 return $ braces $ "\\em " <> contents
@@ -339,7 +345,9 @@ inlineToConTeXt (Math DisplayMath str) =
339345 return $ text "\\startformula " <> text str <> text " \\stopformula" <> space
340346inlineToConTeXt (RawInline "context" str) = return $ text str
341347inlineToConTeXt (RawInline "tex" str) = return $ text str
342−inlineToConTeXt (RawInline _ _) = return empty
348+inlineToConTeXt il@(RawInline _ _) = do
349+ report $ InlineNotRendered il
350+ return empty
343351inlineToConTeXt (LineBreak) = return $ text "\\crlf" <> cr
344352inlineToConTeXt SoftBreak = do
345353 wrapText <- gets (writerWrapText . stOptions)
@@ -416,10 +424,11 @@ inlineToConTeXt (Span (_,_,kvs) ils) = do
416424 fmap (wrapLang . wrapDir) $ inlineListToConTeXt ils
417425
418426-- | Craft the section header, inserting the section reference, if supplied.
419−sectionHeader :: Attr
427+sectionHeader :: PandocMonad m
428+ => Attr
420429 -> Int
421430 -> [Inline]
422− -> State WriterState Doc
431+ -> WM m Doc
423432sectionHeader (ident,classes,_) hdrLevel lst = do
424433 contents <- inlineListToConTeXt lst
425434 st <- get