{-# LANGUAGE TupleSections #-}
module Hakyll.Web.Template.List
( applyTemplateList
, applyJoinTemplateList
, chronological
, recentFirst
, sortChronological
, sortRecentFirst
) where
import Control.Monad (liftM)
import Data.List (intersperse, sortBy)
import Data.Ord (comparing)
import Data.Time.Locale.Compat (defaultTimeLocale)
import Hakyll.Core.Compiler
import Hakyll.Core.Identifier
import Hakyll.Core.Item
import Hakyll.Core.Metadata
import Hakyll.Web.Template
import Hakyll.Web.Template.Context
applyTemplateList :: Template
-> Context a
-> [Item a]
-> Compiler String
applyTemplateList = applyJoinTemplateList ""
applyJoinTemplateList :: String
-> Template
-> Context a
-> [Item a]
-> Compiler String
applyJoinTemplateList delimiter tpl context items = do
items' <- mapM (applyTemplate tpl context) items
return $ concat $ intersperse delimiter $ map itemBody items'
chronological :: MonadMetadata m => [Item a] -> m [Item a]
chronological =
sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
where
sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
mapM (\x -> liftM (x,) (f x)) xs
recentFirst :: MonadMetadata m => [Item a] -> m [Item a]
recentFirst = liftM reverse . chronological
sortChronological
:: MonadMetadata m => [Identifier] -> m [Identifier]
sortChronological ids =
liftM (map itemIdentifier) $ chronological [Item i () | i <- ids]
sortRecentFirst
:: MonadMetadata m => [Identifier] -> m [Identifier]
sortRecentFirst ids =
liftM (map itemIdentifier) $ recentFirst [Item i () | i <- ids]