HStringTemplate 0.3.2 → 0.4
raw patch · 3 files changed
+12/−16 lines, 3 files
Files
- HStringTemplate.cabal +1/−1
- Text/StringTemplate/Base.hs +6/−5
- Text/StringTemplate/Group.hs +5/−10
HStringTemplate.cabal view
@@ -1,5 +1,5 @@ name: HStringTemplate-version: 0.3.2+version: 0.4 synopsis: StringTemplate implementation in Haskell. description: A port of the Java library by Terrence Parr. category: Text
Text/StringTemplate/Base.hs view
@@ -2,7 +2,7 @@ module Text.StringTemplate.Base (StringTemplate(..), StringTemplateShows(..), ToSElem(..), STGroup,- Stringable(..), stShowsToSE,+ Stringable(..), stShowsToSE, inSGen, toString, toPPDoc, render, newSTMP, newAngleSTMP, getStringTemplate, getStringTemplate', setAttribute, setManyAttrib, withContext, optInsertTmpl, setEncoder,@@ -53,7 +53,6 @@ h (x:_) = x; h _ = n; t (_:y:xs) = (y:xs); t _ = [n]; m (x:xs) = (x:xs); m _ = [n]; - {-------------------------------------------------------------------- StringTemplate and the API --------------------------------------------------------------------}@@ -143,6 +142,9 @@ data SEnv a = SEnv {smp :: SMap a, sopts :: [(String, (SEnv a -> SElem a))], sgen :: STGroup a, senc :: String -> String} +inSGen :: (STGroup a -> STGroup a) -> StringTemplate a -> StringTemplate a+inSGen f st@STMP{senv = env} = st {senv = env {sgen = f (sgen env)} }+ envLookup :: (Monad m) => String -> SEnv a -> m (SElem a) envLookup x = M.lookup x . smp envInsert :: (String, SElem a) -> SEnv a -> SEnv a@@ -324,7 +326,7 @@ exprn :: Stringable a => GenParser Char (Char,Char) (SEnv a -> a) exprn = do exprs <- comlist subexprn <?> "expression"- templ <- many (char ':' >> iterApp <$> comlist (anonTmpl <|> regTemplate))+ templ <- many (char ':' >> iterApp <$> comlist (anonTmpl <|> regTemplate)) <?> "template call" return $ fromMany (showVal <*> head exprs) ((sequence exprs >>=) . seqTmpls) templ @@ -350,7 +352,6 @@ braceConcat = LI . foldr go [] <$$> sequence <$> around '['(comlist subexprn)']' where go (LI x) lst = x++lst; go x lst = x:lst - literal :: GenParser Char st (b -> SElem a) literal = justSTR <$> (around '"' (concat <$> many (escapedChar "\"")) '"' <|> around '\'' (concat <$> many (escapedChar "'")) '\'')@@ -366,7 +367,7 @@ functn :: Stringable a => GenParser Char (Char,Char) (SEnv a -> SElem a) functn = do f <- string "first" <|> string "rest" <|> string "strip"- <|> try (string "length") <|> string "last"+ <|> try (string "length") <|> string "last" <?> "function" (fApply f .) <$> around '(' subexprn ')' where fApply str (LI xs) | str == "first" = if null xs then SNull else head xs
Text/StringTemplate/Group.hs view
@@ -27,11 +27,6 @@ (<$$>) :: (Functor f1, Functor f) => (a -> b) -> f (f1 a) -> f (f1 b) (<$$>) x y = ((<$>) . (<$>)) x y -sgInsert :: (String -> StFirst (StringTemplate a)) -> StringTemplate a -> StringTemplate a-sgInsert g st = let e = senv st in st {senv = e {sgen = sgen e `mappend` g} }-sgOverride :: STGroup a -> StringTemplate a -> StringTemplate a-sgOverride g st = let e = senv st in st {senv = e {sgen = g `mappend` sgen e} }- {-------------------------------------------------------------------- Group API --------------------------------------------------------------------}@@ -41,7 +36,7 @@ groupStringTemplates :: [(String,StringTemplate a)] -> STGroup a groupStringTemplates xs = newGen where newGen s = StFirst (M.lookup s ng)- ng = M.fromList $ map (second $ sgInsert newGen) xs+ ng = M.fromList $ map (second $ inSGen (`mappend` newGen)) xs -- | Given a path, returns a group which generates all files in said directory -- which have the proper \"st\" extension.@@ -71,12 +66,12 @@ -- | Adds a supergroup to any StringTemplate group such that templates from -- the original group are now able to call ones from the supergroup as well. addSuperGroup :: STGroup a -> STGroup a -> STGroup a-addSuperGroup f g = sgInsert g <$$> f+addSuperGroup f g = inSGen (`mappend` g) <$$> f -- | Adds a \"subgroup\" to any StringTemplate group such that templates from -- the original group now have template calls \"shadowed\" by the subgroup. addSubGroup :: STGroup a -> STGroup a -> STGroup a-addSubGroup f g = sgOverride g <$$> f+addSubGroup f g = inSGen (g `mappend`) <$$> f -- | Merges two groups into a single group. This function is left-biased, -- prefering bindings from the first group when there is a conflict.@@ -85,12 +80,12 @@ -- | Adds a set of global options to a group optInsertGroup :: [(String, String)] -> STGroup a -> STGroup a-optInsertGroup opts f = optInsertTmpl opts <$$> f+optInsertGroup opts f = (inSGen (optInsertGroup opts) . optInsertTmpl opts) <$$> f -- | Sets an encoding function of a group that all values are -- rendered with in each enclosed template setEncoderGroup :: (Stringable a) => (String -> String) -> STGroup a -> STGroup a-setEncoderGroup x f = setEncoder x <$$> f+setEncoderGroup x f = (inSGen (setEncoderGroup x) . setEncoder x) <$$> f -- | For any requested template, returns a message that the template was -- unable to be found. Useful to add as a super group for a set of templates