haiji 0.2.1.0 → 0.2.1.1
raw patch · 5 files changed
+48/−52 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- haiji.cabal +1/−1
- src/Text/Haiji/Dictionary.hs +1/−1
- src/Text/Haiji/Parse.hs +1/−7
- src/Text/Haiji/Runtime.hs +23/−20
- test/tests.hs +22/−23
haiji.cabal view
@@ -2,7 +2,7 @@ -- see http://haskell.org/cabal/users-guide/ name: haiji-version: 0.2.1.0+version: 0.2.1.1 synopsis: A typed template engine, subset of jinja2 description: Haiji is a template engine which is subset of jinja2. This is designed to free from the unintended rendering result
src/Text/Haiji/Dictionary.hs view
@@ -21,7 +21,7 @@ import Data.Dynamic import qualified Data.HashMap.Strict as M import Data.Maybe-import Data.Monoid+import Data.Monoid () import qualified Data.Text as T import qualified Data.Text.Lazy as LT import qualified Data.Text.Lazy.Encoding as LT
src/Text/Haiji/Parse.hs view
@@ -6,7 +6,6 @@ ( Jinja2(..) , parseString , parseFile- , runQWF ) where #if MIN_VERSION_base(4,8,0)@@ -39,15 +38,12 @@ parseString = (toJinja2 <$>) . either error readAllFile . parseOnly parser . T.pack class Quasi q => QuasiWithFile q where- runQWF :: q a -> Q a withFile :: FilePath -> q LT.Text instance QuasiWithFile IO where- runQWF = runIO withFile file = LT.readFile file instance QuasiWithFile Q where- runQWF = id withFile file = runQ (addDependentFile file >> runIO (withFile file)) parseFileWith :: QuasiWithFile q => (LT.Text -> LT.Text) -> FilePath -> q Jinja2@@ -79,9 +75,7 @@ parseFile = parseFileWith deleteLastOneLF where deleteLastOneLF :: LT.Text -> LT.Text deleteLastOneLF xs- | "%}\n" `LT.isSuffixOf` xs = LT.init xs- | "\n\n" `LT.isSuffixOf` xs = LT.init xs- | not ("\n" `LT.isSuffixOf` xs) = xs `LT.append` "\n"+ | "\n" `LT.isSuffixOf` xs = LT.init xs | otherwise = xs parseIncludeFile :: QuasiWithFile q => FilePath -> q Jinja2
src/Text/Haiji/Runtime.hs view
@@ -49,30 +49,33 @@ obj <- eval x case obj of JSON.String s -> return $ (`escapeBy` esc) $ toLT s- JSON.Number n -> case floatingOrInteger n of- Left r -> undefined+ JSON.Number n -> case floatingOrInteger (n :: Scientific) of+ Left r -> let _ = (r :: Double) in error "invalid value type" Right i -> return $ (`escapeBy` esc) $ toLT (i :: Integer) _ -> undefined haijiAST env parentBlock children (Condition p ts fs) =- do JSON.Bool cond <- eval p- if cond- then haijiASTs env parentBlock children ts- else maybe (return "") (haijiASTs env parentBlock children) fs+ do b <- eval p+ case b of+ JSON.Bool True -> haijiASTs env parentBlock children ts+ JSON.Bool False -> maybe (return "") (haijiASTs env parentBlock children) fs+ _ -> error "invalid condition type" haijiAST env parentBlock children (Foreach k xs loopBody elseBody) =- do JSON.Array dicts <- eval xs- p <- ask- let len = V.length dicts- if 0 < len- then return $ LT.concat- [ runReader (haijiASTs env parentBlock children loopBody)- (let JSON.Object obj = p- in JSON.Object- $ HM.insert "loop" (loopVariables len ix)- $ HM.insert (T.pack $ show k) x obj)- | (ix, x) <- zip [0..] (V.toList dicts)- ]- else maybe (return "") (haijiASTs env parentBlock children) elseBody-+ do arr <- eval xs+ case arr of+ JSON.Array dicts -> do+ p <- ask+ let len = V.length dicts+ if 0 < len+ then return $ LT.concat+ [ runReader (haijiASTs env parentBlock children loopBody)+ (let JSON.Object obj = p+ in JSON.Object+ $ HM.insert "loop" (loopVariables len ix)+ $ HM.insert (T.pack $ show k) x obj)+ | (ix, x) <- zip [0..] (V.toList dicts)+ ]+ else maybe (return "") (haijiASTs env parentBlock children) elseBody+ _ -> error "invalid array type" haijiAST _env _parentBlock _children (Raw raw) = return $ LT.pack raw haijiAST _env _parentBlock _children (Base _asts) = undefined haijiAST env parentBlock children (Block _base name _scoped body) =
test/tests.hs view
@@ -17,7 +17,7 @@ import Text.Haiji.Runtime import Data.Aeson import Data.Default-import Data.Monoid+import Data.Monoid () import qualified Data.Text as T import qualified Data.Text.Lazy as LT import qualified Data.Text.Lazy.IO as LT@@ -31,17 +31,16 @@ jinja2 :: Show a => FilePath -> a -> IO LT.Text jinja2 template dict = do- (code, out, err) <- readProcessWithExitCode "python2" [] script+ (code, out, err) <- readProcessWithExitCode "python3" [] script unless (code == ExitSuccess) $ LT.putStrLn err return out where script = LT.unlines- [ "import sys, codecs, json"+ [ "import json" , "from jinja2 import Environment, PackageLoader"- , "sys.stdout = codecs.lookup('utf_8')[-1](sys.stdout)" , "env = Environment(loader=PackageLoader('example', '.'),autoescape=True)" , "template = env.get_template('" <> LT.pack template <> "')" , "object = json.loads(" <> LT.pack (show $ show dict) <> ")"- , "print template.render(object),"+ , "print(template.render(object),end='')" , "exit()" ] @@ -75,44 +74,44 @@ case_empty :: Assertion case_empty = do expected <- jinja2 "test/empty.tmpl" empty- expected @=? render $(haijiFile def "test/empty.tmpl") empty tmpl <- readTemplateFile def "test/empty.tmpl" expected @=? render tmpl (toJSON empty)+ expected @=? render $(haijiFile def "test/empty.tmpl") empty case_lf1 :: Assertion case_lf1 = do expected <- jinja2 "test/lf1.tmpl" empty- expected @=? render $(haijiFile def "test/lf1.tmpl") empty tmpl <- readTemplateFile def "test/lf1.tmpl" expected @=? render tmpl (toJSON empty)+ expected @=? render $(haijiFile def "test/lf1.tmpl") empty case_lf2 :: Assertion case_lf2 = do expected <- jinja2 "test/lf2.tmpl" empty- expected @=? render $(haijiFile def "test/lf2.tmpl") empty tmpl <- readTemplateFile def "test/lf2.tmpl" expected @=? render tmpl (toJSON empty)+ expected @=? render $(haijiFile def "test/lf2.tmpl") empty case_line_without_newline :: Assertion case_line_without_newline = do expected <- jinja2 "test/line_without_newline.tmpl" empty- expected @=? render $(haijiFile def "test/line_without_newline.tmpl") empty tmpl <- readTemplateFile def "test/line_without_newline.tmpl" expected @=? render tmpl (toJSON empty)+ expected @=? render $(haijiFile def "test/line_without_newline.tmpl") empty case_line_with_newline :: Assertion case_line_with_newline = do expected <- jinja2 "test/line_with_newline.tmpl" empty- expected @=? render $(haijiFile def "test/line_with_newline.tmpl") empty tmpl <- readTemplateFile def "test/line_with_newline.tmpl" expected @=? render tmpl (toJSON empty)+ expected @=? render $(haijiFile def "test/line_with_newline.tmpl") empty case_variables :: Assertion case_variables = do expected <- jinja2 "test/variables.tmpl" dict- expected @=? render $(haijiFile def "test/variables.tmpl") dict tmpl <- readTemplateFile def "test/variables.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/variables.tmpl") dict where dict = [key|foo|] ("normal" :: T.Text) `merge` [key|_foo|] ("start '_'" :: LT.Text) `merge`@@ -123,9 +122,9 @@ case_HTML_escape :: Assertion case_HTML_escape = do expected <- jinja2 "test/HTML_escape.tmpl" dict- expected @=? render $(haijiFile def "test/HTML_escape.tmpl") dict tmpl <- readTemplateFile def "test/HTML_escape.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/HTML_escape.tmpl") dict where dict = [key|foo|] ([' '..'\126'] :: String) @@ -135,25 +134,25 @@ [key|bar|] bar `merge` [key|baz|] baz expected <- jinja2 "test/condition.tmpl" dict- expected @=? render $(haijiFile def "test/condition.tmpl") dict tmpl <- readTemplateFile def "test/condition.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/condition.tmpl") dict case_foreach :: Assertion case_foreach = do expected <- jinja2 "test/foreach.tmpl" dict- expected @=? render $(haijiFile def "test/foreach.tmpl") dict tmpl <- readTemplateFile def "test/foreach.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/foreach.tmpl") dict where dict = [key|foo|] ([0,2..10] :: [Int]) case_foreach_shadowing :: Assertion case_foreach_shadowing = do expected <- jinja2 "test/foreach.tmpl" dict- expected @=? render $(haijiFile def "test/foreach.tmpl") dict tmpl <- readTemplateFile def "test/foreach.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/foreach.tmpl") dict False @=? ("bar" `LT.isInfixOf` expected) where dict = [key|foo|] ([0,2..10] :: [Int]) `merge`@@ -162,9 +161,9 @@ case_foreach_else_block :: Assertion case_foreach_else_block = do expected <- jinja2 "test/foreach_else_block.tmpl" dict- expected @=? render $(haijiFile def "test/foreach_else_block.tmpl") dict tmpl <- readTemplateFile def "test/foreach_else_block.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/foreach_else_block.tmpl") dict where dict = [key|foo|] ([] :: [Int]) @@ -174,18 +173,18 @@ testInclude (["","\n","\n\n"] :: [String]) where testInclude xs = do expected <- jinja2 "test/include.tmpl" dict- expected @=? render $(haijiFile def "test/include.tmpl") dict tmpl <- readTemplateFile def "test/include.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/include.tmpl") dict where dict = [key|foo|] xs case_raw :: Assertion case_raw = do expected <- jinja2 "test/raw.tmpl" dict- expected @=? render $(haijiFile def "test/raw.tmpl") dict tmpl <- readTemplateFile def "test/raw.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/raw.tmpl") dict where dict = [key|foo|] ([0,2..10] :: [Int]) `merge` [key|bar|] ("bar" :: String)@@ -193,36 +192,36 @@ case_loop_variables :: Assertion case_loop_variables = do expected <- jinja2 "test/loop_variables.tmpl" dict- expected @=? render $(haijiFile def "test/loop_variables.tmpl") dict tmpl <- readTemplateFile def "test/loop_variables.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/loop_variables.tmpl") dict where dict = [key|foo|] ([0,2..10] :: [Integer]) case_whitespace_control :: Assertion case_whitespace_control = do expected <- jinja2 "test/whitespace_control.tmpl" dict- expected @=? render $(haijiFile def "test/whitespace_control.tmpl") dict tmpl <- readTemplateFile def "test/whitespace_control.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/whitespace_control.tmpl") dict where dict = [key|seq|] ([0,2..10] :: [Integer]) case_comment :: Assertion case_comment = do expected <- jinja2 "test/comment.tmpl" dict- expected @=? render $(haijiFile def "test/comment.tmpl") dict tmpl <- readTemplateFile def "test/comment.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/comment.tmpl") dict where dict = [key|seq|] ([0,2..10] :: [Integer]) case_extends :: Assertion case_extends = do expected <- jinja2 "test/child.tmpl" dict- expected @=? render $(haijiFile def "test/child.tmpl") dict tmpl <- readTemplateFile def "test/child.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/child.tmpl") dict where dict = [key|foo|] ("foo" :: T.Text) `merge` [key|bar|] ("bar" :: T.Text) `merge`@@ -231,9 +230,9 @@ case_many_variables :: Assertion case_many_variables = do expected <- jinja2 "test/many_variables.tmpl" dict --- expected @=? render $(haijiFile def "test/many_variables.tmpl") dict tmpl <- readTemplateFile def "test/many_variables.tmpl" expected @=? render tmpl (toJSON dict)+ expected @=? render $(haijiFile def "test/many_variables.tmpl") dict where dict = [key|a|] ("b" :: T.Text) `merge` [key|b|] ("b" :: LT.Text) `merge`