File: fmt.html

package info (click to toggle)
tcllib 1.8-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,628 kB
  • ctags: 4,897
  • sloc: tcl: 88,012; sh: 7,856; ansic: 4,174; xml: 1,765; yacc: 753; perl: 84; f90: 84; makefile: 60; python: 33; ruby: 13; php: 11
file content (472 lines) | stat: -rw-r--r-- 13,736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# -*- tcl -*-
#
# fmt.html
#
# Copyright (c) 2001-2003 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# Definitions to convert a tcl based manpage definition into
# a manpage based upon HTML markup.
#
################################################################
################################################################

dt_source _common.tcl   ; # Shared code
dt_source _html.tcl     ; # HTML basic formatting

proc c_copyrightsymbol {} {markup "&copy;"}

proc bgcolor {} {return ""}
proc border  {} {return 0}
proc Year    {} {clock format [clock seconds] -format %Y}

# possibleReference text gi --
#	Check if $text is a potential cross-reference;
#	if so, format as a reference;
#	otherwise format as a $gi element.
#
proc c_possibleReference {text gi} {
    global SectionNames
    set id [c_sectionId $text]
    if {[info exists SectionNames($id)]} {
	return [taga a [list href #$id]]$text[tag/ a]
    } else {
	return [tag $gi]$text[tag/ $gi]
    }
}

c_holdBuffers require

################################################################
## Backend for HTML markup

# --------------------------------------------------------------
# Handling of lists. Simplified, the global check of nesting and
# legality of list commands allows us to throw away most of the
# existing checks.

global liststack ; # stack of list tags to use in list_end
global hintstack ; # stack of hint information.
global chint     ; # current hint settings
global lmark     ; # boolean flag, 1 = list item command was last
#                ; #               0 = something other than a list item command

set    liststack [list]
set    hintstack [list]
set    chint     ""
set    lmark     0

proc llevel {} {global liststack ; return [llength $liststack]}

proc lpush {t hint} {
    global  liststack hintstack chint
    lappend liststack [tag/ $t]
    lappend hintstack $chint
    set     chint $hint
    return [tag $t]
}

proc lpop {} {
    global liststack hintstack chint
    set t         [lindex   $liststack end]
    set liststack [lreplace $liststack end end]
    set chint     [lindex   $hintstack end]
    set hintstack [lreplace $hintstack end end]
    return $t
}

proc lsmark {value} {
    global lmark ; set lmark $value ; return
}

proc limark {} {
    # hint and mark processing.
    # hint: compact list, do not create additional whitespace
    if {[lcompact]} {return ""}

    # hint: wide list, create additional whitespace.
    # mark: exception: two list items following each other have no whitespace.
    global lmark ; if {$lmark} {return ""}
    return [tag br][tag br]\n
}

proc lcompact {} {global chint ; string equal $chint compact}

proc fmt_plain_text {text} {
    # Control list state
    set redux [string map [list " " "" "\t" "" "\n" ""] $text]
    if {$redux != {}} {lsmark 0}
    return $text
}

################################################################
# Formatting commands.

c_pass 1 fmt_manpage_begin {title section version} {c_cinit ; c_clrSections ; return}
c_pass 2 fmt_manpage_begin {title section version} {
    XrefInit
    c_cinit
    set module      [dt_module]
    set shortdesc   [c_get_module]
    set description [c_get_title]
    set copyright   [c_get_copyright]

    set     hdr ""
    append  hdr "[markup <html><head>]\n"
    append  hdr "[markup <title>]$title - $shortdesc [markup </title>]\n"

    # Engine parameter - insert 'meta'
    if {[set meta [Get meta]] != {}} {append hdr [markup $meta]\n}

    append  hdr "[markup </head>]\n"
    append  hdr [ht_comment [c_provenance]]\n
    if {$copyright != {}} {
	append  hdr [ht_comment $copyright]\n
    }
    append  hdr [ht_comment "CVS: \$Id\$ $title.$section"]\n
    append  hdr \n
    append  hdr [markup <body>]\n

    # Engine parameter - insert 'header'
    if {[set header [Get header]] != {}} {append hdr [markup $header]\n}

    append  hdr "[markup <h1>] [string trimleft $title :]($section) $version $module \"$shortdesc\"[markup </h1>]\n"
    append  hdr [fmt_section NAME]\n
    append  hdr "[fmt_para] $title - $description"
    return $hdr
}

c_pass 1 fmt_moddesc   {desc} {c_set_module $desc}
c_pass 2 fmt_moddesc   {desc} NOP

c_pass 1 fmt_titledesc {desc} {c_set_title $desc}
c_pass 2 fmt_titledesc {desc} NOP

c_pass 1 fmt_copyright {desc} {c_set_copyright $desc}
c_pass 2 fmt_copyright {desc} NOP

c_pass 1 fmt_manpage_end {} {c_creset ; return}
c_pass 2 fmt_manpage_end {} {
    c_creset
    set res ""

    set sa [c_xref_seealso]
    set kw [c_xref_keywords]
    set ct [c_get_copyright]

    if {[llength $sa] > 0} {
	append res [fmt_section {SEE ALSO}] \n
	append res [join [XrefList [lsort $sa] sa] ", "] \n
    }
    if {[llength $kw] > 0} {
	append res [fmt_section KEYWORDS] \n
	append res [join [XrefList [lsort $kw] kw] ", "] \n
    }
    if {$ct != {}} {
	append res [fmt_section COPYRIGHT] \n
	append res [join [split $ct \n] [tag br]\n] [tag br]\n
    }

    # Engine parameter - insert 'footer'
    if {[set footer [Get footer]] != {}} {append res [markup $footer]\n}

    append res [markup </body></html>]
    return $res
}

c_pass 1 fmt_section {name} {c_newSection $name 1 end}
c_pass 2 fmt_section {name} {
    set id [c_sectionId $name]
    return "[markup <h2><]a name=[markup \"]$id[markup \">]$name[markup </a></h2>\n<p>]"
}

c_pass 1 fmt_subsection {name} {c_newSection $name 2 end}
c_pass 2 fmt_subsection {name} {
    set id [c_sectionId $name]
    return "[markup <h3><]a name=[markup \"]$id[markup \">]$name[markup </a></h3>\n<p>]"
}

proc fmt_para {} {return [markup <p>]}

c_pass 2 fmt_require {pkg {version {}}} NOP
c_pass 1 fmt_require {pkg {version {}}} {
    set result "package require [markup <b>]$pkg"
    if {$version != {}} {
	append result " $version"
    }
    append result [markup "</b><br>"]
    c_hold require $result
    return
}

c_pass 2 fmt_usage {cmd args} NOP
c_pass 1 fmt_usage {cmd args} {c_hold synopsis "[trtop][td]$cmd [join $args " "][markup </td></tr>]"}

c_pass 1 fmt_call {cmd args} {
    c_hold synopsis "[trtop][td][markup "<a href=\"#[c_cnext]\">"]$cmd [join $args " "][markup </a></td></tr>]"
}
c_pass 2 fmt_call {cmd args} {
    return "[fmt_lst_item "[markup "<a name=\"[c_cnext]\">"]$cmd [join $args " "][markup </a>]"]\n"
}

c_pass 1 fmt_description {} NOP
c_pass 2 fmt_description {} {
    set result ""
    set syn [c_held synopsis]
    set req [c_held require]

    # Create the TOC.

    # Pass 1: We have a number of special sections which were not
    #         listed explicitly in the document sources. Add them
    #         now. Note the inverse order for the section added
    #         at the beginning.

    c_newSection DESCRIPTION 1 0
    if {$syn != {} || $req != {}} {c_newSection SYNOPSIS 1 0}
    c_newSection {TABLE OF CONTENTS} 1 0

    if {[llength [c_xref_seealso]] > 0}  {c_newSection {SEE ALSO} 1 end}
    if {[llength [c_xref_keywords]] > 0} {c_newSection KEYWORDS   1 end}
    if {[c_get_copyright]         != {}} {c_newSection COPYRIGHT  1 end}

    set sections $::SectionList

    # Pass 2: Generate the markup for the TOC, indenting the
    #         links according to the level of the section.

    append result [fmt_section {TABLE OF CONTENTS}]

    foreach {name id level} $sections {
	append result \
		[markup [string repeat "&nbsp;&nbsp;&nbsp;&nbsp;" $level]] \
		[taga a [list href #$id]] \
		$name \
		[tag/ a][tag br]\n
    }

    # Implicit sections coming after the TOC (Synopsis, then the
    # description which starts the actual document). The other
    # implict sections are added at the end of the document and
    # are generated by 'fmt_manpage_end' in the second pass.

    if {$syn != {} || $req != {}} {
	append result [fmt_section SYNOPSIS]\n
    }
    if {$req != {}} {
	append result $req \n
	append result [markup <br>]
    }
    if {$syn != {}} {
	proc bgcolor {} {return lightyellow}

	append result [btable][tr][td][table]${syn}\n[markup </table></td></tr></table>]\n

	proc bgcolor {} {return ""}
    }
    append result [fmt_section DESCRIPTION]
    return $result
}

################################################################

proc fmt_list_begin  {what {hint {}}} {
    switch -exact -- $what {
	enum        {set tag ol}
	bullet      {set tag ul}
	arg - cmd - opt - tkoption -
	definitions {set tag dl}
    }
    return [if {[llevel]} {limark} else {}][lpush $tag $hint][lsmark 1]
}

proc fmt_list_end {}        {return [lpop][lsmark 1]}
proc fmt_lst_item {text}    {return [limark][tag dt]$text[tag dd][lsmark 1]}
proc fmt_bullet   {}        {return [limark][tag li][lsmark 1]}
proc fmt_enum     {}        {return [limark][tag li][lsmark 1]}
proc fmt_cmd_def  {command} {fmt_lst_item [cmd $command]}

proc fmt_arg_def {type name {mode {}}} {
    set    text ""
    append text "$type [fmt_arg $name]"
    if {$mode != {}} {
	append text " ($mode)"
    }
    fmt_lst_item $text
}
proc fmt_opt_def {name {arg {}}} {
    set text [fmt_option $name]
    if {$arg != {}} {append text " $arg"}
    fmt_lst_item $text
}
proc fmt_tkoption_def {name dbname dbclass} {
    set    text ""
    append text "Command-Line Switch:\t[fmt_option $name][markup <br>]\n"
    append text "Database Name:\t[strong $dbname][markup <br>]\n"
    append text "Database Class:\t[strong $dbclass][markup <br>]\n"
    fmt_lst_item $text
}


################################################################

proc fmt_example_begin {} {
    lsmark 0
    return [markup "<p><table><tr><td bgcolor=black>&nbsp;</td><td><pre class='sample'>"]
}
proc fmt_example_end   {} {
    return [markup "</pre></td></tr></table></p>"]
}
proc fmt_example {code} {
    return "[fmt_example_begin][fmt_plain_text $code][fmt_example_end]"
}

proc fmt_nl {} {
    if {[lcompact]} {return [tag br]}
    return [tag br][tag br]
}
proc fmt_arg    {text} {return "[markup "<i class='arg'>"]$text[markup </i>]" }
proc fmt_cmd    {text} {return "[markup "<b class='cmd'>"][XrefMatch $text sa][markup </b>]" }

proc fmt_emph	{text}	{ em $text }

proc strong {text} {return "[markup <strong>]$text[markup </strong>]"}
proc em     {text} {return "[markup <em>]$text[markup </em>]"}


proc fmt_opt     {text} {return "?$text?" }
proc fmt_comment {text} {ht_comment $text}
proc fmt_sectref {text {label {}}} {
    global SectionNames
    if {![string length $label]} {set label $text}
    set id [c_sectionId $text]
    if {[info exists SectionNames($id)]} {
    	return "[markup <]a href=[markup \"]#$id[markup \">]$label[markup </a>]"
    } else {
    	return "[markup <b>]$label[markup </b>]"
    }
}
proc fmt_syscmd  {text} {strong [XrefMatch $text sa]}
proc fmt_method  {text} {strong $text}
proc fmt_option  {text} {strong $text}
proc fmt_widget  {text} {strong $text}
proc fmt_fun     {text} {strong $text}
proc fmt_type    {text} {strong $text}
proc fmt_package {text} {strong [XrefMatch $text sa kw]}
proc fmt_class   {text} {strong $text}
proc fmt_var     {text} {strong $text}
proc fmt_file    {text} {return "\"[strong $text]\""}
proc fmt_namespace     {text} {strong $text}
proc fmt_uri     {text {label {}}} {
    if {$label == {}} {set label $text}
    return "[markup <]a href=[markup \"]$text[markup \">]$label[markup </a>]"
}
proc fmt_term    {text} {em [XrefMatch $text kw sa]}
proc fmt_const   {text} {strong $text}

################################################################

global xref ; array set xref {}

global    __var
array set __var {
    meta   {}
    header {}
    footer {}
    xref   {}
}
proc Get               {varname}      {global __var ; return $__var($varname)}
proc fmt_listvariables {}             {global __var ; return [array names __var]}
proc fmt_varset        {varname text} {
    global __var
    if {![info exists __var($varname)]} {return -code error "Unknown engine variable \"$varname\""}
    set __var($varname) $text
    return
}

################################################################

proc XrefInit {} {
    global xref __var
    foreach item $__var(xref) {
	foreach {pattern fname fragment} $item break
	set fname_ref [dt_fmap $fname]
	if {$fragment != {}} {append fname_ref #$fragment}
	set xref($pattern) $fname_ref
    }
    proc XrefInit {} {}
    return
}

proc XrefMatch {word args} {
    global xref

    foreach ext $args {
	if {$ext != {}} {
	    if {[info exists xref($ext,$word)]} {
		return [XrefLink $xref($ext,$word) $word]
	    }
	}
    }
    if {[info exists xref($word)]} {
	return [XrefLink $xref($word) $word]
    }

    # Convert the word to all-lower case and then try again.

    set lword [string tolower $word]

    foreach ext $args {
	if {$ext != {}} {
	    if {[info exists xref($ext,$lword)]} {
		return [XrefLink $xref($ext,$lword) $word]
	    }
	}
    }
    if {[info exists xref($lword)]} {
	return [XrefLink $xref($lword) $word]
    }

    return $word
}

proc XrefList {list {ext {}}} {
    set res [list]
    foreach w $list {lappend res [XrefMatch $w $ext]}
    return $res
}

proc XrefLink {dest label} {
    # Ensure that the link is properly done relative to this file!

    set save $dest

    #puts_stderr "XrefLink $dest $label"

    set here [file split [dt_fmap [dt_file]]]
    set dest [file split $dest]

    #puts_stderr "XrefLink < $here"
    #puts_stderr "XrefLink > $dest"

    while {[string equal [lindex $dest 0] [lindex $here 0]]} {
	set dest [lrange $dest 1 end]
	set here [lrange $here 1 end]
	if {[llength $dest] == 0} {break}
    }
    set ul [llength $dest]
    set hl [llength $here]

    if {$ul == 0} {
	set dest [lindex [file split $save] end]
    } else {
	while {$hl > 1} {
	    set dest [linsert $dest 0 ..]
	    incr hl -1
	}
	set dest [eval file join $dest]
    }

    #puts_stderr "XrefLink --> $dest"

    return "[markup "<a href=\"$dest\">"] $label [markup </a>]" ; # "
}