git.iko.soy/typeable/pandoc
on master help sign in
Made renderTemplate polymorphic; added TemplateTarget class.
d5907b3fiddlosopher
src/Text/Pandoc/Templates.hs+26−4
@@ -1,3 +1,4 @@
1+{-# LANGUAGE TypeSynonymInstances #-}
1{-
2{-
2Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
3
4
@@ -49,7 +50,9 @@ Conditional keywords should not be indented, or unexpected spacing
49problems may occur.
50problems may occur.
50-}
51-}
51
52
52−module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
53+module Text.Pandoc.Templates ( renderTemplate
54+ , TemplateTarget
55+ , getDefaultTemplate) where
53
56
54import Text.ParserCombinators.Parsec
57import Text.ParserCombinators.Parsec
55import Control.Monad (liftM, when)
58import Control.Monad (liftM, when)
@@ -57,6 +60,9 @@ import qualified Control.Exception as E (try, IOException)
57import System.FilePath
60import System.FilePath
58import Text.Pandoc.Shared (readDataFile)
61import Text.Pandoc.Shared (readDataFile)
59import Data.List (intercalate)
62import Data.List (intercalate)
63+import Text.PrettyPrint (text, Doc)
64+import Text.XHtml (primHtml, Html)
65+import Data.ByteString.Lazy.UTF8 (ByteString, fromString)
60
66
61-- | Get the default template, either from the application's user data
67-- | Get the default template, either from the application's user data
62-- directory (~/.pandoc on unix) or from the cabal data directory.
68-- directory (~/.pandoc on unix) or from the cabal data directory.
@@ -79,14 +85,30 @@ adjustPosition str = do
79 else TemplateState (length lastline) x
85 else TemplateState (length lastline) x
80 return str
86 return str
81
87
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+
82-- | Renders a template
103-- | Renders a template
104+renderTemplate :: TemplateTarget a
83−renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables
105+ => [(String,String)] -- ^ Assoc. list of values for variables
84 -> String -- ^ Template
106 -> String -- ^ Template
85− -> String
107+ -> a
86renderTemplate vals templ =
108renderTemplate vals templ =
87 case runParser (do x <- parseTemplate; eof; return x) (TemplateState 0 vals) "template" templ of
109 case runParser (do x <- parseTemplate; eof; return x) (TemplateState 0 vals) "template" templ of
88 Left e -> error $ show e
110 Left e -> error $ show e
89− Right r -> concat r
111+ Right r -> toTarget $ concat r
90
112
91reservedWords :: [String]
113reservedWords :: [String]
92reservedWords = ["else","endif"]
114reservedWords = ["else","endif"]
src/Text/Pandoc/Writers/HTML.hs+4−3
@@ -89,7 +89,7 @@ writeHtml opts d =
89 let (tit, auths, date, toc, body', newvars) = evalState (pandocToHtml opts d)
89 let (tit, auths, date, toc, body', newvars) = evalState (pandocToHtml opts d)
90 defaultWriterState
90 defaultWriterState
91 in if writerStandalone opts
91 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
93 else body'
93 else body'
94
94
95-- result is (title, authors, date, toc, body, new variables)
95-- result is (title, authors, date, toc, body, new variables)
@@ -135,14 +135,15 @@ pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
135 [("math", renderHtmlFragment math) | stMath st]
135 [("math", renderHtmlFragment math) | stMath st]
136 return (tit, auths, date, toc, thebody, newvars)
136 return (tit, auths, date, toc, thebody, newvars)
137
137
138+inTemplate :: TemplateTarget a
138−inTemplate :: WriterOptions
139+ => WriterOptions
139 -> Html
140 -> Html
140 -> [Html]
141 -> [Html]
141 -> Html
142 -> Html
142 -> Html
143 -> Html
143 -> Html
144 -> Html
144 -> [(String,String)]
145 -> [(String,String)]
145− -> String
146+ -> a
146inTemplate opts tit auths date toc body' newvars =
147inTemplate opts tit auths date toc body' newvars =
147 let renderedTit = showHtmlFragment tit
148 let renderedTit = showHtmlFragment tit
148 topTitle' = stripTags renderedTit
149 topTitle' = stripTags renderedTit