heist-1.0.1.2: An Haskell template system supporting both HTML5 and XML.

Safe HaskellNone
LanguageHaskell98

Heist

Contents

Description

This module defines the core data types used by Heist. In practice you will also want to import one or both of Heist.Compiled or Heist.Interpreted to get functions needed for writing splices.

The Heist template system allows you to build custom HTML and XML based markup languages. With Heist you can define your own domain-specific tags implemented with Haskell and use them in your templates.

Synopsis

Primary Heist initialization functions

loadTemplates :: FilePath -> IO (Either [String] TemplateRepo) Source #

Loads templates from disk. This function returns just a template map so you can load multiple directories and combine the maps before initializing your HeistState.

reloadTemplates :: TemplateRepo -> IO (Either [String] TemplateRepo) Source #

Reloads all the templates an an existing TemplateRepo.

addTemplatePathPrefix :: ByteString -> TemplateRepo -> TemplateRepo Source #

Adds a path prefix to a templates in a map returned by loadTemplates. If you want to add multiple levels of directories, separate them with slashes as in "foo/bar". Using an empty string as a path prefix will leave the map unchanged.

initHeist :: Monad n => HeistConfig n -> IO (Either [String] (HeistState n)) Source #

This is the main Heist initialization function. You pass in a map of all templates and all of your splices and it constructs and returns a HeistState.

We don't provide functions to add either type of loadtime splices to your HeistState after initHeist because it doesn't make any sense unless you re-initialize all templates with the new splices. If you add any old-style runtime heist splices after calling this function, they will still work fine if you use Heist.Interpreted.renderTemplate. If you add any templates later, then those templates will be available for interpreted rendering, but not for compiled rendering.

In the past you could add templates to your HeistState after initialization using its Monoid instance. Due to implementation details, this is no longer possible. All of your templates must be known when you call this function.

initHeistWithCacheTag :: MonadIO n => HeistConfig n -> IO (Either [String] (HeistState n, CacheTagState)) Source #

Wrapper around initHeist that also sets up a cache tag. It sets up both compiled and interpreted versions of the cache tag splices. If you need to configure the cache tag differently than how this function does it, you will still probably want to pattern your approach after this function's implementation.

defaultInterpretedSplices :: MonadIO m => Splices (Splice m) Source #

The built-in set of static splices. All the splices that used to be enabled by default are included here. To get the normal Heist behavior you should include these in the scLoadTimeSplices list in your SpliceConfig. If you are using interpreted splice mode, then you might also want to bind the deprecatedContentCheck splice to the content tag as a load time splice.

defaultLoadTimeSplices :: MonadIO m => Splices (Splice m) Source #

The built-in set of splices that you should use in compiled splice mode. This list includes everything from defaultInterpretedSplices plus a splice for the content tag that errors out when it sees any instance of the old content tag, which has now been moved to two separate tags called apply-content and bind-content.

emptyHeistConfig :: HeistConfig m Source #

An empty HeistConfig that uses the "h" namespace with error checking turned on.

Core Heist data types

data SpliceConfig m Source #

The splices and templates Heist will use. To bind a splice simply include it in the appropriate place here.

type TemplateLocation = IO (Either [String] TemplateRepo) Source #

An IO action for getting a template repo from this location. By not just using a directory path here, we support templates loaded from a database, retrieved from the network, or anything else you can think of.

type Template = [Node] Source #

A Template is a forest of XML nodes. Here we deviate from the "single root node" constraint of well-formed XML because we want to allow templates to contain document fragments that may not have a single root.

type TPath = [ByteString] Source #

Reversed list of directories. This holds the path to the template currently being processed.

type MIMEType = ByteString Source #

MIME Type. The type alias is here to make the API clearer.

data DocumentFile Source #

Holds data about templates read from disk.

Constructors

DocumentFile 

type AttrSplice m = Text -> RuntimeSplice m [(Text, Text)] Source #

Type alias for attribute splices. The function parameter is the value of the bound attribute splice. The return value is a list of attribute key/value pairs that get substituted in the place of the bound attribute.

data RuntimeSplice m a Source #

Monad used for runtime splice execution.

Instances

MonadTrans RuntimeSplice Source # 

Methods

lift :: Monad m => m a -> RuntimeSplice m a #

Monad m => Monad (RuntimeSplice m) Source # 

Methods

(>>=) :: RuntimeSplice m a -> (a -> RuntimeSplice m b) -> RuntimeSplice m b #

