auto-print / files / scripts / make-reference-docx.sh
make-reference-docx.sh
| 1 | #!/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 |
| 4 | set -eu |
| 5 | |
| 6 | root=$(cd "$(dirname "$0")/.." && pwd) |
| 7 | work=$(mktemp -d) |
| 8 | trap 'rm -rf "$work"' EXIT |
| 9 | |
| 10 | pandoc --print-default-data-file reference.docx > "$work/default.docx" |
| 11 | mkdir "$work/files" |
| 12 | (cd "$work/files" && unzip -q ../default.docx) |
| 13 | |
| 14 | python3 - "$work/files/word/styles.xml" <<'EOF' |
| 15 | import sys |
| 16 | path = sys.argv[1] |
| 17 | styles = open(path, encoding="utf-8").read() |
| 18 | # Centered, in italics, with 2 inches (2880 twentieths of a point) of space above. |
| 19 | dedication = ( |
| 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 | ) |
| 24 | assert styles.count("</w:styles>") == 1 and 'w:styleId="Dedication"' not in styles |
| 25 | open(path, "w", encoding="utf-8").write(styles.replace("</w:styles>", dedication + "</w:styles>")) |
| 26 | EOF |
| 27 | |
| 28 | # [Content_Types].xml goes first, as Word expects. -D leaves out folder entries. |
| 29 | rm -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') |
| 31 | echo "Wrote $root/assets/reference.docx" |