File: fmt.text

package info (click to toggle)
tcllib 1.12-dfsg-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 25,336 kB
  • ctags: 7,235
  • sloc: tcl: 126,727; ansic: 10,090; sh: 9,855; xml: 1,766; yacc: 753; makefile: 127; perl: 84; f90: 84; pascal: 74; python: 33; ruby: 13; php: 11
file content (457 lines) | stat: -rw-r--r-- 12,327 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
# -*- tcl -*-
#
# fmt.text -- Engine to convert a doctools document into plain text.
#
# Copyright (c) 2003 Andreas Kupries <andreas_kupries@sourceforge.net>
#
################################################################
################################################################

# Load shared code and modify it to our needs.

dt_source _common.tcl
dt_source _text.tcl
proc c_copyrightsymbol {} {return "(c)"}

rename fmt_initialize     BaseInitialize
proc   fmt_initialize {} {BaseInitialize ; TextInitialize ; return}

################################################################
# Special manpage environments

proc NewExample {} {
    global currentEnv
    return [NewEnv Example {
	set    currentEnv(verbatim) 1
	append currentEnv(prefix)   "| "
	set    currentEnv(example) .
    }] ; # {}
}

proc Example {} {
    global currentEnv
    if {![info exists currentEnv(exenv)]} {
	SaveContext
	set verb [NewExample]
	RestoreContext

	# Remember verbatim mode in the base environment
	set currentEnv(exenv) $verb
	SaveEnv
    }
    return $currentEnv(exenv)
}

proc NewList {what} {
    # List environments
    # Per list several environments are required.

    switch -exact -- $what {
	enumerated {NewOrderedList}
	itemized   {NewUnorderedList}
	arguments -
	commands  -
	options   -
	tkoptions -
	definitions {NewDefinitionList}
    }
}

proc NewUnorderedList {} {
    global currentEnv lmarginIncrement

    # Itemized list - unordered list - bullet
    # 1. Base environment provides indentation.
    # 2. First paragraph in a list item.
    # 3. All other paragraphs.

    set base [NewEnv Itemized {
	incr currentEnv(lmargin)   $lmarginIncrement

	set bullet [Bullet currentEnv(bulleting)]
    }] ; # {}
    set first [NewEnv First {
	set currentEnv(wspfx) [::textutil::repeat::blank $lmarginIncrement]
	set currentEnv(listtype)  bullet
	set currentEnv(bullet) $bullet
    }] ; SetContext $base ; # {}

    set next [NewEnv Next {
	incr currentEnv(lmargin)   $lmarginIncrement
    }] ; SetContext $base ; # {}

    set currentEnv(_first) $first
    set currentEnv(_next)  $next
    set currentEnv(pcount) 0
    SaveEnv
    return
}

proc NewOrderedList {} {
    global currentEnv lmarginIncrement

    # Ordered list - enumeration - enum
    # 1. Base environment provides indentation.
    # 2. First paragraph in a list item.
    # 3. All other paragraphs.

    set base [NewEnv Enumerated {
	incr currentEnv(lmargin)   $lmarginIncrement

	set bullet [EnumBullet currentEnv(enumeration)]
    }] ; # {}
    set first [NewEnv First {
	set currentEnv(wspfx)  [::textutil::repeat::blank $lmarginIncrement]
	set currentEnv(listtype)  enum
	set currentEnv(bullet) $bullet
    }] ; SetContext $base ; # {}

    set next [NewEnv Next {
	incr currentEnv(lmargin)   $lmarginIncrement
    }] ; SetContext $base ; # {}

    set currentEnv(_first) $first
    set currentEnv(_next)  $next
    set currentEnv(pcount) 0
    SaveEnv
    return
}

proc NewDefinitionList {} {
    global currentEnv lmarginIncrement

    # Definition list - terms & definitions
    # 1. Base environment provides indentation.
    # 2. Term environment
    # 3. Definition environment

    set base [NewEnv DefL {
	incr currentEnv(lmargin)   $lmarginIncrement
    }] ; # {}
    set term [NewEnv Term {
	set currentEnv(verbatim) 1
    }] ; SetContext $base ; # {}

    set def [NewEnv Def {
	incr currentEnv(lmargin) $lmarginIncrement
    }] ; SetContext $base ; # {}

    set currentEnv(_term)       $term
    set currentEnv(_definition) $def
    SaveEnv
    return
}

################################################################
# Final layouting.

c_holdBuffers require

proc fmt_postprocess {text} {text_postprocess $text}


################################################################
# Implementations of the formatting commands.

c_pass 1 fmt_plain_text {text} NOP
c_pass 2 fmt_plain_text {text} {text_plain_text $text}

