#!/bin/bash CSS=$(cat ./styles.css) HTML_END=" " 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='' fi else if [[ "$out_base" == "index" ]]; then nav='' else nav='' fi fi if [[ -n "$nav" ]]; then nav="$nav
" fi HTML_BEGIN=" $nav" { echo "$HTML_BEGIN" sed -E 's/href="(\.\/[^.]+)"/href="\1\/"/g' "$html_raw" | \ sed -E 's/href="([^"]*)\.md"/href="\1.html"/g' | \ python3 -c " import sys, re text = sys.stdin.read() text = re.sub(r'

(.*?)

', r'

\1

', text, flags=re.DOTALL) text = re.sub(r'href=\"([^\"]+)\"', lambda m: 'href=\"' + m.group(1).replace('?', '%3F').replace('!', '%21') + '\"', text) print(text, end='') " echo "$HTML_END" } > "$html_final" echo "Converted: $mdfile" done