@@ -0,0 +1,82 @@
1+-- | Provides an easy way to combine several pages in a list. The applications
2+-- are obvious:
3+--
4+-- * A post list on a blog
5+--
6+-- * An image list in a gallery
7+--
8+-- * A sitemap
9+--
10+module Hakyll.Web.Page.List
11+ ( setFieldPageList
12+ , pageListCompiler
13+ , chronological
14+ , recentFirst
15+ , sortByBaseName
16+ ) where
17+
18+import Control.Arrow ((>>>), arr)
19+import Data.List (sortBy)
20+import Data.Monoid (Monoid, mconcat)
21+import Data.Ord (comparing)
22+import System.FilePath (takeBaseName)
23+
24+import Hakyll.Core.Compiler
25+import Hakyll.Core.Identifier
26+import Hakyll.Core.Identifier.Pattern
27+import Hakyll.Web.Page
28+import Hakyll.Web.Page.Metadata
29+import Hakyll.Web.Template
30+
31+-- | Set a field of a page to a listing of pages
32+--
33+setFieldPageList :: ([Page String] -> [Page String])
34+ -- ^ Determines list order
35+ -> Identifier Template
36+ -- ^ Applied to every page
37+ -> String
38+ -- ^ Key indicating which field should be set
39+ -> Pattern (Page String)
40+ -- ^ Selects pages to include in the list
41+ -> Compiler (Page String) (Page String)
42+ -- ^ Compiler that sets the page list in a field
43+setFieldPageList sort template key pattern =
44+ requireAllA pattern $ setFieldA key $ pageListCompiler sort template
45+
46+-- | Create a list of pages
47+--
48+pageListCompiler :: ([Page String] -> [Page String]) -- ^ Determine list order
49+ -> Identifier Template -- ^ Applied to pages
50+ -> Compiler [Page String] String -- ^ Compiles page list
51+pageListCompiler sort template =
52+ arr sort >>> applyTemplateToList template >>> arr concatPages
53+
54+-- | Apply a template to every page in a list
55+--
56+applyTemplateToList :: Identifier Template
57+ -> Compiler [Page String] [Page String]
58+applyTemplateToList identifier =
59+ require identifier $ \posts template -> map (applyTemplate template) posts
60+
61+-- | Concatenate the bodies of a page list
62+--
63+concatPages :: Monoid m => [Page m] -> m
64+concatPages = mconcat . map pageBody
65+
66+-- | Sort pages chronologically. This function assumes that the pages have a
67+-- @year-month-day-title.extension@ naming scheme -- as is the convention in
68+-- Hakyll.
69+--
70+chronological :: [Page a] -> [Page a]
71+chronological = sortBy $ comparing $ takeBaseName . getField "path"
72+
73+-- | The reverse of 'chronological'
74+--
75+recentFirst :: [Page a] -> [Page a]
76+recentFirst = reverse . chronological
77+
78+-- | Deprecated, see 'chronological'
79+--
80+sortByBaseName :: [Page a] -> [Page a]
81+sortByBaseName = chronological
82+{-# DEPRECATED sortByBaseName "Use chronological" #-}