@@ -35,6 +35,7 @@ import Data.List ( isSuffixOf, intercalate, intersperse )
3535import Control.Monad.State
3636import Control.Monad (liftM)
3737import Text.PrettyPrint.HughesPJ hiding ( Str )
38+import Text.Pandoc.Templates ( renderTemplate )
3839
3940data WriterState =
4041 WriterState { stNextRef :: Int -- number of next URL reference
@@ -52,54 +53,34 @@ writeConTeXt options document =
5253 , stOrderedListLevel = 0
5354 , stOptions = options
5455 }
55− in render $
56− evalState (pandocToConTeXt options document) defaultWriterState
56+ in evalState (pandocToConTeXt options document) defaultWriterState
5757
58−pandocToConTeXt :: WriterOptions -> Pandoc -> State WriterState Doc
59−pandocToConTeXt options (Pandoc meta blocks) = do
60− return empty -- TODO
61−-- main <- blockListToConTeXt blocks
62−-- let before = if null (writerIncludeBefore options)
63−-- then empty
64−-- else text $ writerIncludeBefore options
65−-- let after = if null (writerIncludeAfter options)
66−-- then empty
67−-- else text $ writerIncludeAfter options
68−-- let body = before $$ main $$ after
69−-- head' <- if writerStandalone options
70−-- then contextHeader options meta
71−-- else return empty
72−-- let toc = if writerTableOfContents options
73−-- then text "\\placecontent\n"
74−-- else empty
75−-- let foot = if writerStandalone options
76−-- then text "\\stoptext\n"
77−-- else empty
78−-- return $ head' $$ toc $$ body $$ foot
79−
80−-- | Insert bibliographic information into ConTeXt header.
81−contextHeader :: WriterOptions -- ^ Options, including ConTeXt header
82− -> Meta -- ^ Meta with bibliographic information
83− -> State WriterState Doc
84−contextHeader options (Meta title authors date) = do
85− return empty -- TODO
86−-- titletext <- if null title
87−-- then return empty
88−-- else inlineListToConTeXt title
89−-- let authorstext = if null authors
90−-- then ""
91−-- else if length authors == 1
92−-- then stringToConTeXt $ head authors
93−-- else stringToConTeXt $ (intercalate ", " $
94−-- init authors) ++ " & " ++ last authors
95−-- let datetext = if date == ""
96−-- then ""
97−-- else stringToConTeXt date
98−-- let titleblock = text "\\doctitle{" <> titletext <> char '}' $$
99−-- text ("\\author{" ++ authorstext ++ "}") $$
100−-- text ("\\date{" ++ datetext ++ "}")
101−-- let header = text $ writerHeader options
102−-- return $ header $$ titleblock $$ text "\\starttext\n\\maketitle\n"
58+pandocToConTeXt :: WriterOptions -> Pandoc -> State WriterState String
59+pandocToConTeXt options (Pandoc (Meta title authors date) blocks) = do
60+ body <- blockListToConTeXt blocks
61+ let before = if null (writerIncludeBefore options)
62+ then empty
63+ else text $ writerIncludeBefore options
64+ let after = if null (writerIncludeAfter options)
65+ then empty
66+ else text $ writerIncludeAfter options
67+ let main = render $ before $$ body $$ after
68+ titletext <- if null title
69+ then return ""
70+ else liftM render $ inlineListToConTeXt title
71+ authorstext <- mapM (liftM render . inlineListToConTeXt) authors
72+ datetext <- if null date
73+ then return ""
74+ else liftM render $ inlineListToConTeXt date
75+ let context = writerVariables options ++
76+ [ ("toc", if writerTableOfContents options then "yes" else "")
77+ , ("body", main)
78+ , ("title", titletext)
79+ , ("authors", intercalate "\\\\" authorstext)
80+ , ("date", datetext) ]
81+ return $ if writerStandalone options
82+ then renderTemplate context $ writerTemplate options
83+ else main
10384
10485-- escape things as needed for ConTeXt
10586