-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | An implementation of the Jinja2 template language in Haskell -- -- Ginger is Jinja, minus the most blatant pythonisms. Wants to be -- feature complete, but isn't quite there yet. @package ginger @version 0.9.1.0 -- | A HTML type, useful for implementing type-safe conversion between -- plain text and HTML. The HTML representation used here assumed Unicode -- throughout, and UTF-8 should be used as the encoding when sending -- Html objects as responses to a HTTP client. module Text.Ginger.Html -- | A chunk of HTML source. data Html -- | Convert a chunk of HTML source code into an Html value as-is. -- Note that this bypasses any and all HTML encoding; the caller is -- responsible for taking appropriate measures against XSS and other -- potential vulnerabilities. In other words, the input to this function -- is considered pre-sanitized. unsafeRawHtml :: Text -> Html -- | Safely convert plain text to HTML. html :: Text -> Html -- | Extract HTML source code from an Html value. htmlSource :: Html -> Text -- | Types that support conversion to HTML. class ToHtml s toHtml :: ToHtml s => s -> Html instance GHC.Classes.Ord Text.Ginger.Html.Html instance GHC.Classes.Eq Text.Ginger.Html.Html instance GHC.Show.Show Text.Ginger.Html.Html instance GHC.Base.Monoid Text.Ginger.Html.Html instance GHC.Base.Semigroup Text.Ginger.Html.Html instance Text.Ginger.Html.ToHtml Data.Text.Internal.Text instance Text.Ginger.Html.ToHtml [GHC.Types.Char] instance Text.Ginger.Html.ToHtml Text.Ginger.Html.Html -- | GVal is a generic unitype value, representing the kind of values that -- Ginger can understand. -- -- Most of the types in this module are parametrized over an m -- type, which is the host monad for template execution, as passed to -- runGingerT. For most kinds of values, m is -- transparent, and in many cases a ToGVal instance can be written -- that works for all possible m; the reason we need to -- parametrize the values themselves over the carrier monad is because we -- want to support impure functions, which requires access to the -- underlying carrier monad (e.g. IO). module Text.Ginger.GVal -- | A variant type designed as the unitype for the template language. Any -- value referenced in a template, returned from within a template, or -- used in a template context, will be a GVal. m, in most -- cases, should be a Monad. -- -- Some laws apply here, most notably: -- --
-- >>> asText $ list ("Found " ~: (6 :: Int) ~: " items" ~: [] :: [GVal IO])
-- "Found 6 items"
--
(~:) :: ToGVal m a => a -> Cons m -> Cons m
infixr 5 ~:
-- | Construct a GVal from a list of GVals. This is equivalent to the
-- toGVal implementation of [GVal m], but typed more
-- narrowly for clarity and disambiguation.
list :: Cons m -> GVal m
-- | Check if the given GVal is a list-like object
isList :: GVal m -> Bool
-- | Check if the given GVal is a dictionary-like object
isDict :: GVal m -> Bool
-- | Treat a GVal as a flat list and look up a value by integer
-- index. If the value is not a List, or if the index exceeds the list
-- length, return Nothing.
lookupIndex :: Int -> GVal m -> Maybe (GVal m)
-- | Helper function; look up a value by an integer index when the index
-- may or may not be available. If no index is given, return
-- Nothing.
lookupIndexMay :: Maybe Int -> GVal m -> Maybe (GVal m)
-- | Strictly-typed lookup: treat value as a dictionary-like object and
-- look up the value at a given key.
lookupKey :: Text -> GVal m -> Maybe (GVal m)
-- | Loosely-typed lookup: try dictionary-style lookup first (treat index
-- as a string, and container as a dictionary), if that doesn't yield
-- anything (either because the index is not string-ish, or because the
-- container doesn't provide dictionary-style access), try index-based
-- lookup.
lookupLoose :: GVal m -> GVal m -> Maybe (GVal m)
-- | Like lookupLoose, but fall back to the given default value if
-- the key is not in the dictionary, or if the indexee is not a
-- dictionary-like object.
lookupLooseDef :: GVal m -> GVal m -> GVal m -> GVal m
(~!) :: FromGVal m v => GVal m -> GVal m -> Maybe v
-- | Treat a GVal as a dictionary and list all the keys, with no
-- particular ordering.
keys :: GVal m -> Maybe [Text]
-- | Convert a GVal to a number.
toNumber :: GVal m -> Maybe Scientific
-- | Convert a GVal to an Int. The conversion will fail when
-- the value is not numeric, and also if it is too large to fit in an
-- Int.
toInt :: GVal m -> Maybe Int
-- | Convert a GVal to an Integer The conversion will fail
-- when the value is not an integer
toInteger :: GVal m -> Maybe Integer
-- | Convert a GVal to an Int, falling back to the given
-- default if the conversion fails.
toIntDef :: Int -> GVal m -> Int
-- | Convert a GVal to an Int, falling back to zero (0) if
-- the conversion fails.
toInt0 :: GVal m -> Int
-- | Loose cast to boolean.
--
-- Numeric zero, empty strings, empty lists, empty objects,
-- Null, and boolean False are considered falsy, anything
-- else (including functions) is considered true-ish.
toBoolean :: GVal m -> Bool
-- | Dynamically cast to a function. This yields Just a
-- Function if the value is a function, Nothing if it's
-- not.
toFunction :: GVal m -> Maybe (Function m)
picoToScientific :: Pico -> Scientific
scientificToPico :: Scientific -> Pico
class FromGVal m a
fromGValEither :: FromGVal m a => GVal m -> Either String a
fromGVal :: FromGVal m a => GVal m -> Maybe a
fromGValM :: (Monad m, FromGVal m a) => GVal m -> m a
pairwise :: (a -> b) -> (a, a) -> (b, b)
packPair :: ([Char], [Char]) -> (Text, Text)
unpackPair :: (Text, Text) -> ([Char], [Char])
instance Text.Ginger.GVal.FromGVal m GHC.Types.Int
instance Text.Ginger.GVal.FromGVal m Data.Scientific.Scientific
instance Text.Ginger.GVal.FromGVal m GHC.Integer.Type.Integer
instance Text.Ginger.GVal.FromGVal m Data.Text.Internal.Text
instance Text.Ginger.GVal.FromGVal m (Text.Ginger.GVal.GVal m)
instance Text.Ginger.GVal.FromGVal m Data.ByteString.Internal.ByteString
instance Text.Ginger.GVal.FromGVal m Data.ByteString.Lazy.Internal.ByteString
instance Text.Ginger.GVal.FromGVal m a => Text.Ginger.GVal.FromGVal m (GHC.Maybe.Maybe a)
instance Text.Ginger.GVal.FromGVal m GHC.Types.Bool
instance Text.Ginger.GVal.FromGVal m Data.Aeson.Types.Internal.Value
instance Text.Ginger.GVal.FromGVal m ()
instance Text.Ginger.GVal.FromGVal m a => Text.Ginger.GVal.FromGVal m [a]
instance (Text.Ginger.GVal.FromGVal m a, Text.Ginger.GVal.FromGVal m b) => Text.Ginger.GVal.FromGVal m (a, b)
instance (Text.Ginger.GVal.FromGVal m a, Text.Ginger.GVal.FromGVal m b, Text.Ginger.GVal.FromGVal m c) => Text.Ginger.GVal.FromGVal m (a, b, c)
instance (Text.Ginger.GVal.FromGVal m a, Text.Ginger.GVal.FromGVal m b, Text.Ginger.GVal.FromGVal m c, Text.Ginger.GVal.FromGVal m d) => Text.Ginger.GVal.FromGVal m (a, b, c, d)
instance Text.Ginger.GVal.FromGVal m Data.Time.Calendar.Days.Day
instance Text.Ginger.GVal.FromGVal m Data.Time.LocalTime.Internal.TimeOfDay.TimeOfDay
instance Text.Ginger.GVal.FromGVal m Data.Time.LocalTime.Internal.LocalTime.LocalTime
instance Text.Ginger.GVal.FromGVal m Data.Time.LocalTime.Internal.ZonedTime.ZonedTime
instance Text.Ginger.GVal.FromGVal m Data.Time.LocalTime.Internal.TimeZone.TimeZone
instance Text.Ginger.GVal.FromGVal m Data.Time.Format.Locale.TimeLocale
instance Text.Ginger.GVal.ToGVal m (Text.Ginger.GVal.GVal m)
instance Text.Ginger.GVal.ToGVal m ()
instance Text.Ginger.GVal.ToGVal m v => Text.Ginger.GVal.ToGVal m (GHC.Maybe.Maybe v)
instance Text.Ginger.GVal.ToGVal m v => Text.Ginger.GVal.ToGVal m [v]
instance Text.Ginger.GVal.ToGVal m v => Text.Ginger.GVal.ToGVal m (Data.HashMap.Base.HashMap Data.Text.Internal.Text v)
instance Text.Ginger.GVal.ToGVal m GHC.Types.Int
instance Text.Ginger.GVal.ToGVal m GHC.Integer.Type.Integer
instance Text.Ginger.GVal.ToGVal m Data.Scientific.Scientific
instance Text.Ginger.GVal.ToGVal m Data.Time.Calendar.Days.Day
instance Text.Ginger.GVal.ToGVal m Data.Time.LocalTime.Internal.TimeOfDay.TimeOfDay
instance Text.Ginger.GVal.ToGVal m Data.Time.LocalTime.Internal.LocalTime.LocalTime
instance Text.Ginger.GVal.ToGVal m Data.Time.LocalTime.Internal.TimeZone.TimeZone
instance Text.Ginger.GVal.ToGVal m Data.Time.Format.Locale.TimeLocale
instance Text.Ginger.GVal.ToGVal m Data.Time.LocalTime.Internal.ZonedTime.ZonedTime
instance (Text.Ginger.GVal.ToGVal m a, Text.Ginger.GVal.ToGVal m b) => Text.Ginger.GVal.ToGVal m (a, b)
instance (Text.Ginger.GVal.ToGVal m a, Text.Ginger.GVal.ToGVal m b, Text.Ginger.GVal.ToGVal m c) => Text.Ginger.GVal.ToGVal m (a, b, c)
instance (Text.Ginger.GVal.ToGVal m a, Text.Ginger.GVal.ToGVal m b, Text.Ginger.GVal.ToGVal m c, Text.Ginger.GVal.ToGVal m d) => Text.Ginger.GVal.ToGVal m (a, b, c, d)
instance Text.Ginger.GVal.ToGVal m GHC.Types.Bool
instance Text.Ginger.GVal.ToGVal m GHC.Types.Char
instance Text.Ginger.GVal.ToGVal m Data.Text.Internal.Text
instance Text.Ginger.GVal.ToGVal m Data.Text.Internal.Lazy.Text
instance Text.Ginger.GVal.ToGVal m Data.ByteString.Internal.ByteString
instance Text.Ginger.GVal.ToGVal m Data.ByteString.Lazy.Internal.ByteString
instance Text.Ginger.GVal.ToGVal m Text.Ginger.Html.Html
instance Text.Ginger.GVal.ToGVal m Data.Aeson.Types.Internal.Value
instance Data.Default.Class.Default (Text.Ginger.GVal.GVal m)
instance Data.Aeson.Types.ToJSON.ToJSON (Text.Ginger.GVal.GVal m)
instance GHC.Show.Show (Text.Ginger.GVal.GVal m)
instance Text.Ginger.Html.ToHtml (Text.Ginger.GVal.GVal m)
instance Text.Printf.PrintfArg (Text.Ginger.GVal.GVal m)
instance Data.String.IsString (Text.Ginger.GVal.GVal m)
-- | Implements Ginger's Abstract Syntax Tree.
module Text.Ginger.AST
-- | A context variable name.
type VarName = Text
-- | Top-level data structure, representing a fully parsed template.
data Template a
Template :: Statement a -> HashMap VarName (Block a) -> Maybe (Template a) -> Template a
[templateBody] :: Template a -> Statement a
[templateBlocks] :: Template a -> HashMap VarName (Block a)
[templateParent] :: Template a -> Maybe (Template a)
-- | A macro definition ( {% macro %} )
data Macro a
Macro :: [VarName] -> Statement a -> Macro a
[macroArgs] :: Macro a -> [VarName]
[macroBody] :: Macro a -> Statement a
-- | A block definition ( {% block %} )
data Block a
Block :: Statement a -> Block a
[blockBody] :: Block a -> Statement a
-- | Ginger statements.
data Statement a
-- | A sequence of multiple statements
MultiS :: a -> [Statement a] -> Statement a
-- | Run wrapped statement in a local scope
ScopedS :: a -> Statement a -> Statement a
-- | Establish an indented context around the wrapped statement
IndentS :: a -> Expression a -> Statement a -> Statement a
-- | Literal output (anything outside of any tag)
LiteralS :: a -> Html -> Statement a
-- | {{ expression }}
InterpolationS :: a -> Expression a -> Statement a
-- | Evaluate expression
ExpressionS :: a -> Expression a -> Statement a
-- | {% if expression %}statement{% else %}statement{% endif %}
IfS :: a -> Expression a -> Statement a -> Statement a -> Statement a
-- | {% switch expression %}{% case expression %}statement{% endcase
-- %}...{% default %}statement{% enddefault %}{% endswitch %}
SwitchS :: a -> Expression a -> [(Expression a, Statement a)] -> Statement a -> Statement a
-- | {% for index, varname in expression %}statement{% endfor %}
ForS :: a -> Maybe VarName -> VarName -> Expression a -> Statement a -> Statement a
-- | {% set varname = expr %}
SetVarS :: a -> VarName -> Expression a -> Statement a
-- | {% macro varname %}statements{% endmacro %}
DefMacroS :: a -> VarName -> Macro a -> Statement a
BlockRefS :: a -> VarName -> Statement a
-- | {% include "template" %}
PreprocessedIncludeS :: a -> Template a -> Statement a
-- | The do-nothing statement (NOP)
NullS :: a -> Statement a
-- | Try catch finally
TryCatchS :: a -> Statement a -> [CatchBlock a] -> Statement a -> Statement a
stmtAnnotation :: () => Statement p -> p
-- | A catch block
data CatchBlock a
Catch :: Maybe Text -> Maybe VarName -> Statement a -> CatchBlock a
[catchWhat] :: CatchBlock a -> Maybe Text
[catchCaptureAs] :: CatchBlock a -> Maybe VarName
[catchBody] :: CatchBlock a -> Statement a
-- | Expressions, building blocks for the expression minilanguage.
data Expression a
-- | String literal expression: "foobar"
StringLiteralE :: a -> Text -> Expression a
-- | Numeric literal expression: 123.4
NumberLiteralE :: a -> Scientific -> Expression a
-- | Boolean literal expression: true
BoolLiteralE :: a -> Bool -> Expression a
-- | Literal null
NullLiteralE :: a -> Expression a
-- | Variable reference: foobar
VarE :: a -> VarName -> Expression a
-- | List construct: [ expr, expr, expr ]
ListE :: a -> [Expression a] -> Expression a
-- | Object construct: { expr: expr, expr: expr, ... }
ObjectE :: a -> [(Expression a, Expression a)] -> Expression a
-- | foo[bar] (also dot access)
MemberLookupE :: a -> Expression a -> Expression a -> Expression a
-- | foo(bar=baz, quux)
CallE :: a -> Expression a -> [(Maybe Text, Expression a)] -> Expression a
-- | (foo, bar) -> expr
LambdaE :: a -> [Text] -> Expression a -> Expression a
-- | expr ? expr : expr
TernaryE :: a -> Expression a -> Expression a -> Expression a -> Expression a
-- | do { statement; }
DoE :: a -> Statement a -> Expression a
exprAnnotation :: () => Expression p -> p
class Annotated f
annotation :: Annotated f => f p -> p
instance GHC.Base.Functor Text.Ginger.AST.Block
instance GHC.Show.Show a => GHC.Show.Show (Text.Ginger.AST.Block a)
instance GHC.Base.Functor Text.Ginger.AST.Template
instance GHC.Show.Show a => GHC.Show.Show (Text.Ginger.AST.Template a)
instance GHC.Base.Functor Text.Ginger.AST.Macro
instance GHC.Show.Show a => GHC.Show.Show (Text.Ginger.AST.Macro a)
instance GHC.Base.Functor Text.Ginger.AST.CatchBlock
instance GHC.Show.Show a => GHC.Show.Show (Text.Ginger.AST.CatchBlock a)
instance GHC.Base.Functor Text.Ginger.AST.Statement
instance GHC.Show.Show a => GHC.Show.Show (Text.Ginger.AST.Statement a)
instance GHC.Base.Functor Text.Ginger.AST.Expression
instance GHC.Show.Show a => GHC.Show.Show (Text.Ginger.AST.Expression a)
instance Text.Ginger.AST.Annotated Text.Ginger.AST.Expression
instance Text.Ginger.AST.Annotated Text.Ginger.AST.Statement
instance Text.Ginger.AST.Annotated Text.Ginger.AST.Block
instance Text.Ginger.AST.Annotated Text.Ginger.AST.Macro
instance Text.Ginger.AST.Annotated Text.Ginger.AST.Template
-- | Ginger parser.
module Text.Ginger.Parse
-- | Parse Ginger source from memory. The initial template is taken
-- directly from the provided Source, while all subsequent
-- includes are loaded through the provided IncludeResolver.
parseGinger :: forall m. Monad m => IncludeResolver m -> Maybe SourceName -> Source -> m (Either ParserError (Template SourcePos))
-- | Parse Ginger source from a file. Both the initial template and all
-- subsequent includes are loaded through the provided
-- IncludeResolver. A consequence of this is that if you pass a
-- "null resolver" (like `const (return Nothing)`), this function will
-- always fail.
parseGingerFile :: forall m. Monad m => IncludeResolver m -> SourceName -> m (Either ParserError (Template SourcePos))
-- | Parse Ginger source from memory. Flavor of parseGinger that
-- takes additional ParserOptions.
parseGinger' :: Monad m => ParserOptions m -> Source -> m (Either ParserError (Template SourcePos))
-- | Parse Ginger source from a file. Flavor of parseGingerFile that
-- takes additional ParserOptions.
parseGingerFile' :: Monad m => ParserOptions m -> SourceName -> m (Either ParserError (Template SourcePos))
-- | Error information for Ginger parser errors.
data ParserError
ParserError :: String -> Maybe SourcePos -> ParserError
-- | Human-readable error message
[peErrorMessage] :: ParserError -> String
[peSourcePosition] :: ParserError -> Maybe SourcePos
data ParserOptions m
ParserOptions :: IncludeResolver m -> Maybe SourceName -> Bool -> Bool -> Bool -> Delimiters -> ParserOptions m
-- | How to load templates / includes
[poIncludeResolver] :: ParserOptions m -> IncludeResolver m
-- | Current source file name, if any
[poSourceName] :: ParserOptions m -> Maybe SourceName
-- | Disable newline stripping
[poKeepTrailingNewline] :: ParserOptions m -> Bool
-- | Enable auto-stripping of {% block %}s
[poLStripBlocks] :: ParserOptions m -> Bool
-- | Enable auto-trimming of {% block %}s
[poTrimBlocks] :: ParserOptions m -> Bool
-- | Interpolation, tag, and comment delimiters
[poDelimiters] :: ParserOptions m -> Delimiters
-- | Default parser options for a given resolver
mkParserOptions :: Monad m => IncludeResolver m -> ParserOptions m
-- | Delimiter configuration.
data Delimiters
Delimiters :: String -> String -> String -> String -> String -> String -> Delimiters
[delimOpenInterpolation] :: Delimiters -> String
[delimCloseInterpolation] :: Delimiters -> String
[delimOpenTag] :: Delimiters -> String
[delimCloseTag] :: Delimiters -> String
[delimOpenComment] :: Delimiters -> String
[delimCloseComment] :: Delimiters -> String
-- | Default delimiter configuration: {{ }} for interpolation,
-- {% %} for tags, {} for comments.
defDelimiters :: Delimiters
-- | Formats a parser errror into something human-friendly. If template
-- source code is not provided, only the line and column numbers and the
-- error message are printed. If template source code is provided, the
-- offending source line is also printed, with a caret (^)
-- marking the exact location of the error.
formatParserError :: Maybe String -> ParserError -> String
-- | Used to resolve includes. Ginger will call this function whenever it
-- encounters an {% include %}, {% import %}, or {% extends %} directive.
-- If the required source code is not available, the resolver should
-- return Nothing, else Just the source.
type IncludeResolver m = SourceName -> m (Maybe Source)
-- | Input type for the parser (source code).
type Source = String
type SourceName = String
-- | The abstract data type SourcePos represents source positions.
-- It contains the name of the source (i.e. file name), a line number and
-- a column number. SourcePos is an instance of the Show,
-- Eq and Ord class.
data SourcePos
-- | Extracts the name of the source from a source position.
sourceName :: SourcePos -> SourceName
-- | Extracts the line number from a source position.
sourceLine :: SourcePos -> Line
-- | Extracts the column number from a source position.
sourceColumn :: SourcePos -> Column
-- | Set the name of the source.
setSourceName :: SourcePos -> SourceName -> SourcePos
instance GHC.Generics.Generic Text.Ginger.Parse.ParserError
instance GHC.Show.Show Text.Ginger.Parse.ParserError
instance GHC.Exception.Type.Exception Text.Ginger.Parse.ParserError
instance Text.Ginger.GVal.ToGVal m Text.Parsec.Pos.SourcePos
module Text.PrintfA
data PrintfArgT
P :: a -> PrintfArgT
data PrintfTypeT
T :: (forall r. PrintfType r => r) -> PrintfTypeT
[unT] :: PrintfTypeT -> forall r. PrintfType r => r
printfa :: PrintfType t => String -> [PrintfArgT] -> t
-- | The internals of the Run monad, and various things needed to
-- make the magic happen. You will not normally need to import this
-- module; Run re-exports the things you probably want. However,
-- if you want to provide your own run monad that extends Run
-- somehow, this module may be of use.
module Text.Ginger.Run.Type
-- | Execution context. Determines how to look up variables from the
-- environment, and how to write out template output.
data GingerContext p m h
GingerContext :: (VarName -> Run p m h (GVal (Run p m h))) -> (h -> Run p m h ()) -> (RuntimeError p -> Run p m h ()) -> (GVal (Run p m h) -> h) -> Maybe (Newlines h) -> GingerContext p m h
[contextLookup] :: GingerContext p m h -> VarName -> Run p m h (GVal (Run p m h))
[contextWrite] :: GingerContext p m h -> h -> Run p m h ()
[contextWarn] :: GingerContext p m h -> RuntimeError p -> Run p m h ()
[contextEncode] :: GingerContext p m h -> GVal (Run p m h) -> h
[contextNewlines] :: GingerContext p m h -> Maybe (Newlines h)
-- | Deprecated: Compatibility alias for makeContextHtml
makeContext :: (VarName -> GVal (Run p (Writer Html) Html)) -> GingerContext p (Writer Html) Html
-- | Deprecated: Compatibility alias for makeContextHtmlM
makeContextM :: (Monad m, Functor m) => (VarName -> Run p m Html (GVal (Run p m Html))) -> (Html -> m ()) -> GingerContext p m Html
-- | Create an execution context for runGinger. The argument is a lookup
-- function that maps top-level context keys to ginger values.
-- makeContext is a specialized version of makeContextM,
-- targeting the Writer Html monad (which is what is used
-- for the non-monadic template interpreter runGinger).
--
-- The type of the lookup function may look intimidating, but in most
-- cases, marshalling values from Haskell to Ginger is a matter of
-- calling toGVal on them, so the 'GVal (Run (Writer Html))' part
-- can usually be ignored. See the GVal module for details.
makeContext' :: Monoid h => (VarName -> GVal (Run p (Writer h) h)) -> (GVal (Run p (Writer h) h) -> h) -> Maybe (Newlines h) -> GingerContext p (Writer h) h
-- | Create an execution context for runGingerT. Takes a lookup function,
-- which returns ginger values into the carrier monad based on a lookup
-- key, and a writer function (outputting HTML by whatever means the
-- carrier monad provides, e.g. putStr for IO, or
-- tell for Writers).
makeContextM' :: (Monad m, Functor m) => (VarName -> Run p m h (GVal (Run p m h))) -> (h -> m ()) -> (GVal (Run p m h) -> h) -> Maybe (Newlines h) -> GingerContext p m h
makeContextExM' :: (Monad m, Functor m) => (VarName -> Run p m h (GVal (Run p m h))) -> (h -> m ()) -> (RuntimeError p -> m ()) -> (GVal (Run p m h) -> h) -> Maybe (Newlines h) -> GingerContext p m h
makeContextHtml :: (VarName -> GVal (Run p (Writer Html) Html)) -> GingerContext p (Writer Html) Html
makeContextHtmlM :: (Monad m, Functor m) => (VarName -> Run p m Html (GVal (Run p m Html))) -> (Html -> m ()) -> GingerContext p m Html
makeContextHtmlExM :: (Monad m, Functor m) => (VarName -> Run p m Html (GVal (Run p m Html))) -> (Html -> m ()) -> (RuntimeError p -> m ()) -> GingerContext p m Html
makeContextText :: (VarName -> GVal (Run p (Writer Text) Text)) -> GingerContext p (Writer Text) Text
makeContextTextM :: (Monad m, Functor m) => (VarName -> Run p m Text (GVal (Run p m Text))) -> (Text -> m ()) -> GingerContext p m Text
makeContextTextExM :: (Monad m, Functor m) => (VarName -> Run p m Text (GVal (Run p m Text))) -> (Text -> m ()) -> (RuntimeError p -> m ()) -> GingerContext p m Text
easyContext :: (Monad m, ContextEncodable h, ToGVal (Run p m h) v) => (h -> m ()) -> v -> GingerContext p m h
-- | Typeclass that defines how to encode GVals into a given type.
class ContextEncodable h
encode :: forall m. ContextEncodable h => GVal m -> h
newlines :: ContextEncodable h => Maybe (Newlines h)
-- | Lift a value from the host monad m into the Run monad.
liftRun :: Monad m => m a -> Run p m h a
-- | Lift a function from the host monad m into the Run
-- monad.
liftRun2 :: Monad m => (a -> m b) -> a -> Run p m h b
-- | Internal type alias for our template-runner monad stack.
type Run p m h = ExceptT (RuntimeError p) (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
data RunState p m h
RunState :: HashMap VarName (GVal (Run p m h)) -> h -> Template p -> Maybe Text -> Maybe [h] -> Bool -> p -> RunState p m h
[rsScope] :: RunState p m h -> HashMap VarName (GVal (Run p m h))
[rsCapture] :: RunState p m h -> h
[rsCurrentTemplate] :: RunState p m h -> Template p
[rsCurrentBlockName] :: RunState p m h -> Maybe Text
[rsIndentation] :: RunState p m h -> Maybe [h]
[rsAtLineStart] :: RunState p m h -> Bool
[rsCurrentSourcePos] :: RunState p m h -> p
data RuntimeError p
-- | Generic runtime error
RuntimeError :: Text -> RuntimeError p
-- | Tried to use a block that isn't defined | Invalid arguments to
-- function (function name, explanation)
UndefinedBlockError :: Text -> RuntimeError p
ArgumentsError :: Maybe Text -> Text -> RuntimeError p
-- | Wrong type, expected one of...
TypeError :: [Text] -> Maybe Text -> RuntimeError p
-- | Invalid index
IndexError :: Text -> RuntimeError p
EvalParseError :: ParserError -> RuntimeError p
NotAFunctionError :: RuntimeError p
RuntimeErrorAt :: p -> RuntimeError p -> RuntimeError p
runtimeErrorWhat :: RuntimeError p -> Text
runtimeErrorWhere :: RuntimeError p -> [p]
runtimeErrorMessage :: RuntimeError p -> Text
-- | A Newlines determines the rules by which a h value can
-- be split into lines, how a list of lines can be joined into a single
-- value, and how to remove leading whitespace.
data Newlines h
Newlines :: (h -> [h]) -> ([h] -> h) -> (h -> h) -> (h -> Bool) -> Newlines h
[splitLines] :: Newlines h -> h -> [h]
[joinLines] :: Newlines h -> [h] -> h
[stripIndent] :: Newlines h -> h -> h
[endsWithNewline] :: Newlines h -> h -> Bool
-- | Hoist a context onto a different output type. hoistContext fwd rev
-- context returns a context over a different output type, applying
-- the fwd and rev projections to convert between the
-- original and desired output types.
hoistContext :: Monad m => (h -> t) -> (t -> h) -> GingerContext p m h -> GingerContext p m t
-- | Hoist a Run action onto a different output type. hoistRun
-- fwd rev action hoists the action from Run p m h
-- a to Run p m t a, applying fwd and rev
-- to convert between the output types.
hoistRun :: Monad m => (h -> t) -> (t -> h) -> Run p m h a -> Run p m t a
-- | Hoist a Newlines onto a different output type. You don't
-- normally need to use this directly; see hoistRun and/or
-- hoistContext.
hoistNewlines :: (h -> t) -> (t -> h) -> Newlines h -> Newlines t
-- | Hoist a RunState onto a different output type. You don't
-- normally need to use this directly; see hoistRun and/or
-- hoistContext.
hoistRunState :: Monad m => (h -> t) -> (t -> h) -> RunState p m h -> RunState p m t
warn :: Monad m => RuntimeError p -> Run p m h ()
warnFromMaybe :: Monad m => RuntimeError p -> a -> Maybe a -> Run p m h a
throwHere :: Monad m => RuntimeError p -> Run p m h a
-- | withSourcePos pos action runs action in a context
-- where the current source location is set to pos. The original
-- source position is restored when action finishes.
withSourcePos :: (Monad m, Applicative m, Functor m) => p -> Run p m h a -> Run p m h a
getSourcePos :: (Monad m, Applicative m, Functor m) => Run p m h p
instance GHC.Show.Show p => GHC.Show.Show (Text.Ginger.Run.Type.RuntimeError p)
instance Data.Default.Class.Default (Text.Ginger.Run.Type.RuntimeError p)
instance Text.Ginger.GVal.ToGVal m p => Text.Ginger.GVal.ToGVal m (Text.Ginger.Run.Type.RuntimeError p)
instance Text.Ginger.Run.Type.ContextEncodable Data.Text.Internal.Text
instance Text.Ginger.Run.Type.ContextEncodable Text.Ginger.Html.Html
module Text.Ginger.Run.FuncUtils
unaryFunc :: forall m h p. Monad m => (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
binaryFunc :: forall m h p. Monad m => (GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
ignoreArgNames :: ([a] -> b) -> [(c, a)] -> b
variadicNumericFunc :: Monad m => Scientific -> ([Scientific] -> Scientific) -> [(Maybe Text, GVal (Run p m h))] -> Run p m h (GVal (Run p m h))
unaryNumericFunc :: Monad m => Scientific -> (Scientific -> Scientific) -> [(Maybe Text, GVal (Run p m h))] -> Run p m h (GVal (Run p m h))
variadicStringFunc :: Monad m => ([Text] -> Text) -> [(Maybe Text, GVal (Run p m h))] -> Run p m h (GVal (Run p m h))
-- | Match args according to a given arg spec, Python style. The return
-- value is a triple of (matched, args, kwargs, unmatchedNames),
-- where matches is a hash map of named captured arguments, args
-- is a list of remaining unmatched positional arguments, kwargs is a
-- list of remaining unmatched named arguments, and
-- unmatchedNames contains the argument names that haven't been
-- matched.
extractArgs :: [Text] -> [(Maybe Text, a)] -> (HashMap Text a, [a], HashMap Text a, [Text])
-- | Parse argument list into type-safe argument structure.
extractArgsT :: ([Maybe a] -> b) -> [Text] -> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) b
-- | Parse argument list into flat list of matched arguments.
extractArgsL :: [Text] -> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [Maybe a]
extractArgsDefL :: [(Text, a)] -> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
injectDefaults :: [a] -> [Maybe a] -> [a]
module Text.Ginger.Run.VM
-- | Helper function to run a State action with a temporary state,
-- reverting to the old state after the action has finished.
withLocalState :: (Monad m, MonadState s m) => m a -> m a
-- | Helper function to run a Scope action with a temporary scope,
-- reverting to the old scope after the action has finished.
withLocalScope :: Monad m => Run p m h a -> Run p m h a
-- | Override the encoder used for converting GVals to the output
-- type. This can be used for things like temporarily disabling HTML
-- encoding.
withEncoder :: (ContextEncodable h, Monad m) => (GVal (Run p m h) -> h) -> Run p m h a -> Run p m h a
setVar :: Monad m => VarName -> GVal (Run p m h) -> Run p m h ()
getVar :: Monad m => VarName -> Run p m h (GVal (Run p m h))
clearCapture :: (Monoid h, Monad m) => Run p m h ()
appendCapture :: (Monoid h, Monad m) => h -> Run p m h ()
fetchCapture :: Monad m => Run p m h h
-- | Implementations of the default template context functions and values.
-- You will not normally need to import this module, unless you want to
-- provide your own context with things that reuse functionality from the
-- default ones.
module Text.Ginger.Run.Builtins
tshow :: Show a => a -> Text
gfnRawHtml :: Monad m => Function (Run p m h)
gfnUrlEncode :: Monad m => Function (Run p m h)
gfnDefault :: Monad m => Function m
gfnEscape :: Monad m => Function m
gfnEscaped :: Monad m => Function m
gfnAny :: Monad m => Function m
gfnAll :: Monad m => Function m
gfnIn :: Monad m => Function m
inList :: GVal m -> GVal m -> Bool
inDict :: GVal m -> GVal m -> Bool
gfnApply :: Monad m => Function (Run p m h)
gfnEquals :: Monad m => Function m
gfnNEquals :: Monad m => Function m
gfnContains :: Monad m => Function (Run p m h)
gfnConcat :: Monad m => Function m
looseEquals :: GVal m -> GVal m -> Bool
gfnLess :: Monad m => Function m
gfnGreater :: Monad m => Function m
gfnLessEquals :: Monad m => Function m
gfnGreaterEquals :: Monad m => Function m
less :: Monad m => GVal m -> GVal m -> Maybe Bool
greater :: Monad m => GVal m -> GVal m -> Maybe Bool
lessEq :: Monad m => GVal m -> GVal m -> Maybe Bool
greaterEq :: Monad m => GVal m -> GVal m -> Maybe Bool
difference :: Num a => [a] -> a
ratio :: (Show a, Fractional a, Num a) => [a] -> a
intRatio :: (Integral a, Num a) => [a] -> a
modulo :: (Integral a, Num a) => [a] -> a
capitalize :: Text -> Text
gfnCenter :: Monad m => Function m
gfnLength :: Monad m => Function (Run p m h)
gfnSlice :: Monad m => Function (Run p m h)
gfnReplace :: Monad m => Function (Run p m h)
gfnSplit :: Monad m => Function (Run p m h)
gfnCompose :: forall m p h. Monad m => Function (Run p m h)
gfnPartial :: forall m p h. Monad m => Function (Run p m h)
gfnZip :: forall m p h. Monad m => Function (Run p m h)
gfnZipWith :: forall m p h. Monad m => Function (Run p m h)
gfnMap :: Monad m => Function m
gfnSort :: forall p m h. Monad m => Function (Run p m h)
onFst :: (a -> a -> b) -> (a, c) -> (a, d) -> b
listToIndexedDict :: [a] -> [(Text, a)]
center :: Text -> Int -> Text -> Text
gfnFileSizeFormat :: Monad m => Function m
formatFileSize :: Bool -> Integer -> String
gfnPrintf :: Monad m => Function (Run p m h)
gvalToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalDictToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalListToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalAutoParseDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalToTZ :: GVal m -> Maybe TimeZone
parseTZ :: String -> Maybe TimeZone
gfnDateFormat :: Monad m => Function (Run p m h)
getTimeLocale :: Monad m => GVal (Run p m h) -> Run p m h TimeLocale
gfnFilter :: Monad m => Function (Run p m h)
printfG :: String -> [GVal m] -> String
gfnDictsort :: Monad m => Function (Run p m h)
gfnJSON :: Monad m => Function (Run p m h)
gfnDivisibleBy :: Monad m => Function (Run p m h)
gfnEven :: Monad m => Function (Run p m h)
gfnOdd :: Monad m => Function (Run p m h)
gfoRegex :: Monad m => GVal (Run p m h)
gfnReMatchOne :: forall p m h. Monad m => Function (Run p m h)
gfnReMatch :: forall p m h. Monad m => Function (Run p m h)
gfnReTest :: Monad m => Function (Run p m h)
matchTextToGVal :: Monad m => MatchText String -> GVal m
fnReMatch :: Monad m => (Regex -> String -> a) -> [(Maybe Text, GVal (Run p m h))] -> Run p m h a
parseCompOpts :: Monad m => GVal (Run p m h) -> Run p m h CompOption
-- | Execute Ginger templates in an arbitrary monad.
--
-- Usage example:
--
-- -- render :: Template -> Text -> Text -> Text -- render template username imageURL = do -- let contextLookup varName = -- case varName of -- "username" -> toGVal username -- "imageURL" -> toGVal imageURL -- _ -> def -- def for GVal is equivalent to a NULL value -- context = makeContextHtml contextLookup -- in htmlSource $ runGinger context template --module Text.Ginger.Run -- | Simplified interface to render a ginger template "into" a monad. -- -- easyRenderM emit context template renders the -- template with the given context object (which should -- represent some sort of dictionary-like object) by feeding any output -- to the emit function. easyRenderM :: (Monad m, ContextEncodable h, Monoid h, ToGVal (Run p m h) v, ToGVal (Run p m h) h, ToGVal (Run p m h) p) => (h -> m ()) -> v -> Template p -> m (Either (RuntimeError p) (GVal (Run p m h))) -- | Simplified interface to render a ginger template in a pure fashion. -- -- easyRender context template renders the template -- with the given context object (which should represent some -- sort of dictionary-like object) by returning the concatenated output. easyRender :: (ContextEncodable h, Monoid h, ToGVal (Run p (Writer h) h) v, ToGVal (Run p (Writer h) h) h, ToGVal (Run p (Writer h) h) p) => v -> Template p -> h easyContext :: (Monad m, ContextEncodable h, ToGVal (Run p m h) v) => (h -> m ()) -> v -> GingerContext p m h -- | Monadically run a Ginger template. The m parameter is the -- carrier monad. runGingerT :: (ToGVal (Run p m h) h, ToGVal (Run p m h) p, Monoid h, Monad m, Applicative m, Functor m) => GingerContext p m h -> Template p -> m (Either (RuntimeError p) (GVal (Run p m h))) -- | Purely expand a Ginger template. The underlying carrier monad is -- Writer h, which is used to collect the output and -- render it into a h value. runGinger :: (ToGVal (Run p (Writer h) h) h, ToGVal (Run p (Writer h) h) p, Monoid h) => GingerContext p (Writer h) h -> Template p -> h -- | Deprecated: Compatibility alias for makeContextHtml makeContext :: (VarName -> GVal (Run p (Writer Html) Html)) -> GingerContext p (Writer Html) Html -- | Deprecated: Compatibility alias for makeContextHtmlM makeContextM :: (Monad m, Functor m) => (VarName -> Run p m Html (GVal (Run p m Html))) -> (Html -> m ()) -> GingerContext p m Html -- | Create an execution context for runGinger. The argument is a lookup -- function that maps top-level context keys to ginger values. -- makeContext is a specialized version of makeContextM, -- targeting the Writer Html monad (which is what is used -- for the non-monadic template interpreter runGinger). -- -- The type of the lookup function may look intimidating, but in most -- cases, marshalling values from Haskell to Ginger is a matter of -- calling toGVal on them, so the 'GVal (Run (Writer Html))' part -- can usually be ignored. See the GVal module for details. makeContext' :: Monoid h => (VarName -> GVal (Run p (Writer h) h)) -> (GVal (Run p (Writer h) h) -> h) -> Maybe (Newlines h) -> GingerContext p (Writer h) h -- | Create an execution context for runGingerT. Takes a lookup function, -- which returns ginger values into the carrier monad based on a lookup -- key, and a writer function (outputting HTML by whatever means the -- carrier monad provides, e.g. putStr for IO, or -- tell for Writers). makeContextM' :: (Monad m, Functor m) => (VarName -> Run p m h (GVal (Run p m h))) -> (h -> m ()) -> (GVal (Run p m h) -> h) -> Maybe (Newlines h) -> GingerContext p m h makeContextExM' :: (Monad m, Functor m) => (VarName -> Run p m h (GVal (Run p m h))) -> (h -> m ()) -> (RuntimeError p -> m ()) -> (GVal (Run p m h) -> h) -> Maybe (Newlines h) -> GingerContext p m h makeContextHtml :: (VarName -> GVal (Run p (Writer Html) Html)) -> GingerContext p (Writer Html) Html makeContextHtmlM :: (Monad m, Functor m) => (VarName -> Run p m Html (GVal (Run p m Html))) -> (Html -> m ()) -> GingerContext p m Html makeContextHtmlExM :: (Monad m, Functor m) => (VarName -> Run p m Html (GVal (Run p m Html))) -> (Html -> m ()) -> (RuntimeError p -> m ()) -> GingerContext p m Html makeContextText :: (VarName -> GVal (Run p (Writer Text) Text)) -> GingerContext p (Writer Text) Text makeContextTextM :: (Monad m, Functor m) => (VarName -> Run p m Text (GVal (Run p m Text))) -> (Text -> m ()) -> GingerContext p m Text makeContextTextExM :: (Monad m, Functor m) => (VarName -> Run p m Text (GVal (Run p m Text))) -> (Text -> m ()) -> (RuntimeError p -> m ()) -> GingerContext p m Text -- | Execution context. Determines how to look up variables from the -- environment, and how to write out template output. data GingerContext p m h -- | Internal type alias for our template-runner monad stack. type Run p m h = ExceptT (RuntimeError p) (StateT (RunState p m h) (ReaderT (GingerContext p m h) m)) -- | Lift a value from the host monad m into the Run monad. liftRun :: Monad m => m a -> Run p m h a -- | Lift a function from the host monad m into the Run -- monad. liftRun2 :: Monad m => (a -> m b) -> a -> Run p m h b -- | Match args according to a given arg spec, Python style. The return -- value is a triple of (matched, args, kwargs, unmatchedNames), -- where matches is a hash map of named captured arguments, args -- is a list of remaining unmatched positional arguments, kwargs is a -- list of remaining unmatched named arguments, and -- unmatchedNames contains the argument names that haven't been -- matched. extractArgs :: [Text] -> [(Maybe Text, a)] -> (HashMap Text a, [a], HashMap Text a, [Text]) -- | Parse argument list into type-safe argument structure. extractArgsT :: ([Maybe a] -> b) -> [Text] -> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) b -- | Parse argument list into flat list of matched arguments. extractArgsL :: [Text] -> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [Maybe a] extractArgsDefL :: [(Text, a)] -> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a] -- | Hoist a context onto a different output type. hoistContext fwd rev -- context returns a context over a different output type, applying -- the fwd and rev projections to convert between the -- original and desired output types. hoistContext :: Monad m => (h -> t) -> (t -> h) -> GingerContext p m h -> GingerContext p m t -- | Hoist a Run action onto a different output type. hoistRun -- fwd rev action hoists the action from Run p m h -- a to Run p m t a, applying fwd and rev -- to convert between the output types. hoistRun :: Monad m => (h -> t) -> (t -> h) -> Run p m h a -> Run p m t a -- | Hoist a Newlines onto a different output type. You don't -- normally need to use this directly; see hoistRun and/or -- hoistContext. hoistNewlines :: (h -> t) -> (t -> h) -> Newlines h -> Newlines t -- | Hoist a RunState onto a different output type. You don't -- normally need to use this directly; see hoistRun and/or -- hoistContext. hoistRunState :: Monad m => (h -> t) -> (t -> h) -> RunState p m h -> RunState p m t data RuntimeError p -- | Generic runtime error RuntimeError :: Text -> RuntimeError p -- | Tried to use a block that isn't defined | Invalid arguments to -- function (function name, explanation) UndefinedBlockError :: Text -> RuntimeError p ArgumentsError :: Maybe Text -> Text -> RuntimeError p -- | Wrong type, expected one of... TypeError :: [Text] -> Maybe Text -> RuntimeError p -- | Invalid index IndexError :: Text -> RuntimeError p EvalParseError :: ParserError -> RuntimeError p NotAFunctionError :: RuntimeError p RuntimeErrorAt :: p -> RuntimeError p -> RuntimeError p runtimeErrorWhat :: RuntimeError p -> Text runtimeErrorWhere :: RuntimeError p -> [p] runtimeErrorMessage :: RuntimeError p -> Text -- | A syntax tree optimizer module Text.Ginger.Optimizer class Optimizable a optimize :: Optimizable a => a -> a instance GHC.Base.Monoid Text.Ginger.Optimizer.Collected instance GHC.Base.Semigroup Text.Ginger.Optimizer.Collected instance GHC.Enum.Bounded Text.Ginger.Optimizer.Purity instance GHC.Classes.Ord Text.Ginger.Optimizer.Purity instance GHC.Read.Read Text.Ginger.Optimizer.Purity instance GHC.Enum.Enum Text.Ginger.Optimizer.Purity instance GHC.Classes.Eq Text.Ginger.Optimizer.Purity instance GHC.Show.Show Text.Ginger.Optimizer.Purity instance Text.Ginger.GVal.ToGVal m Text.Ginger.Optimizer.Collected instance GHC.Base.Semigroup Text.Ginger.Optimizer.Purity instance GHC.Base.Monoid Text.Ginger.Optimizer.Purity instance Text.Ginger.Optimizer.Optimizable (Text.Ginger.AST.Template a) instance Text.Ginger.Optimizer.Optimizable (Text.Ginger.AST.Statement a) instance Text.Ginger.Optimizer.Optimizable (Text.Ginger.AST.Block a) instance Text.Ginger.Optimizer.Optimizable (Text.Ginger.AST.Macro a) instance Text.Ginger.Optimizer.Optimizable (Text.Ginger.AST.Expression a) -- | A Haskell implementation of the Jinja2 template language. -- -- Ginger aims to be as close to the original Jinja language as possible, -- but avoiding blatant pythonisms and features that make little sense -- outside of an impure dynamic host language context, especially when -- this would require sacrificing runtime performance. module Text.Ginger