aboutsummaryrefslogtreecommitdiff
path: root/site.hs
diff options
context:
space:
mode:
Diffstat (limited to 'site.hs')
-rw-r--r--site.hs31
1 files changed, 26 insertions, 5 deletions
diff --git a/site.hs b/site.hs
index d3b7f15..7989dff 100644
--- a/site.hs
+++ b/site.hs
@@ -4,7 +4,7 @@
module Main where
import Control.Monad ((>=>), filterM, join, unless, when)
-import Control.Monad.Extra (whenM)
+import Control.Monad.Extra (ifM, whenM)
import Control.Monad.Trans.State.Lazy
import qualified Data.Aeson as AE
import qualified Data.Aeson.Key as K
@@ -27,10 +27,13 @@ import Lens.Micro
import Lens.Micro.Aeson
import Lens.Micro.Mtl
import qualified Options.Applicative
+import System.Directory (doesFileExist)
import System.FilePath
( (</>)
, isAbsolute
+ , joinPath
, splitDirectories
+ , splitFileName
, splitPath
, takeDirectory
, takeFileName
@@ -171,6 +174,24 @@ addGlobalMeta (Y.Object m) = do
r <- fromString <$> use urlBase
pure . Y.Object $ KM.insert "root" r m
+-- | Get the expected timestamp file for a given filepath
+timestampFile :: FilePath -> Site FilePath
+timestampFile fp = do
+ sfx <- use timestampSuffix
+ pure . uncurry (</>) . fmap (++ sfx) . splitFileName $ fp
+
+-- | If a timestamp file for the page exists, add the timestamp metadata.
+addTimeMeta :: PageInfo -> Y.Value -> Site Y.Value
+addTimeMeta pi m'@(Y.Object m) = do
+ tspath <- timestampFile $ pi ^. pagePath
+ io $
+ ifM
+ (doesFileExist tspath)
+ (do putStrLn $ "timestamp <- " ++ tspath
+ ts <- Y.String <$> TIO.readFile tspath
+ pure . Y.Object $ KM.insert "timestamp" ts m)
+ (pure m')
+
-- | Add page-specific information to the metadata. In this instance, this just
-- expands the tags for rendering. Eventually would be nice to have the timestamps
-- and possibly other info sourced right here.
@@ -182,7 +203,7 @@ addPageMeta pi (Y.Object m) = do
_String .
to T.unpack .
to splitDirectories
- pure . Y.Object $ KM.insert "htags" (Y.array htagMeta) m
+ addTimeMeta pi . Y.Object $ KM.insert "htags" (Y.array htagMeta) m
-- | If viable for a page (by config), add the TOC field
addTOC :: PageInfo -> Text.Pandoc.Definition.Pandoc -> Y.Value -> Site Y.Value
@@ -358,7 +379,7 @@ listFilename tag = indexFilename $ "list" </> tag
-- | Fold the hierarchical tag bits to a slashed path.
tagPath :: [String] -> FilePath
-tagPath = foldr (</>) ""
+tagPath = joinPath
-- | Make a link to the tag page
tagLink :: [String] -> Site FilePath
@@ -366,11 +387,11 @@ tagLink = rootUrl . ("tag" </>) . tagPath
-- | Fold the hierarchical tag bits to a slashed path.
listPath :: [String] -> FilePath
-listPath = foldr (</>) ""
+listPath = joinPath
-- | Make a link to the tag page
listLink :: [String] -> Site FilePath
-listLink = rootUrl . ("list" </>) . tagPath
+listLink = rootUrl . ("list" </>) . listPath
-- | Make metadata for printing out a single hierarchical tag (all levels clickable)
makeHTagMeta :: ([String] -> Site FilePath) -> [String] -> Site Y.Value