module Hakyll.Web.Page.List
( setFieldPageList
, pageListCompiler
, chronological
, recentFirst
, sortByBaseName
) where
import Control.Arrow ((>>>), arr)
import Data.List (sortBy)
import Data.Monoid (Monoid, mconcat)
import Data.Ord (comparing)
import System.FilePath (takeBaseName)
import Hakyll.Core.Compiler
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
import Hakyll.Web.Page
import Hakyll.Web.Page.Metadata
import Hakyll.Web.Template
setFieldPageList :: ([Page String] -> [Page String])
-> Identifier Template
-> String
-> Pattern (Page String)
-> Compiler (Page String) (Page String)
setFieldPageList sort template key pattern =
requireAllA pattern $ setFieldA key $ pageListCompiler sort template
pageListCompiler :: ([Page String] -> [Page String])
-> Identifier Template
-> Compiler [Page String] String
pageListCompiler sort template =
arr sort >>> applyTemplateToList template >>> arr concatPages
applyTemplateToList :: Identifier Template
-> Compiler [Page String] [Page String]
applyTemplateToList identifier =
require identifier $ \posts template -> map (applyTemplate template) posts
concatPages :: Monoid m => [Page m] -> m
concatPages = mconcat . map pageBody
chronological :: [Page a] -> [Page a]
chronological = sortBy $ comparing $ takeBaseName . getField "path"
recentFirst :: [Page a] -> [Page a]
recentFirst = reverse . chronological
sortByBaseName :: [Page a] -> [Page a]
sortByBaseName = chronological