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