on master help sign in
Generalize getTimeUTC, chronological, recentFirst
commit45c618b413b69d119354e63dafbf96533a7ee849 authorSimonas Kazlauskas <git@kazlauskas.me> dateSat 23 Feb 2013, 13:34 (14 years ago) parent7183884
2 files changed+8−6
src/Hakyll/Web/Template/Context.hs+4−3 src/Hakyll/Web/Template/List.hs+4−3
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+4−3
@@ -28,6 +28,7 @@ import System.Locale (defaultTimeLocale)
2828import Hakyll.Core.Compiler
2929import Hakyll.Core.Identifier
3030import Hakyll.Core.Item
31+import Hakyll.Core.Metadata
3132import Hakyll.Web.Template
3233import Hakyll.Web.Template.Context
3334
@@ -59,7 +60,7 @@ applyJoinTemplateList delimiter tpl context items = do
5960-- | Sort pages chronologically. This function assumes that the pages have a
6061-- @year-month-day-title.extension@ naming scheme -- as is the convention in
6162-- Hakyll.
62−chronological :: [Item a] -> Compiler [Item a]
63+chronological :: MonadMetadata m => [Item a] -> m [Item a]
6364chronological = sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
6465 where sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
6566 sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
@@ -67,5 +68,5 @@ chronological = sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
6768
6869--------------------------------------------------------------------------------
6970-- | The reverse of 'chronological'
70−recentFirst :: [Item a] -> Compiler [Item a]
71−recentFirst i = return . reverse =<< chronological i
71+recentFirst :: (MonadMetadata m, Functor m) => [Item a] -> m [Item a]
72+recentFirst = fmap reverse . chronological