@@ -1,3 +1,4 @@
1+{-# LANGUAGE TypeSynonymInstances #-}
2Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
@@ -49,7 +50,9 @@ Conditional keywords should not be indented, or unexpected spacing
49problems may occur.
50problems may occur.
52−module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
53+module Text.Pandoc.Templates ( renderTemplate
55+ , getDefaultTemplate) where
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)
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
88+class TemplateTarget a where
89+ toTarget :: String -> a
91+instance TemplateTarget String where
94+instance TemplateTarget ByteString where
95+ toTarget = fromString
97+instance TemplateTarget Html where
100+instance TemplateTarget Doc where
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
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
91reservedWords :: [String]
113reservedWords :: [String]
92reservedWords = ["else","endif"]
114reservedWords = ["else","endif"]