-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Required Variables in Template #19
base: master
Are you sure you want to change the base?
Support for Required Variables in Template #19
Conversation
Added renderTemplateM which allows for failure if a required variable is not in context. The template parser now checks for an '!' prefixing a variable. If found, the variable is considered required.
Context $ M.adjust | ||
(\z -> case z of | ||
_ | null vs -> x | ||
MapVal m -> | ||
MapVal $ addToContext (Variable vs fs) x m | ||
MapVal $ addToContext (Variable vs fs r) x m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure if I correctly modified addToContext
@@ -377,10 +377,11 @@ pOpen = pOpenDollar <|> pOpenBraces | |||
|
|||
pVar :: Monad m => Parser m Variable | |||
pVar = do | |||
required <- True <$ P.try (P.string "!") <|> pure False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may not be the syntax you are looking for.
|
||
-- | Render a compiled template in a "context" which provides | ||
-- values for the template's variables. | ||
renderTemplate :: (TemplateTarget a, ToContext a b) | ||
=> Template a -> b -> Doc a | ||
renderTemplate t x = S.evalState (renderTemp t (toContext x)) 0 | ||
renderTemplate t x = runIdentity $ renderTemplateM t x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe renderTemplate should be moved to Text.DocTemplates
resolveVariable' v val = | ||
case applyPipes (varPipes v) $ multiLookup (varParts v) val of | ||
ListVal xs -> mconcat $ map (resolveVariable' mempty) xs | ||
ListVal xs -> foldMapA (resolveVariable' mempty) xs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please double check ListVal line
BoolVal True -> resolved True ["true"] | ||
BoolVal False -> resolved False ["false"] | ||
NullVal | ||
| varRequired v -> missingRequired v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does missing Required need to be added to any other?
@@ -373,22 +376,42 @@ instance Monoid (Resolved a) where | |||
mappend = (<>) | |||
mempty = Resolved False [] | |||
|
|||
resolveVariable :: TemplateTarget a | |||
=> Variable -> Context a -> Resolved a | |||
class Monad m => MissingRequired m where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any other instances that should be added?
because PandocError accepts Text not String
Allows an error to be thrown if a required variable is missing from the context. This has only been applied to the HTML writer but could be added to all the writers. I do not think this is a breaking change since existing templates do not have required variables. It requires the new features in doctemplates pull request jgm/doctemplates#19
Added renderTemplateM which allows for failure if a required variable is not in context. The template parser now checks for an '!' prefixing a variable. If found, the variable is considered required.
Short script demonstration
closes #18