on master help sign in
Generalize getTimeUTC, chronological, recentFirst
45c618bSimonas Kazlauskas
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+4−3
@@ -28,6 +28,7 @@ import System.Locale (defaultTimeLocale)
28import Hakyll.Core.Compiler
28import Hakyll.Core.Compiler
29import Hakyll.Core.Identifier
29import Hakyll.Core.Identifier
30import Hakyll.Core.Item
30import Hakyll.Core.Item
31+import Hakyll.Core.Metadata
31import Hakyll.Web.Template
32import Hakyll.Web.Template
32import Hakyll.Web.Template.Context
33import Hakyll.Web.Template.Context
33
34
@@ -59,7 +60,7 @@ applyJoinTemplateList delimiter tpl context items = do
59-- | Sort pages chronologically. This function assumes that the pages have a
60-- | Sort pages chronologically. This function assumes that the pages have a
60-- @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
61-- Hakyll.
62-- Hakyll.
62−chronological :: [Item a] -> Compiler [Item a]
63+chronological :: MonadMetadata m => [Item a] -> m [Item a]
63chronological = sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
64chronological = sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
64 where sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
65 where sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
65 sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
66 sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
@@ -67,5 +68,5 @@ chronological = sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
67
68
68--------------------------------------------------------------------------------
69--------------------------------------------------------------------------------
69-- | The reverse of 'chronological'
70-- | The reverse of 'chronological'
70−recentFirst :: [Item a] -> Compiler [Item a]
71+recentFirst :: (MonadMetadata m, Functor m) => [Item a] -> m [Item a]
71−recentFirst i = return . reverse =<< chronological i
72+recentFirst = fmap reverse . chronological