on master help sign in
Add applyJoinTemplateList, and a test for it
commit652ceb03f1185ad8d41e7a3b91f00e1064bdd4ba authorJasper Van der Jeugt <m@jaspervdj.be> dateWed 16 Jan 2013, 10:50 (14 years ago) parente53ca67
3 files changed+55−14
src/Hakyll/Web/Template/Internal.hs+18−8 src/Hakyll/Web/Template/List.hs+17−4 tests/Hakyll/Web/Template/Tests.hs+20−2
src/Hakyll/Web/Template/Internal.hs+18−8
@@ -1,37 +1,47 @@
1+--------------------------------------------------------------------------------
12-- | Module containing the template data structure
2−--
3−{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable #-}
3+{-# LANGUAGE DeriveDataTypeable #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
45module Hakyll.Web.Template.Internal
56 ( Template (..)
67 , TemplateElement (..)
78 ) where
89
9−import Control.Applicative ((<$>))
1010
11−import Data.Binary (Binary, get, getWord8, put, putWord8)
12−import Data.Typeable (Typeable)
11+--------------------------------------------------------------------------------
12+import Control.Applicative ((<$>))
13+import Data.Binary (Binary, get, getWord8, put, putWord8)
14+import Data.Typeable (Typeable)
1315
14−import Hakyll.Core.Writable
1516
17+--------------------------------------------------------------------------------
18+import Hakyll.Core.Writable
19+
20+
21+--------------------------------------------------------------------------------
1622-- | Datatype used for template substitutions.
17−--
1823newtype Template = Template
1924 { unTemplate :: [TemplateElement]
2025 }
2126 deriving (Show, Eq, Binary, Typeable)
2227
28+
29+--------------------------------------------------------------------------------
2330instance Writable Template where
2431 -- Writing a template is impossible
2532 write _ _ = return ()
2633
34+
35+--------------------------------------------------------------------------------
2736-- | Elements of a template.
28−--
2937data TemplateElement
3038 = Chunk String
3139 | Key String
3240 | Escaped
3341 deriving (Show, Eq, Typeable)
3442
43+
44+--------------------------------------------------------------------------------
3545instance Binary TemplateElement where
3646 put (Chunk string) = putWord8 0 >> put string
3747 put (Key key) = putWord8 1 >> put key
src/Hakyll/Web/Template/List.hs+17−4
@@ -9,13 +9,14 @@
99-- * A sitemap
1010module Hakyll.Web.Template.List
1111 ( applyTemplateList
12+ , applyJoinTemplateList
1213 , chronological
1314 , recentFirst
1415 ) where
1516
1617
1718--------------------------------------------------------------------------------
18−import Data.List (sortBy)
19+import Data.List (intersperse, sortBy)
1920import Data.Ord (comparing)
2021import System.FilePath (takeBaseName)
2122
@@ -29,14 +30,26 @@ import Hakyll.Web.Template.Context
2930
3031
3132--------------------------------------------------------------------------------
32−-- | Set a field of a page to a listing of pages
33+-- | Generate a string of a listing of pages, after applying a template to each
34+-- page.
3335applyTemplateList :: Template
3436 -> Context a
3537 -> [Item a]
3638 -> Compiler String
37−applyTemplateList tpl context items = do
39+applyTemplateList = applyJoinTemplateList ""
40+
41+
42+--------------------------------------------------------------------------------
43+-- | Join a listing of pages with a string in between, after applying a template
44+-- to each page.
45+applyJoinTemplateList :: String
46+ -> Template
47+ -> Context a
48+ -> [Item a]
49+ -> Compiler String
50+applyJoinTemplateList delimiter tpl context items = do
3851 items' <- mapM (applyTemplate tpl context) items
39− return $ concat $ map itemBody items'
52+ return $ concat $ intersperse delimiter $ map itemBody items'
4053
4154
4255--------------------------------------------------------------------------------
tests/Hakyll/Web/Template/Tests.hs+20−2
@@ -9,7 +9,7 @@ module Hakyll.Web.Template.Tests
99import Data.Monoid (mconcat)
1010import Test.Framework (Test, testGroup)
1111import Test.Framework.Providers.HUnit (testCase)
12−import Test.HUnit (Assertion, (@=?))
12+import Test.HUnit (Assertion, (@=?), (@?=))
1313
1414
1515--------------------------------------------------------------------------------
@@ -18,13 +18,16 @@ import Hakyll.Core.Provider
1818import Hakyll.Web.Pandoc
1919import Hakyll.Web.Template
2020import Hakyll.Web.Template.Context
21+import Hakyll.Web.Template.Internal
22+import Hakyll.Web.Template.List
2123import TestSuite.Util
2224
2325
2426--------------------------------------------------------------------------------
2527tests :: Test
2628tests = testGroup "Hakyll.Core.Template.Tests"
27− [ testCase "case01" case01
29+ [ testCase "case01" case01
30+ , testCase "applyJoinTemplateList" testApplyJoinTemplateList
2831 ]
2932
3033
@@ -49,3 +52,18 @@ testContext = mconcat
4952 [ functionField "echo" (\args _ -> return $ unwords args)
5053 , defaultContext
5154 ]
55+
56+
57+--------------------------------------------------------------------------------
58+testApplyJoinTemplateList :: Assertion
59+testApplyJoinTemplateList = do
60+ store <- newTestStore
61+ provider <- newTestProvider store
62+ str <- testCompilerDone store provider "item3" $
63+ applyJoinTemplateList ", " tpl defaultContext [i1, i2]
64+
65+ str @?= "<b>Hello</b>, <b>World</b>"
66+ where
67+ i1 = Item "item1" "Hello"
68+ i2 = Item "item2" "World"
69+ tpl = Template [Chunk "<b>", Key "body", Chunk "</b>"]