@@ -44,7 +44,7 @@ data WriterState =
4444 WriterState { stNotes :: [[Block]]
4545 , stLinks :: KeyTable
4646 , stImages :: KeyTable
47− , stIncludes :: [Doc]
47+ , stIncludes :: [String]
4848 , stOptions :: WriterOptions
4949 }
5050
@@ -69,12 +69,14 @@ pandocToRST (Pandoc meta blocks) = do
6969 then metaBlock $+$ text (writerHeader opts)
7070 else empty
7171 body <- blockListToRST blocks
72+ includes <- get >>= (return . concat . stIncludes)
73+ let includes' = if null includes then empty else text includes
7274 notes <- get >>= (notesToRST . reverse . stNotes)
7375 -- note that the notes may contain refs, so we do them first
7476 refs <- get >>= (keyTableToRST . reverse . stLinks)
7577 pics <- get >>= (pictTableToRST . reverse . stImages)
76− return $ head $+$ before' $+$ body $+$ notes $+$ text "" $+$ refs $+$
77− pics $+$ after'
78+ return $ head $+$ before' $+$ includes' $+$ body $+$ notes $+$ text "" $+$
79+ refs $+$ pics $+$ after'
7880
7981-- | Return RST representation of reference key table.
8082keyTableToRST :: KeyTable -> State WriterState Doc
@@ -117,8 +119,11 @@ pictToRST (label, (src, _)) = do
117119
118120-- | Take list of inline elements and return wrapped doc.
119121wrappedRST :: WriterOptions -> [Inline] -> State WriterState Doc
120−wrappedRST opts inlines = mapM (wrapIfNeeded opts inlineListToRST)
121− (splitBy LineBreak inlines) >>= return . vcat
122+wrappedRST opts inlines = do
123+ lineBreakDoc <- inlineToRST LineBreak
124+ chunks <- mapM (wrapIfNeeded opts inlineListToRST)
125+ (splitBy LineBreak inlines)
126+ return $ vcat $ intersperse lineBreakDoc chunks
122127
123128-- | Escape special characters for RST.
124129escapeString :: String -> String
@@ -293,10 +298,18 @@ inlineToRST Apostrophe = return $ char '\''
293298inlineToRST Ellipses = return $ text "..."
294299inlineToRST (Code str) = return $ text $ "``" ++ str ++ "``"
295300inlineToRST (Str str) = return $ text $ escapeString str
296−inlineToRST (Math str) = return $ text $ "$" ++ str ++ "$"
301+inlineToRST (Math str) = do
302+ includes <- get >>= (return . stIncludes)
303+ let rawMathRole = ".. role:: math(raw)\n\
304+ \ :format: html latex\n"
305+ if not (rawMathRole `elem` includes)
306+ then modify $ \st -> st { stIncludes = rawMathRole : includes }
307+ else return ()
308+ return $ text $ ":math:`$" ++ str ++ "$`"
297309inlineToRST (TeX str) = return empty
298310inlineToRST (HtmlInline str) = return empty
299−inlineToRST (LineBreak) = return $ char ' ' -- RST doesn't have linebreaks
311+inlineToRST (LineBreak) = do
312+ return $ empty -- there's no line break in RST
300313inlineToRST Space = return $ char ' '
301314inlineToRST (Link [Code str] (src, tit)) | src == str ||
302315 src == "mailto:" ++ str = do