aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Templates.hs
blob: 98eed72923869c8bcdd681f3a7de3c06b25d9739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{-
Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

{- |
   Module      : Text.Pandoc.Templates
   Copyright   : Copyright (C) 2009 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

A simple templating system with variable substitution and conditionals.
Example:

> > renderTemplate [("name","Sam"),("salary","50,000")] $
>   "Hi, $name$.  $if(salary)$You make $$$salary$.$else$No salary data.$endif$" 
> > "Hi, John.  You make $50,000."

A slot for an interpolated variable is a variable name surrounded
by dollar signs.  To include a literal @$@ in your template, use
@$$@.  Variable names must begin with a letter and can contain letters,
numbers, @_@, and @-@.

A conditional begins with @$if(variable_name)$@ and ends with @$endif$@.
It may optionally contain an @$else$@ section.  The if section is
used if @variable_name@ has a non-null value, otherwise the else section
is used.
-}

module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where

import Text.ParserCombinators.Parsec
import Control.Monad (liftM)
import qualified Control.Exception as E (try, IOException)
import System.FilePath
import System.Directory
import Prelude hiding (readFile)
import System.IO.UTF8 (readFile)
import Paths_pandoc

-- | Get the default template, either from the application's user data
-- directory (~/.pandoc on unix) or from the cabal data directory.
getDefaultTemplate :: String -> IO (Either E.IOException String)
getDefaultTemplate "native" = return $ Right ""
getDefaultTemplate "odt" = getDefaultTemplate "opendocument"
getDefaultTemplate format = do
  let format' = takeWhile (/='+') format  -- strip off "+lhs" if present
  ut <- getTemplateFromUserDataDirectory format'
  case ut of
       Right t -> return $ Right t
       Left _  -> getTemplateFromCabalDataDirectory format'
 
getTemplateFromUserDataDirectory :: String -> IO (Either E.IOException String)
getTemplateFromUserDataDirectory format = E.try $ do
  userDir <- getAppUserDataDirectory "pandoc"
  let templatePath = userDir </> "templates" </> format <.> "template"
  readFile templatePath

getTemplateFromCabalDataDirectory :: String -> IO (Either E.IOException String)
getTemplateFromCabalDataDirectory format = E.try $ do 
  templatePath <- getDataFileName $ "templates" </> format <.> "template"
  readFile templatePath

-- | Renders a template 
renderTemplate :: [(String,String)]  -- ^ Assoc. list of values for variables
               -> String             -- ^ Template
               -> String
renderTemplate vals templ =
  case runParser (do x <- parseTemplate; eof; return x) vals "template" templ of
       Left e        -> show e
       Right r       -> concat r

reservedWords :: [String]
reservedWords = ["else","endif"]

parseTemplate :: GenParser Char [(String,String)] [String]
parseTemplate =
  many $ plaintext <|> escapedDollar <|> conditional <|> variable

plaintext :: GenParser Char [(String,String)] String
plaintext = many1 $ satisfy (/='$')

escapedDollar :: GenParser Char [(String,String)] String
escapedDollar = try $ string "$$" >> return "$"

conditional :: GenParser Char [(String,String)] String
conditional = try $ do
  string "$if("
  id' <- ident
  string ")$"
  skipMany (oneOf " \t")
  optional newline
  ifContents <- liftM concat parseTemplate
  elseContents <- option "" $ do try (string "$else$")
                                 skipMany (oneOf " \t")
                                 optional newline
                                 liftM concat parseTemplate
  string "$endif$"
  skipMany (oneOf " \t")
  optional newline
  st <- getState
  return $ case lookup id' st of
             Just ""  -> elseContents
             Just _   -> ifContents
             Nothing  -> elseContents

ident :: GenParser Char [(String,String)] String
ident = do
  first <- letter
  rest <- many (alphaNum <|> oneOf "_-")
  let id' = first : rest
  if id' `elem` reservedWords
     then pzero
     else return id'

variable :: GenParser Char [(String,String)] String
variable = try $ do
  char '$'
  id' <- ident
  char '$'
  st <- getState
  return $ case lookup id' st of
           Just val  -> val
           Nothing   -> ""