<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: manpage code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 04:05:37 GMT</pubDate>
    <description>DZone Snippets: manpage code</description>
    <item>
      <title>pman  --  create, print, save, view PDF man pages</title>
      <link>http://snippets.dzone.com/posts/show/4274</link>
      <description>pman  --  create, print, save, view PDF man pages&lt;br /&gt;&lt;br /&gt;Author:  ntk&lt;br /&gt;License:    &lt;a href="http://www.opensource.org/licenses/mit-license.php"&gt;The MIT License&lt;/a&gt;, Copyright (c) 2007 ntk&lt;br /&gt;Description:    (batch) convert &lt;a href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/"&gt;man pages&lt;/a&gt; into PDF documents and save them to a specified directory; (batch) print or view PDF man pages from the command line&lt;br /&gt;Platform:    Mac OS X 10.4.10; man bash&lt;br /&gt;Installation:    put pman() into ~/.bash_login (or alternatives)        &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# Usage:&lt;br /&gt;   &lt;br /&gt;pman ls; pman getopts                                 # convert a singel man page to a PDF file, save and open it&lt;br /&gt;pman 8 sticky                                         # same with manual section number&lt;br /&gt;pman m toe                                     &lt;br /&gt;pman -b ls 'open(2)' dd "chmod(2)" curl 'open(n)'     # batch convert man pages into PDF files&lt;br /&gt;pman -p rm srm open\(2\) 'toe(m)' 'ncurses(3)'        # print man pages using the default printer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;pman() {&lt;br /&gt;&lt;br /&gt;section="$1"    &lt;br /&gt;manpage="$2"    &lt;br /&gt;&lt;br /&gt;mandir="/Users/Shared/manpages"    #  save the created PDF man pages to the specified directory&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# batch process man pages to PDF files with the "-b" switch and save them to $mandir&lt;br /&gt;# example: pman -b ls 'open(2)' dd 'chmod(2)' 'open(n)' 'sticky(8)'&lt;br /&gt;# cf. man -aW open for "man n open"&lt;br /&gt;&lt;br /&gt;if [[ "$1" = "-b" ]]; then         &lt;br /&gt;&lt;br /&gt;if [[ ! -d $mandir ]]; then        &lt;br /&gt;   mkdir -p $mandir&lt;br /&gt;   chmod 1777 $mandir&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;shift   # remove "-b" from "$@"&lt;br /&gt;&lt;br /&gt;for manfile in "$@"; do &lt;br /&gt;&lt;br /&gt;# example for $manfile: open(2)&lt;br /&gt;manpage="`echo $manfile | grep -Eos '^[^\(]+'`"                              # extract name of man page&lt;br /&gt;section="`echo $manfile | grep -Eos '\([^\)]+\)' | grep -Eos '[^\(\)]+'`"    # extract section of man page&lt;br /&gt;&lt;br /&gt;if [[ ! "$section" ]]; then&lt;br /&gt;   section="1"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [[ ! -f "`man ${section} -W ${manpage} 2&gt;/dev/null`" ]]; then&lt;br /&gt;#if [[ ! -f "`man -W ${section} ${manpage} 2&gt;/dev/null `" ]]; then&lt;br /&gt;   echo "No such man page: man ${section} ${manpage}"&lt;br /&gt;   continue&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;manfile="${mandir}/${manpage}(${section}).pdf"&lt;br /&gt;echo "$manfile"&lt;br /&gt;&lt;br /&gt;if [[ ! -f "$manfile" ]]; then&lt;br /&gt;   man $section -t $manpage 2&gt;/dev/null | pstopdf -i -o "$manfile" 2&gt;/dev/null&lt;br /&gt;   chmod 1755 "$manfile"&lt;br /&gt;   # hide file extension .pdf&lt;br /&gt;   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;return 0&lt;br /&gt;   &lt;br /&gt;fi          # END of batch processing man pages to PDF files&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# print PDF man pages using the default printer (see man lpr and man lpoptions)&lt;br /&gt;# if necessary, create the specified PDF man pages and save them to $mandir&lt;br /&gt;# example: pman -p rm srm&lt;br /&gt;&lt;br /&gt;if [[ "$1" = "-p" ]]; then         &lt;br /&gt;&lt;br /&gt;if [[ ! -d $mandir ]]; then        &lt;br /&gt;   mkdir -p $mandir&lt;br /&gt;   chmod 1777 $mandir&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;shift   # remove "-p" from "$@"&lt;br /&gt;&lt;br /&gt;for manfile in "$@"; do &lt;br /&gt;&lt;br /&gt;# example for $manfile: open(2)&lt;br /&gt;manpage="`echo $manfile | grep -Eos '^[^\(]+'`"                              # extract name of man page&lt;br /&gt;section="`echo $manfile | grep -Eos '\([^\)]+\)' | grep -Eos '[^\(\)]+'`"    # extract section of man page&lt;br /&gt;&lt;br /&gt;if [[ ! "$section" ]]; then&lt;br /&gt;   section="1"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [[ ! -f "`man ${section} -W ${manpage} 2&gt;/dev/null`" ]]; then&lt;br /&gt;   echo "No such man page: man ${section} ${manpage}"&lt;br /&gt;   continue&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;manfile="${mandir}/${manpage}(${section}).pdf"&lt;br /&gt;echo "$manfile"&lt;br /&gt;&lt;br /&gt;if [[ ! -f "$manfile" ]]; then&lt;br /&gt;   man -t $section $manpage 2&gt;/dev/null | pstopdf -i -o "$manfile" 2&gt;/dev/null&lt;br /&gt;   chmod 1755 "$manfile"&lt;br /&gt;   # hide file extension .pdf&lt;br /&gt;   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi&lt;br /&gt;   lpr "$manfile"&lt;br /&gt;else&lt;br /&gt;   lpr "$manfile"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;return 0&lt;br /&gt;   &lt;br /&gt;fi          # END of printing man pages using the default printer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# convert a single man page to a PDF file, save it to $mandir and then open it in a PDF viewer&lt;br /&gt;&lt;br /&gt;if [[ -z "$1" ]] || [[ $# -gt 2 ]]; then       # check number of arguments&lt;br /&gt;#if [[ -z "$1" || $# -gt 2 ]]; then  &lt;br /&gt;#if [ -z "$1" -o $# -gt 2 ]; then&lt;br /&gt;  echo "Wrong number of arguments!" &lt;br /&gt;  return 1&lt;br /&gt;fi &lt;br /&gt;&lt;br /&gt;if [[ ! "$manpage" ]]; then     # turn "pman ls" into "pman 1 ls"&lt;br /&gt;   manpage="$section"         # if $manpage is an empty string because there has been no "$2" then $manpage is set to "$section" and ...&lt;br /&gt;   section="1"                # ... $section is set to "1"&lt;br /&gt;fi                            &lt;br /&gt;&lt;br /&gt;if [[ ! -f "`man ${section} -W ${manpage} 2&gt;/dev/null`" ]]; then&lt;br /&gt;   echo "No such man page: man ${section} ${manpage}"&lt;br /&gt;   return 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [[ ! -d $mandir ]]; then&lt;br /&gt;   mkdir -p $mandir&lt;br /&gt;   chmod 1777 $mandir&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;manfile="${mandir}/${manpage}(${section}).pdf"&lt;br /&gt;&lt;br /&gt;if [[ -f "$manfile" ]]; then&lt;br /&gt;   open "$manfile"&lt;br /&gt;else&lt;br /&gt;   man $section -t $manpage 2&gt;/dev/null | pstopdf -i -o "$manfile" 2&gt;/dev/null&lt;br /&gt;   chmod 1755 "$manfile"&lt;br /&gt;   # hide file extension .pdf&lt;br /&gt;   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi&lt;br /&gt;   open "$manfile"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;return 0&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 07 Jul 2007 10:54:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4274</guid>
      <author>ntk ()</author>
    </item>
  </channel>
</rss>
