update article main page generator
This commit is contained in:
parent
70005c9228
commit
45ce87e6b1
|
@ -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
32
articles/generate_listing.nu
Executable 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 ""
|
||||
}
|
||||
}
|
|
@ -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
|
Loading…
Reference in a new issue