@@ -33,16 +33,18 @@ module Text.Pandoc.Writers.RST ( writeRST) where
3333import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
3535import Text.Pandoc.Blocks
36+import Text.Pandoc.Templates (renderTemplate)
3637import Data.List ( isPrefixOf, isSuffixOf, drop, intersperse, transpose )
3738import Text.PrettyPrint.HughesPJ hiding ( Str )
3839import Control.Monad.State
3940import Control.Applicative ( (<$>) )
41+import Control.Monad (liftM)
4042
4143data WriterState =
4244 WriterState { stNotes :: [[Block]]
4345 , stLinks :: KeyTable
4446 , stImages :: KeyTable
45− , stIncludes :: [String]
47+ , stHasMath :: Bool
4648 , stOptions :: WriterOptions
4749 }
4850
@@ -50,34 +52,38 @@ data WriterState =
5052writeRST :: WriterOptions -> Pandoc -> String
5153writeRST opts document =
5254 let st = WriterState { stNotes = [], stLinks = [],
53− stImages = [], stIncludes = [],
55+ stImages = [], stHasMath = False,
5456 stOptions = opts }
55− in render $ evalState (pandocToRST document) st
57+ in evalState (pandocToRST document) st
5658
5759-- | Return RST representation of document.
58−pandocToRST :: Pandoc -> State WriterState Doc
60+pandocToRST :: Pandoc -> State WriterState String
5961pandocToRST (Pandoc meta blocks) = do
60− return empty -- TODO
61−-- opts <- get >>= (return . stOptions)
62−-- let before = writerIncludeBefore opts
63−-- after = writerIncludeAfter opts
64−-- header = writerHeader opts
65−-- before' = if null before then empty else text before
66−-- after' = if null after then empty else text after
67−-- header' = if null header then empty else text header
68−-- metaBlock <- metaToRST opts meta
69−-- let head' = if (writerStandalone opts)
70−-- then metaBlock $+$ header'
71−-- else empty
72−-- body <- blockListToRST blocks
73−-- includes <- get >>= (return . concat . stIncludes)
74−-- let includes' = if null includes then empty else text includes
75−-- notes <- get >>= (notesToRST . reverse . stNotes)
76−-- -- note that the notes may contain refs, so we do them first
77−-- refs <- get >>= (keyTableToRST . reverse . stLinks)
78−-- pics <- get >>= (pictTableToRST . reverse . stImages)
79−-- return $ head' $+$ before' $+$ includes' $+$ body $+$ notes $+$ text "" $+$
80−-- refs $+$ pics $+$ after'
62+ opts <- liftM stOptions get
63+ let before = writerIncludeBefore opts
64+ after = writerIncludeAfter opts
65+ before' = if null before then empty else text before
66+ after' = if null after then empty else text after
67+ metaBlock <- metaToRST opts meta
68+ let head' = if writerStandalone opts
69+ then render metaBlock
70+ else ""
71+ body <- blockListToRST blocks
72+ notes <- liftM (reverse . stNotes) get >>= notesToRST
73+ -- note that the notes may contain refs, so we do them first
74+ refs <- liftM (reverse . stLinks) get >>= keyTableToRST
75+ pics <- liftM (reverse . stImages) get >>= pictTableToRST
76+ hasMath <- liftM stHasMath get
77+ let main = render $ before' $+$ body $+$ notes $+$
78+ text "" $+$ refs $+$ pics $+$ after'
79+ let context = writerVariables opts ++
80+ [ ("body", main)
81+ , ("titleblock", head')
82+ ] ++
83+ [ ("math", "yes") | hasMath ]
84+ if writerStandalone opts
85+ then return $ renderTemplate context $ writerTemplate opts
86+ else return main
8187
8288-- | Return RST representation of reference key table.
8389keyTableToRST :: KeyTable -> State WriterState Doc
@@ -310,12 +316,7 @@ inlineToRST Ellipses = return $ text "..."
310316inlineToRST (Code str) = return $ text $ "``" ++ str ++ "``"
311317inlineToRST (Str str) = return $ text $ escapeString str
312318inlineToRST (Math t str) = do
313− includes <- get >>= (return . stIncludes)
314− let rawMathRole = ".. role:: math(raw)\n" ++
315− " :format: html latex\n"
316− if not (rawMathRole `elem` includes)
317− then modify $ \st -> st { stIncludes = rawMathRole : includes }
318− else return ()
319+ modify $ \st -> st{ stHasMath = True }
319320 return $ if t == InlineMath
320321 then text $ ":math:`$" ++ str ++ "$`"
321322 else text $ ":math:`$$" ++ str ++ "$$`"