Text.Pandoc.Templates

pandoc-1.17.0.1: Conversion between markup formats

Copyright | Copyright (C) 2009-2015 John MacFarlane
License | GNU GPL, version 2 or above
Maintainer | John MacFarlane <jgm@berkeley.edu>
Stability | alpha
Portability | portable
Safe Haskell | None
Language | Haskell98

Text.Pandoc.Templates

Description

A simple templating system with variable substitution and conditionals. The following program illustrates its use:

    import Data.Text
    import Data.Aeson
    import Text.Pandoc.Templates

    data Employee = Employee { firstName :: String
                             , lastName  :: String
                             , salary    :: Maybe Int }
    instance ToJSON Employee where
      toJSON e = object [ "name" .= object [ "first" .= firstName e
                                           , "last"  .= lastName e ]
                        , "salary" .= salary e ]

    employees :: [Employee]
    employees = [ Employee "John" "Doe" Nothing
                , Employee "Omar" "Smith" (Just 30000)
                , Employee "Sara" "Chen" (Just 60000) ]

    template :: Template
    template = either error id $ compileTemplate
      "$for(employee)$Hi, $employee.name.first$. $if(employee.salary)$You make $employee.salary$.$else$No salary data.$endif$$sep$\n$endfor$"

    main = putStrLn $ renderTemplate template $ object ["employee" .= employees ]

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 `.`.

The values of variables are determined by a JSON object that is passed as a parameter to `renderTemplate`. So, for example, `title` will return the value of the `title` field, and `employee.salary` will return the value of the `salary` field of the object that is the value of the `employee` field.

The value of a variable will be indented to the same level as the variable.

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.

Conditional keywords should not be indented, or unexpected spacing problems may occur.

The `$for$` keyword can be used to iterate over an array. If the value of the associated variable is not an array, a single iteration will be performed on its value.

You may optionally specify separators using `$sep$`, as in the example above.

Synopsis

- renderTemplate :: (ToJSON a, TemplateTarget b) => Template -> a -> b
- renderTemplate' :: (ToJSON a, TemplateTarget b) => String -> a -> b
- class TemplateTarget a where
  - toTarget :: Text -> a
- varListToJSON :: [(String, String)] -> Value
- compileTemplate :: Text -> Either String Template
- data Template
- getDefaultTemplate :: Maybe FilePath -> String -> IO (Either IOException String)

# Documentation

renderTemplate :: (ToJSON a, TemplateTarget b) => Template -> a -> b

renderTemplate' :: (ToJSON a, TemplateTarget b) => String -> a -> b

Like `renderTemplate`, but compiles the template first, raising an error if compilation fails.

class TemplateTarget a where

Methods

toTarget :: Text -> a

Instances

TemplateTarget String |
TemplateTarget ByteString |
TemplateTarget Text |
TemplateTarget Html |

varListToJSON :: [(String, String)] -> Value

compileTemplate :: Text -> Either String Template

data Template

Instances

Monoid Template |

getDefaultTemplate

Arguments

:: Maybe FilePath | User data directory to search first
-> String | Name of writer
-> IO (Either IOException String) |

Get default template for the specified writer.