git.iko.soy/typeable/pandoc
on master help sign in
Made renderTemplate polymorphic; added TemplateTarget class.

Now renderTemplate can return an Html, a Doc, a ByteString, or a String. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1712 788f1e2b-df1e-0410-8736-df70ead52e1b

commitd5907b3034994c98d9ea534574b36942717bb241 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 31 Dec 2009, 01:14 (17 years ago) parent22dcf7a
2 files changed+30−7
src/Text/Pandoc/Templates.hs+26−4 src/Text/Pandoc/Writers/HTML.hs+4−3
src/Text/Pandoc/Templates.hs+26−4
@@ -1,3 +1,4 @@
1+{-# LANGUAGE TypeSynonymInstances #-}
12{-
23Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
34
@@ -49,7 +50,9 @@ Conditional keywords should not be indented, or unexpected spacing
4950problems may occur.
5051-}
5152
52−module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
53+module Text.Pandoc.Templates ( renderTemplate
54+ , TemplateTarget
55+ , getDefaultTemplate) where
5356
5457import Text.ParserCombinators.Parsec
5558import Control.Monad (liftM, when)
@@ -57,6 +60,9 @@ import qualified Control.Exception as E (try, IOException)
5760import System.FilePath
5861import Text.Pandoc.Shared (readDataFile)
5962import Data.List (intercalate)
63+import Text.PrettyPrint (text, Doc)
64+import Text.XHtml (primHtml, Html)
65+import Data.ByteString.Lazy.UTF8 (ByteString, fromString)
6066
6167-- | Get the default template, either from the application's user data
6268-- directory (~/.pandoc on unix) or from the cabal data directory.
@@ -79,14 +85,30 @@ adjustPosition str = do
7985 else TemplateState (length lastline) x
8086 return str
8187
88+class TemplateTarget a where
89+ toTarget :: String -> a
90+
91+instance TemplateTarget String where
92+ toTarget = id
93+
94+instance TemplateTarget ByteString where
95+ toTarget = fromString
96+
97+instance TemplateTarget Html where
98+ toTarget = primHtml
99+
100+instance TemplateTarget Doc where
101+ toTarget = text
102+
82103-- | Renders a template
83−renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables
104+renderTemplate :: TemplateTarget a
105+ => [(String,String)] -- ^ Assoc. list of values for variables
84106 -> String -- ^ Template
85− -> String
107+ -> a
86108renderTemplate vals templ =
87109 case runParser (do x <- parseTemplate; eof; return x) (TemplateState 0 vals) "template" templ of
88110 Left e -> error $ show e
89− Right r -> concat r
111+ Right r -> toTarget $ concat r
90112
91113reservedWords :: [String]
92114reservedWords = ["else","endif"]
src/Text/Pandoc/Writers/HTML.hs+4−3
@@ -89,7 +89,7 @@ writeHtml opts d =
8989 let (tit, auths, date, toc, body', newvars) = evalState (pandocToHtml opts d)
9090 defaultWriterState
9191 in if writerStandalone opts
92− then primHtml $ inTemplate opts tit auths date toc body' newvars
92+ then inTemplate opts tit auths date toc body' newvars
9393 else body'
9494
9595-- result is (title, authors, date, toc, body, new variables)
@@ -135,14 +135,15 @@ pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
135135 [("math", renderHtmlFragment math) | stMath st]
136136 return (tit, auths, date, toc, thebody, newvars)
137137
138−inTemplate :: WriterOptions
138+inTemplate :: TemplateTarget a
139+ => WriterOptions
139140 -> Html
140141 -> [Html]
141142 -> Html
142143 -> Html
143144 -> Html
144145 -> [(String,String)]
145− -> String
146+ -> a
146147inTemplate opts tit auths date toc body' newvars =
147148 let renderedTit = showHtmlFragment tit
148149 topTitle' = stripTags renderedTit