os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tools/man2help2.tcl
First public contribution.
3 # This file defines procedures that are used during the second pass of
4 # the man page conversion. It converts the man format input to rtf
5 # form suitable for use by the Windows help compiler.
7 # Copyright (c) 1996 by Sun Microsystems, Inc.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 # RCS: @(#) $Id: man2help2.tcl,v 1.12 2002/10/03 13:34:32 dkf Exp $
15 # Global variables used by these scripts:
17 # state - state variable that controls action of text proc.
19 # topics - array indexed by (package,section,topic) with value
22 # keywords - array indexed by keyword string with value of topic ID.
24 # curID - current topic ID, starts at 0 and is incremented for
25 # each new topic file.
27 # curPkg - current package name (e.g. Tcl).
29 # curSect - current section title (e.g. "Tcl Built-In Commands").
34 # This procedure is invoked to set the initial values of all of the
35 # global variables, before processing a man page.
41 uplevel \#0 unset state
44 set state(paragraphPending) 0
45 set state(breakPending) 0
46 set state(firstIndent) 0
47 set state(leftIndent) 0
50 set state(paragraph) 0
51 set state(textState) 0
53 set state(startCode) "{\\b "
54 set state(startEmphasis) "{\\i "
55 set state(endCode) "}"
56 set state(endEmphasis) "}"
59 set state(offset) [getTwips 0.5i]
60 set state(leftMargin) [getTwips 0.5i]
61 set state(nestingLevel) 0
66 # set up international character table
76 # Arranges for future text to use a special font, rather than
77 # the default paragraph font.
80 # font - Name of new font to use.
82 proc beginFont {font} {
86 if {[string equal $state(curFont) $font]} {
90 puts -nonewline $file $state(start$font)
91 set state(curFont) $font
97 # Reverts to the default font for the paragraph type.
105 if {[string compare $state(curFont) ""]} {
106 puts -nonewline $file $state(end$state(curFont))
107 set state(curFont) ""
114 # This procedure is called the first time that text is output for a
115 # paragraph. It outputs the header information for the paragraph.
123 if $state(breakPending) {
126 if $state(paragraphPending) {
127 puts $file [format "\\par\n\\pard\\fi%.0f\\li%.0f" \
128 $state(firstIndent) $state(leftIndent)]
129 foreach tab $state(tabs) {
130 puts $file [format "\\tx%.0f" $tab]
134 puts $file "\\sb$state(sb)"
138 set state(breakPending) 0
139 set state(paragraphPending) 0
145 # This procedure adds text to the current state(paragraph). If this is
146 # the first text in the state(paragraph) then header information for the
147 # state(paragraph) is output before the text.
150 # string - Text to output in the state(paragraph).
153 global file state chars
156 set string [string map [list \
165 # Check if this is the beginning of an international character string.
166 # If so, look up the sequence in the chars table and substitute the
167 # appropriate hex value.
170 if {[regexp {^'([^']*)'} $string dummy ch]} {
171 if {[info exists chars($ch)]} {
172 regsub {^'[^']*'} $string "\\\\'$chars($ch)" string
174 puts stderr "Unknown international character '$ch'"
180 switch $state(textState) {
182 if {$state(inTP) == 0} {
183 set string [insertRef $string]
187 global topics curPkg curSect
188 foreach i [split $string] {
189 if {![regexp -nocase {^[a-z_0-9]+} [string trim $i] i ]} {
192 if {![catch {set ref $topics($curPkg,$curSect,$i)} ]} {
193 regsub $i $string [link $i $ref] string
201 puts -nonewline $file "$string"
208 # This procedure looks for a string in the cross reference table and
209 # generates a hot-link to the appropriate topic. Tries to find the
210 # nearest reference in the manual.
213 # string - Text to output in the state(paragraph).
215 proc insertRef {string} {
216 global NAME_file curPkg curSect topics curID
218 set string [string trim $string]
220 if {[info exists topics($curPkg,$curSect,$string)]} {
221 set ref $topics($curPkg,$curSect,$string)
223 set sites [array names topics "$curPkg,*,$string"]
224 set count [llength $sites]
226 set ref $topics([lindex $sites 0])
228 set sites [array names topics "*,*,$string"]
229 set count [llength $sites]
231 set ref $topics([lindex $sites 0])
236 if {($ref != {}) && ($ref != $curID)} {
237 set string [link $string $ref]
246 # This procedure is invoked to process macro invocations that start
247 # with "." (instead of ').
250 # name - The name of the macro (without the ".").
251 # args - Any additional arguments to the macro.
253 proc macro {name args} {
257 if {[llength $args] != 3 && [llength $args] != 2} {
258 puts stderr "Bad .AP macro: .$name [join $args " "]"
261 setTabs {1.25i 2.5i 3.75i}
263 text [lindex $args 0]
266 text [lindex $args 1]
269 if {[llength $args] == 3} {
270 text "([lindex $args 2])"
275 # next page and previous page
283 puts -nonewline $::file "\\f0\\fs20 "
285 set state(breakPending) 0
287 set state(leftIndent) [expr {$state(leftIndent) - $state(offset)}]
294 set state(leftIndent) [expr {$state(leftIndent) + $state(offset)}]
296 puts -nonewline $::file "\\f1\\fs18 "
324 if {[llength $args] != 3} {
325 puts stderr "Bad .OP macro: .$name [join $args " "]"
327 set state(nestingLevel) 0
331 text "Command-Line Name:"
334 set x [lindex $args 0]
335 regsub -all {\\-} $x - x
339 text "Database Name:"
342 text [lindex $args 1]
345 text "Database Class:"
348 text [lindex $args 2]
367 set state(nestingLevel) 0
371 set temp $state(textState)
372 set state(textState) REF
374 set state(textState) $temp
376 text " manual entry for detailed descriptions of the above options."
382 SHmacro "STANDARD OPTIONS"
383 set state(nestingLevel) 0
390 if {$args != "man.macros"} {
391 puts stderr "Unknown macro: .$name [join $args " "]"
398 set count [lindex $args 0]
415 puts -nonewline $file "{\\ul "
416 text [lindex $args 0]
417 puts -nonewline $file "}"
418 if {[llength $args] == 2} {
419 text [lindex $args 1]
425 puts stderr "Unknown macro: .$name [join $args " "]"
433 # This procedure returns the string for a hot link to a different
437 # label - String to display in hot-spot.
438 # id - Context string to jump to.
440 proc link {label id} {
441 return "{\\uldb $label}{\\v $id}"
447 # This procedure is invoked to handle font changes in the text
451 # type - Type of font: R, I, B, or S.
459 if {$state(textState) == "REF"} {
460 set state(textState) INSERT
466 if {$state(textState) == "INSERT"} {
467 set state(textState) REF
476 puts stderr "Unknown font: $type"
485 # Insert a text string that may also have \fB-style font changes
486 # and a few other backslash sequences in it.
489 # text - Text to insert.
491 proc formattedText {text} {
494 while {$text != ""} {
495 set index [string first \\ $text]
500 text [string range $text 0 [expr {$index-1}]]
501 set c [string index $text [expr {$index+1}]]
504 font [string index $text [expr {$index+2}]]
505 set text [string range $text [expr {$index+3}] end]
509 set text [string range $text [expr {$index+2}] end]
513 set text [string range $text [expr {$index+2}] end]
516 set text [string range $text [expr {$index+2}] end]
520 regexp {'([^']*)'(.*)} $text all ch text
524 puts stderr "Unknown sequence: \\$c"
525 set text [string range $text [expr {$index+2}] end]
534 # This procedure is invoked to handle dash characters ("\-" in
535 # troff). It outputs a special dash character.
542 if {[string equal $state(textState) "NAME"]} {
543 set state(textState) 0
551 # This procedure is invoked to handle tabs in the troff input.
552 # Right now it does nothing.
561 puts -nonewline $file "\\tab "
567 # This procedure handles the ".ta" macro, which sets tab stops.
570 # tabList - List of tab stops, each consisting of a number
571 # followed by "i" (inch) or "c" (cm).
573 proc setTabs {tabList} {
577 foreach arg $tabList {
578 set distance [expr {$state(leftMargin) \
579 + ($state(offset) * $state(nestingLevel)) + [getTwips $arg]}]
580 lappend state(tabs) [expr {round($distance)}]
588 # Generates a line break in the HTML output.
596 set state(breakPending) 1
603 # This procedure is invoked to handle newlines in the troff input.
604 # It outputs either a space character or a newline character, depending
616 } elseif {$state(noFill)} {
626 # This procedure is invoked to generate a page break.
633 if {[string equal $curVer ""]} {
637 puts $file {\pard\sb400\qc}
638 puts $file "Last change: $curVer\\page"
645 # This procedure is called to handle a special character.
648 # name - Special character named in troff \x or \(xx construct.
653 switch -exact $name {
659 puts -nonewline $file " "
663 puts -nonewline $file " \\emspace "
667 puts -nonewline $file "\\\\"
671 puts -nonewline $file "\\'b1 "
678 puts -nonewline $file "·"
681 puts stderr "Unknown character: $name"
689 # This procedure handles macros that are invoked with a leading "'"
690 # character instead of space. Right now it just generates an
694 # name - The name of the macro (without the ".").
695 # args - Any additional arguments to the macro.
697 proc macro2 {name args} {
698 puts stderr "Unknown macro: '$name [join $args " "]"
705 # Subsection head; handles the .SH macro.
708 # name - Section name.
710 proc SHmacro {argList} {
713 set args [join $argList " "]
714 if {[llength $argList] < 1} {
715 puts stderr "Bad .SH macro: .SH $args"
718 # control what the text proc does with text
721 NAME {set state(textState) NAME}
722 DESCRIPTION {set state(textState) INSERT}
723 INTRODUCTION {set state(textState) INSERT}
724 "WIDGET-SPECIFIC OPTIONS" {set state(textState) INSERT}
725 "SEE ALSO" {set state(textState) SEE}
726 KEYWORDS {set state(textState) KEY; return}
729 if {$state(breakPending) != -1} {
730 set state(breakPending) 1
732 set state(breakPending) 0
746 # This procedure is invoked to handle ".IP" macros, which may take any
747 # of the following forms:
749 # .IP [1] Translate to a "1Step" state(paragraph).
750 # .IP [x] (x > 1) Translate to a "Step" state(paragraph).
751 # .IP Translate to a "Bullet" state(paragraph).
752 # .IP text count Translate to a FirstBody state(paragraph) with special
753 # indent and tab stop based on "count", and tab after
757 # argList - List of arguments to the .IP macro.
759 # HTML limitations: 'count' in '.IP text count' is ignored.
761 proc IPmacro {argList} {
764 set length [llength $argList]
773 formattedText [lindex $argList 0]
778 set count [lindex $argList 1]
779 set tab [expr $count * 0.1]i
783 formattedText [lindex $argList 0]
787 puts stderr "Bad .IP macro: .IP [join $argList " "]"
793 # This procedure is invoked to handle ".TP" macros, which may take any
794 # of the following forms:
796 # .TP x Translate to an state(indent)ed state(paragraph) with the
797 # specified state(indent) (in 100 twip units).
798 # .TP Translate to an state(indent)ed state(paragraph) with
799 # default state(indent).
802 # argList - List of arguments to the .IP macro.
804 # HTML limitations: 'x' in '.TP x' is ignored.
806 proc TPmacro {argList} {
808 set length [llength $argList]
812 set val [expr {([lindex $argList 0] * 100.0)/1440}]i
823 # This procedure handles the .TH macro. It generates the non-scrolling
824 # header section for a given man page, and enters information into the
825 # table of contents. The .TH macro has the following form:
827 # .TH name section date footer header
830 # argList - List of arguments to the .TH macro.
832 proc THmacro {argList} {
833 global file curPkg curSect curID id_keywords state curVer bitmap
835 if {[llength $argList] != 5} {
836 set args [join $argList " "]
837 puts stderr "Bad .TH macro: .TH $args"
840 set name [lindex $argList 0] ;# Tcl_UpVar
841 set page [lindex $argList 1] ;# 3
842 set curVer [lindex $argList 2] ;# 7.4
843 set curPkg [lindex $argList 3] ;# Tcl
844 set curSect [lindex $argList 4] ;# {Tcl Library Procedures}
846 regsub -all {\\ } $curSect { } curSect ;# Clean up for [incr\ Tcl]
848 puts $file "#{\\footnote $curID}" ;# Context string
849 puts $file "\${\\footnote $name}" ;# Topic title
850 set browse "${curSect}${name}"
851 regsub -all {[ _-]} $browse {} browse
852 puts $file "+{\\footnote $browse}" ;# Browse sequence
854 # Suppress duplicates
855 foreach i $id_keywords($curID) {
858 foreach i [array names keys] {
859 set i [string trim $i]
860 if {[string length $i] > 0} {
861 puts $file "K{\\footnote $i}" ;# Keyword strings
865 puts $file "\\pard\\tx3000\\sb100\\sa100\\fs24\\keepn"
871 if {[info exists bitmap]} {
872 # a right justified bitmap
873 puts $file "\\\{bmrt $bitmap\\\}"
876 set state(breakPending) -1
881 # Set the indents for a new paragraph, and start a paragraph break
884 # leftIndent - The new left margin for body lines.
885 # firstIndent - The offset from the left margin for the first line.
887 proc nextPara {leftIndent {firstIndent 0i}} {
889 set state(leftIndent) [getTwips $leftIndent]
890 set state(firstIndent) [getTwips $firstIndent]
891 set state(paragraphPending) 1
897 # This procedure sets the left and hanging state(indent)s for a line.
898 # State(Indent)s are specified in units of inches or centimeters, and are
899 # relative to the current nesting level and left margin.
902 # leftState(Indent) - The new left margin for lines after the first.
903 # firstState(Indent) - The new left margin for the first line of a state(paragraph).
905 proc newPara {leftIndent {firstIndent 0i}} {
907 if $state(paragraph) {
908 puts -nonewline $file "\\line\n"
910 if {$leftIndent != ""} {
911 set state(leftIndent) [expr {$state(leftMargin) \
912 + ($state(offset) * $state(nestingLevel)) \
913 + [getTwips $leftIndent]}]
915 set state(firstIndent) [getTwips $firstIndent]
916 set state(paragraphPending) 1
922 # This procedure converts a distance in inches or centimeters into
923 # twips (1/1440 of an inch).
926 # arg - A number followed by "i" or "c"
928 proc getTwips {arg} {
929 if {[scan $arg "%f%s" distance units] != 2} {
930 puts stderr "bad distance \"$arg\""
935 set distance [expr {$distance * 567}]
938 set distance [expr {$distance * 1440}]
941 puts stderr "bad units in distance \"$arg\""
948 # incrNestingLevel --
950 # This procedure does the work of the .RS macro, which increments
951 # the number of state(indent)ations that affect things like .PP.
956 proc incrNestingLevel {} {
959 incr state(nestingLevel)
960 set oldp $state(paragraph)
961 set state(paragraph) 0
963 set state(paragraph) $oldp
966 # decrNestingLevel --
968 # This procedure does the work of the .RE macro, which decrements
969 # the number of indentations that affect things like .PP.
974 proc decrNestingLevel {} {
977 if {$state(nestingLevel) == 0} {
978 puts stderr "Nesting level decremented below 0"
980 incr state(nestingLevel) -1