on master help sign in
Add option to specify date in directory structure
commit41e7b8cf18ae29a57b518437c706a7c7c377fc15 authorTaeer Bar-Yam <Radvendii@users.noreply.github.com> dateSat 25 May 2019, 02:28 (7 years ago) parent29c453d
3 files changed+15−2
lib/Hakyll/Web/Template/Context.hs+9−2 tests/Hakyll/Web/Template/Context/Tests.hs+5 tests/data/posts/2019/05/10/tomorrow.md+1
lib/Hakyll/Web/Template/Context.hs+9−2
@@ -34,7 +34,7 @@ module Hakyll.Web.Template.Context
3434--------------------------------------------------------------------------------
3535import Control.Applicative (Alternative (..))
3636import Control.Monad (msum)
37−import Data.List (intercalate)
37+import Data.List (intercalate, tails)
3838#if MIN_VERSION_base(4,9,0)
3939import Data.Semigroup (Semigroup (..))
4040#endif
@@ -269,6 +269,12 @@ titleField = mapContext takeBaseName . pathField
269269-- that begins with @yyyy-mm-dd@ . For example:
270270-- @folder//yyyy-mm-dd-title//dist//main.extension@ .
271271-- In case of multiple matches, the rightmost one is used.
272+--
273+-- As another alternative, if none of the above matches, and the file has a
274+-- path which contains nested directories specifying a date, then that date
275+-- will be used. In other words, if the path is of the form
276+-- @**//yyyy//mm//dd//**//main.extension@ .
277+-- As above, in case of multiple matches, the rightmost one is used.
272278
273279dateField :: String -- ^ Key in which the rendered date should be placed
274280 -> String -- ^ Format to use on the date
@@ -305,7 +311,8 @@ getItemUTC locale id' = do
305311 maybe empty' return $ msum $
306312 [tryField "published" fmt | fmt <- formats] ++
307313 [tryField "date" fmt | fmt <- formats] ++
308− [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fnCand | fnCand <- reverse paths]
314+ [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fnCand | fnCand <- reverse paths] ++
315+ [parseTime' "%Y-%m-%d" $ intercalate "-" $ fnCand | fnCand <- map (take 3) $ reverse . tails $ paths]
309316 where
310317 empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++
311318 "could not parse time for " ++ show id'
tests/Hakyll/Web/Template/Context/Tests.hs+5
@@ -45,6 +45,11 @@ testDateField = do
4545 "posts/2018-09-26.md" "date" $
4646 dateField "date" "%B %e, %Y"
4747 date3 @=? "September 26, 2018"
48+
49+ date4 <- testContextDone store provider
50+ "posts/2019/05/10/tomorrow.md" "date" $
51+ dateField "date" "%B %e, %Y"
52+ date4 @=? "May 10, 2019"
4853 cleanTestEnv
4954
5055
tests/data/posts/2019/05/10/tomorrow.mdnew file+1
@@ -0,0 +1 @@
1+This day hasn't happened yet (as of writing this).