(>>) :: RuntimeSplice m a -> RuntimeSplice m b -> RuntimeSplice m b #

return :: a -> RuntimeSplice m a #

fail :: String -> RuntimeSplice m a #

Functor m => Functor (RuntimeSplice m) Source # 

Methods

fmap :: (a -> b) -> RuntimeSplice m a -> RuntimeSplice m b #

(<$) :: a -> RuntimeSplice m b -> RuntimeSplice m a #

Monad m => Applicative (RuntimeSplice m) Source # 

Methods

pure :: a -> RuntimeSplice m a #

(<*>) :: RuntimeSplice m (a -> b) -> RuntimeSplice m a -> RuntimeSplice m b #

liftA2 :: (a -> b -> c) -> RuntimeSplice m a -> RuntimeSplice m b -> RuntimeSplice m c #

(*>) :: RuntimeSplice m a -> RuntimeSplice m b -> RuntimeSplice m b #

(<*) :: RuntimeSplice m a -> RuntimeSplice m b -> RuntimeSplice m a #

MonadIO m => MonadIO (RuntimeSplice m) Source # 

Methods

liftIO :: IO a -> RuntimeSplice m a #

(Monad m, Monoid a) => Monoid (RuntimeSplice m a) Source # 

data Chunk m Source #

Opaque type representing pieces of output from compiled splices.

Instances

Show (Chunk m) Source # 

Methods

showsPrec :: Int -> Chunk m -> ShowS #

show :: Chunk m -> String #

showList :: [Chunk m] -> ShowS #

data HeistState m Source #

Holds all the state information needed for template processing. You will build a HeistState using initHeist and any of Heist's HeistState -> HeistState "filter" functions. Then you use the resulting HeistState in calls to renderTemplate.

m is the runtime monad

data CompileException Source #

Exception type for splice compile errors. Wraps the original exception and provides context. data (Exception e) => CompileException e = CompileException

data HeistT n m a Source #

HeistT is the monad transformer used for splice processing. HeistT intentionally does not expose any of its functionality via MonadState or MonadReader functions. We define passthrough instances for the most common types of monads. These instances allow the user to use HeistT in a monad stack without needing calls to lift.

n is the runtime monad (the parameter to HeistState).

m is the monad being run now. In this case, "now" is a variable concept. The type HeistT n n means that "now" is runtime. The type HeistT n IO means that "now" is IO, and more importantly it is NOT runtime. In Heist, the rule of thumb is that IO means load time and n means runtime.

Instances

MonadBase b m => MonadBase b (HeistT n m) Source # 

Methods

liftBase :: b α -> HeistT n m α #

MonadBaseControl b m => MonadBaseControl b (HeistT n m) Source # 

Associated Types

type StM (HeistT n m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (HeistT n m) b -> b a) -> HeistT n m a #

restoreM :: StM (HeistT n m) a -> HeistT n m a #

MonadState s m => MonadState s (HeistT n m) Source #

MonadState passthrough instance

Methods

get :: HeistT n m s #

put :: s -> HeistT n m () #

state :: (s -> (a, s)) -> HeistT n m a #

MonadReader r m => MonadReader r (HeistT n m) Source #

MonadReader passthrough instance

Methods

ask :: HeistT n m r #

local :: (r -> r) -> HeistT n m a -> HeistT n m a #

reader :: (r -> a) -> HeistT n m a #

MonadError e m => MonadError e (HeistT n m) Source #

MonadError passthrough instance

Methods

throwError :: e -> HeistT n m a #

catchError :: HeistT n m a -> (e -> HeistT n m a) -> HeistT n m a #

MonadTrans (HeistT n) Source #

MonadTrans instance

Methods

lift :: Monad m => m a -> HeistT n m a #

MonadTransControl (HeistT n) Source # 

Associated Types

type StT (HeistT n :: (* -> *) -> * -> *) a :: * #

Methods

liftWith :: Monad m => (Run (HeistT n) -> m a) -> HeistT n m a #

restoreT :: Monad m => m (StT (HeistT n) a) -> HeistT n m a #

Monad m => Monad (HeistT n m) Source #

Monad instance

Methods

(>>=) :: HeistT n m a -> (a -> HeistT n m b) -> HeistT n m b #

(>>) :: HeistT n m a -> HeistT n m b -> HeistT n m b #

return :: a -> HeistT n m a #

fail :: String -> HeistT n m a #

