Skip to content

Commit

Permalink
reformatting code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Boucey committed Nov 4, 2016
1 parent c8922c4 commit 5aaade3
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 121 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Glabrous [![Build Status](https://travis-ci.org/MichelBoucey/glabrous.svg?branch=master)](https://travis-ci.org/MichelBoucey/glabrous)

Glabrous is a minimalistic Mustache-like syntax - using only the simplest Mustache tag: {{name}} -, truly logic-less, HTML agnostic pure Text template library.
Glabrous is a minimalistic Mustache-like syntax - using only the simplest Mustache tag: {{name}} -, truly logic-less, HTML agnostic pure Text template DSL library.

Any improvement is welcome.
215 changes: 108 additions & 107 deletions src/Text/Glabrous.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@
--

module Text.Glabrous
(

-- * 'Template'
Template (..)
, Tag

-- ** Get a 'Template'
, fromText
, readTemplateFile

-- ** 'Template' operations
, toText
, isFinal
, tagsOf
, tagsRename
, compress
, writeTemplateFile

-- * 'Context'
, Context (..)

-- ** Get a 'Context'
, initContext
, fromList
, fromTemplate

-- ** 'Context' operations
, setVariables
, deleteVariables
, variablesOf
, isSet
, unsetContext

-- ** JSON 'Context' file
, readContextFile
, writeContextFile
, initContextFile

-- * Processing
, process
, processWithDefault
, partialProcess
, G.Result (..)
, partialProcess'

) where
(

-- * 'Template'
Template (..)
, Tag

-- ** Get a 'Template'
, fromText
, readTemplateFile

-- ** 'Template' operations
, toText
, isFinal
, tagsOf
, tagsRename
, compress
, writeTemplateFile

-- * 'Context'
, Context (..)

-- ** Get a 'Context'
, initContext
, fromList
, fromTemplate

-- ** 'Context' operations
, setVariables
, deleteVariables
, variablesOf
, isSet
, unsetContext

-- ** JSON 'Context' file
, readContextFile
, writeContextFile
, initContextFile

-- * Processing
, process
, processWithDefault
, partialProcess
, G.Result (..)
, partialProcess'

) where

import Control.Monad
import Data.Aeson
Expand All @@ -71,22 +71,22 @@ import Text.Glabrous.Types as G

-- | Optimize a 'Template' content after (many) 'partialProcess'(') rewriting(s).
compress :: Template -> Template
compress Template {..} =
Template { content = go content [] }
compress Template{..} =
Template { content = go content [] }
where
go ts !ac = do
let (a,b) = span isLiteral ts
u = uncons b
if not (null a)
then case u of
Just (c,d) -> go d (ac ++ [concatLiterals a] ++ [c])
Nothing -> ac ++ [concatLiterals a]
else case u of
Just (e,f) -> go f (ac ++ [e])
Nothing -> ac
let (a,b) = span isLiteral ts
u = uncons b
if not (null a)
then case u of
Just (c,d) -> go d (ac ++ [concatLiterals a] ++ [c])
Nothing -> ac ++ [concatLiterals a]
else case u of
Just (e,f) -> go f (ac ++ [e])
Nothing -> ac
where
concatLiterals =
foldr trans (Literal "")
foldr trans (Literal "")
where
trans (Literal a) (Literal b) = Literal (a `T.append` b)
trans _ _ = undefined
Expand All @@ -100,26 +100,26 @@ initContext = Context { variables = H.empty }
-- >λ>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 Context {..} =
go ts variables
setVariables ts Context{..} =
go ts variables
where
go _ts vs =
case uncons _ts of
Just ((k,v),ts') -> go ts' $ H.insert k v vs
Nothing -> Context { variables = vs }
case uncons _ts of
Just ((k,v),ts') -> go ts' $ H.insert k v vs
Nothing -> Context { variables = vs }

-- | Delete variables from a 'Context' by these names.
--
-- >λ>deleteVariables ["something"] context
-- >Context {variables = fromList [("etc.","..."),("about","Haskell"),("name","")]}
deleteVariables :: [T.Text] -> Context -> Context
deleteVariables ts Context {..} =
go ts variables
deleteVariables ts Context{..} =
go ts variables
where
go _ts vs =
case uncons _ts of
Just (k,ts') -> go ts' $ H.delete k vs
Nothing -> Context { variables = vs }
case uncons _ts of
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.
--
Expand Down Expand Up @@ -162,7 +162,7 @@ writeContextFile f c = L.writeFile f $ encodePretty c
--
initContextFile :: FilePath -> Context -> IO ()
initContextFile f Context {..} = L.writeFile f $
encodePretty Context { variables = H.map (const T.empty) variables }
encodePretty Context { variables = H.map (const T.empty) variables }

-- | Build 'Just' a (sub)'Context' made of unset variables
-- of the given context, or 'Nothing'.
Expand All @@ -172,19 +172,19 @@ initContextFile f Context {..} = L.writeFile f $
--
unsetContext :: Context -> Maybe Context
unsetContext Context {..} = do
let vs = H.filter (== T.empty) variables
guard (vs /= H.empty)
return Context { variables = vs }
let vs = H.filter (== T.empty) variables
guard (vs /= H.empty)
return Context { variables = vs }

-- | 'True' if the all variables of
-- the given 'Context' are not empty.
isSet :: Context -> Bool
isSet Context {..} =
H.foldr (\v b -> b && v /= T.empty) True variables
isSet Context{..} =
H.foldr (\v b -> b && v /= T.empty) True variables

-- | Get the list of the given 'Context' variables
variablesOf :: Context -> [T.Text]
variablesOf Context {..} = H.keys variables
variablesOf Context{..} = H.keys variables

-- | Get a 'Template' from a file.
readTemplateFile :: FilePath -> IO (Either String Template)
Expand All @@ -198,34 +198,34 @@ writeTemplateFile f t = I.writeFile f $ toText t
-- as it is, with its 'Tag's, if they exist. No
-- 'Context' is processed.
toText :: Template -> T.Text
toText Template {..} =
T.concat $ trans <$> content
toText Template{..} =
T.concat $ trans <$> content
where
trans (Literal c) = c
trans (Tag k) = T.concat ["{{",k,"}}"]

-- | Get the list of 'Tag's in the given 'Template'.
tagsOf :: Template -> [Tag]
tagsOf Template {..} =
(\(Tag k) -> k) <$> filter isTag content
tagsOf Template{..} =
(\(Tag k) -> k) <$> filter isTag content
where
isTag (Tag _) = True
isTag _ = False

tagsRename :: [(T.Text,T.Text)] -> Template -> Template
tagsRename ts Template {..} =
Template { content = rename <$> content }
tagsRename ts Template{..} =
Template { content = rename <$> content }
where
rename t@(Tag n) =
case lookup n ts of
Just r -> Tag r
Nothing -> t
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
isFinal Template {..} = all isLiteral content
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'.
Expand All @@ -234,25 +234,26 @@ process = processWithDefault T.empty

-- | Process and replace missing variables in 'Context'
-- with the given default replacement 'T.Text'.
processWithDefault :: T.Text -- ^ Default replacement text
-> Template
-> Context
-> T.Text
processWithDefault d Template {..} c = toTextWithContext (const d) c content
processWithDefault
:: T.Text -- ^ Default replacement text
-> Template
-> Context
-> T.Text
processWithDefault d Template{..} c = toTextWithContext (const d) c content

-- | Process a (sub)'Context' present in the given template, leaving
-- untouched, if they exist, other 'Tag's, to obtain a new template.
partialProcess :: Template -> Context -> Template
partialProcess Template {..} c =
Template { content = transTags content c }
partialProcess Template{..} c =
Template { content = transTags content c }
where
transTags ts Context {..} =
trans <$> ts
transTags ts Context{..} =
trans <$> ts
where
trans i@(Tag k) =
case H.lookup k variables of
Just v -> Literal v
Nothing -> i
case H.lookup k variables of
Just v -> Literal v
Nothing -> i
trans t = t

-- | Process a (sub)'Context' present in the given template, and
Expand All @@ -262,16 +263,16 @@ partialProcess Template {..} c =
-- >λ>partialProcess' template context
-- >Partial {template = Template {content = [Literal "Some ",Tag "tags",Literal " are unused in this ",Tag "text",Literal "."]}, tags = ["tags","text"]}
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'
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'
where
trans (!c',!ts) t' =
case t' of
Tag k ->
case H.lookup k variables of
Just v -> (c' ++ [Literal v],ts)
Nothing -> (c' ++ [t'],ts ++ [k])
Literal _ -> (c' ++ [t'],ts)
case t' of
Tag k ->
case H.lookup k variables of
Just v -> (c' ++ [Literal v],ts)
Nothing -> (c' ++ [t'],ts ++ [k])
Literal _ -> (c' ++ [t'],ts)

26 changes: 13 additions & 13 deletions src/Text/Glabrous/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Text.Glabrous.Types as G

toTextWithContext :: (T.Text -> T.Text) -> Context -> [Token] -> T.Text
toTextWithContext tagDefault Context{..} ts =
T.concat $ trans <$> ts
T.concat $ trans <$> ts
where
trans (Tag k) = fromMaybe (tagDefault k) (H.lookup k variables)
trans (Literal c) = c
Expand All @@ -25,30 +25,30 @@ toTextWithContext tagDefault Context{..} ts =
--
fromText :: T.Text -> Either String Template
fromText t =
case parseOnly tokens t of
Right ts -> Right Template { content = ts }
Left e -> Left e
case parseOnly tokens t of
Right ts -> Right Template { content = ts }
Left e -> Left e

isLiteral :: Token -> Bool
isLiteral (Literal _) = True
isLiteral _ = False

tokens :: Parser [Token]
tokens =
many' token
many' token
where
token = literal <|> tag <|> leftover
leftover = do
c <- takeWhile1 $ not . content
return (Literal c)
c <- takeWhile1 $ not . content
return (Literal c)
literal = do
c <- takeWhile1 content
return (Literal c)
c <- takeWhile1 content
return (Literal c)
tag = do
_ <- string "{{"
Literal t <- literal
_ <- string "}}"
return (Tag t)
_ <- string "{{"
Literal t <- literal
_ <- string "}}"
return (Tag t)
content '}' = False
content '{' = False
content _ = True
Expand Down

0 comments on commit 5aaade3

Please sign in to comment.