[25] | 1 | # man2html1.tcl -- |
---|
| 2 | # |
---|
| 3 | # This file defines procedures that are used during the first pass of the |
---|
| 4 | # man page to html conversion process. It is sourced by h.tcl. |
---|
| 5 | # |
---|
| 6 | # Copyright (c) 1996 by Sun Microsystems, Inc. |
---|
| 7 | # |
---|
| 8 | # SCCS: @(#) man2html1.tcl 1.2 96/03/21 10:48:29 |
---|
| 9 | # |
---|
| 10 | |
---|
| 11 | package require Tcl 8.4 |
---|
| 12 | |
---|
| 13 | # Global variables used by these scripts: |
---|
| 14 | # |
---|
| 15 | # state - state variable that controls action of text proc. |
---|
| 16 | # |
---|
| 17 | # curFile - tail of current man page. |
---|
| 18 | # |
---|
| 19 | # file - file pointer; for both xref.tcl and contents.html |
---|
| 20 | # |
---|
| 21 | # NAME_file - array indexed by NAME and containing file names used |
---|
| 22 | # for hyperlinks. |
---|
| 23 | # |
---|
| 24 | # KEY_file - array indexed by KEYWORD and containing file names used |
---|
| 25 | # for hyperlinks. |
---|
| 26 | # |
---|
| 27 | # lib - contains package name. Used to label section in contents.html |
---|
| 28 | # |
---|
| 29 | # inDT - in dictionary term. |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | # text -- |
---|
| 33 | # |
---|
| 34 | # This procedure adds entries to the hypertext arrays NAME_file |
---|
| 35 | # and KEY_file. |
---|
| 36 | # |
---|
| 37 | # DT: might do this: if first word of $dt matches $name and [llength $name==1] |
---|
| 38 | # and [llength $dt > 1], then add to NAME_file. |
---|
| 39 | # |
---|
| 40 | # Arguments: |
---|
| 41 | # string - Text to index. |
---|
| 42 | |
---|
| 43 | proc text string { |
---|
| 44 | global state curFile NAME_file KEY_file inDT |
---|
| 45 | |
---|
| 46 | switch $state { |
---|
| 47 | NAME { |
---|
| 48 | foreach i [split $string ","] { |
---|
| 49 | lappend NAME_file([string trim $i]) $curFile |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | KEY { |
---|
| 53 | foreach i [split $string ","] { |
---|
| 54 | lappend KEY_file([string trim $i]) $curFile |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | DT - |
---|
| 58 | OFF - |
---|
| 59 | DASH {} |
---|
| 60 | default { |
---|
| 61 | puts stderr "text: unknown state: $state" |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | # macro -- |
---|
| 68 | # |
---|
| 69 | # This procedure is invoked to process macro invocations that start |
---|
| 70 | # with "." (instead of '). |
---|
| 71 | # |
---|
| 72 | # Arguments: |
---|
| 73 | # name - The name of the macro (without the "."). |
---|
| 74 | # args - Any additional arguments to the macro. |
---|
| 75 | |
---|
| 76 | proc macro {name args} { |
---|
| 77 | switch $name { |
---|
| 78 | SH - SS { |
---|
| 79 | global state |
---|
| 80 | |
---|
| 81 | switch $args { |
---|
| 82 | NAME { |
---|
| 83 | if {$state eq "INIT"} { |
---|
| 84 | set state NAME |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | DESCRIPTION {set state DT} |
---|
| 88 | INTRODUCTION {set state DT} |
---|
| 89 | KEYWORDS {set state KEY} |
---|
| 90 | default {set state OFF} |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | } |
---|
| 94 | TP { |
---|
| 95 | global inDT |
---|
| 96 | set inDT 1 |
---|
| 97 | } |
---|
| 98 | TH { |
---|
| 99 | global lib state inDT |
---|
| 100 | set inDT 0 |
---|
| 101 | set state INIT |
---|
| 102 | if {[llength $args] != 5} { |
---|
| 103 | set args [join $args " "] |
---|
| 104 | puts stderr "Bad .TH macro: .$name $args" |
---|
| 105 | } |
---|
| 106 | set lib [lindex $args 3] ;# Tcl or Tk |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | # dash -- |
---|
| 113 | # |
---|
| 114 | # This procedure is invoked to handle dash characters ("\-" in |
---|
| 115 | # troff). It only function in pass1 is to terminate the NAME state. |
---|
| 116 | # |
---|
| 117 | # Arguments: |
---|
| 118 | # None. |
---|
| 119 | |
---|
| 120 | proc dash {} { |
---|
| 121 | global state |
---|
| 122 | if {$state eq "NAME"} { |
---|
| 123 | set state DASH |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | # newline -- |
---|
| 129 | # |
---|
| 130 | # This procedure is invoked to handle newlines in the troff input. |
---|
| 131 | # It's only purpose is to terminate a DT (dictionary term). |
---|
| 132 | # |
---|
| 133 | # Arguments: |
---|
| 134 | # None. |
---|
| 135 | |
---|
| 136 | proc newline {} { |
---|
| 137 | global inDT |
---|
| 138 | set inDT 0 |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | # initGlobals, tab, font, char, macro2 -- |
---|
| 143 | # |
---|
| 144 | # These procedures do nothing during the first pass. |
---|
| 145 | # |
---|
| 146 | # Arguments: |
---|
| 147 | # None. |
---|
| 148 | |
---|
| 149 | proc initGlobals {} {} |
---|
| 150 | proc tab {} {} |
---|
| 151 | proc font type {} |
---|
| 152 | proc char name {} |
---|
| 153 | proc macro2 {name args} {} |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | # doListing -- |
---|
| 157 | # |
---|
| 158 | # Writes an ls like list to a file. Searches NAME_file for entries |
---|
| 159 | # that match the input pattern. |
---|
| 160 | # |
---|
| 161 | # Arguments: |
---|
| 162 | # file - Output file pointer. |
---|
| 163 | # pattern - glob style match pattern |
---|
| 164 | |
---|
| 165 | proc doListing {file pattern} { |
---|
| 166 | global NAME_file |
---|
| 167 | |
---|
| 168 | set max_len 0 |
---|
| 169 | foreach name [lsort [array names NAME_file]] { |
---|
| 170 | set ref $NAME_file($name) |
---|
| 171 | if [string match $pattern $ref] { |
---|
| 172 | lappend type $name |
---|
| 173 | if {[string length $name] > $max_len} { |
---|
| 174 | set max_len [string length $name] |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | if [catch {llength $type} ] { |
---|
| 179 | puts stderr " doListing: no names matched pattern ($pattern)" |
---|
| 180 | return |
---|
| 181 | } |
---|
| 182 | incr max_len |
---|
| 183 | set ncols [expr {90/$max_len}] |
---|
| 184 | set nrows [expr {int(ceil([llength $type] / double($ncols)))} ] |
---|
| 185 | |
---|
| 186 | # ? max_len ncols nrows |
---|
| 187 | |
---|
| 188 | set index 0 |
---|
| 189 | foreach f $type { |
---|
| 190 | lappend row([expr {$index % $nrows}]) $f |
---|
| 191 | incr index |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | puts -nonewline $file "<PRE>" |
---|
| 195 | for {set i 0} {$i<$nrows} {incr i} { |
---|
| 196 | foreach name $row($i) { |
---|
| 197 | set str [format "%-*s" $max_len $name] |
---|
| 198 | regsub $name $str "<A HREF=\"$NAME_file($name).html\">$name</A>" str |
---|
| 199 | puts -nonewline $file $str |
---|
| 200 | } |
---|
| 201 | puts $file {} |
---|
| 202 | } |
---|
| 203 | puts $file "</PRE>" |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | # doContents -- |
---|
| 208 | # |
---|
| 209 | # Generates a HTML contents file using the NAME_file array |
---|
| 210 | # as its input database. |
---|
| 211 | # |
---|
| 212 | # Arguments: |
---|
| 213 | # file - name of the contents file. |
---|
| 214 | # packageName - string used in the title and sub-heads of the HTML |
---|
| 215 | # page. Normally name of the package without version |
---|
| 216 | # numbers. |
---|
| 217 | |
---|
| 218 | proc doContents {file packageName} { |
---|
| 219 | global footer |
---|
| 220 | |
---|
| 221 | set file [open $file w] |
---|
| 222 | |
---|
| 223 | puts $file "<HTML><HEAD><TITLE>$packageName Manual</TITLE></HEAD><BODY>" |
---|
| 224 | puts $file "<H3>$packageName</H3>" |
---|
| 225 | doListing $file "*.1" |
---|
| 226 | |
---|
| 227 | puts $file "<HR><H3>$packageName Commands</H3>" |
---|
| 228 | doListing $file "*.n" |
---|
| 229 | |
---|
| 230 | puts $file "<HR><H3>$packageName Library</H3>" |
---|
| 231 | doListing $file "*.3" |
---|
| 232 | |
---|
| 233 | puts $file $footer |
---|
| 234 | puts $file "</BODY></HTML>" |
---|
| 235 | close $file |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | # do -- |
---|
| 240 | # |
---|
| 241 | # This is the toplevel procedure that searches a man page |
---|
| 242 | # for hypertext links. It builds a data base consisting of |
---|
| 243 | # two arrays: NAME_file and KEY file. It runs the man2tcl |
---|
| 244 | # program to turn the man page into a script, then it evals |
---|
| 245 | # that script. |
---|
| 246 | # |
---|
| 247 | # Arguments: |
---|
| 248 | # fileName - Name of the file to scan. |
---|
| 249 | |
---|
| 250 | proc do fileName { |
---|
| 251 | global curFile |
---|
| 252 | set curFile [file tail $fileName] |
---|
| 253 | set file stdout |
---|
| 254 | puts " Pass 1 -- $fileName" |
---|
| 255 | flush stdout |
---|
| 256 | if [catch {eval [exec man2tcl [glob $fileName]]} msg] { |
---|
| 257 | global errorInfo |
---|
| 258 | puts stderr $msg |
---|
| 259 | puts "in" |
---|
| 260 | puts $errorInfo |
---|
| 261 | exit 1 |
---|
| 262 | } |
---|
| 263 | } |
---|