diff options
-rw-r--r-- | lib/Hakyll/Web/Template/Context.hs | 11 | ||||
-rw-r--r-- | tests/Hakyll/Web/Template/Context/Tests.hs | 5 | ||||
-rw-r--r-- | tests/data/posts/2019/05/10/tomorrow.md | 1 |
3 files changed, 15 insertions, 2 deletions
diff --git a/lib/Hakyll/Web/Template/Context.hs b/lib/Hakyll/Web/Template/Context.hs index 0d71386..8038253 100644 --- a/lib/Hakyll/Web/Template/Context.hs +++ b/lib/Hakyll/Web/Template/Context.hs @@ -34,7 +34,7 @@ module Hakyll.Web.Template.Context -------------------------------------------------------------------------------- import Control.Applicative (Alternative (..)) import Control.Monad (msum) -import Data.List (intercalate) +import Data.List (intercalate, tails) #if MIN_VERSION_base(4,9,0) import Data.Semigroup (Semigroup (..)) #endif @@ -269,6 +269,12 @@ titleField = mapContext takeBaseName . pathField -- 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. +-- +-- As another alternative, if none of the above matches, and the file has a +-- path which contains nested directories specifying a date, then that date +-- will be used. In other words, if the path is of the form +-- @**//yyyy//mm//dd//**//main.extension@ . +-- As above, 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 @@ -305,7 +311,8 @@ getItemUTC locale id' = do maybe empty' return $ msum $ [tryField "published" fmt | fmt <- formats] ++ [tryField "date" fmt | fmt <- formats] ++ - [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fnCand | fnCand <- reverse paths] + [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fnCand | fnCand <- reverse paths] ++ + [parseTime' "%Y-%m-%d" $ intercalate "-" $ fnCand | fnCand <- map (take 3) $ reverse . tails $ paths] where empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++ "could not parse time for " ++ show id' diff --git a/tests/Hakyll/Web/Template/Context/Tests.hs b/tests/Hakyll/Web/Template/Context/Tests.hs index 202b745..3adedd8 100644 --- a/tests/Hakyll/Web/Template/Context/Tests.hs +++ b/tests/Hakyll/Web/Template/Context/Tests.hs @@ -45,6 +45,11 @@ testDateField = do "posts/2018-09-26.md" "date" $ dateField "date" "%B %e, %Y" date3 @=? "September 26, 2018" + + date4 <- testContextDone store provider + "posts/2019/05/10/tomorrow.md" "date" $ + dateField "date" "%B %e, %Y" + date4 @=? "May 10, 2019" cleanTestEnv diff --git a/tests/data/posts/2019/05/10/tomorrow.md b/tests/data/posts/2019/05/10/tomorrow.md new file mode 100644 index 0000000..b3edce5 --- /dev/null +++ b/tests/data/posts/2019/05/10/tomorrow.md @@ -0,0 +1 @@ +This day hasn't happened yet (as of writing this). |