Functor m => Functor (HeistT n m) Source #

Functor instance

Methods

fmap :: (a -> b) -> HeistT n m a -> HeistT n m b #

(<$) :: a -> HeistT n m b -> HeistT n m a #

MonadFix m => MonadFix (HeistT n m) Source #

MonadFix passthrough instance

Methods

mfix :: (a -> HeistT n m a) -> HeistT n m a #

(Monad m, Functor m) => Applicative (HeistT n m) Source #

Applicative instance

Methods

pure :: a -> HeistT n m a #

(<*>) :: HeistT n m (a -> b) -> HeistT n m a -> HeistT n m b #

liftA2 :: (a -> b -> c) -> HeistT n m a -> HeistT n m b -> HeistT n m c #

(*>) :: HeistT n m a -> HeistT n m b -> HeistT n m b #

(<*) :: HeistT n m a -> HeistT n m b -> HeistT n m a #

(Functor m, MonadPlus m) => Alternative (HeistT n m) Source #

Alternative passthrough instance

Methods

empty :: HeistT n m a #

(<|>) :: HeistT n m a -> HeistT n m a -> HeistT n m a #

some :: HeistT n m a -> HeistT n m [a] #

many :: HeistT n m a -> HeistT n m [a] #

MonadPlus m => MonadPlus (HeistT n m) Source #

MonadPlus passthrough instance

Methods

mzero :: HeistT n m a #

mplus :: HeistT n m a -> HeistT n m a -> HeistT n m a #

MonadIO m => MonadIO (HeistT n m) Source #

MonadIO instance

Methods

liftIO :: IO a -> HeistT n m a #

MonadCont m => MonadCont (HeistT n m) Source #

MonadCont passthrough instance

Methods

callCC :: ((a -> HeistT n m b) -> HeistT n m a) -> HeistT n m a #

type StT (HeistT n) a Source # 
type StT (HeistT n) a = (a, HeistState n)
type StM (HeistT n m) a Source # 
type StM (HeistT n m) a = ComposeSt (HeistT n) m a

Lenses (can be used with lens or lens-family)

scInterpretedSplices :: Functor f => (Splices (Splice m) -> f (Splices (Splice m))) -> SpliceConfig m -> f (SpliceConfig m) Source #

Lens for interpreted splices :: Simple Lens (SpliceConfig m) (Splices (I.Splice m))

scLoadTimeSplices :: Functor f => (Splices (Splice IO) -> f (Splices (Splice IO))) -> SpliceConfig m -> f (SpliceConfig m) Source #

Lens for load time splices :: Simple Lens (SpliceConfig m) (Splices (I.Splice IO))

scCompiledSplices :: Functor f => (Splices (Splice m) -> f (Splices (Splice m))) -> SpliceConfig m -> f (SpliceConfig m) Source #

Lens for complied splices :: Simple Lens (SpliceConfig m) (Splices (C.Splice m))

scAttributeSplices :: Functor f => (Splices (AttrSplice m) -> f (Splices (AttrSplice m))) -> SpliceConfig m -> f (SpliceConfig m) Source #

Lens for attribute splices :: Simple Lens (SpliceConfig m) (Splices (AttrSplice m))

scTemplateLocations :: Functor f => ([TemplateLocation] -> f [TemplateLocation]) -> SpliceConfig m -> f (SpliceConfig m) Source #

Lens for template locations :: Simple Lens (SpliceConfig m) [TemplateLocation]

scCompiledTemplateFilter :: Functor f => ((TPath -> Bool) -> f (TPath -> Bool)) -> SpliceConfig m -> f (SpliceConfig m) Source #

Lens for compiled template filter :: Simple Lens (SpliceConfig m) (TBool -> Bool)

hcSpliceConfig :: Functor f => (SpliceConfig m -> f (SpliceConfig m)) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for the SpliceConfig :: Simple Lens (HeistConfig m) (SpliceConfig m)

hcNamespace :: Functor f => (Text -> f Text) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for the namespace :: Simple Lens (HeistConfig m) Text

hcErrorNotBound :: Functor f => (Bool -> f Bool) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for the namespace error flag :: Simple Lens (HeistConfig m) Bool

hcInterpretedSplices :: Functor f => (Splices (Splice m) -> f (Splices (Splice m))) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for interpreted splices :: Simple Lens (HeistConfig m) (Splices (I.Splice m))

