MakerFLOSS/build-slides.sh

126 lines
4.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_DIR="${OUTPUT_DIR:-$REPO_ROOT/slides}"
SLIDES_TITLE="${SLIDES_TITLE:-MakerFLOSS Slides}"
REPO_WEB_URL="${REPO_WEB_URL:-https://forgejo.makerfloss.eu/sjat/MakerFLOSS}"
mkdir -p "$OUTPUT_DIR"
# Wipe previously built slides so deleted/de-tagged .md files disappear.
find "$OUTPUT_DIR" -maxdepth 1 -name "*.html" -delete
# Find all markdown files with marp: true frontmatter
SLIDES=()
while IFS= read -r f; do
SLIDES+=("$f")
done < <(grep -rl "^marp: true" "$REPO_ROOT/docs" --include="*.md" --exclude-dir=superpowers 2>/dev/null || true)
if [ ${#SLIDES[@]} -eq 0 ]; then
echo "No marp presentations found in docs/."
exit 0
fi
echo "Found ${#SLIDES[@]} presentation(s):"
printf ' %s\n' "${SLIDES[@]}"
# Create temp output directory for Docker fallback
TEMP_OUTPUT=$(mktemp -d)
chmod 777 "$TEMP_OUTPUT"
trap "rm -rf '$TEMP_OUTPUT'" EXIT
2026-05-10 12:21:09 +02:00
if command -v marp &>/dev/null; then
for slide in "${SLIDES[@]}"; do
BASENAME=$(basename "${slide%.*}")
marp --html --output "$OUTPUT_DIR/${BASENAME}.html" "$slide"
done
else
echo "marp not found locally — using Docker (marpteam/marp-cli)..."
for slide in "${SLIDES[@]}"; do
REL_SLIDE="${slide#${REPO_ROOT}/}"
BASENAME=$(basename "${slide%.*}")
docker run --rm \
-v "$REPO_ROOT":/home/marp/app:ro \
-v "$TEMP_OUTPUT":/home/marp/output \
marpteam/marp-cli --html --output "/home/marp/output/${BASENAME}.html" "$REL_SLIDE"
done
cp "$TEMP_OUTPUT"/*.html "$OUTPUT_DIR/" 2>/dev/null || true
fi
# Inject mermaid.js into any HTML that contains mermaid code blocks.
# Marp emits fenced mermaid blocks as <pre><code class="language-mermaid">.
# The script finds those elements, replaces them with <div class="mermaid">,
# then loads and runs mermaid.js from CDN.
inject_mermaid() {
local html_file="$1"
python3 - "$html_file" << 'PYEOF'
import sys
path = sys.argv[1]
snippet = """\
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
document.querySelectorAll('code.language-mermaid').forEach(el => {
const div = document.createElement('div');
div.className = 'mermaid';
div.textContent = el.textContent;
el.parentElement.replaceWith(div);
});
mermaid.initialize({ startOnLoad: false, theme: 'dark' });
await mermaid.run();
</script>"""
with open(path, encoding='utf-8') as f:
content = f.read()
new_content = content.replace('</body>', snippet + '\n</body>', 1)
if new_content == content:
print(f"Warning: </body> not found in {path}", file=sys.stderr)
sys.exit(1)
with open(path, 'w', encoding='utf-8') as f:
f.write(new_content)
PYEOF
}
if command -v python3 &>/dev/null; then
for html_file in "$OUTPUT_DIR"/*.html; do
[ -f "$html_file" ] || continue
if grep -q 'class="language-mermaid"' "$html_file"; then
inject_mermaid "$html_file"
echo " Injected mermaid.js into $(basename "$html_file")"
fi
done
else
echo "Warning: python3 not found — skipping mermaid injection"
fi
# Regenerate index.html listing every built deck.
INDEX="$OUTPUT_DIR/index.html"
{
printf '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">'
printf '<meta name="viewport" content="width=device-width,initial-scale=1">'
printf '<title>%s</title>' "$SLIDES_TITLE"
printf '<style>'
printf 'body{font-family:sans-serif;max-width:700px;margin:3rem auto;padding:0 1.5rem;background:#fafafa;}'
printf 'h1{font-size:1.6rem;margin-bottom:.25rem;}p.sub{color:#666;margin-top:0;}'
printf 'ul{list-style:none;padding:0;margin-top:2rem;}'
printf 'li{border:1px solid #e0e0e0;border-radius:6px;margin:.75rem 0;background:#fff;}'
printf 'li a{display:block;padding:1rem 1.25rem;text-decoration:none;color:#1a1a1a;font-size:1.05rem;}'
printf 'li a:hover{background:#f0f4ff;border-radius:6px;}'
printf 'footer{margin-top:3rem;color:#aaa;font-size:.8rem;}'
printf '</style></head><body>'
printf '<h1>%s</h1>' "$SLIDES_TITLE"
printf '<p class="sub">Built from <a href="%s">%s</a></p>' \
"$REPO_WEB_URL" "${REPO_WEB_URL#https://}"
printf '<ul>'
find "$OUTPUT_DIR" -maxdepth 1 -name "*.html" ! -name "index.html" | sort | while IFS= read -r html; do
name=$(basename "$html" .html)
printf '<li><a href="%s">%s</a></li>' "$(basename "$html")" "$name"
done
printf '</ul>'
printf '<footer>Last built: %s &middot; <a href="https://marp.app">Marp</a></footer>' "$(date -Iseconds)"
printf '</body></html>'
} > "$INDEX"
echo "Done — slides in $OUTPUT_DIR/"