From 45ce87e6b1b70d68b56b25e5a723d6650a924444 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Tue, 26 Nov 2024 14:38:41 +0100 Subject: [PATCH] update article main page generator --- articles/Tupfile | 2 +- articles/generate_listing.nu | 32 ++++++++++++++++++++++++++++++++ articles/generate_listing.sh | 23 ----------------------- 3 files changed, 33 insertions(+), 24 deletions(-) create mode 100755 articles/generate_listing.nu delete mode 100755 articles/generate_listing.sh diff --git a/articles/Tupfile b/articles/Tupfile index c8d9bfc..924243a 100644 --- a/articles/Tupfile +++ b/articles/Tupfile @@ -1,5 +1,5 @@ include_rules HTML_TEMPLATE = article.html -: *.md |> ./generate_listing.sh > %o |> index.md +: *.md |> ./generate_listing.nu > %o |> index.md : foreach *.md |> !html |> diff --git a/articles/generate_listing.nu b/articles/generate_listing.nu new file mode 100755 index 0000000..8df24a7 --- /dev/null +++ b/articles/generate_listing.nu @@ -0,0 +1,32 @@ +#!/usr/bin/env nu + +def get_metadata [path: string] { + let lines = open $path | lines + let anchors = $lines | enumerate | filter {|l| ($l.item | str starts-with "---")} | take 2 + let header = $lines + | range (($anchors | first | get 'index') + 1)..(($anchors | last | get 'index') - 1) + let metadata = $header | split column -n 2 --regex '\s*:\s*' | rename key value + let record = $metadata | reduce -f {} {|it, acc| $acc | upsert $it.key ($it.value | str trim --char '"') } + $record | +} + +let pages = (glob *.md) ++ (glob **/index.md) +let sorted_pages = $pages | wrap 'path' + | upsert metadata {|row| (get_metadata $row.path)} + | sort-by --reverse metadata.date + +print "--- +title: Articles +--- +" + +let _ = $sorted_pages | each {|p| + let rel_path = $p.path | path relative-to (pwd) + let html_path = $rel_path | path parse --extension md | upsert extension { 'html' } | path join + print --no-newline $"- ($p.metadata.date): [($p.metadata.title)]\(($html_path)\)" + if $p.metadata.update? != null { + print $" \(Updated: ($p.metadata.update)\)" + } else { + print "" + } +} diff --git a/articles/generate_listing.sh b/articles/generate_listing.sh deleted file mode 100755 index cf62c2d..0000000 --- a/articles/generate_listing.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - -cat << EOF ---- -title: Articles ---- - -EOF - -listing="" -for file in *.md; do - if [ $file = "index.md" ]; then - continue - fi - - link=$(basename $file .md).html - date=$(sed -n 's/date: \(.*\)/\1/p' $file) - title=$(sed -n 's/title: \(.*\)/\1/p' $file) - listing="$listing- $date: [$title]($link)\n" -done -echo -e $listing | sort --reverse