Skip to content

Commit 252ccce

Browse files
emhoracekLibby
authored andcommitted
Use plain old functions instead of StateT
1 parent 1c2c328 commit 252ccce

File tree

6 files changed

+321
-293
lines changed

6 files changed

+321
-293
lines changed

src/Web/Larceny.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ renderWith l sub s = renderRelative l sub s []
130130
renderRelative :: Library s -> Substitutions s -> s -> Path -> Path -> IO (Maybe Text)
131131
renderRelative l sub s givenPath targetPath =
132132
case findTemplate l givenPath targetPath of
133-
(pth, Just (Template run)) -> Just <$> evalStateT (run pth sub l) s
133+
(pth, Just (Template run)) -> Just <$> fst <$> run pth sub l s
134134
(_, Nothing) -> return Nothing
135135

136136
-- | Load all the templates in some directory into a Library.

src/Web/Larceny/Fills.hs

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

3-
module Web.Larceny.Fills ( textFill
4-
, textFill'
5-
, rawTextFill
6-
, rawTextFill'
7-
, mapSubs
8-
, mapSubs'
9-
, fillChildren
10-
, fillChildrenWith
11-
, fillChildrenWith'
12-
, maybeFillChildrenWith
13-
, maybeFillChildrenWith'
14-
, ifFill
15-
, useAttrs
16-
, a
17-
, (%)) where
3+
module Web.Larceny.Fills
4+
( textFill
5+
, textFill'
6+
, rawTextFill
7+
, rawTextFill'
8+
, mapSubs
9+
, mapSubs'
10+
, fillChildren
11+
, fillChildrenWith
12+
, fillChildrenWith'
13+
, maybeFillChildrenWith
14+
, maybeFillChildrenWith'
15+
, ifFill
16+
, useAttrs
17+
, a
18+
, (%)
19+
)
20+
where
1821

1922
import Control.Exception
20-
import Control.Monad.State (StateT)
21-
import qualified Data.Map as M
22-
import Data.Maybe (fromMaybe)
23-
import Data.Text (Text)
24-
import qualified Data.Text as T
25-
import qualified HTMLEntities.Text as HE
23+
import Control.Monad ( foldM )
24+
import Control.Monad.State ( StateT
25+
, runStateT
26+
)
27+
import qualified Data.Map as M
28+
import Data.Maybe ( fromMaybe )
29+
import Data.Text ( Text )
30+
import qualified Data.Text as T
31+
import qualified HTMLEntities.Text as HE
2632
------------
2733
import Web.Larceny.Types
2834

@@ -64,18 +70,20 @@ import Web.Larceny.Types
6470
-- @
6571
-- > This list is not empty.
6672
ifFill :: Fill s
67-
ifFill =
68-
useAttrs (a "condition" % a "exists") ifFill'
69-
where ifFill' :: Maybe Bool -> Maybe Text -> Fill s
70-
ifFill' mCondition mExisting =
71-
let condition = fromMaybe True mCondition
72-
existing = fromMaybe "exist" mExisting
73-
bool = condition && existing /= ""
74-
thenElseSubs = subs [("then", thenFill bool)
75-
,("else", thenFill (not bool))] in
76-
fillChildrenWith thenElseSubs
77-
thenFill True = fillChildren
78-
thenFill False = textFill ""
73+
ifFill = useAttrs (a "condition" % a "exists") ifFill'
74+
where
75+
ifFill' :: Maybe Bool -> Maybe Text -> Fill s
76+
ifFill' mCondition mExisting =
77+
let
78+
condition = fromMaybe True mCondition
79+
existing = fromMaybe "exist" mExisting
80+
bool = condition && existing /= ""
81+
thenElseSubs =
82+
subs [("then", thenFill bool), ("else", thenFill (not bool))]
83+
in
84+
fillChildrenWith thenElseSubs
85+
thenFill True = fillChildren
86+
thenFill False = textFill ""
7987

