glabrous-2.0.6.2: A template DSL library
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Glabrous

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

newtype Template Source #

Constructors

Template 

Fields

Instances

Instances details
Generic Template Source # 
Instance details

Defined in Text.Glabrous.Types

Associated Types

type Rep Template :: Type -> Type #

Methods

from :: Template -> Rep Template x #

to :: Rep Template x -> Template #

Show Template Source # 
Instance details

Defined in Text.Glabrous.Types

Serialize Template Source # 
Instance details

Defined in Text.Glabrous.Types

Eq Template Source # 
Instance details

Defined in Text.Glabrous.Types

type Rep Template Source # 
Instance details

Defined in Text.Glabrous.Types

type Rep Template = D1 ('MetaData "Template" "Text.Glabrous.Types" "glabrous-2.0.6.2-JAOge6N9YJl1My40cftHUC" 'True) (C1 ('MetaCons "Template" 'PrefixI 'True) (S1 ('MetaSel ('Just "content") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Token])))

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

addTag Source #

Arguments

:: Template

The template to work on

-> Text

Text to be replaced by the new tag

-> Text

New tag's name

-> Maybe Template

Just a new template or nothing

Get Just a new Template with new tag(s) inside, or Nothing.

tagsOf :: Template -> [Token] 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.

insertTemplate Source #

Arguments

:: Template

The Template to insert in

-> Token

The Tag to be replaced

-> Template

The Template to be inserted

-> Maybe Template

Just the new Template, or Nothing

get Just a new Template by inserting a Template into another one by replacing the Tag, or Nothing.

λ>insertTemplate t0 (Tag "template1") t1

insertManyTemplates :: Template -> [(Token, Template)] -> Maybe Template Source #

get Just a new Template by inserting many Templates, if there is at least one tag correspondence, or Nothing.

λ>insertManyTemplates t0 [(Tag "template1",t1),(Tag "template2",t2)]

Context

newtype Context Source #

Constructors

Context 

Instances

Instances details
FromJSON Context Source # 
Instance details

Defined in Text.Glabrous.Types

ToJSON Context Source # 
Instance details

Defined in Text.Glabrous.Types

Show Context Source # 
Instance details

Defined in Text.Glabrous.Types

Eq Context Source # 
Instance details

Defined in Text.Glabrous.Types

Methods

(==) :: Context -> Context -> Bool #

(/=) :: Context -> Context -> Bool #

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","")]})

join :: Context -> Context -> Either Context Context Source #

Join two Contexts if they don't share variables name, or get the intersection Context out of them.

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

Instances details
Show Result Source # 
Instance details

Defined in Text.Glabrous.Types

Eq Result Source # 
Instance details

Defined in Text.Glabrous.Types

Methods

(==) :: Result -> Result -> Bool #

(/=) :: Result -> Result -> Bool #

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","")]}}