glabrous 0.1.0.1 → 0.1.1.0
raw patch · 5 files changed
+75/−19 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.Glabrous.Types: instance Data.Aeson.Types.FromJSON.FromJSON Text.Glabrous.Types.Context
- Text.Glabrous.Types: instance Data.Aeson.Types.ToJSON.ToJSON Text.Glabrous.Types.Context
+ Text.Glabrous: compress :: Template -> Template
+ Text.Glabrous: tagsRename :: [(Text, Text)] -> Template -> Template
+ Text.Glabrous.Types: instance Data.Aeson.Types.Class.FromJSON Text.Glabrous.Types.Context
+ Text.Glabrous.Types: instance Data.Aeson.Types.Class.ToJSON Text.Glabrous.Types.Context
- Text.Glabrous: Final :: Text -> Result
+ Text.Glabrous: Final :: !Text -> Result
- Text.Glabrous.Types: Final :: Text -> Result
+ Text.Glabrous.Types: Final :: !Text -> Result
- Text.Glabrous.Types: Literal :: Text -> Token
+ Text.Glabrous.Types: Literal :: !Text -> Token
- Text.Glabrous.Types: Tag :: Text -> Token
+ Text.Glabrous.Types: Tag :: !Text -> Token
Files
- README.md +10/−0
- glabrous.cabal +3/−2
- src/Text/Glabrous.hs +52/−14
- src/Text/Glabrous/Types.hs +2/−2
- tests/hspec.hs +8/−1
+ README.md view
@@ -0,0 +1,10 @@+# Glabrous [](https://travis-ci.org/MichelBoucey/glabrous)++++Glabrous is a minimalistic Mustache-like syntax, truly logic-less, pure Text template library.++* Use only the simplest Mustache tag {{name}}+* HTML agnostic++Bug fixes & improvements are welcome.
glabrous.cabal view
@@ -1,5 +1,5 @@ name: glabrous-version: 0.1.0.1+version: 0.1.1.0 synopsis: A template library description: A minimalistic, Mustache-like syntax, truly logic-less, pure Text template library@@ -12,10 +12,11 @@ category: Text, Web build-type: Simple cabal-version: >=1.10+extra-source-files: README.md source-repository head type: git- location: https://github.com/MichelBoucey/glabrous+ location: https://github.com/MichelBoucey/glabrous.git library hs-source-dirs: src
src/Text/Glabrous.hs view
@@ -1,6 +1,6 @@-{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE BangPatterns #-} {-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE RecordWildCards #-} -- | A minimalistic Mustache-like syntax, truly logic-less, -- pure 'T.Text' template library@@ -19,8 +19,10 @@ , readTemplateFile -- ** 'Template' operations , toText- , tagsOf , isFinal+ , tagsOf+ , tagsRename+ , compress , writeTemplateFile -- * Context , Context (..)@@ -55,14 +57,40 @@ import Text.Glabrous.Internal import Text.Glabrous.Types as G +-- | Optimize a 'Template' content after (many) partialProcess(') rewriting(s).+compress :: Template -> Template+compress t =+ Template { content = packC (content t) [] }+ where+ packC ts !o = do+ let (a,b) = span isL ts+ if not (null a)+ then packC b (o ++ [concatL a])+ else if not (null b)+ then do let (a',b') = span (not.isL) b+ if not (null a')+ then packC b' (o ++ a')+ else if not (null b')+ then packC b' o+ else o+ else o+ where+ isL (Literal _) = True+ isL (Tag _) = False+ concatL _ts =+ foldr trans (Literal "") _ts+ where+ trans (Literal a) (Literal b) = Literal (a `T.append` b)+ trans _ _ = undefined+ -- | Build an empty 'Context'. initContext :: Context initContext = Context { variables = H.empty } -- | Populate with variables and/or update variables in the given 'Context'. ----- >λ>setVariables [("something","something new"), ("about","haskell")] context--- >Context {variables = fromList [("etc.","..."),("about","haskell"),("something","something new"),("name","")]}+-- >λ>setVariables [("something","something new"), ("about","Haskell")] context+-- >Context {variables = fromList [("etc.","..."),("about","Haskell"),("something","something new"),("name","")]} setVariables :: [(T.Text,T.Text)] -> Context -> Context setVariables ts c@Context{..} = case uncons ts of@@ -73,7 +101,7 @@ -- | Delete variables from a 'Context' by these names. -- -- >λ>deleteVariables ["something"] context--- >Context {variables = fromList [("etc.","..."),("about","haskell"),("name","")]}+-- >Context {variables = fromList [("etc.","..."),("about","Haskell"),("name","")]} deleteVariables :: [T.Text] -> Context -> Context deleteVariables ts c@Context{..} = case uncons ts of@@ -102,8 +130,8 @@ -- -- @ -- {--- "something": "something else"--- "etc.": "...",+-- "something": "something else",+-- "etc.": "..." -- } -- @ --@@ -115,8 +143,8 @@ -- -- @ -- {--- "something": ""--- "etc.": "",+-- "something": "",+-- "etc.": "" -- } -- @ --@@ -149,8 +177,8 @@ -- as it is, with its 'Tag's, if they exist (no -- 'Context' is processed). toText :: Template -> T.Text-toText t =- T.concat $ trans <$> content t+toText Template{..} =+ T.concat $ trans <$> content where trans (Literal c) = c trans (Tag k) = T.concat ["{{",k,"}}"]@@ -163,6 +191,16 @@ isTag (Tag _) = True isTag _ = False +tagsRename :: [(T.Text,T.Text)] -> Template -> Template+tagsRename ts Template{..} =+ Template { content = rename <$> content }+ where+ rename t@(Tag n) =+ case lookup n ts of+ Just r -> Tag r+ Nothing -> t+ rename l@(Literal _) = l+ -- | 'True' if a 'Template' has no more 'Tag' -- inside and can be used as a final 'T.Text'. isFinal :: Template -> Bool@@ -221,9 +259,9 @@ case t' of Tag k -> case H.lookup k variables of- Just v -> (addToken (Literal v) c', ts)+ Just v -> (addToken (Literal v) c',ts) Nothing -> (addToken t' c',ts ++ [k])- Literal _ -> (addToken t' c', ts)+ Literal _ -> (addToken t' c',ts) where addToken a b = Template { content = content b ++ [a] }
src/Text/Glabrous/Types.hs view
@@ -6,7 +6,7 @@ import qualified Data.HashMap.Strict as H import qualified Data.Text as T -data Token = Tag T.Text | Literal T.Text deriving (Show,Eq)+data Token = Tag !T.Text | Literal !T.Text deriving (Show,Eq) data Template = Template { content :: ![Token] } deriving (Show,Eq) @@ -24,5 +24,5 @@ type Tag = T.Text -data Result = Final T.Text | Partial { template :: !Template, tags :: ![Tag]} deriving (Show,Eq)+data Result = Final !T.Text | Partial { template :: !Template, tags :: ![Tag]} deriving (Show,Eq)
tests/hspec.hs view
@@ -88,8 +88,15 @@ it "apply to a template with a subcontext outputs a template with its tags" $ (return $ partialProcess' template' pcontext) `shouldReturn` Partial {template = Template {content = [Literal "Affirmanti incumbit probatio. Lorem ",Literal " Dolor! ",Literal " sit amet, ",Tag "consectetur",Literal " adipiscing elit. Proin bibendum mauris ",Literal " Vitae! ",Literal " dui venenatis pretium. Maecenas vestibulum justo at accumsan ",Tag "mattis",Literal ". Nulla id finibus sem. Cras dolor nunc, consectetur in tincidunt id, facilisis in lorem. Nullam sed eros venenatis, tempor felis in, ultrices enim. Donec sit amet ligula ac orci cursus finibus ut eget lectus. Praesent feugiat massa non mi venenatis ",Literal " Faucibus! ",Literal ". Fabricando fit faber."]}, tags = ["consectetur","mattis"]} + describe "compress" $+ it "optimize a template after partialProcess(')" $+ (return $ compress $ partialProcess template' pcontext) `shouldReturn` Template {content = [Literal "Affirmanti incumbit probatio. Lorem Dolor! sit amet, ",Tag "consectetur",Literal " adipiscing elit. Proin bibendum mauris Vitae! dui venenatis pretium. Maecenas vestibulum justo at accumsan ",Tag "mattis",Literal ". Nulla id finibus sem. Cras dolor nunc, consectetur in tincidunt id, facilisis in lorem. Nullam sed eros venenatis, tempor felis in, ultrices enim. Donec sit amet ligula ac orci cursus finibus ut eget lectus. Praesent feugiat massa non mi venenatis Faucibus! . Fabricando fit faber."]}++ describe "tagsRename" $+ it "renames tags for a new template" $+ (return $ tagsRename [("consectetur","causatur"),("mattis","generis")] $ compress $ partialProcess template' pcontext) `shouldReturn` Template {content = [Literal "Affirmanti incumbit probatio. Lorem Dolor! sit amet, ",Tag "causatur",Literal " adipiscing elit. Proin bibendum mauris Vitae! dui venenatis pretium. Maecenas vestibulum justo at accumsan ",Tag "generis",Literal ". Nulla id finibus sem. Cras dolor nunc, consectetur in tincidunt id, facilisis in lorem. Nullam sed eros venenatis, tempor felis in, ultrices enim. Donec sit amet ligula ac orci cursus finibus ut eget lectus. Praesent feugiat massa non mi venenatis Faucibus! . Fabricando fit faber."]}+ describe "partialProcess'" $ afterAll_ clean $ it "apply to a template with a context outputs a final text" $ (return $ partialProcess' template' context') `shouldReturn` Final "Affirmanti incumbit probatio. Lorem Dolor! sit amet, Consectetur! adipiscing elit. Proin bibendum mauris Vitae! dui venenatis pretium. Maecenas vestibulum justo at accumsan Mattis! . Nulla id finibus sem. Cras dolor nunc, consectetur in tincidunt id, facilisis in lorem. Nullam sed eros venenatis, tempor felis in, ultrices enim. Donec sit amet ligula ac orci cursus finibus ut eget lectus. Praesent feugiat massa non mi venenatis Faucibus! . Fabricando fit faber."-