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

Add tests for compliance with lambda spec #50

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Text/Mustache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ not an instance of 'ToMustache' but an instance of 'ToJSON' you can use the
]
@

To define a mustache lambda, provide a `Text -> Text` function. This function
takes in the rendered text and returns text that should be displayed instead.
This doesn't completely implement the mustache spec for functions, but it should
suffice for most use cases.

For more advanced use cases, look at any 'ToMustache' instances for other
function types.

@
-- outputs: "Hello Alice"
substitute [mustache|{{#greeting}}{{name}}{{/greeting}}|] $ object
[ "name" ~> "Alice"
, "greeting" ~> \\t -> "Hello " <> t <> "!"
]
@

All operators are also provided in a unicode form, for those that, like me, enjoy
unicode operators.

Expand Down Expand Up @@ -192,6 +208,7 @@ import Text.Mustache.Types
import qualified Data.Text as T


{-# DEPRECATED overText "Use toMustache instead" #-}
-- | Creates a 'Lambda' which first renders the contained section and then applies the supplied function
overText :: (T.Text -> T.Text) -> Value
overText f = toMustache $ fmap (f . snd) . catchSubstitute . substituteAST
overText = toMustache
5 changes: 5 additions & 0 deletions src/Text/Mustache/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ lambdaHelper conv f = Lambda $ (<$> askContext) . wrapper

instance ToMustache (STree -> SubM Text) where
toMustache f = Lambda (fmap (return . TextBlock) . f)

instance ToMustache (Text -> Text) where
toMustache f = Lambda $ \tree -> do
(_, res) <- catchSubstitute $ substituteAST tree
return [TextBlock $ f res]
Loading