1
0
Files
Fanfiction/convert-md.sh
2026-05-15 21:31:45 +02:00

68 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
CSS=$(cat ./styles.css)
HTML_END="
</body>
</html>"
cd "$(dirname "$0")"
ROOT_DIR="$(pwd)"
find . -type f -name "*.md" -print0 | while IFS= read -r -d '' mdfile; do
dir=$(dirname "$mdfile")
base=$(basename "$mdfile" .md)
if [[ "$base" == "README" ]]; then
out_base="index"
else
out_base="$base"
fi
html_raw="$dir/$out_base.html.raw"
html_final="$dir/$out_base.html"
pandoc "$mdfile" -t html -o "$html_raw"
abs_dir="$(cd "$dir" && pwd)"
rel_depth="${abs_dir#$ROOT_DIR}"
rel_depth="${rel_depth#/}"
nav=""
if [[ -z "$rel_depth" ]]; then
if [[ "$out_base" != "index" ]]; then
nav='<nav><a href="./index.html"><- Index</a></nav>'
fi
else
if [[ "$out_base" == "index" ]]; then
nav='<nav><a href="../index.html"><- Home</a></nav>'
else
nav='<nav><a href="./index.html"><- Index</a> | <a href="../index.html"><- Home</a></nav>'
fi
fi
if [[ -n "$nav" ]]; then
nav="$nav<hr>"
fi
HTML_BEGIN="<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<style>$CSS</style>
</head>
<body>
$nav"
{
echo "$HTML_BEGIN"
sed -E 's/href="(\.\/[^.]+)"/href="\1\/"/g' "$html_raw" | \
python3 -c "import sys, re; print(re.sub(r'href=\"([^\"]+)\"', lambda m: 'href=\"' + m.group(1).replace('?', '%3F').replace('!', '%21') + '\"', sys.stdin.read()), end='')"
echo "$HTML_END"
} > "$html_final"
echo "Converted: $mdfile"
done