on master help sign in
Merge pull request #112 from simukis/sorting
14fc440Jasper Van der Jeugt
src/Hakyll/Web/Template/Context.hs+4−3
@@ -167,9 +167,10 @@ dateFieldWith locale key format = field key $ \i -> do
167-- | Parser to try to extract and parse the time from the @published@
167-- | Parser to try to extract and parse the time from the @published@
168-- field or from the filename. See 'renderDateField' for more information.
168-- field or from the filename. See 'renderDateField' for more information.
169-- Exported for user convenience.
169-- Exported for user convenience.
170+getItemUTC :: MonadMetadata m
170−getItemUTC :: TimeLocale -- ^ Output time locale
171+ => TimeLocale -- ^ Output time locale
171 -> Identifier -- ^ Input page
172 -> Identifier -- ^ Input page
172− -> Compiler UTCTime -- ^ Parsed UTCTime
173+ -> m UTCTime -- ^ Parsed UTCTime
173getItemUTC locale id' = do
174getItemUTC locale id' = do
174 metadata <- getMetadata id'
175 metadata <- getMetadata id'
175 let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
176 let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
@@ -180,7 +181,7 @@ getItemUTC locale id' = do
180 [tryField "date" fmt | fmt <- formats] ++
181 [tryField "date" fmt | fmt <- formats] ++
181 [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn]
182 [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn]
182 where
183 where
183− empty' = compilerThrow $ "Hakyll.Web.Template.Context.getItemUTC: " ++
184+ empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++
184 "could not parse time for " ++ show id'
185 "could not parse time for " ++ show id'
185 parseTime' = parseTime locale
186 parseTime' = parseTime locale
186 formats =
187 formats =
src/Hakyll/Web/Template/List.hs+13−5
@@ -1,3 +1,4 @@
1+{-# LANGUAGE TupleSections #-}
1--------------------------------------------------------------------------------
2--------------------------------------------------------------------------------
2-- | Provides an easy way to combine several items in a list. The applications
3-- | Provides an easy way to combine several items in a list. The applications
3-- are obvious:
4-- are obvious:
@@ -16,15 +17,18 @@ module Hakyll.Web.Template.List
16
17
17
18
18--------------------------------------------------------------------------------
19--------------------------------------------------------------------------------
20+import Control.Monad (liftM)
19import Data.List (intersperse, sortBy)
21import Data.List (intersperse, sortBy)
20import Data.Ord (comparing)
22import Data.Ord (comparing)
21import System.FilePath (takeBaseName)
23import System.FilePath (takeBaseName)
24+import System.Locale (defaultTimeLocale)
22
25
23
26
24--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
25import Hakyll.Core.Compiler
28import Hakyll.Core.Compiler
26import Hakyll.Core.Identifier
29import Hakyll.Core.Identifier
27import Hakyll.Core.Item
30import Hakyll.Core.Item
31+import Hakyll.Core.Metadata
28import Hakyll.Web.Template
32import Hakyll.Web.Template
29import Hakyll.Web.Template.Context
33import Hakyll.Web.Template.Context
30
34
@@ -56,11 +60,15 @@ applyJoinTemplateList delimiter tpl context items = do
56-- | Sort pages chronologically. This function assumes that the pages have a
60-- | Sort pages chronologically. This function assumes that the pages have a
57-- @year-month-day-title.extension@ naming scheme -- as is the convention in
61-- @year-month-day-title.extension@ naming scheme -- as is the convention in
58-- Hakyll.
62-- Hakyll.
59−chronological :: [Item a] -> [Item a]
63+chronological :: MonadMetadata m => [Item a] -> m [Item a]
60−chronological = sortBy $ comparing $ takeBaseName . toFilePath . itemIdentifier
64+chronological =
61−
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
62
70
63--------------------------------------------------------------------------------
71--------------------------------------------------------------------------------
64-- | The reverse of 'chronological'
72-- | The reverse of 'chronological'
65−recentFirst :: [Item a] -> [Item a]
73+recentFirst :: (MonadMetadata m, Functor m) => [Item a] -> m [Item a]
66−recentFirst = reverse . chronological
74+recentFirst = fmap reverse . chronological