diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2014-10-23 00:10:13 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2014-10-23 00:10:13 +0200 |
commit | 8ef1d6e58f88d15d70986ce8f289fea4ddb4570e (patch) | |
tree | 9263e560a8fc01ff3b512f53f5a298d0193ba3c0 /src/Hakyll/Web/Template | |
parent | edfd1b6a491a5ada06e46896e67c4d061c22c409 (diff) | |
parent | 51b147bbe1d7d0a6f5b52feb3a9a589482f1751f (diff) | |
download | hakyll-8ef1d6e58f88d15d70986ce8f289fea4ddb4570e.tar.gz |
Merge pull request #304 from nushio3/master
Parse article date from directory names in addition to file name
Diffstat (limited to 'src/Hakyll/Web/Template')
-rw-r--r-- | src/Hakyll/Web/Template/Context.hs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs index a741272..a606a69 100644 --- a/src/Hakyll/Web/Template/Context.hs +++ b/src/Hakyll/Web/Template/Context.hs @@ -33,7 +33,7 @@ import qualified Data.Map as M import Data.Monoid (Monoid (..)) import Data.Time.Clock (UTCTime (..)) import Data.Time.Format (formatTime, parseTime) -import System.FilePath (takeBaseName, takeFileName) +import System.FilePath (takeBaseName, splitDirectories) import System.Locale (TimeLocale, defaultTimeLocale) @@ -218,7 +218,11 @@ titleField = mapContext takeBaseName . pathField -- Alternatively, when the metadata has a field called @path@ in a -- @folder/yyyy-mm-dd-title.extension@ format (the convention for pages) -- and no @published@ metadata field set, this function can render --- the date. +-- the date. This pattern matches the file name or directory names +-- that begins with @yyyy-mm-dd@ . For example: +-- @folder//yyyy-mm-dd-title//dist//main.extension@ . +-- In case of multiple matches, the rightmost one is used. + dateField :: String -- ^ Key in which the rendered date should be placed -> String -- ^ Format to use on the date -> Context a -- ^ Resulting context @@ -249,12 +253,12 @@ getItemUTC :: MonadMetadata m getItemUTC locale id' = do metadata <- getMetadata id' let tryField k fmt = M.lookup k metadata >>= parseTime' fmt - fn = takeFileName $ toFilePath id' + paths = splitDirectories $ toFilePath id' maybe empty' return $ msum $ [tryField "published" 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 "-" fnCand | fnCand <- reverse paths] where empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++ "could not parse time for " ++ show id' |