Files
CharlesNWybleResume/bin/resume-build

118 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Resume builder wrapper that runs Pandoc inside a Docker container.
#
# Layout assumptions (relative to repo root):
# - Input Markdown files: resumes/input/*.md
# - Output artifacts: output/*.pdf and output/*.docx
# - Templates: templates/resume-template.tex, templates/resume-reference.docx
#
# Usage:
# bin/resume-build # build all inputs to both PDF and DOCX (single container)
#
# Config via env vars:
# PANDOC_IMAGE (default: pandoc/latex:latest)
# PDF_ENGINE (default: xelatex)
# TEMPLATE (default: templates/resume-template.tex)
# REFDOC (default: templates/resume-reference.docx)
# INPUT_DIR (default: resumes/input)
# OUTPUT_DIR (default: output)
PANDOC_IMAGE=${PANDOC_IMAGE:-danteev/texlive:latest}
PDF_ENGINE=${PDF_ENGINE:-xelatex}
INPUT_DIR=${INPUT_DIR:-resumes/input}
OUTPUT_DIR=${OUTPUT_DIR:-output}
TEMPLATE=${TEMPLATE:-templates/resume-template.tex}
REFDOC=${REFDOC:-templates/resume-reference.docx}
TEXLIVE_INSTALL=${TEXLIVE_INSTALL:-""}
REPO_ROOT=$(cd "$(dirname "$0")/.." && pwd)
cd "$REPO_ROOT"
err() { echo "[resume-build] $*" >&2; }
log() { echo "[resume-build] $*"; }
require() {
command -v "$1" >/dev/null 2>&1 || { err "Missing dependency: $1"; exit 1; }
}
run_docker() {
# Runs a command in the pandoc container with repo mounted at /data
local user opt_user
if user=$(id -u 2>/dev/null) && group=$(id -g 2>/dev/null); then
opt_user=( -u "${user}:${group}" )
else
opt_user=()
fi
local cmd=( docker run --rm
"${opt_user[@]}"
-v "$REPO_ROOT:/data"
-w /data
--entrypoint /bin/sh \
"$PANDOC_IMAGE" )
"${cmd[@]}" -c "$*"
}
cmd_pandoc_pdf() { # echo PDF pandoc command
local in_md=$1 out_pdf=$2 template=$3 engine=$4
printf 'pandoc %q -o %q --from markdown+autolink_bare_uris --template=%q --pdf-engine=%q' \
"$in_md" "$out_pdf" "$template" "$engine"
}
cmd_pandoc_docx() { # echo DOCX pandoc command
local in_md=$1 out_docx=$2 refdoc=$3
printf 'pandoc %q -o %q --from markdown+autolink_bare_uris --reference-doc=%q' \
"$in_md" "$out_docx" "$refdoc"
}
build_all() {
require docker
[[ -d "$INPUT_DIR" ]] || { err "Input dir not found: $INPUT_DIR"; exit 1; }
mkdir -p "$OUTPUT_DIR"
shopt -s nullglob
local files=( "$INPUT_DIR"/*.md )
if (( ${#files[@]} == 0 )); then
err "No markdown files in $INPUT_DIR"; exit 1
fi
local batch="set -e; if [ -n \"${TEXLIVE_INSTALL}\" ]; then tlmgr install ${TEXLIVE_INSTALL} || true; fi"; local first=1
for f in "${files[@]}"; do
local base md_in pdf_out docx_out
base=$(basename "${f%.md}")
md_in="/data/${f}"
pdf_out="/data/${OUTPUT_DIR}/${base}.pdf"
docx_out="/data/${OUTPUT_DIR}/${base}.docx"
log "PDF <- $f"
batch+=" && "
batch+=$(cmd_pandoc_pdf "$md_in" "$pdf_out" "/data/${TEMPLATE}" "$PDF_ENGINE")
log "DOCX <- $f"
batch+=" && "
batch+=$(cmd_pandoc_docx "$md_in" "$docx_out" "/data/${REFDOC}")
done
run_docker "$batch"
}
usage() {
cat <<EOF
Usage: $0
Builds all Markdown files in "$INPUT_DIR" to both PDF and DOCX into "$OUTPUT_DIR".
Env:
PANDOC_IMAGE=${PANDOC_IMAGE}
INPUT_DIR=${INPUT_DIR}
OUTPUT_DIR=${OUTPUT_DIR}
TEMPLATE=${TEMPLATE}
REFDOC=${REFDOC}
PDF_ENGINE=${PDF_ENGINE}
EOF
}
main() {
if [[ $# -gt 0 ]]; then usage; exit 1; fi
build_all
}
main "$@"