User can supply custom time locale for renderDate

This commit is contained in:
Jasper Van der Jeugt 2010-06-22 11:53:15 +02:00
parent 739be369d9
commit 6059de5401

View file

@ -6,13 +6,14 @@ module Text.Hakyll.ContextManipulations
, changeUrl , changeUrl
, copyValue , copyValue
, renderDate , renderDate
, renderDateWithLocale
, changeExtension , changeExtension
, renderBody , renderBody
) where ) where
import Control.Monad (liftM) import Control.Monad (liftM)
import Control.Arrow (arr) import Control.Arrow (arr)
import System.Locale (defaultTimeLocale) import System.Locale (TimeLocale, defaultTimeLocale)
import System.FilePath (takeFileName, addExtension, dropExtension) import System.FilePath (takeFileName, addExtension, dropExtension)
import Data.Time.Format (parseTime, formatTime) import Data.Time.Format (parseTime, formatTime)
import Data.Time.Clock (UTCTime) import Data.Time.Clock (UTCTime)
@ -66,11 +67,24 @@ copyValue source destination = renderValue source destination id
-- > renderDate "date" "%B %e, %Y" "Date unknown" -- > renderDate "date" "%B %e, %Y" "Date unknown"
-- --
-- Will render something like @January 32, 2010@. -- Will render something like @January 32, 2010@.
--
renderDate :: String -- ^ Key in which the rendered date should be placed. renderDate :: String -- ^ Key in which the rendered date should be placed.
-> String -- ^ Format to use on the date. -> String -- ^ Format to use on the date.
-> String -- ^ Default key, in case the date cannot be parsed. -> String -- ^ Default key, in case the date cannot be parsed.
-> HakyllAction Context Context -> HakyllAction Context Context
renderDate key format defaultValue = renderValue "path" key renderDate' renderDate = renderDateWithLocale defaultTimeLocale
-- | This is an extended version of 'renderDate' that allows you to specify a
-- time locale that is used for outputting the date. For more details, see
-- 'renderDate'.
--
renderDateWithLocale :: TimeLocale -- ^ Output time locale.
-> String -- ^ Destination key.
-> String -- ^ Format to use on the date.
-> String -- ^ Default key.
-> HakyllAction Context Context
renderDateWithLocale locale key format defaultValue =
renderValue "path" key renderDate'
where where
renderDate' filePath = fromMaybe defaultValue $ do renderDate' filePath = fromMaybe defaultValue $ do
let dateString = substituteRegex "^([0-9]*-[0-9]*-[0-9]*).*" "\\1" let dateString = substituteRegex "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
@ -78,7 +92,7 @@ renderDate key format defaultValue = renderValue "path" key renderDate'
time <- parseTime defaultTimeLocale time <- parseTime defaultTimeLocale
"%Y-%m-%d" "%Y-%m-%d"
dateString :: Maybe UTCTime dateString :: Maybe UTCTime
return $ formatTime defaultTimeLocale format time return $ formatTime locale format time
-- | Change the extension of a file. This is only needed when you want to -- | Change the extension of a file. This is only needed when you want to
-- render, for example, mardown to @.php@ files instead of @.html@ files. -- render, for example, mardown to @.php@ files instead of @.html@ files.