Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

BebeSparkelSparkel
Copy link

@BebeSparkelSparkel BebeSparkelSparkel commented Dec 18, 2022

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

:set -XOverloadedStrings
:set -XTypeApplications
import Data.Map as M
Right t <- compileTemplate @IO @Text "somefile" "$abc$ - $!def$"
putStrLn $ "t: " <> show t
let c = M.fromList [("abc","ABC"), ("def","DEF")] :: Map Text Text
let d = renderTemplateM t c :: Either String (Doc Text)
putStrLn $ "d: " <> show d
let c' = M.fromList [("abc","ABC")] :: Map Text Text
let d' = renderTemplateM t c' :: Either String (Doc Text)
putStrLn $ "d': " <> show d'

closes #18

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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?

BebeSparkelSparkel added a commit to BebeSparkelSparkel/pandoc that referenced this pull request Dec 18, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error on missing context variable
1 participant