c_pass 1 fmt_manpage_begin {title section version} NOP
c_pass 2 fmt_manpage_begin {title section version} {
    Off
    set module      [dt_module]
    set shortdesc   [c_get_module]
    set description [c_get_title]
    set copyright   [c_get_copyright]

    set     hdr [list]
    lappend hdr "$title - $shortdesc"
    lappend hdr [c_provenance]
    lappend hdr "[string trimleft $title :]($section) $version $module \"$shortdesc\""
    set     hdr [join $hdr \n]

    Text $hdr
    CloseParagraph [Verbatim]
    Section NAME
    Text "$title - $description"
    CloseParagraph
    return
}

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 {} NOP
c_pass 2 fmt_manpage_end {} {
    set sa [c_xref_seealso]
    set kw [c_xref_keywords]
    set ca [c_xref_category]
    set ct [c_get_copyright]

    CloseParagraph
    if {[llength $sa] > 0} {Section {SEE ALSO} ; Text [join [lsort $sa] ", "] ; CloseParagraph}
    if {[llength $kw] > 0} {Section KEYWORDS   ; Text [join [lsort $kw] ", "] ; CloseParagraph}
    if {$ca ne ""}         {Section CATEGORY   ; Text $ca                     ; CloseParagraph}
    if {$ct != {}}         {Section COPYRIGHT  ; Text $ct ; CloseParagraph [Verbatim]}
    return
}

c_pass 1 fmt_section     {name {id {}}} NOP
c_pass 2 fmt_section     {name {id {}}} {CloseParagraph ; Section $name ; return}

c_pass 1 fmt_subsection  {name {id {}}} NOP
c_pass 2 fmt_subsection  {name {id {}}} {CloseParagraph ; Subsection $name ; return}

c_pass 1 fmt_para {} NOP
c_pass 2 fmt_para {} {CloseParagraph ; return}

c_pass 2 fmt_require {pkg {version {}}} NOP
c_pass 1 fmt_require {pkg {version {}}} {
    set result "package require $pkg"
    if {$version != {}} {append result " $version"}
    c_hold require $result
    return
}

c_pass 1 fmt_usage {cmd args} {c_hold synopsis "$cmd [join $args " "]"}
c_pass 2 fmt_usage {cmd args} NOP

c_pass 1 fmt_call  {cmd args} {c_hold synopsis "$cmd [join $args " "]"}
c_pass 2 fmt_call  {cmd args} {fmt_lst_item "$cmd [join $args " "]"}


c_pass 1 fmt_description {id} NOP
c_pass 2 fmt_description {id} {
    On
    set syn [c_held synopsis]
    set req [c_held require]

    if {$syn != {} || $req != {}} {
	Section SYNOPSIS
	if {($req != {}) && ($syn != {})} {
	    Text $req\n\n$syn
	} else {
	    if {$req != {}} {Text $req}
	    if {$syn != {}} {Text $syn}
	}
	CloseParagraph [Verbatim]
    }

    Section DESCRIPTION
    return
}

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

c_pass 1 fmt_list_begin {what {hint {}}} NOP
c_pass 2 fmt_list_begin {what {hint {}}} {
    #puts_stderr "<<fmt_list_begin $what>>"

    global currentEnv
    if {[info exists currentEnv(_definition)]} {
	CloseParagraph $currentEnv(_definition)
    } elseif {[info exists currentEnv(pcount)]} {
	if {$currentEnv(pcount) == 0} {CloseParagraph $currentEnv(_first)}
	if {$currentEnv(pcount) >  0} {CloseParagraph $currentEnv(_next)}
	incr currentEnv(pcount)
    } else {
	CloseParagraph
    }
    SaveContext
    NewList $what
    Off

    #puts_stderr "<<fmt_list_begin _____>>"
    return
}

c_pass 1 fmt_list_end {} NOP
c_pass 2 fmt_list_end {} {
    #puts_stderr "<<fmt_list_end>>"

    global currentEnv
    if {[info exists currentEnv(_definition)]} {
	CloseParagraph $currentEnv(_definition)
    } else {
	if {$currentEnv(pcount) == 0} {CloseParagraph $currentEnv(_first)}
	if {$currentEnv(pcount) >  0} {CloseParagraph $currentEnv(_next)}
    }
    RestoreContext

    #puts_stderr "<<fmt_list_end ____>>"
    return
}

c_pass 1 fmt_lst_item {text} NOP
c_pass 2 fmt_lst_item {text} {
    global currentEnv

    #puts_stderr "<<fmt_lst_item \{$text\}>>"

    if {[IsOff]} {
	On
    } else {
	CloseParagraph $currentEnv(_definition)
    }
    Text $text
    CloseParagraph $currentEnv(_term)

    #puts_stderr "<<fmt_lst_item _____>>"
    return
}

c_pass 1 fmt_bullet {} NOP
c_pass 2 fmt_bullet {} {
    global currentEnv
    if {[IsOff]} {On ; return}
    if {$currentEnv(pcount) == 0} {CloseParagraph $currentEnv(_first)}
    if {$currentEnv(pcount) >  0} {CloseParagraph $currentEnv(_next)}
    set  currentEnv(pcount) 0
    return
}

