Skip to content

Instantly share code, notes, and snippets.

@psagers
Created November 7, 2011 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psagers/1346437 to your computer and use it in GitHub Desktop.
Save psagers/1346437 to your computer and use it in GitHub Desktop.
Render man pages to PDF on OS X.
#!/bin/bash
REMOTE=
REFRESH=
CACHE_SUBDIR="localhost"
MAN_OPTS=
function main ()
{
while getopts "hfr:" arg; do
if [ "$arg" = "?" ]; then
usage
fi
case $arg in
h) usage;;
f) REFRESH=1;;
r) REMOTE="$OPTARG"; CACHE_SUBDIR="$OPTARG";;
esac
done
shift $(( $OPTIND - 1 ))
for man_path in $(run_man -w "$@"); do
handle_man_path "$man_path"
done
}
function usage ()
{
echo "Usage: $(basename "$0") [-r <ssh-host>] [-f] [--] <man args>"
exit 1
}
function handle_man_path ()
{
man_path="$1"
pdf_path="${HOME}/Library/Caches/pman/${CACHE_SUBDIR}${man_path}"
pdf_path="${pdf_path%.gz}.pdf"
if [[ ! -e "$pdf_path" || -n "$REFRESH" ]]; then
mkdir -p "$(dirname "$pdf_path")"
run_man -t "$man_path" | pstopdf -i -o "$pdf_path"
fi
if [[ -e "$pdf_path" ]]; then
open "$pdf_path"
fi
}
function run_man ()
{
if [[ -z "$REMOTE" ]]; then
man ${MAN_OPTS} "$@"
else
ssh "$REMOTE" -- man ${MAN_OPTS} "$@"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment