diff options
| -rw-r--r-- | mk/willora.mk | 19 | ||||
| -rw-r--r-- | script/epub_font_stuffer.sh | 73 |
2 files changed, 81 insertions, 11 deletions
diff --git a/mk/willora.mk b/mk/willora.mk index ded3dc9..d2aca2a 100644 --- a/mk/willora.mk +++ b/mk/willora.mk @@ -70,6 +70,7 @@ ERBBER_SCRIPT= script/erbber.rb UNICODE_TABLE= script/unicodify.sed UNICODE_TABLE_2= script/unicodify2.sed DOCX_FIXUP= script/docx_fixup.sed +EPUB_FONT_STUFFER= script/epub_font_stuffer.sh DOCX_MANUSCRIPT_REF= lib/WilloraPDF_Manuscript_Reference.docx ODT_MANUSCRIPT_REF= lib/WilloraPDF_Manuscript_Reference.odt @@ -228,11 +229,11 @@ EPUB_DESCR!= cat ${EPUB_BLURBFILE} # # XXX Try to include EPUB_DESCR somewhere other than ARGV CLEANFILES+= ${EPUB_OUT} -${EPUB_OUT}: Gemfile.lock ${EPUB_ADOC_TOTAL} ${EPUB_COVER_FILE} ${EPUB_STYLESHEET} ${EPUB_BLURBFILE} +${EPUB_OUT}: Gemfile.lock ${EPUB_ADOC_TOTAL} ${EPUB_COVER_FILE} ${EPUB_STYLESHEET} ${EPUB_BLURBFILE} ${EPUB_FONT_STUFFER} ${BUNDLE} exec asciidoctor-epub3 \ -v \ -d book \ - -o ${.TARGET} \ + -o prestuffed.epub \ -a uuid="${EPUB_ISBN}" \ -a revdate="${EPUB_REVDATE}" \ -a producer="${PUBLISHER}" \ @@ -244,15 +245,11 @@ ${EPUB_OUT}: Gemfile.lock ${EPUB_ADOC_TOTAL} ${EPUB_COVER_FILE} ${EPUB_STYLESHEE -a media=${MEDIA} \ -a text-align=justify \ ${EPUB_ADOC_TOTAL} - rm -rf ./epub-cleanup ./cleaned.epub; \ - mkdir -p ./epub-cleanup; \ - cd ./epub-cleanup; \ - unzip -q ../${.TARGET}; \ - rsync -avr ${FONTDIR}/ ./EPUB/fonts/; \ - zip ../cleaned.epub -r .; \ - cd ..; \ - rm -rf ./epub-cleanup; \ - mv ./cleaned.epub ${.TARGET}; + bash ${EPUB_FONT_STUFFER} \ + --fontdir ${FONTDIR} \ + --input prestuffed.epub \ + --output ${.TARGET} + rm -f prestuffed.epub CLEANFILES+= ${EPUB_ADOC_TOTAL} ${EPUB_ADOC_TOTAL}: ${BASE_ERB} ${ERBBER_SCRIPT} diff --git a/script/epub_font_stuffer.sh b/script/epub_font_stuffer.sh new file mode 100644 index 0000000..fe9939f --- /dev/null +++ b/script/epub_font_stuffer.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# +# epub_font_stuffer +# Charlotte Koch <charlotte@magentastripe.com> +# +# By default, the EPUB generated by asciidoctor-epub3(1) includes only a +# base set of fonts. This script augments the EPUB by including *all* the +# fonts from the given directory. +# +# This file is part of WilloraPDF. +# +# REQUIREMENTS: zip(1), unzip(1), rsync(1) +# + +set -e + +INPUT="" +OUTPUT="" +FONTDIR="" + +# Parse and verify command line arguments. + +die() { + echo "FATAL: $1" + exit 1 +} + +while [ $# -gt 0 ]; do + case "$1" in + --input) + INPUT="$2" + shift 2 + ;; + --output) + OUTPUT="$2" + shift 2 + ;; + --fontdir) + FONTDIR="$2" + shift 2 + ;; + *) + die "Unknown option: $1" + ;; + esac +done + +test -n "${INPUT}" || die "Missing argument: --input" +test -n "${OUTPUT}" || die "Missing argument: --output" +test -n "${FONTDIR}" || die "Missing argument: --fontdir" + +test -f "${INPUT}" || die "Can't find input file: ${INPUT}" +test -d "${FONTDIR}" || die "Can't find font directory: ${FONTDIR}" + +# Work around the need to change directories in the subsequent zip(1) +# command +real_output="$(pwd)/${OUTPUT}" + +# Extract the EPUB to a temporary location, copy the additional fonts into +# it, then zip it back up. +workdir="$(mktemp -d)" + +function cleanup() { + rm -rf ${workdir} +} + +trap cleanup EXIT + +unzip -q -d ${workdir} ${INPUT} +rsync -avr ${FONTDIR}/ ${workdir}/EPUB/fonts/ +cd ${workdir} +zip -r ${real_output} . +cd - |