8088
-- | A plain text fill.
8189
--
@@ -100,7 +108,9 @@ rawTextFill t = rawTextFill' (return t)
100108
-- textFill' getTextFromDatabase
101109
-- @
102110
textFill' :: StateT s IO Text -> Fill s
103-
textFill' t = Fill $ \_m _t _l -> HE.text <$> t
111+
textFill' t = Fill $ \_m _t _l st-> do
112+
(t, st') <- runStateT t st
113+
return (HE.text t, st')
104114

105115
-- | Use state or IO, then fill in some text.
106116
--
@@ -109,7 +119,7 @@ textFill' t = Fill $ \_m _t _l -> HE.text <$> t
109119
-- textFill' getTextFromDatabase
110120
-- @
111121
rawTextFill' :: StateT s IO Text -> Fill s
112-
rawTextFill' t = Fill $ \_m _t _l -> t
122+
rawTextFill' t = Fill $ \_m _t _l st -> runStateT t st
113123

114124
-- | Create substitutions for each element in a list and fill the child nodes
115125
-- with those substitutions.
@@ -121,20 +131,26 @@ rawTextFill' t = Fill $ \_m _t _l -> t
121131
-- @
122132
--
123133
-- > Bonnie Thunders Donna Matrix Beyonslay
124-
mapSubs :: (a -> Substitutions s)
125-
-> [a]
126-
-> Fill s
127-
mapSubs f xs = Fill $ \_attrs (pth, tpl) lib ->
128-
T.concat <$> mapM (\n -> runTemplate tpl pth (f n) lib) xs
134+
mapSubs :: (a -> Substitutions s) -> [a] -> Fill s
135+
mapSubs f xs = Fill $ \_attrs (pth, tpl) lib st -> foldM
136+
(\(text, st) item -> do
137+
(t, st') <- runTemplate tpl pth (f item) lib st
138+
return (text <> t, st')
139+
)
140+
("", st)
141+
xs
129142

130143
-- | Create substitutions for each element in a list (using IO/state if
131144
-- needed) and fill the child nodes with those substitutions.
132145
mapSubs' :: (a -> StateT s IO (Substitutions s)) -> [a] -> Fill s
133-
mapSubs' f xs = Fill $
134-
\_m (pth, tpl) lib ->
135-
T.concat <$> mapM (\x -> do
136-
s' <- f x
137-
runTemplate tpl pth s' lib) xs
146+
mapSubs' f xs = Fill $ \_m (pth, tpl) lib st -> foldM
147+
(\(text, st) item -> do
148+
(s', st' ) <- runStateT (f item) st
149+
(t , st'') <- runTemplate tpl pth s' lib st'
150+
return (text <> t, st'')
151+
)
152+
("", st)
153+
xs
138154

139155
-- | Fill in the child nodes of the blank with substitutions already
140156
-- available.
@@ -182,8 +198,8 @@ fillChildrenWith' m = maybeFillChildrenWith' (Just <$> m)
182198
-- > Bonnie Thunders
183199
maybeFillChildrenWith :: Maybe (Substitutions s) -> Fill s
184200
maybeFillChildrenWith Nothing = textFill ""
185-
maybeFillChildrenWith (Just s) = Fill $ \_s (pth, Template tpl) l ->
186-
tpl pth s l
201+
maybeFillChildrenWith (Just s) =
202+
Fill $ \_a (pth, Template tpl) l st -> tpl pth s l st
187203

188204
-- | Use state and IO and maybe fill in with some substitutions.
189205
--
@@ -198,11 +214,11 @@ maybeFillChildrenWith (Just s) = Fill $ \_s (pth, Template tpl) l ->
198214
--
199215
-- > Bonnie Thunders
200216
maybeFillChildrenWith' :: StateT s IO (Maybe (Substitutions s)) -> Fill s
201-
maybeFillChildrenWith' sMSubs = Fill $ \_s (pth, Template tpl) l -> do
202-
mSubs <- sMSubs
217+
maybeFillChildrenWith' sMSubs = Fill $ \_s (pth, Template tpl) l s -> do
218+
(mSubs, s') <- runStateT sMSubs s
203219
case mSubs of
204-
Nothing -> return ""
205-
Just s -> tpl pth s l
220+
Nothing -> return ("", s)
221+
Just s -> tpl pth s l s'
206222

207223
-- | Use attributes from the the blank as arguments to the fill.
208224
--
@@ -220,11 +236,9 @@ maybeFillChildrenWith' sMSubs = Fill $ \_s (pth, Template tpl) l -> do
220236
-- attributes that you can use in Fills. You can use `a` and `%` to
221237
-- create these. The second argument is a function that uses the
222238
-- values of those attributes to create a Fill.
223-
useAttrs :: (Attributes -> k -> Fill s)
224-
-> k
225-
-> Fill s
226-
useAttrs k fill= Fill $ \atrs (pth, tpl) lib ->
227-
unFill (k atrs fill) atrs (pth, tpl) lib
239+
useAttrs :: (Attributes -> k -> Fill s) -> k -> Fill s
240+
useAttrs k fill =
241+
Fill $ \atrs (pth, tpl) lib s -> unFill (k atrs fill) atrs (pth, tpl) lib s
228242

229243
-- | Prepend `a` to the name of an attribute to pass the value of that
230244
-- attribute to the fill.
@@ -234,8 +248,8 @@ useAttrs k fill= Fill $ \atrs (pth, tpl) lib ->
234248
-- is rendered.
235249
a :: (FromAttribute a) => Text -> Attributes -> (a -> b) -> b
236250
a attrName attrs k =
237-
let mAttr = M.lookup attrName attrs in
238-
k (either (\e -> throw $ e attrName) id (fromAttribute mAttr))
251+
let mAttr = M.lookup attrName attrs
252+
in k (either (\e -> throw $ e attrName) id (fromAttribute mAttr))
239253

240254
-- | Use with `a` to use multiple attributes in the fill.
241255
--
@@ -250,7 +264,5 @@ a attrName attrs k =
250264
-- @
251265
--
252266
-- > A really l...
253-
(%) :: (Attributes -> a -> b)
254-
-> (Attributes -> b -> c)
255-
-> Attributes -> a -> c
267+
(%) :: (Attributes -> a -> b) -> (Attributes -> b -> c) -> Attributes -> a -> c
256268
(%) f1 f2 attrs k = f2 attrs (f1 attrs k)

0 commit comments

Comments
 (0)