hcLoadTimeSplices :: Functor f => (Splices (Splice IO) -> f (Splices (Splice IO))) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for load time splices :: Simple Lens (HeistConfig m) (Splices (I.Splice IO))

hcCompiledSplices :: Functor f => (Splices (Splice m) -> f (Splices (Splice m))) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for compiled splices :: Simple Lens (HeistConfig m) (Splices (C.Splice m))

hcAttributeSplices :: Functor f => (Splices (AttrSplice m) -> f (Splices (AttrSplice m))) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for attribute splices :: Simple Lens (HeistConfig m) (Splices (AttrSplice m))

hcTemplateLocations :: Functor f => ([TemplateLocation] -> f [TemplateLocation]) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for template locations :: Simple Lens (HeistConfig m) [TemplateLocation]

hcCompiledTemplateFilter :: Functor f => ((TPath -> Bool) -> f (TPath -> Bool)) -> HeistConfig m -> f (HeistConfig m) Source #

Lens for compiled template filter :: Simple Lens (SpliceConfig m) (TBool -> Bool)

HeistT functions

templateNames :: HeistState m -> [TPath] Source #

Gets the names of all the templates defined in a HeistState.

compiledTemplateNames :: HeistState m -> [TPath] Source #

Gets the names of all the templates defined in a HeistState.

hasTemplate :: ByteString -> HeistState n -> Bool Source #

Returns True if the given template can be found in the heist state.

spliceNames :: HeistState m -> [Text] Source #

Gets the names of all the interpreted splices defined in a HeistState.

compiledSpliceNames :: HeistState m -> [Text] Source #

Gets the names of all the compiled splices defined in a HeistState.

evalHeistT :: Monad m => HeistT n m a -> Node -> HeistState n -> m a Source #

Evaluates a template monad as a computation in the underlying monad.

getParamNode :: Monad m => HeistT n m Node Source #

Gets the node currently being processed.

<speech author="Shakespeare">
  To sleep, perchance to dream.
</speech>

When you call getParamNode inside the code for the speech splice, it returns the Node for the speech tag and its children. getParamNode >>= childNodes returns a list containing one TextNode containing part of Hamlet's speech. liftM (getAttribute "author") getParamNode would return Just "Shakespeare".

getContext :: Monad m => HeistT n m TPath Source #

Gets the current context

getTemplateFilePath :: Monad m => HeistT n m (Maybe FilePath) Source #

Gets the full path to the file holding the template currently being processed. Returns Nothing if the template is not associated with a file on disk or if there is no template being processed.

localParamNode :: Monad m => (Node -> Node) -> HeistT n m a -> HeistT n m a Source #

HeistT's local.

getsHS :: Monad m => (HeistState n -> r) -> HeistT n m r Source #

HeistT's gets.

getHS :: Monad m => HeistT n m (HeistState n) Source #

HeistT's get.

putHS :: Monad m => HeistState n -> HeistT n m () Source #

HeistT's put.

modifyHS :: Monad m => (HeistState n -> HeistState n) -> HeistT n m () Source #

HeistT's modify.

restoreHS :: Monad m => HeistState n -> HeistT n m () Source #

Restores the HeistState. This function is almost like putHS except it preserves the current doctypes and splice errors. You should use this function instead of putHS to restore an old state. This was needed because doctypes needs to be in a "global scope" as opposed to the template call "local scope" of state items such as recursionDepth, curContext, and spliceMap.

localHS :: Monad m => (HeistState n -> HeistState n) -> HeistT n m a -> HeistT n m a Source #

Abstracts the common pattern of running a HeistT computation with a modified heist state.

getDoc :: String -> IO (Either String DocumentFile) Source #

Reads an HTML template from disk.

getXMLDoc :: String -> IO (Either String DocumentFile) Source #

Reads an XML template from disk.

tellSpliceError :: Monad m => Text -> HeistT n m () Source #

Adds an error message to the list of splice processing errors.

spliceErrorText :: SpliceError -> Text Source #

Transform a SpliceError record to a Text message.

orError :: Monad m => HeistT n m b -> String -> HeistT n m b Source #

If Heist is running in fail fast mode, then this function will throw an exception with the second argument as the error message. Otherwise, the first argument will be executed to represent silent failure.

This behavior allows us to fail quickly if an error crops up during load-time splice processing or degrade more gracefully if the error occurs while a user request is being processed.

type Splices s = MapSyntax Text s Source #

Convenient type alies for splices.