packages feed

ginger 0.3.7.2 → 0.3.8.0

raw patch · 5 files changed

+59/−1 lines, 5 filesdep ~basedep ~timePVP ok

version bump matches the API change (PVP)

Dependency ranges changed: base, time

API changes (from Hackage documentation)

+ Text.Ginger.AST: SwitchS :: Expression -> [(Expression, Statement)] -> Statement -> Statement

Files

ginger.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                ginger-version:             0.3.7.2+version:             0.3.8.0 synopsis:            An implementation of the Jinja2 template language in Haskell description:         Ginger is Jinja, minus the most blatant pythonisms. Wants                      to be feature complete, but isn't quite there yet.
src/Text/Ginger/AST.hs view
@@ -39,6 +39,7 @@     | LiteralS Html -- ^ Literal output (anything outside of any tag)     | InterpolationS Expression -- ^ {{ expression }}     | IfS Expression Statement Statement -- ^ {% if expression %}statement{% else %}statement{% endif %}+    | SwitchS Expression [(Expression, Statement)] Statement -- ^ {% switch expression %}{% case expression %}statement{% endcase %}...{% default %}statement{% enddefault %}{% endswitch %}     | ForS (Maybe VarName) VarName Expression Statement -- ^ {% for index, varname in expression %}statement{% endfor %}     | SetVarS VarName Expression -- ^ {% set varname = expr %}     | DefMacroS VarName Macro -- ^ {% macro varname %}statements{% endmacro %}
src/Text/Ginger/Parse.hs view
@@ -197,6 +197,7 @@ statementP = interpolationStmtP            <|> commentStmtP            <|> ifStmtP+           <|> switchStmtP            <|> setStmtP            <|> forStmtP            <|> includeP@@ -256,6 +257,25 @@     falseStmt <- elifBranchP <|> elseBranchP <|> return NullS     -- No endif here: the parent {% if %} owns that one.     return $ IfS condExpr trueStmt falseStmt++switchStmtP :: Monad m => Parser m Statement+switchStmtP = do+    pivotExpr <- try $ fancyTagP "switch" expressionP+    cases <- many switchCaseP+    def <- option NullS $ switchDefaultP+    simpleTagP "endswitch"+    return $ SwitchS pivotExpr cases def++switchCaseP :: Monad m => Parser m (Expression, Statement)+switchCaseP = do+    cmpExpr <- try $ fancyTagP "case" expressionP+    body <- statementsP+    simpleTagP "endcase"+    return (cmpExpr, body)++switchDefaultP :: Monad m => Parser m Statement+switchDefaultP = do+    try (simpleTagP "default") *> statementsP <* simpleTagP "enddefault"  setStmtP :: Monad m => Parser m Statement setStmtP = fancyTagP "set" setStmtInnerP
src/Text/Ginger/Run.hs view
@@ -250,6 +250,24 @@     cond <- runExpression condExpr     runStatement $ if toBoolean cond then true else false +runStatement (SwitchS pivotExpr cases def) = do+    pivot <- runExpression pivotExpr+    let branches =+            [ \cont -> do+                cond <- runExpression condExpr+                if pivot `looseEquals` cond+                    then runStatement body+                    else cont+            | (condExpr, body)+            <- cases+            ] +++            [ Prelude.const (runStatement def)+            ]+    go branches+    where+        go [] = return ()+        go (x:xs) = x (go xs)+ runStatement (SetVarS name valExpr) = do     val <- runExpression valExpr     setVar name val
test/Text/Ginger/SimulationTests.hs view
@@ -171,6 +171,25 @@         , testCase "if false then \"yes\" else if true then \"maybe\" else \"no\"" $ do             mkTestHtml [] [] "{% if false %}yes{% elif true %}maybe{% else %}no{% endif %}" "maybe"         ]+    , testGroup "Switch"+        [ testCase "switch 1 of 1, 2, default" $ do+            mkTestHtml [] []+                ( "{% switch 1 %}\n" +++                  "{% case 1 %}One{% endcase %}\n" +++                  "{% case 2 %}Two{% endcase %}\n" +++                  "{% default %}Default{% enddefault %}\n" +++                  "{% endswitch %}\n"+                )+                "One"+        , testCase "switch 1 of 1, 2" $ do+            mkTestHtml [] []+                ( "{% switch 1 %}\n" +++                  "{% case 1 %}One{% endcase %}\n" +++                  "{% case 2 %}Two{% endcase %}\n" +++                  "{% endswitch %}\n"+                )+                "One"+        ]     , testGroup "Comparisons"         [ testCase "if 1 == 1 then \"yes\" else \"no\"" $ do             mkTestHtml [] [] "{% if (1 == 1) %}yes{% else %}no{% endif %}" "yes"