summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template/List.hs
blob: e8da74f87ebf8d9ca87ae8bb21bcfba7906c1235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
--------------------------------------------------------------------------------
-- | Provides an easy way to combine several items in a list. The applications
-- are obvious:
--
-- * A post list on a blog
--
-- * An image list in a gallery
--
-- * A sitemap
module Hakyll.Web.Template.List
    ( applyTemplateList
    , chronological
    , recentFirst
    ) where


--------------------------------------------------------------------------------
import           Data.List                   (sortBy)
import           Data.Ord                    (comparing)
import           System.FilePath             (takeBaseName)


--------------------------------------------------------------------------------
import           Hakyll.Core.Compiler
import           Hakyll.Core.Identifier
import           Hakyll.Core.Item
import           Hakyll.Web.Template
import           Hakyll.Web.Template.Context


--------------------------------------------------------------------------------
-- | Set a field of a page to a listing of pages
applyTemplateList :: Template
                  -> Context a
                  -> [Item a]
                  -> Compiler String
applyTemplateList tpl context items = do
    items' <- mapM (applyTemplate tpl context) items
    return $ concat $ map itemBody items'


--------------------------------------------------------------------------------
-- | Sort pages chronologically. This function assumes that the pages have a
-- @year-month-day-title.extension@ naming scheme -- as is the convention in
-- Hakyll.
chronological :: [Item a] -> [Item a]
chronological = sortBy $ comparing $ takeBaseName . toFilePath . itemIdentifier


--------------------------------------------------------------------------------
-- | The reverse of 'chronological'
recentFirst :: [Item a] -> [Item a]
recentFirst = reverse . chronological