c_pass 1 fmt_enum {} NOP
c_pass 2 fmt_enum {} {
    global currentEnv
    if {[IsOff]} {On ; return}
    if {$currentEnv(pcount) == 0} {CloseParagraph $currentEnv(_first)}
    if {$currentEnv(pcount) >  0} {CloseParagraph $currentEnv(_next)}
    set  currentEnv(pcount) 0
    return
}

c_pass 1 fmt_cmd_def  {command} NOP
c_pass 2 fmt_cmd_def  {command} {fmt_lst_item [fmt_cmd $command]}

c_pass 1 fmt_arg_def {type name {mode {}}} NOP
c_pass 2 fmt_arg_def {type name {mode {}}} {
    set text "$type [fmt_arg $name]"
    if {$mode != {}} {append text " ($mode)"}
    fmt_lst_item $text
    return
}

c_pass 1 fmt_opt_def {name {arg {}}} NOP
c_pass 2 fmt_opt_def {name {arg {}}} {
    set text [fmt_option $name]
    if {$arg != {}} {append text " $arg"}
    fmt_lst_item $text
    return
}

c_pass 1 fmt_tkoption_def {name dbname dbclass} NOP
c_pass 2 fmt_tkoption_def {name dbname dbclass} {
    set    text ""
    append text "Command-Line Switch:\t[fmt_option $name]\n"
    append text "Database Name:\t[strong $dbname]\n"
    append text "Database Class:\t[strong $dbclass]\n"
    fmt_lst_item $text
}

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

c_pass 1 fmt_example_begin {} NOP
c_pass 2 fmt_example_begin {} {
    global currentEnv para
    if {[info exists currentEnv(_definition)]} {
	CloseParagraph $currentEnv(_definition)
    } elseif {[info exists currentEnv(pcount)]} {
	if {$para != {}} {
	    if {$currentEnv(pcount) == 0} {CloseParagraph $currentEnv(_first)}
	    if {$currentEnv(pcount) >  0} {CloseParagraph $currentEnv(_next)}
	    incr currentEnv(pcount)
	}
    } else {
	CloseParagraph
    }
    return
}

c_pass 1 fmt_example_end {} NOP
c_pass 2 fmt_example_end {} {
    global currentEnv para
    set penv {}
    if {[info exists currentEnv(_definition)]} {
	set penv $currentEnv(_definition)
    } elseif {[info exists currentEnv(pcount)]} {
	if {$currentEnv(pcount) == 0} {set penv $currentEnv(_first)}
	if {$currentEnv(pcount) >  0} {set penv $currentEnv(_next)}
	incr currentEnv(pcount)
    }
    if {$penv != {}} {
	# Save current list context, get chosen paragraph context and
	# then create an example context form this. After closing the
	# paragraph we get back our main list context.

	SaveContext
	SetContext $penv
	CloseParagraph [Example]
	RestoreContext
    } else {
	CloseParagraph [Example]
    }
    return
}

c_pass 1 fmt_example {code} NOP
c_pass 2 fmt_example {code} {
    fmt_example_begin
    fmt_plain_text $code
    fmt_example_end
    return
}

c_pass 1 fmt_nl {} NOP
c_pass 2 fmt_nl {} {
    global currentEnv
    if {[info exists currentEnv(_definition)]} {
	CloseParagraph $currentEnv(_definition)
    } else {
	if {$currentEnv(pcount) == 0} {CloseParagraph $currentEnv(_first)}
	if {$currentEnv(pcount) >  0} {CloseParagraph $currentEnv(_next)}
	incr currentEnv(pcount)
    }
    return
}

################################################################
# Visual markup of words and phrases.

proc fmt_arg     {text} {return $text}
proc fmt_cmd     {text} {return $text}
proc fmt_emph	 {text} {em     $text }
proc fmt_opt     {text} {return "?$text?" }
proc fmt_comment {text} {return}
proc fmt_sectref {text {label {}}} {
    if {![string length $label]} {set label $text}
    return "-> $text"
}
proc fmt_syscmd  {text} {strong $text}
proc fmt_method  {text} {return $text}
proc fmt_option  {text} {return $text}
proc fmt_widget  {text} {strong $text}
proc fmt_fun     {text} {strong $text}
proc fmt_type    {text} {strong $text}
proc fmt_package {text} {strong $text}
proc fmt_class   {text} {strong $text}
proc fmt_var     {text} {strong $text}
proc fmt_file    {text} {return "\"$text\""}
proc fmt_namespace     {text} {strong $text}
proc fmt_uri     {text {label {}}} {
    if {$label == {}} {
	# Without label we use the link directly as part of the text.
	return "<URL:$text>"
    } else {
	return "[em $label] <URL:$text>"
    }
}
proc fmt_term    {text} {em     $text}
proc fmt_const   {text} {strong $text}

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