glabrous-0.1.1.0: A template library

Safe HaskellNone
LanguageHaskell2010

Text.Glabrous

Contents

Description

A minimalistic Mustache-like syntax, truly logic-less, pure Text template library

  • Use only the simplest Mustache tag {{name}} called a variable.
  • HTML agnostic

Synopsis

Template

data Template Source

Constructors

Template 

Fields

content :: ![Token]
 

type Tag = Text Source

Get a Template

fromText :: Text -> Either String Template Source

Build a Template from a Text.

λ>fromText "Glabrous templates use only the simplest Mustache tag: {{name}}."
Right (Template {content = [Literal "Glabrous templates use only the simplest Mustache tag: ",Tag "name",Literal "."]})

Template operations

toText :: Template -> Text Source

Output the content of the given Template as it is, with its Tags, if they exist (no Context is processed).

isFinal :: Template -> Bool Source

True if a Template has no more Tag inside and can be used as a final Text.

tagsOf :: Template -> [Tag] Source

Get the list of Tags in the given Template.

compress :: Template -> Template Source

Optimize a Template content after (many) partialProcess(') rewriting(s).

writeTemplateFile :: FilePath -> Template -> IO () Source

Write a Template to a file.

Context

Get a Context

initContext :: Context Source

Build an empty Context.

fromList :: [(Text, Text)] -> Context Source

Build a Context from a list of Tags and replacement Texts.

λ>fromList [("something","something else"), ("etc.","...")]
Context {variables = fromList [("etc.","..."),("something","something else")]}

fromTemplate :: Template -> Context Source

Extract Tags from a Template and build a Context with all its variables empty.

Context operations

setVariables :: [(Text, Text)] -> Context -> Context Source

Populate with variables and/or update variables in the given Context.

λ>setVariables [("something","something new"), ("about","Haskell")] context
Context {variables = fromList [("etc.","..."),("about","Haskell"),("something","something new"),("name","")]}

deleteVariables :: [Text] -> Context -> Context Source

Delete variables from a Context by these names.

λ>deleteVariables ["something"] context
Context {variables = fromList [("etc.","..."),("about","Haskell"),("name","")]}

unsetContext :: Context -> Maybe Context Source

Build Just a (sub)Context made of unset variables of the given context, or Nothing.

λ>unsetContext context
Just (Context {variables = fromList [("name","")]})

JSON Context file

writeContextFile :: FilePath -> Context -> IO () Source

Write a Context to a file.

{
    "something": "something else",
    "etc.": "..."
}

initContextFile :: FilePath -> Context -> IO () Source

Based on the given Context, write a Context file with all its variables empty.

{
    "something": "",
    "etc.": ""
}

Processing

process :: Template -> Context -> Text Source

Process, discard Tags which are not in the Context and leave them without replacement text in the final Text.

processWithDefault Source

Arguments

:: Text

Default replacement text

-> Template 
-> Context 
-> Text 

Process and replace missing variables in Context with the given default replacement Text.

partialProcess :: Template -> Context -> Template Source

Process a (sub)Context present in the given template, leaving untouched, if they exist, other Tags, to obtain a new template.

data Result Source

Constructors

Final !Text 
Partial 

Fields

template :: !Template
 
tags :: ![Tag]
 

partialProcess' :: Template -> Context -> Result Source

Process a (sub)Context present in the given template, and get either a Final Text or a new Template with the list of its Tags.

λ>partialProcess' template context
Partial {template = Template {content = [Literal "Some ",Tag "tags",Literal " are unused in this ",Tag "text",Literal "."]}, tags = ["tags","text"]}