git.iko.soy/typeable/pandoc
on master help sign in
Fixed RST writer to use new templates.

Use stHasMath instead of stIncludes. This gives the user more control over how the math directive is defined. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1715 788f1e2b-df1e-0410-8736-df70ead52e1b

commitd35f34fb44c61b445316997e35c77d8650bf4423 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 31 Dec 2009, 01:14 (17 years ago) parent8995278
3 files changed+42−65
src/Text/Pandoc/Writers/RST.hs+32−31 templates/rst.template+9−34 tests/writer.rst+1
src/Text/Pandoc/Writers/RST.hs+32−31
@@ -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 ++ "$$`"
templates/rst.template+9−34
@@ -1,39 +1,14 @@
1−===============
2−:math:`$title$`
3−===============
4−
5−:Author: $authors$
6−:Date: $date$
7−
8−.. contents::
9−
10−$header-includes$
1+$if(titleblock)$
2+$titleblock$
113
4+$endif$
5+$if(math)$
126.. role:: math(raw)
137 :format: html latex
148
15−section oen
16−===========
17−
18−
19−1. one
20−
21− a. two
22−
23− iii. three
24−
25−
26−
27−::
28−
29− hi
30−
31−footnote [1]_
32−
33−.. [1]
34− with code
35−
36− ::
37−
38− code
9+$endif$
10+$if(header-includes)$
11+$header-includes$
3912
13+$endif$
14+$body$
tests/writer.rst+1
@@ -887,3 +887,4 @@ indented.
887887
888888.. |lalune| image:: lalune.jpg
889889.. |movie| image:: movie.jpg
890+