on master help sign in
Merge pull request #112 from simukis/sorting

Make chronological and recentFirst to respect metadata

commit14fc440511cdb0323eeecd4002bd82d26f177100 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSat 23 Feb 2013, 03:52 (14 years ago) parent007caa76b680c9
2 files changed+17−8
src/Hakyll/Web/Template/Context.hs+4−3 src/Hakyll/Web/Template/List.hs+13−5
src/Hakyll/Web/Template/Context.hs+4−3
@@ -167,9 +167,10 @@ dateFieldWith locale key format = field key $ \i -> do
167167-- | Parser to try to extract and parse the time from the @published@
168168-- field or from the filename. See 'renderDateField' for more information.
169169-- Exported for user convenience.
170−getItemUTC :: TimeLocale -- ^ Output time locale
170+getItemUTC :: MonadMetadata m
171+ => TimeLocale -- ^ Output time locale
171172 -> Identifier -- ^ Input page
172− -> Compiler UTCTime -- ^ Parsed UTCTime
173+ -> m UTCTime -- ^ Parsed UTCTime
173174getItemUTC locale id' = do
174175 metadata <- getMetadata id'
175176 let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
@@ -180,7 +181,7 @@ getItemUTC locale id' = do
180181 [tryField "date" fmt | fmt <- formats] ++
181182 [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn]
182183 where
183− empty' = compilerThrow $ "Hakyll.Web.Template.Context.getItemUTC: " ++
184+ empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++
184185 "could not parse time for " ++ show id'
185186 parseTime' = parseTime locale
186187 formats =
src/Hakyll/Web/Template/List.hs+13−5
@@ -1,3 +1,4 @@
1+{-# LANGUAGE TupleSections #-}
12--------------------------------------------------------------------------------
23-- | Provides an easy way to combine several items in a list. The applications
34-- are obvious:
@@ -16,15 +17,18 @@ module Hakyll.Web.Template.List
1617
1718
1819--------------------------------------------------------------------------------
20+import Control.Monad (liftM)
1921import Data.List (intersperse, sortBy)
2022import Data.Ord (comparing)
2123import System.FilePath (takeBaseName)
24+import System.Locale (defaultTimeLocale)
2225
2326
2427--------------------------------------------------------------------------------
2528import Hakyll.Core.Compiler
2629import Hakyll.Core.Identifier
2730import Hakyll.Core.Item
31+import Hakyll.Core.Metadata
2832import Hakyll.Web.Template
2933import Hakyll.Web.Template.Context
3034
@@ -56,11 +60,15 @@ applyJoinTemplateList delimiter tpl context items = do
5660-- | Sort pages chronologically. This function assumes that the pages have a
5761-- @year-month-day-title.extension@ naming scheme -- as is the convention in
5862-- Hakyll.
59−chronological :: [Item a] -> [Item a]
60−chronological = sortBy $ comparing $ takeBaseName . toFilePath . itemIdentifier
61−
63+chronological :: MonadMetadata m => [Item a] -> m [Item a]
64+chronological =
65+ sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
66+ where
67+ sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
68+ sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
69+ mapM (\x -> liftM (x,) (f x)) xs
6270
6371--------------------------------------------------------------------------------
6472-- | The reverse of 'chronological'
65−recentFirst :: [Item a] -> [Item a]
66−recentFirst = reverse . chronological
73+recentFirst :: (MonadMetadata m, Functor m) => [Item a] -> m [Item a]
74+recentFirst = fmap reverse . chronological