glabrous 0.1.3.0 → 0.2.0
raw patch · 4 files changed
+34/−25 lines, 4 filesdep ~aesondep ~aeson-prettydep ~attoparsecPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: aeson, aeson-pretty, attoparsec, bytestring, cereal, cereal-text, either, text, unordered-containers
API changes (from Hackage documentation)
- Text.Glabrous: [tags] :: Result -> ![Tag]
- Text.Glabrous.Types: [tags] :: Result -> ![Tag]
- 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: [context] :: Result -> !Context
+ Text.Glabrous: fromTagsList :: [Text] -> Context
+ Text.Glabrous.Types: [context] :: Result -> !Context
+ 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: Partial :: !Template -> ![Tag] -> Result
+ Text.Glabrous: Partial :: !Template -> !Context -> Result
- Text.Glabrous.Types: Partial :: !Template -> ![Tag] -> Result
+ Text.Glabrous.Types: Partial :: !Template -> !Context -> Result
Files
- glabrous.cabal +11/−10
- src/Text/Glabrous.hs +18/−11
- src/Text/Glabrous/Types.hs +3/−2
- tests/hspec.hs +2/−2
glabrous.cabal view
@@ -1,5 +1,5 @@ name: glabrous-version: 0.1.3.0+version: 0.2.0 synopsis: A template DSL library description: A minimalistic, Mustache-like syntax, truly logic-less, pure Text template DSL library@@ -23,16 +23,17 @@ exposed-modules: Text.Glabrous , Text.Glabrous.Types other-modules: Text.Glabrous.Internal- build-depends: aeson >= 0.11- , aeson-pretty >= 0.7- , attoparsec >= 0.13+ build-depends: aeson >= 0.11.2 && < 0.12+ , aeson-pretty >= 0.7.2 && < 0.8+ , attoparsec >= 0.13.0 && < 0.14 , base >= 4.7 && < 5- , bytestring >= 0.10.6- , cereal >= 0.5.2- , cereal-text- , either >= 4.4- , text >= 1.2- , unordered-containers >= 0.2+ , bytestring >= 0.10.6 && < 0.11+ , cereal >= 0.5.2 && < 0.6+ , cereal-text >= 0.1.0 && < 0.2+ , either >= 4.4.1 && < 4.5+ , text >= 1.2.2 && < 1.3+ , unordered-containers >= 0.2.7 && < 0.3+ default-language: Haskell2010 GHC-options: -Wall
src/Text/Glabrous.hs view
@@ -33,6 +33,7 @@ -- ** Get a 'Context' , initContext+ , fromTagsList , fromList , fromTemplate @@ -129,12 +130,18 @@ fromList :: [(T.Text, T.Text)] -> Context fromList ts = Context { variables = H.fromList ts } --- | Build an adhoc 'Context' for the given--- 'Template' with all its variables empty.+-- | Build an unset 'Context' from a list of 'Tag's.+--+-- >λ>fromTagsList ["something","etc."]+-- >Context {variables = fromList [("etc.",""),("something","")]}+fromTagsList :: [T.Text] -> Context+fromTagsList ts = fromList $ (\t -> (t,T.empty)) <$> ts++-- | Build an unset ad hoc 'Context' from the given 'Template'. fromTemplate :: Template -> Context fromTemplate t = setVariables ((\e -> (e,T.empty)) <$> tagsOf t) initContext --- | Get a 'Context' from a file.+-- | Get a 'Context' from a JSON file. readContextFile :: FilePath -> IO (Maybe Context) readContextFile f = decode <$> L.readFile f @@ -150,7 +157,7 @@ writeContextFile :: FilePath -> Context -> IO () writeContextFile f c = L.writeFile f $ encodePretty c --- | Based on the given 'Context', write a+-- | Based on the given 'Context', write a JSON -- 'Context' file with all its variables empty. -- -- @@@ -182,7 +189,7 @@ isSet Context{..} = H.foldr (\v b -> b && v /= T.empty) True variables --- | Get the list of the given 'Context' variables+-- | Get the list of the given 'Context' variables. variablesOf :: Context -> [T.Text] variablesOf Context{..} = H.keys variables @@ -228,7 +235,7 @@ isFinal Template{..} = all isLiteral content -- | Process, discard 'Tag's which are not in the 'Context'--- and leave them without replacement text in the final 'T.Text'.+-- and replace them with nothing in the final 'T.Text'. process :: Template -> Context -> T.Text process = processWithDefault T.empty @@ -257,20 +264,20 @@ trans t = t -- | Process a (sub)'Context' present in the given template, and--- get either a 'Final' 'T.Text' or a new 'Template' with the list--- of its 'Tag's.+-- get either a 'Final' 'T.Text' or a new 'Template' with its unset+-- ad hoc 'Context'. -- -- >λ>partialProcess' template context--- >Partial {template = Template {content = [Literal "Some ",Tag "tags",Literal " are unused in this ",Tag "text",Literal "."]}, tags = ["tags","text"]}+-- >Partial {template = Template {content = [Literal "Some ",Tag "tags",Literal " are unused in this ",Tag "text",Literal "."]}, context = Context {variables = fromList [("text",""),("tags","")]}} partialProcess' :: Template -> Context -> G.Result partialProcess' t c@Context{..} = case foldl trans ([],[]) (content t) of (f,[]) -> Final $ toTextWithContext (const T.empty) c f- (p,p') -> G.Partial Template { content = p } p'+ (p,p') -> G.Partial Template { content = p } (fromTagsList p') where trans (!c',!ts) t' = case t' of- Tag k ->+ Tag k -> case H.lookup k variables of Just v -> (c' ++ [Literal v],ts) Nothing -> (c' ++ [t'],ts ++ [k])
src/Text/Glabrous/Types.hs view
@@ -8,6 +8,7 @@ import qualified Data.Text as T import Data.Serialize import Data.Serialize.Text ()+import Control.Arrow (second) import GHC.Generics data Token@@ -31,7 +32,7 @@ instance ToJSON Context where toJSON (Context h) =- object $ (\(k,v) -> (k,String v)) <$> H.toList h+ object (second String <$> H.toList h) instance FromJSON Context where parseJSON (Object o) = return@@ -42,6 +43,6 @@ data Result = Final !T.Text- | Partial { template :: !Template , tags :: ![Tag] }+ | Partial { template :: !Template , context :: !Context } deriving (Eq, Show)
tests/hspec.hs view
@@ -7,7 +7,7 @@ import Data.Text import System.Directory (removeFile) import Test.Hspec-import Text.Glabrous+import Text.Glabrous as G import Text.Glabrous.Types templateText :: Text@@ -90,7 +90,7 @@ describe "partialProcess'" $ 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"]}+ (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."]}, G.context = Context {variables = H.fromList [("consectetur",""),("mattis","")]}} describe "compress" $ it "optimize a template after partialProcess(')" $