#!/bin/sh # # man2ps man-base-directory [chapter-directory]* # # Convert 'man' pages to PDF-annotated PostScript # # 2003/06/05 Ben Hekster # # get the man base directory case $# in 0) echo "usage: man2ps man-base-directory [chapter-directory]*" 1>&2; exit 1;; esac directory=$1; shift # get the chapters case $# in 0) # assume everything in the base directory is a chapter-directory chapters=`cd $directory; ls`;; *) # use remaining parameters as chapter directories chapters=$*;; esac # build a table of bookmarks for the PDF file toc=/tmp/$$.toc trap "rm $$.*; exit 1" 1 2 15 # decide whether to create a two-level bookmark hierarchy case $# in 1) ;; *) supertoc=/tmp/$$.supertoc;; esac # for all chapters for chapter in $chapters do # skip topics that are not directories if test ! -d $directory/$chapter; then continue; fi # display the chapter we are working on if test "$supertoc"; then echo ">>> $chapter <<<" 1>&2; fi # get the topics of that chapter topics=`cd $directory/$chapter; ls *` # for each man topic for topic in $topics do # extract just the topic name without the "dot chapter" readableTopic=${topic%.[0-9]} # tell user what we are working on echo $readableTopic 1>&2 # see whether the topic is a simple redirection to another topic redirect=`sed -n -e '1 s/.so .*\///p' $directory/$chapter/$topic` if test $redirect then # output a bookmark that redirects to the actual topic # (assume that it is in the same directory and chapter) echo "[ /Title ($readableTopic) /Dest /$redirect /OUT pdfmark" >>$toc else # convert to grops intermediate representation troff -msafer -mandoc -Tps $directory/$chapter/$topic | # insert pdfmark for bookmark named destination sed -e "/^p1$/a\\ x X ps: exec [ /Dest /$topic /DEST pdfmark\\ " \ $filter && # next time through, filter out some lines that grops doesn't need repeated filter='-e 1,/x.init/d' # build up the bookmark table of contents echo "[ /Title ($readableTopic) /Dest /$topic /OUT pdfmark" >>$toc fi done | # convert grops intermediate representation to PostScript grops # building a two-level bookmark hierarchy? if test $supertoc; then # count the number of topics in this chapter topicCount=`cat $toc | wc -l` # output a chapter bookmark echo "[ /Title ($chapter) /Count $topicCount /OUT pdfmark" >>$supertoc # and the topic bookmarks cat $toc >>$supertoc rm $toc; fi done | # concatenate bookmarks into PostScript sed \ -e "/%%Trailer/r ${supertoc:-$toc}" rm /tmp/$$.*