ginger 0.3.9.0 → 0.3.9.1
raw patch · 6 files changed
+158/−3 lines, 6 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Text.Ginger.AST: SwitchS :: Expression -> [(Expression, Statement)] -> Statement -> Statement
Files
- ginger.cabal +3/−1
- src/Text/Ginger/AST.hs +1/−0
- src/Text/Ginger/Parse.hs +21/−0
- src/Text/Ginger/Run.hs +18/−0
- test/Text/Ginger/SimulationTests.hs +25/−2
- test/fixtures/silly-locale.json +90/−0
ginger.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: ginger-version: 0.3.9.0+version: 0.3.9.1 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.@@ -15,7 +15,9 @@ category: Text build-type: Simple -- extra-source-files:+data-files: test/fixtures/*.json cabal-version: >=1.10+bug-reports: https://github.com/tdammers/ginger/issues library exposed-modules: Text.Ginger
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@@ -257,6 +258,25 @@ -- 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 @@ -706,6 +726,7 @@ c2 <- anyChar case c2 of 'n' -> return '\n'+ 'r' -> return '\r' 'b' -> return '\b' 'v' -> return '\v' '0' -> return '\0'
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
@@ -38,8 +38,12 @@ [] [] "- {%- set x=1 -%} -" "--" ] , testGroup "Literals"- [ testCase "String: \"foobar\"" $ mkTestHtml- [] [] "{{ \"foobar\" }}" "foobar"+ [ testGroup "Strings"+ [ testCase "String: \"foobar\"" $ mkTestHtml+ [] [] "{{ \"foobar\" }}" "foobar"+ , testCase "String: \"\\r\\n\\t\\b\"" $ mkTestHtml+ [] [] "{{ \"\\r\\n\\t\\b\" }}" "\r\n\t\b"+ ] , testGroup "Numbers" [ testCase "123" $ mkTestHtml [] [] "{{ 123 }}" "123"@@ -166,6 +170,25 @@ mkTestHtml [] [] "{% if false %}yes{% elif false %}maybe{% else %}no{% endif %}" "no" , 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
+ test/fixtures/silly-locale.json view
@@ -0,0 +1,90 @@+{+ "dateTimeFmt" : "It be %A, in the year %y, the clock striketh %H",+ "wDays" : [+ [+ "The Day Of Sunshine",+ "Sun"+ ],+ [+ "The Day Of Monning",+ "Mon"+ ],+ [+ "The Day Of Two",+ "Tue"+ ],+ [+ "The Midst Of The Week",+ "Wed"+ ],+ [+ "The Day Of Thor",+ "Thu"+ ],+ [+ "The Day Of Freya",+ "Fri"+ ],+ [+ "The Day Of Saturn",+ "Sat"+ ]+ ],+ "amPm" : [+ "Morning",+ "Evening"+ ],+ "timeFmt" : "%H o'clock",+ "months" : [+ [+ "January",+ "Jan"+ ],+ [+ "February",+ "Feb"+ ],+ [+ "March",+ "Mar"+ ],+ [+ "April",+ "Apr"+ ],+ [+ "May",+ "May"+ ],+ [+ "June",+ "Jun"+ ],+ [+ "July",+ "Jul"+ ],+ [+ "August",+ "Aug"+ ],+ [+ "September",+ "Sep"+ ],+ [+ "October",+ "Oct"+ ],+ [+ "November",+ "Nov"+ ],+ [+ "December",+ "Dec"+ ]+ ],+ "time12Fmt" : "%I:%M:%S %p",+ "dateFmt" : "%d-%m, %y"+}