From 718388495b41089eddcac9ae55aae4ca68620505 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 23 Feb 2013 12:47:01 +0200 Subject: chronological, recentFirst now respects metadata Solves #111 --- src/Hakyll/Web/Template/List.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Hakyll/Web/Template/List.hs b/src/Hakyll/Web/Template/List.hs index 6d2a341..5f94369 100644 --- a/src/Hakyll/Web/Template/List.hs +++ b/src/Hakyll/Web/Template/List.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE TupleSections #-} -------------------------------------------------------------------------------- -- | Provides an easy way to combine several items in a list. The applications -- are obvious: @@ -16,9 +17,11 @@ module Hakyll.Web.Template.List -------------------------------------------------------------------------------- +import Control.Monad (liftM) import Data.List (intersperse, sortBy) import Data.Ord (comparing) import System.FilePath (takeBaseName) +import System.Locale (defaultTimeLocale) -------------------------------------------------------------------------------- @@ -56,11 +59,13 @@ applyJoinTemplateList delimiter tpl context items = do -- | Sort pages chronologically. This function assumes that the pages have a -- @year-month-day-title.extension@ naming scheme -- as is the convention in -- Hakyll. -chronological :: [Item a] -> [Item a] -chronological = sortBy $ comparing $ takeBaseName . toFilePath . itemIdentifier - +chronological :: [Item a] -> Compiler [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 -------------------------------------------------------------------------------- -- | The reverse of 'chronological' -recentFirst :: [Item a] -> [Item a] -recentFirst = reverse . chronological +recentFirst :: [Item a] -> Compiler [Item a] +recentFirst i = return . reverse =<< chronological i -- cgit v1.2.3 From 45c618b413b69d119354e63dafbf96533a7ee849 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 23 Feb 2013 13:34:54 +0200 Subject: Generalize getTimeUTC, chronological, recentFirst --- src/Hakyll/Web/Template/Context.hs | 7 ++++--- src/Hakyll/Web/Template/List.hs | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs index 8aab989..1a9aba3 100644 --- a/src/Hakyll/Web/Template/Context.hs +++ b/src/Hakyll/Web/Template/Context.hs @@ -167,9 +167,10 @@ dateFieldWith locale key format = field key $ \i -> do -- | Parser to try to extract and parse the time from the @published@ -- field or from the filename. See 'renderDateField' for more information. -- Exported for user convenience. -getItemUTC :: TimeLocale -- ^ Output time locale +getItemUTC :: MonadMetadata m + => TimeLocale -- ^ Output time locale -> Identifier -- ^ Input page - -> Compiler UTCTime -- ^ Parsed UTCTime + -> m UTCTime -- ^ Parsed UTCTime getItemUTC locale id' = do metadata <- getMetadata id' let tryField k fmt = M.lookup k metadata >>= parseTime' fmt @@ -180,7 +181,7 @@ getItemUTC locale id' = do [tryField "date" fmt | fmt <- formats] ++ [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn] where - empty' = compilerThrow $ "Hakyll.Web.Template.Context.getItemUTC: " ++ + empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++ "could not parse time for " ++ show id' parseTime' = parseTime locale formats = diff --git a/src/Hakyll/Web/Template/List.hs b/src/Hakyll/Web/Template/List.hs index 5f94369..1401119 100644 --- a/src/Hakyll/Web/Template/List.hs +++ b/src/Hakyll/Web/Template/List.hs @@ -28,6 +28,7 @@ import System.Locale (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 @@ -59,7 +60,7 @@ applyJoinTemplateList delimiter tpl context items = do -- | Sort pages chronologically. This function assumes that the pages have a -- @year-month-day-title.extension@ naming scheme -- as is the convention in -- Hakyll. -chronological :: [Item a] -> Compiler [Item a] +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)) $ @@ -67,5 +68,5 @@ chronological = sortByM $ getItemUTC defaultTimeLocale . itemIdentifier -------------------------------------------------------------------------------- -- | The reverse of 'chronological' -recentFirst :: [Item a] -> Compiler [Item a] -recentFirst i = return . reverse =<< chronological i +recentFirst :: (MonadMetadata m, Functor m) => [Item a] -> m [Item a] +recentFirst = fmap reverse . chronological -- cgit v1.2.3 From 6b680c98873b91392e8e89d9ec4f008050f95f19 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 23 Feb 2013 13:38:34 +0200 Subject: Fix code style --- src/Hakyll/Web/Template/List.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Hakyll/Web/Template/List.hs b/src/Hakyll/Web/Template/List.hs index 1401119..9571b9e 100644 --- a/src/Hakyll/Web/Template/List.hs +++ b/src/Hakyll/Web/Template/List.hs @@ -61,10 +61,12 @@ applyJoinTemplateList delimiter tpl context items = do -- @year-month-day-title.extension@ naming scheme -- as is the convention in -- Hakyll. 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 +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 -------------------------------------------------------------------------------- -- | The reverse of 'chronological' -- cgit v1.2.3