From c86ece1433da3e8a85d84233f236a7d3b7945534 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 22 Jun 2023 11:43:20 +0200 Subject: [PATCH 1/5] implement sourcing of timestamp files --- Types.hs | 5 +++++ reploy.cabal | 3 ++- site.hs | 31 ++++++++++++++++++++++++++----- templates/header.html | 30 +----------------------------- templates/sidebox.html | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 templates/sidebox.html diff --git a/Types.hs b/Types.hs index 58a8197..455a4f4 100644 --- a/Types.hs +++ b/Types.hs @@ -58,6 +58,7 @@ data SiteState = , _redirectTemplate :: FilePath -- ^ Name of the template for redirect pages , _tagTemplate :: FilePath -- ^ Name of the template for category pages , _listTemplate :: FilePath -- ^ Name of the template for listing pages + , _timestampSuffix :: FilePath -- ^ File to search for a timestamp (e.g., if the prefix is ".ts", a timestamp for file "page.md" will be looked for in "page.md.ts"). These are best autogenerated with a script that sources the data from git or so. , _urlBase :: FilePath -- ^ "Root route" to prepend to all absolute links. , _dumpFinalState :: Bool -- ^ Triggers printing out the structure when the processing finishes. } @@ -128,6 +129,10 @@ siteOptions' = do strOption $ long "url-base" <> short 'u' <> help "Base absolute URL" <> value "/" <> showDefault + _timestampSuffix <- + strOption $ + long "timestamp-prefix" <> + help "Timestamp file suffix for markdowns" <> value ".timestamp" <> showDefault _dumpFinalState <- switch $ long "dump-state" <> diff --git a/reploy.cabal b/reploy.cabal index bb1887a..9a86fa5 100644 --- a/reploy.cabal +++ b/reploy.cabal @@ -11,6 +11,7 @@ executable site , bytestring , containers , data-default + , directory , extra , filepath , hakyll == 4.16.* @@ -23,8 +24,8 @@ executable site , pandoc , pandoc-types , parsec - , SHA , scientific + , SHA , text , transformers , yaml 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 diff --git a/templates/header.html b/templates/header.html index 245aff0..9b6a0ea 100644 --- a/templates/header.html +++ b/templates/header.html @@ -22,33 +22,5 @@
- + {{> sidebox.html}}
diff --git a/templates/sidebox.html b/templates/sidebox.html new file mode 100644 index 0000000..8c27a29 --- /dev/null +++ b/templates/sidebox.html @@ -0,0 +1,37 @@ + From 65944a1cdfe3c06df737e5c54aec43292d0180bf Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 22 Jun 2023 11:17:44 +0200 Subject: [PATCH 2/5] make a separate scripts directory (this gonna grow) --- make-search-index.js => scripts/make-search-index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename make-search-index.js => scripts/make-search-index.js (90%) diff --git a/make-search-index.js b/scripts/make-search-index.js similarity index 90% rename from make-search-index.js rename to scripts/make-search-index.js index 8d9f4cb..37c5ce5 100644 --- a/make-search-index.js +++ b/scripts/make-search-index.js @@ -11,7 +11,7 @@ * * Usage: * site ....some args.... --search-data-output tmp/search-raw.json - * node make-search-index.js tmp/search-raw.json _site/search-index.json _site/search-metadata.json + * node scripts/make-search-index.js tmp/search-raw.json _site/search-index.json _site/search-metadata.json */ lunr = require("lunr") From 91900a323ca89592c200e55dc8afc65c8a9abe3a Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 22 Jun 2023 11:29:39 +0200 Subject: [PATCH 3/5] add a script for sourcing the timestamps from gits Fixes #2 --- scripts/source-timestamps.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 scripts/source-timestamps.sh diff --git a/scripts/source-timestamps.sh b/scripts/source-timestamps.sh new file mode 100755 index 0000000..74ff9f0 --- /dev/null +++ b/scripts/source-timestamps.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +# change this with environment +DEFAULT_NOT_SOURCE_REGEX="/\\/assets\\//" +NOT_SOURCE_REGEX="${NOT_SOURCE_REGEX:-$DEFAULT_NOT_SOURCE_REGEX}" +TIMESTAMP_SUFFIX="${SUFFIX:-.timestamp}" +LOGFILE="${LOGFILE:-/dev/null}" + +[ -z "$1" ] && echo "$0: no inputs specified?" >/dev/stderr + +while [ -n "$1" ] +do + echo "sourcing directory '$1' ..." >> "$LOGFILE" + find "$1" -type f -name '*.md' | grep -v "$NOT_SOURCE_REGEX" | while read file ; do + fn=`basename "$file"` + dir=`dirname "$file"` + tsfn="$fn.timestamp" + ( + echo "making timestamp in '$dir' for file '$fn' ..." >> "$LOGFILE" + cd "$dir" + if [ -f "$tsfn" ] + then echo "... but it already exists; skipping!" >> "$LOGFILE" + else git log -n 1 --pretty=format:%cs -- "$fn" > "$tsfn" + fi + ) + done + shift +done From fa7d1fab5550822b1f4cad753f122515a818a600 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 22 Jun 2023 11:34:00 +0200 Subject: [PATCH 4/5] avoid overwriting timestamps if they are forced by markdown already --- Types.hs | 3 ++- cards/404.md | 2 ++ cards/index.md | 1 + cards/search.md | 1 + site.hs | 20 +++++++++++--------- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Types.hs b/Types.hs index 455a4f4..7f09b05 100644 --- a/Types.hs +++ b/Types.hs @@ -132,7 +132,8 @@ siteOptions' = do _timestampSuffix <- strOption $ long "timestamp-prefix" <> - help "Timestamp file suffix for markdowns" <> value ".timestamp" <> showDefault + help "Timestamp file suffix for markdowns" <> + value ".timestamp" <> showDefault _dumpFinalState <- switch $ long "dump-state" <> diff --git a/cards/404.md b/cards/404.md index 60148b7..a0e09ea 100644 --- a/cards/404.md +++ b/cards/404.md @@ -2,6 +2,8 @@ mount: /404 title: Page not found search: off +toc: off +timestamp: null --- # Page not found! diff --git a/cards/index.md b/cards/index.md index b493711..fb19e6e 100644 --- a/cards/index.md +++ b/cards/index.md @@ -3,6 +3,7 @@ mount: / title: Home template: index.html toc: off +timestamp: null --- #### What are How-To Cards? diff --git a/cards/search.md b/cards/search.md index cefd3eb..42713f0 100644 --- a/cards/search.md +++ b/cards/search.md @@ -4,6 +4,7 @@ title: Search template: search.html search: off toc: off +timestamp: null --- # Search diff --git a/site.hs b/site.hs index 7989dff..d60486e 100644 --- a/site.hs +++ b/site.hs @@ -182,15 +182,17 @@ timestampFile fp = do -- | 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') +addTimeMeta pi m'@(Y.Object m) + | "timestamp" `KM.member` m = pure m' -- do not overwrite the timestamp if present + | otherwise = 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 From a2dd9384b4e0eb67346848bdb90a35b039640669 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 22 Jun 2023 11:40:08 +0200 Subject: [PATCH 5/5] tag the docker images with short sha to allow depending on versions cc @ronan.tremoreux --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 56fe225..4c3daad 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,6 +18,8 @@ build: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $CI_REGISTRY_IMAGE:latest . - docker push $CI_REGISTRY_IMAGE:latest + - docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA + - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA rules: - if: '$CI_COMMIT_REF_NAME == "master"' tags: