#!/usr/bin/env bash # bookcompile -- multi-format book build (PDF + EPUB + DOCX) set -euo pipefail usage() { echo "Usage: bookcompile [--title TITLE] [--author AUTHOR] [--cover cover.jpg]" echo "" echo "Expects chapters-dir/ containing numbered .md files (01-intro.md, 02-body.md, ...)" exit 1 } [ $# -lt 1 ] && usage dir="$1"; shift title="" author="" cover="" while [ $# -gt 0 ]; do case "$1" in --title) title="$2"; shift 2 ;; --author) author="$2"; shift 2 ;; --cover) cover="$2"; shift 2 ;; *) echo "Unknown option: $1"; usage ;; esac done chapters=$(ls "$dir"/*.md 2>/dev/null | sort -V) [ -z "$chapters" ] && { echo "No .md files in $dir"; exit 1; } mkdir -p build meta=() [ -n "$title" ] && meta+=(--metadata "title=$title") [ -n "$author" ] && meta+=(--metadata "author=$author") # Count words first total_words=$(cat $chapters | wc -w) echo "Source: $(echo "$chapters" | wc -l) files, $total_words words" echo "" # PDF (print-ready) echo "Building PDF..." pdf_args=(--pdf-engine=xelatex -V geometry:margin=1in -V fontsize:12pt -V linestretch:1.5 --toc) pandoc $chapters "${meta[@]}" "${pdf_args[@]}" -o build/manuscript.pdf 2>/dev/null && \ echo " build/manuscript.pdf ($(du -h build/manuscript.pdf | cut -f1))" || \ echo " PDF failed (is xelatex installed?)" # EPUB echo "Building EPUB..." epub_args=(-t epub3 --toc --toc-depth=2) [ -n "$cover" ] && epub_args+=(--epub-cover-image="$cover") pandoc $chapters "${meta[@]}" "${epub_args[@]}" -o build/manuscript.epub 2>/dev/null && \ echo " build/manuscript.epub ($(du -h build/manuscript.epub | cut -f1))" || \ echo " EPUB failed" # DOCX echo "Building DOCX..." pandoc $chapters "${meta[@]}" -o build/manuscript.docx 2>/dev/null && \ echo " build/manuscript.docx ($(du -h build/manuscript.docx | cut -f1))" || \ echo " DOCX failed" # HTML (standalone) echo "Building HTML..." pandoc $chapters "${meta[@]}" --standalone --toc -o build/manuscript.html 2>/dev/null && \ echo " build/manuscript.html ($(du -h build/manuscript.html | cut -f1))" || \ echo " HTML failed" echo "" echo "Done. Output in build/"