@@ -37,7 +37,8 @@ import Data.List (intersperse, isPrefixOf, stripPrefix, transpose)
3737import Data.Maybe (fromMaybe)
3838import Network.URI (isURI)
3939import qualified Text.Pandoc.Builder as B
40−import Text.Pandoc.Class (PandocMonad)
40+import Text.Pandoc.Class (PandocMonad, report)
41+import Text.Pandoc.Logging
4142import Text.Pandoc.Definition
4243import Text.Pandoc.ImageSize
4344import Text.Pandoc.Options
@@ -58,17 +59,19 @@ data WriterState =
5859 , stTopLevel :: Bool
5960 }
6061
62+type RST = StateT WriterState
63+
6164-- | Convert Pandoc to RST.
6265writeRST :: PandocMonad m => WriterOptions -> Pandoc -> m String
63−writeRST opts document = return $
66+writeRST opts document = do
6467 let st = WriterState { stNotes = [], stLinks = [],
6568 stImages = [], stHasMath = False,
6669 stHasRawTeX = False, stOptions = opts,
6770 stTopLevel = True}
68− in evalState (pandocToRST document) st
71+ evalStateT (pandocToRST document) st
6972
7073-- | Return RST representation of document.
71−pandocToRST :: Pandoc -> State WriterState String
74+pandocToRST :: PandocMonad m => Pandoc -> RST m String
7275pandocToRST (Pandoc meta blocks) = do
7376 opts <- gets stOptions
7477 let colwidth = if writerWrapText opts == WrapAuto
@@ -113,12 +116,11 @@ pandocToRST (Pandoc meta blocks) = do
113116 normalizeHeadings _ [] = []
114117
115118-- | Return RST representation of reference key table.
116−refsToRST :: Refs -> State WriterState Doc
119+refsToRST :: PandocMonad m => Refs -> RST m Doc
117120refsToRST refs = mapM keyToRST refs >>= return . vcat
118121
119122-- | Return RST representation of a reference key.
120−keyToRST :: ([Inline], (String, String))
121− -> State WriterState Doc
123+keyToRST :: PandocMonad m => ([Inline], (String, String)) -> RST m Doc
122124keyToRST (label, (src, _)) = do
123125 label' <- inlineListToRST label
124126 let label'' = if ':' `elem` ((render Nothing label') :: String)
@@ -127,26 +129,28 @@ keyToRST (label, (src, _)) = do
127129 return $ nowrap $ ".. _" <> label'' <> ": " <> text src
128130
129131-- | Return RST representation of notes.
130−notesToRST :: [[Block]] -> State WriterState Doc
132+notesToRST :: PandocMonad m => [[Block]] -> RST m Doc
131133notesToRST notes =
132134 mapM (\(num, note) -> noteToRST num note) (zip [1..] notes) >>=
133135 return . vsep
134136
135137-- | Return RST representation of a note.
136−noteToRST :: Int -> [Block] -> State WriterState Doc
138+noteToRST :: PandocMonad m => Int -> [Block] -> RST m Doc
137139noteToRST num note = do
138140 contents <- blockListToRST note
139141 let marker = ".. [" <> text (show num) <> "]"
140142 return $ nowrap $ marker $$ nest 3 contents
141143
142144-- | Return RST representation of picture reference table.
143−pictRefsToRST :: [([Inline], (Attr, String, String, Maybe String))]
144− -> State WriterState Doc
145+pictRefsToRST :: PandocMonad m
146+ => [([Inline], (Attr, String, String, Maybe String))]
147+ -> RST m Doc
145148pictRefsToRST refs = mapM pictToRST refs >>= return . vcat
146149
147150-- | Return RST representation of a picture substitution reference.
148−pictToRST :: ([Inline], (Attr, String, String, Maybe String))
149− -> State WriterState Doc
151+pictToRST :: PandocMonad m
152+ => ([Inline], (Attr, String, String, Maybe String))
153+ -> RST m Doc
150154pictToRST (label, (attr, src, _, mbtarget)) = do
151155 label' <- inlineListToRST label
152156 dims <- imageDimsToRST attr
@@ -178,7 +182,7 @@ escapeString opts (c:cs) =
178182 _ -> '.':escapeString opts cs
179183 _ -> c : escapeString opts cs
180184
181−titleToRST :: [Inline] -> [Inline] -> State WriterState Doc
185+titleToRST :: PandocMonad m => [Inline] -> [Inline] -> RST m Doc
182186titleToRST [] _ = return empty
183187titleToRST tit subtit = do
184188 title <- inlineListToRST tit
@@ -194,8 +198,9 @@ bordered contents c =
194198 border = text (replicate len c)
195199
196200-- | Convert Pandoc block element to RST.
197−blockToRST :: Block -- ^ Block element
198− -> State WriterState Doc
201+blockToRST :: PandocMonad m
202+ => Block -- ^ Block element
203+ -> RST m Doc
199204blockToRST Null = return empty
200205blockToRST (Div attr bs) = do
201206 contents <- blockListToRST bs
@@ -323,22 +328,23 @@ blockToRST (DefinitionList items) = do
323328 return $ blankline $$ chomp (vcat contents) $$ blankline
324329
325330-- | Convert bullet list item (list of blocks) to RST.
326−bulletListItemToRST :: [Block] -> State WriterState Doc
331+bulletListItemToRST :: PandocMonad m => [Block] -> RST m Doc
327332bulletListItemToRST items = do
328333 contents <- blockListToRST items
329334 return $ hang 3 "- " $ contents <> cr
330335
331336-- | Convert ordered list item (a list of blocks) to RST.
332−orderedListItemToRST :: String -- ^ marker for list item
337+orderedListItemToRST :: PandocMonad m
338+ => String -- ^ marker for list item
333339 -> [Block] -- ^ list item (list of blocks)
334− -> State WriterState Doc
340+ -> RST m Doc
335341orderedListItemToRST marker items = do
336342 contents <- blockListToRST items
337343 let marker' = marker ++ " "
338344 return $ hang (length marker') (text marker') $ contents <> cr
339345
340346-- | Convert defintion list item (label, list of blocks) to RST.
341−definitionListItemToRST :: ([Inline], [[Block]]) -> State WriterState Doc
347+definitionListItemToRST :: PandocMonad m => ([Inline], [[Block]]) -> RST m Doc
342348definitionListItemToRST (label, defs) = do
343349 label' <- inlineListToRST label
344350 contents <- liftM vcat $ mapM blockListToRST defs
@@ -346,15 +352,16 @@ definitionListItemToRST (label, defs) = do
346352 return $ label' $$ nest tabstop (nestle contents <> cr)
347353
348354-- | Format a list of lines as line block.
349−linesToLineBlock :: [[Inline]] -> State WriterState Doc
355+linesToLineBlock :: PandocMonad m => [[Inline]] -> RST m Doc
350356linesToLineBlock inlineLines = do
351357 lns <- mapM inlineListToRST inlineLines
352358 return $ (vcat $ map (hang 2 (text "| ")) lns) <> blankline
353359
354360-- | Convert list of Pandoc block elements to RST.
355−blockListToRST' :: Bool
361+blockListToRST' :: PandocMonad m
362+ => Bool
356363 -> [Block] -- ^ List of block elements
357− -> State WriterState Doc
364+ -> RST m Doc
358365blockListToRST' topLevel blocks = do
359366 tl <- gets stTopLevel
360367 modify (\s->s{stTopLevel=topLevel})
@@ -362,12 +369,13 @@ blockListToRST' topLevel blocks = do
362369 modify (\s->s{stTopLevel=tl})
363370 return res
364371
365−blockListToRST :: [Block] -- ^ List of block elements
366− -> State WriterState Doc
372+blockListToRST :: PandocMonad m
373+ => [Block] -- ^ List of block elements
374+ -> RST m Doc
367375blockListToRST = blockListToRST' False
368376
369377-- | Convert list of Pandoc inline elements to RST.
370−inlineListToRST :: [Inline] -> State WriterState Doc
378+inlineListToRST :: PandocMonad m => [Inline] -> RST m Doc
371379inlineListToRST lst =
372380 mapM inlineToRST (removeSpaceAfterDisplayMath $ insertBS lst) >>=
373381 return . hcat
@@ -427,7 +435,7 @@ inlineListToRST lst =
427435 isComplex _ = False
428436
429437-- | Convert Pandoc inline element to RST.
430−inlineToRST :: Inline -> State WriterState Doc
438+inlineToRST :: PandocMonad m => Inline -> RST m Doc
431439inlineToRST (Span _ ils) = inlineListToRST ils
432440inlineToRST (Emph lst) = do
433441 contents <- inlineListToRST lst
@@ -477,12 +485,12 @@ inlineToRST (Math t str) = do
477485 then blankline $$ ".. math::" $$
478486 blankline $$ nest 3 (text str) $$ blankline
479487 else blankline $$ (".. math:: " <> text str) $$ blankline
480−inlineToRST (RawInline f x)
488+inlineToRST il@(RawInline f x)
481489 | f == "rst" = return $ text x
482490 | f == "latex" || f == "tex" = do
483491 modify $ \st -> st{ stHasRawTeX = True }
484492 return $ ":raw-latex:`" <> text x <> "`"
485− | otherwise = return empty
493+ | otherwise = empty <$ report (InlineNotRendered il)
486494inlineToRST (LineBreak) = return cr -- there's no line break in RST (see Para)
487495inlineToRST Space = return space
488496inlineToRST SoftBreak = do
@@ -527,7 +535,7 @@ inlineToRST (Note contents) = do
527535 let ref = show $ (length notes) + 1
528536 return $ " [" <> text ref <> "]_"
529537
530−registerImage :: Attr -> [Inline] -> Target -> Maybe String -> State WriterState Doc
538+registerImage :: PandocMonad m => Attr -> [Inline] -> Target -> Maybe String -> RST m Doc
531539registerImage attr alt (src,tit) mbtarget = do
532540 pics <- gets stImages
533541 txt <- case lookup alt pics of
@@ -542,7 +550,7 @@ registerImage attr alt (src,tit) mbtarget = do
542550 return alt'
543551 inlineListToRST txt
544552
545−imageDimsToRST :: Attr -> State WriterState Doc
553+imageDimsToRST :: PandocMonad m => Attr -> RST m Doc
546554imageDimsToRST attr = do
547555 let (ident, _, _) = attr
548556 name = if null ident