#!/usr/bin/env bash OUTPUT="print.pdf" print() { text | pdf '(art history)' 'David W. Speck' "$@" } text() { cat << '_ENDOFTEXT_' (art history): ( canon{( (artist < context) > concept | (artwork{action gesture object picture sound word} < context) > artobject | ({critic curator publicist} < context) > art{context idea object} | public > myth )} | context > history ) modern: context = {context history} postmodern: artwork{} = artwork{action gesture history object picture sound word} key: ( name = thing () = (composite thing) : = elaboration name{list} = (optionally named thing composed of one or more things from list) < = influence > = creation | = through = = definition ) _ENDOFTEXT_ } width() { local IFS="" local LINE="" local WIDTH=0 while read -r LINE do COLUMNS=${#LINE} if [ ${COLUMNS} -gt ${WIDTH} ] then WIDTH=${COLUMNS} fi done echo ${WIDTH} } pdf() { local TITLE="${1:-untitled}" local AUTHOR="${2:-anonymous}" local PAPERSIZE="${3:-a4}" local FORMAT="${4:-single}" local TEXT=$(cat) local PAGE="" local LAYOUT="" if [ "${FORMAT}" = "single" ] then PAGE="${TEXT}" LAYOUT="--landscape --columns=1 --rows=1" elif [ "${FORMAT}" = "double" ] then PAGE=$(printf "%s\f\n\n%s" "${TEXT}" "${TEXT}") LAYOUT="--portrait --file-align=fill --columns=1 --rows=2" else echo "${0}: ${FORMAT}: Page format can be single or double." >&2 exit 1 fi echo -n "${PAGE}" \ | a2ps \ --title="${TITLE}" \ --medium=${PAPERSIZE} ${LAYOUT} \ --pretty-print=no --no-header --borders=no \ --chars-per-line=$(echo "${TEXT}" | width) \ | sed "s/^\(%%For: \).*/\1${AUTHOR}/" \ | ps2pdf -dCompatibilityLevel=1.2 -sPAPERSIZE=${PAPERSIZE} - - } set -euo pipefail print "$@" > "${OUTPUT}"