1
0
Files
Fanfiction/convert-md.sh
2026-05-12 20:11:51 +02:00

46 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
CSS=$(cat ./styles.css)
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><a href=\"../\">← Parent Directory</a></nav>
<hr>"
HTML_END="
</body>
</html>"
cd "$(dirname "$0")"
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"
{
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