update article main page generator

This commit is contained in:
Fabien Freling 2024-11-26 14:38:41 +01:00
parent 70005c9228
commit 45ce87e6b1
3 changed files with 33 additions and 24 deletions

View file

@ -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 |>

32
articles/generate_listing.nu Executable file
View file

@ -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 ""
}
}

View file

@ -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