fuzzyworld

auto-print / files / scripts / make-reference-docx.sh

make-reference-docx.sh

Blame: the commit that last changed each line · file · history

a6d0aed2 2026-10-24 Pavel Safronov1#!/bin/sh
2# Makes assets/reference.docx: pandoc's default Word template, plus a "Dedication" paragraph style.
3# Needs pandoc, unzip, zip and python3. Run from anywhere: scripts/make-reference-docx.sh
4set -eu
5
6root=$(cd "$(dirname "$0")/.." && pwd)
7work=$(mktemp -d)
8trap 'rm -rf "$work"' EXIT
9
10pandoc --print-default-data-file reference.docx > "$work/default.docx"
11mkdir "$work/files"
12(cd "$work/files" && unzip -q ../default.docx)
13
14python3 - "$work/files/word/styles.xml" <<'EOF'
15import sys
16path = sys.argv[1]
17styles = open(path, encoding="utf-8").read()
18# Centered, in italics, with 2 inches (2880 twentieths of a point) of space above.
19dedication = (
20 '<w:style w:type="paragraph" w:customStyle="1" w:styleId="Dedication"><w:name w:val="Dedication" />'
21 '<w:basedOn w:val="BodyText" /><w:qFormat />'
22 '<w:pPr><w:spacing w:before="2880" /><w:jc w:val="center" /></w:pPr><w:rPr><w:i /><w:iCs /></w:rPr></w:style>'
23)
24assert styles.count("</w:styles>") == 1 and 'w:styleId="Dedication"' not in styles
25open(path, "w", encoding="utf-8").write(styles.replace("</w:styles>", dedication + "</w:styles>"))
26EOF
27
28# [Content_Types].xml goes first, as Word expects. -D leaves out folder entries.
29rm -f "$root/assets/reference.docx"
30(cd "$work/files" && zip -X -D -q "$root/assets/reference.docx" '[Content_Types].xml' && zip -X -D -q -r "$root/assets/reference.docx" . -x '[Content_Types].xml')
31echo "Wrote $root/assets/reference.docx"