Version 7 of A little man page viewer

Updated 2004-01-22 13:19:12

if 0 {Richard Suchenwirth 2004-01-21 - As a little evening project, here's a viewer for man pages that understands (some of) the roff format. Highly experimental, nothing guaranteed, but a nice distraction :}

http://mini.net/files/man2text.jpg


 package require Tk

 set font {Times 10}
 set file [lindex $argv 0]
 wm title . "man [file tail  [file root $file]]"
 pack [scrollbar .s -command ".t yview"] -side right -fill y
 pack [text .t -wrap word -yscrollcommand ".s set" \
        -padx 10 -font $font] \
    -side left -fill both -expand 1
 foreach style {bold italic} {
    .t tag config $style -font [concat $font $style]
 }
 .t tag config right -justify right

 set fp [open $file]
 set tab ""
 foreach line [split [read $fp] \n] {
     set tag ""
     if {$line eq ".EN"} {continue}
     if {$line eq ".PP"} {set line \n\n}
     if {$line eq ".br"} {set line \n}
     if {$line eq ".ti 8"} {set line \n\t}
     if {$line eq ".in 8"} {set tab \t; continue}
     if {$line eq ".in 0"} {set tab ""; continue}
     if [regexp  {^\.SH (.+)} $line -> line] {set line \n\n$line; set tag bold}
     if [regexp  {^\.TH (.+)} $line -> line] {set tag right}
     if [regexp  {^\.I (.+)} $line -> line]  {set tag italic}
     if [regexp  {^\.B (.+)} $line -> line]  {set tag bold}
     if [regexp  {^\.BI (.+)} $line -> line] {set tag {bold italic}}
     if [regexp  {^\.TP} $line] {set line \n\t}

     set line [string map {\\- - \\fB "" \\fR "" \\^ ""} $line]
    .t insert end "$tab$line " $tag
 }
 close $fp

escargo 21 Jan 2003 - Could you give some examples of roff codes that might be encountered that would not be handled? (Some of these formatting commands look like they are one per line. Would some elseif structures be appropriate here?)

RS: I don't have the roff syntax at hand, but I think it contains more that I've covered. I did this just data-driven from a man page from downloaded software (from 1992), which I could otherwise not render - soon later I was told about nroff -man which does it; but at that point I already wanted to do text layout... elseifs would be ok, but the current way is not dangerous as roff dot commands come only one per line. Also, some evidently cut-and-pasted code could be factored out in a foreach loop... unknown dot commands could be highlighted in red (to aid debugging)... etc.

DKF: An interesting one to do would be the Tcl/Tk manual pages, but you'll need quite a bit extra work to do it.


Category Application