template 0.2 → 0.2.0.1
raw patch · 4 files changed
+30/−11 lines, 4 filesdep ~mtldep ~textsetup-changed
Dependency ranges changed: mtl, text
Files
- Data/Text/Template.hs +25/−6
- LICENSE +1/−1
- Setup.lhs +0/−0
- template.cabal +4/−4
Data/Text/Template.hs view
@@ -13,7 +13,7 @@ -- but are not part of the placeholder, such as -- \"${noun}ification\". ----- Any other apperance of \"$\" in the string will result in an+-- Any other appearance of \"$\" in the string will result in an -- 'Prelude.error' being raised. -- -- If you render the same template multiple times it's faster to first@@ -57,7 +57,7 @@ -- ----------------------------------------------------------------------------- --- | A repesentation of a 'Data.Text' template, supporting efficient+-- | A representation of a 'Data.Text' template, supporting efficient -- rendering. newtype Template = Template [Frag] @@ -96,8 +96,30 @@ -- | Creates a template from a template string. template :: T.Text -> Template-template = runParser pTemplate+template = Template . combineLits . runParser pFrags +combineLits :: [Frag] -> [Frag]+combineLits [] = []+combineLits xs =+ let (lits,xs') = span isLit xs+ in case lits of+ [] -> gatherVars xs'+ [lit] -> lit : gatherVars xs'+ _ -> Lit (T.concat (map fromLit lits)) : gatherVars xs'+ where+ gatherVars [] = []+ gatherVars ys =+ let (vars,ys') = span isVar ys+ in vars ++ combineLits ys'++ isLit (Lit _) = True+ isLit _ = False++ isVar = not . isLit++ fromLit (Lit v) = v+ fromLit _ = undefined+ -- | Performs the template substitution, returning a new 'LT.Text'. render :: Template -> Context -> LT.Text render (Template frags) ctxFunc = LT.fromChunks $ map renderFrag frags@@ -139,9 +161,6 @@ -- ----------------------------------------------------------------------------- -- Template parser--pTemplate :: Parser Template-pTemplate = fmap Template pFrags pFrags :: Parser [Frag] pFrags = do
LICENSE view
@@ -1,4 +1,4 @@-Copyright Johan Tibell 2007+Copyright (c) 2007-2010, Johan Tibell All rights reserved.
Setup.lhs view
template.cabal view
@@ -1,5 +1,5 @@ name: template-version: 0.2+version: 0.2.0.1 description: Simple string substitution library that supports \"$\"-based substitution. Meant to be used when Text.Printf or string@@ -19,9 +19,9 @@ exposed-modules: Data.Text.Template build-depends:- base < 5,- mtl,- text == 0.7.*+ base < 5,+ mtl < 1.2,+ text >= 0.7.2 && < 0.11 ghc-options: -funbox-strict-fields -Wall if impl(ghc >= 6.8)