git.iko.soy/typeable/pandoc
on master help sign in
Use an interpreted text role to render math in restructuredText.

See http://www.american.edu/econ/itex2mml/mathhack.rst for the strategy. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1168 788f1e2b-df1e-0410-8736-df70ead52e1b

commit0921704d922efd394c7a6a961ea113d7f629231b authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateFri 4 Jan 2008, 18:59 (19 years ago) parentec3f6b6
3 files changed+34−14
README+4 Text/Pandoc/Writers/RST.hs+20−7 tests/writer.rst+10−7
README+4
@@ -852,6 +852,10 @@ TeX math will be printed in all output formats. In Markdown,
852852reStructuredText, LaTeX, and ConTeXt output, it will appear verbatim
853853between $ characters.
854854
855+In reStructuredText output, it will be rendered using an interpreted
856+text role `:math:`, as described
857+[here](http://www.american.edu/econ/itex2mml/mathhack.rst).
858+
855859In groff man output, it will be rendered verbatim without $'s.
856860
857861In RTF and Docbook output, it will be rendered, as far as possible,
Text/Pandoc/Writers/RST.hs+20−7
@@ -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
tests/writer.rst+10−7
@@ -6,6 +6,9 @@ Pandoc Test Suite
66:Author: Anonymous
77:Date: July 17, 2006
88
9+.. role:: math(raw)
10+ :format: html latex
11+
912This is a set of tests for pandoc. Most of them are adapted from
1013John Gruber's markdown test suite.
1114
@@ -620,14 +623,14 @@ LaTeX
620623
621624-
622625-
623−- $2+2=4$
624−- $x \in y$
625−- $\alpha \wedge \omega$
626−- $223$
627−- $p$-Tree
628−- $\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$
626+- :math:`$2+2=4$`
627+- :math:`$x \in y$`
628+- :math:`$\alpha \wedge \omega$`
629+- :math:`$223$`
630+- :math:`$p$`-Tree
631+- :math:`$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$`
629632- Here's one that has a line break in it:
630− $\alpha + \omega \times x^2$.
633+ :math:`$\alpha + \omega \times x^2$`.
631634
632635These shouldn't be math:
633636