~kris/dots

srice

ref: e98f3b030dc24445bd55c68d95d2d81933fd68b3 srice/.local/bin/bookcompile -rw-r--r-- 2.1 KiB
e98f3b03 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
# bookcompile -- multi-format book build (PDF + EPUB + DOCX)
set -euo pipefail

usage() {
    echo "Usage: bookcompile <chapters-dir> [--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/"