Debian Codegraph @

lib/Hakyll/Web/Template/List.hs

raw
1 --------------------------------------------------------------------------------
2 -- | Provides an easy way to combine several items in a list. The applications
3 -- are obvious:
4 --
5 -- * A post list on a blog
6 --
7 -- * An image list in a gallery
8 --
9 -- * A sitemap
10 {-# LANGUAGE CPP #-}
11 {-# LANGUAGE TupleSections #-}
12 module Hakyll.Web.Template.List
13 ( applyTemplateList
14 , applyJoinTemplateList
15 , chronological
16 , recentFirst
17 , sortChronological
18 , sortRecentFirst
19 ) where
20
21
22 --------------------------------------------------------------------------------
23 import Control.Monad (liftM)
24 #if !MIN_VERSION_base(4,13,0)
25 import Control.Monad.Fail (MonadFail)
26 #endif
27 import Data.List (intersperse, sortBy)
28 import Data.Ord (comparing)
29 import Data.Time.Locale.Compat (defaultTimeLocale)
30
31
32 --------------------------------------------------------------------------------
33 import Hakyll.Core.Compiler
34 import Hakyll.Core.Identifier
35 import Hakyll.Core.Item
36 import Hakyll.Core.Metadata
37 import Hakyll.Web.Template
38 import Hakyll.Web.Template.Context
39
40
41 --------------------------------------------------------------------------------
42 -- | Generate a string of a listing of pages, after applying a template to each
43 -- page.
44 applyTemplateList :: Template
45 -> Context a
46 -> [Item a]
47 -> Compiler String
48 applyTemplateList = applyJoinTemplateList ""
49
50
51 --------------------------------------------------------------------------------
52 -- | Join a listing of pages with a string in between, after applying a template
53 -- to each page.
54 applyJoinTemplateList :: String
55 -> Template
56 -> Context a
57 -> [Item a]
58 -> Compiler String
59 applyJoinTemplateList delimiter tpl context items = do
60 items' <- mapM (applyTemplate tpl context) items
61 return $ concat $ intersperse delimiter $ map itemBody items'
62
63
64 --------------------------------------------------------------------------------
65 -- | Sort pages chronologically. Uses the same method as 'dateField' for
66 -- extracting the date.
67 chronological :: (MonadMetadata m, MonadFail m) => [Item a] -> m [Item a]
68 chronological =
69 sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
70 where
71 sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
72 sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
73 mapM (\x -> liftM (x,) (f x)) xs
74
75
76 --------------------------------------------------------------------------------
77 -- | The reverse of 'chronological'
78 recentFirst :: (MonadMetadata m, MonadFail m) => [Item a] -> m [Item a]
79 recentFirst = liftM reverse . chronological
80
81
82 --------------------------------------------------------------------------------
83 -- | Version of 'chronological' which doesn't need the actual items.
84 sortChronological
85 :: (MonadMetadata m, MonadFail m) => [Identifier] -> m [Identifier]
86 sortChronological ids =
87 liftM (map itemIdentifier) $ chronological [Item i () | i <- ids]
88
89
90 --------------------------------------------------------------------------------
91 -- | Version of 'recentFirst' which doesn't need the actual items.
92 sortRecentFirst
93 :: (MonadMetadata m, MonadFail m) => [Identifier] -> m [Identifier]
94 sortRecentFirst ids =
95 liftM (map itemIdentifier) $ recentFirst [Item i () | i <- ids]