glabrous-0.3.4: A template DSL 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

Instances

Eq Template Source # 
Show Template Source # 
Generic Template Source # 

Associated Types

type Rep Template :: * -> * #

Methods

from :: Template -> Rep Template x #

to :: Rep Template x -> Template #

Serialize Template Source # 
type Rep Template Source # 
type Rep Template = D1 (MetaData "Template" "Text.Glabrous.Types" "glabrous-0.3.4-5Kzw6xAY1NgBuPuf4AV36r" False) (C1 (MetaCons "Template" PrefixI True) (S1 (MetaSel (Just Symbol "content") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 [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

tagsOf :: Template -> [Tag] Source #

Get the list of Tags in the given Template.

isFinal :: Template -> Bool Source #

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

toText :: Template -> Text Source #

Output the content of the given Template as it is, with its Tags, if they exist.

toFinalText :: Template -> Text Source #

Output the content of the given Template with all its Tags removed.

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.

fromTagsList :: [Text] -> Context Source #

Build an unset Context from a list of Tags.

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

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

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

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

fromTemplate :: Template -> Context Source #

Build an unset ad hoc Context from the given Template.

Context operations

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

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

λ>setVariables [("tag","replacement"), ("theme","Haskell")] context
Context {variables = fromList [("etc.","..."),("theme","Haskell"),("tag","replacement"),("name","")]}

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

Delete variables from a Context by these names.

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

variablesOf :: Context -> [Text] Source #

Get the list of the given Context variables.

isSet :: Context -> Bool Source #

True if the all variables of the given Context are not empty.

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

readContextFile :: FilePath -> IO (Maybe Context) Source #

Get a Context from a JSON file.

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

Write a Context to a file.

{
    "tag": "replacement",
    "etc.": "..."
}

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

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

{
    "tag": "",
    "etc.": ""
}

Processing

process :: Template -> Context -> Text Source #

Process, discard Tags which are not in the Context and replace them with nothing 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

Instances

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 its unset ad hoc Context.

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