Export getItemUTC for convenience
This commit is contained in:
parent
652ceb03f1
commit
a71c9c6a12
1 changed files with 10 additions and 6 deletions
|
@ -14,6 +14,7 @@ module Hakyll.Web.Template.Context
|
||||||
, titleField
|
, titleField
|
||||||
, dateField
|
, dateField
|
||||||
, dateFieldWith
|
, dateFieldWith
|
||||||
|
, getItemUTC
|
||||||
, modificationTimeField
|
, modificationTimeField
|
||||||
, modificationTimeFieldWith
|
, modificationTimeFieldWith
|
||||||
, missingField
|
, missingField
|
||||||
|
@ -158,26 +159,29 @@ dateFieldWith :: TimeLocale -- ^ Output time locale
|
||||||
-> String -- ^ Format to use on the date
|
-> String -- ^ Format to use on the date
|
||||||
-> Context a -- ^ Resulting context
|
-> Context a -- ^ Resulting context
|
||||||
dateFieldWith locale key format = field key $ \i -> do
|
dateFieldWith locale key format = field key $ \i -> do
|
||||||
time <- getUTC locale $ itemIdentifier i
|
time <- getItemUTC locale $ itemIdentifier i
|
||||||
return $ formatTime locale format time
|
return $ formatTime locale format time
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- | Parser to try to extract and parse the time from the @published@
|
-- | Parser to try to extract and parse the time from the @published@
|
||||||
-- field or from the filename. See 'renderDateField' for more information.
|
-- field or from the filename. See 'renderDateField' for more information.
|
||||||
getUTC :: TimeLocale -- ^ Output time locale
|
-- Exported for user convenience.
|
||||||
|
getItemUTC :: TimeLocale -- ^ Output time locale
|
||||||
-> Identifier -- ^ Input page
|
-> Identifier -- ^ Input page
|
||||||
-> Compiler UTCTime -- ^ Parsed UTCTime
|
-> Compiler UTCTime -- ^ Parsed UTCTime
|
||||||
getUTC locale id' = do
|
getItemUTC locale id' = do
|
||||||
metadata <- getMetadata id'
|
metadata <- getMetadata id'
|
||||||
let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
|
let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
|
||||||
fn = takeFileName $ toFilePath id'
|
fn = takeFileName $ toFilePath id'
|
||||||
|
|
||||||
maybe empty return $ msum $
|
maybe empty' return $ msum $
|
||||||
[tryField "published" fmt | fmt <- formats] ++
|
[tryField "published" fmt | fmt <- formats] ++
|
||||||
[tryField "date" fmt | fmt <- formats] ++
|
[tryField "date" fmt | fmt <- formats] ++
|
||||||
[parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn]
|
[parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn]
|
||||||
where
|
where
|
||||||
|
empty' = compilerThrow $ "Hakyll.Web.Template.Context.getItemUTC: " ++
|
||||||
|
"could not parse time for " ++ show id'
|
||||||
parseTime' = parseTime locale
|
parseTime' = parseTime locale
|
||||||
formats =
|
formats =
|
||||||
[ "%a, %d %b %Y %H:%M:%S UT"
|
[ "%a, %d %b %Y %H:%M:%S UT"
|
||||||
|
|
Loading…
Reference in a new issue