packages feed

glabrous 0.3.6 → 0.4.0

raw patch · 2 files changed

+38/−9 lines, 2 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- 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: insert :: Template -> Text -> Template -> Maybe Template
+ Text.Glabrous: join :: Context -> Context -> Either Context Context
+ 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

Files

glabrous.cabal view
@@ -1,5 +1,5 @@ name:                glabrous-version:             0.3.6+version:             0.4.0 synopsis:            A template DSL library description:         A minimalistic, Mustache-like syntax, truly logic-less,                      pure Text template DSL library
src/Text/Glabrous.hs view
@@ -28,6 +28,7 @@   , toFinalText   , compress   , writeTemplateFile+  , insert    -- * 'Context'   , Context (..)@@ -44,6 +45,7 @@   , variablesOf   , isSet   , unsetContext+  , join    -- ** JSON 'Context' file   , readContextFile@@ -59,7 +61,7 @@    ) where -import           Control.Monad+import           Control.Monad            (guard) import           Data.Aeson import           Data.Aeson.Encode.Pretty (encodePretty) import qualified Data.ByteString.Lazy     as L@@ -107,7 +109,7 @@   where     go _ts vs =       case uncons _ts of-        Just ((k,v),ts') -> go ts' $ H.insert k v vs+        Just ((k,v),ts') -> go ts' (H.insert k v vs)         Nothing          -> Context { variables = vs }  -- | Delete variables from a 'Context' by these names.@@ -120,7 +122,7 @@   where     go _ts vs =       case uncons _ts of-        Just (k,ts') -> go ts' $ H.delete k vs+        Just (k,ts') -> go ts' (H.delete k vs)         Nothing      -> Context { variables = vs }  -- | Build a 'Context' from a list of 'Tag's and replacement 'T.Text's.@@ -146,6 +148,17 @@ readContextFile :: FilePath -> IO (Maybe Context) readContextFile f = decode <$> L.readFile f +-- | Join two 'Context's if they don't share variables+-- name, or get the intersection 'Context' out of them.+join :: Context+     -> Context+     -> Either Context Context+join c c' = do+  let i = H.intersection (variables c) (variables c')+  if i == H.empty+    then Right Context { variables = H.union (variables c) (variables c') }+    else Left Context { variables = i }+ -- | Write a 'Context' to a file. -- -- @@@ -156,7 +169,7 @@ -- @ -- writeContextFile :: FilePath -> Context -> IO ()-writeContextFile f c = L.writeFile f $ encodePretty c+writeContextFile f c = L.writeFile f (encodePretty c)  -- | Based on the given 'Context', write a JSON -- 'Context' file with all its variables empty.@@ -169,7 +182,7 @@ -- @ -- initContextFile :: FilePath -> Context -> IO ()-initContextFile f Context {..} = L.writeFile f $+initContextFile f Context{..} = L.writeFile f $   encodePretty Context { variables = H.map (const T.empty) variables }  -- | Build 'Just' a (sub)'Context' made of unset variables@@ -200,13 +213,29 @@  -- | Write a 'Template' to a file. writeTemplateFile :: FilePath -> Template -> IO ()-writeTemplateFile f t = I.writeFile f $ toText t+writeTemplateFile f t = I.writeFile f (toText t) +-- | get 'Just' a new 'Template' by inserting a 'Template'+-- into another one by replacing the 'Tag' named, or 'Nothing'.+insert :: Template       -- ^ The Template to insert in+       -> T.Text         -- ^ The Tag name to be replaced+       -> Template       -- ^ The Template to be inserted+       -> Maybe Template -- ^ Just the new Template, or Nothing+insert te tn te' = do+  guard (Tag tn `elem` content te)+  return Template { content = foldl trans [] (content te) }+  where+    trans o t@(Tag tn') =+      if tn' == tn+        then o ++ content te'+        else o ++ [t]+    trans o l = o ++ [l]+ -- | Output the content of the given 'Template' -- as it is, with its 'Tag's, if they exist. toText :: Template -> T.Text toText Template{..} =-  T.concat $ trans <$> content+  T.concat (trans <$> content)   where     trans (Literal c) = c     trans (Tag k)     = T.concat ["{{",k,"}}"]@@ -281,7 +310,7 @@ partialProcess' :: Template -> Context -> G.Result partialProcess' t c@Context{..} =   case foldl trans ([],[]) (content t) of-    (f,[]) -> Final $ toTextWithContext (const T.empty) c f+    (f,[]) -> Final (toTextWithContext (const T.empty) c f)     (p,p') -> G.Partial Template { content = p } (fromTagsList p')   where     trans (!c',!ts) t' =