File: fmt.text

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (565 lines) | stat: -rw-r--r-- 13,394 bytes parent folder | download | duplicates (2)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# -*- tcl -*-
#
# -- Engine to convert a doctools document into plain text.
#
# Copyright (c) 2003-2019 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)"}

# # ## ### ##### ########
## Special manpage contexts

# example     = if present, context is for an example
# exenv       = if present, reference to example variant of current context

proc Example!  {} { CAttrSet example . }
proc Example?  {} { CAttrHas example }

proc NewExample {} {
    return [ContextNew Example { VerbatimOn ; Example! ; Prefix! "| " }] ; # {}
}

proc Example {} {
    if {![CAttrHas exenv]} {
	ContextPush
	set exenv [NewExample]
	ContextPop
	CAttrSet exenv $exenv
	ContextCommit
    }
    return [CAttrGet exenv]
}

proc NewList {what} {
    # List contexts
    # Per list type several contexts are required.
    #puts_stderr "LIST OUT [CAttrName] NewList $what"
    
    switch -exact -- $what {
	enumerated {NewOrderedList}
	itemized   {NewUnorderedList}
	arguments -
	commands  -
	options   -
	tkoptions -
	definitions {NewDefinitionList}
    }

    #puts_stderr "LIST INN [CAttrName]"
    return
}

proc NewUnorderedList {} {
    # Itemized list - unordered list - bullet
    # 1. Base context provides indentation.
    # 2. First paragraph in a list item.
    # 3. All other paragraphs.
    ContextPush

    set base [ContextNew Itemized {
	LC
	MarginIn
	set bullet [IBullet]
    }] ; # {}

    set first [ContextNew First {
	List! bullet $bullet [BlankMargin]
    }] ; ContextSet $base ; # {}

    set next [ContextNew Next {
	MarginIn
    }] ; ContextSet $base ; # {}

    OUL $first $next
    ContextCommit

    ContextPop
    ContextSet $base
    return
}

proc NewOrderedList {} {
    # Ordered list - enumeration - enum
    # 1. Base context provides indentation.
    # 2. First paragraph in a list item.
    # 3. All other paragraphs.
    ContextPush
    
    set base [ContextNew Enumerated {
	LC
	MarginIn
	set bullet [EBullet]
    }] ; # {}

    set first [ContextNew First {
	List! enum $bullet [BlankMargin]
    }] ; ContextSet $base ; # {}

    set next [ContextNew Next {
	MarginIn
    }] ; ContextSet $base ; # {}

    OUL $first $next
    ContextCommit

    ContextPop
    ContextSet $base
    return
}

proc NewDefinitionList {} {
    # Definition list - terms & definitions
    # 1. Base context provides indentation.
    # 2. Term context
    # 3. Definition context
    ContextPush

    set base [ContextNew Definitions {
	LC
	MarginIn
    }] ; # {}

    set term [ContextNew Term {
	VerbatimOn
    }] ; ContextSet $base ; # {}

    set def [ContextNew Def {
	MarginIn
    }] ; ContextSet $base ; # {}

    TD $term $def
    ContextCommit
    
    ContextPop
    ContextSet $base
    return
}

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

proc LC {} {
    # Clear inherited list type information from the current context
    CAttrUnset _first
    CAttrUnset _next
    CAttrUnset _term
    CAttrUnset _definition
}

proc OUL {f n} {
    CAttrSet _first $f
    CAttrSet _next  $n
}

proc TD {t d} {
    CAttrSet _term       $t
    CAttrSet _definition $d
}

proc IsPara   {} { CAttrHas _next }
proc Paras    {} { global __pcount ; set  __pcount }
proc PAdvance {} {
    global __pcount ; incr __pcount 1
    #puts_stderr "    ZZZ/ItemParas/Advance:$__pcount"
}
proc PReset   {} {
    #puts_stderr "    ZZZ/ItemParas/Reset"
    global __pcount ; set  __pcount 0 }

global __pcount  ; set __pcount  0
global __pcstack ; set __pcstack {}

proc PSave {} {
    global __pcount __pcstack
    lappend __pcstack $__pcount
    PReset
}

proc PRestore {} {
    global __pcount __pcstack
    set __pcount  [lindex $__pcstack end]
    set __pcstack [lrange $__pcstack 0 end-1]
    #puts_stderr "    ZZZ/ItemParas/Restore:$__pcount"
    return
}

proc First    {} { CAttrGet _first }
proc Other    {} { CAttrGet _next }
proc Term     {} { CAttrGet _term }
proc Def      {} { CAttrGet _definition }
proc IsDef    {} { CAttrHas _definition }

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

proc CloseCurrent {} {
    if {[IsDef]} {
	#puts_stderr "    ZZZ/CloseCurrent/Definitions"
	# Currently in a definition list.
	CloseParagraph [Def]
    } elseif {[IsPara]} {
	#puts_stderr "    ZZZ/CloseCurrent/UOL"
	# Currently in an (un)ordered list.
	#puts_stderr "    ZZZ/ItemParas/[Paras]"
	if {![Paras]} {
	    # No paragraphs yet, this is first in the item
	    if {[CloseParagraph [First]]} PAdvance
	} else {
	    # More paragraphs in the item
	    if {[CloseParagraph [Other]]} PAdvance
	}
    } else {
	#puts_stderr "    ZZZ/CloseCurrent/Plain"
	# Currently in a regular paragraph
	CloseParagraph
    }
}

proc GetCurrent {} {
    if {[IsDef]} {
	#puts_stderr "    ZZZ/GetCurrent/Definitions"
	# Currently in a definition list.
	return [Def]
    } elseif {[IsPara]} {
	#puts_stderr "    ZZZ/GetCurrent/UOL"
	# Currently in an (un)ordered list.
	#puts_stderr "    ZZZ/ItemParas/[Paras]"
	if {![Paras]} {
	    set res [First]
	} else {
	    set res [Other]
	}
	PAdvance
	return $res
    } else {
	#puts_stderr "    ZZZ/GetCurrent/Plain"
	# Currently in a regular paragraph
	return {}
    }
}

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

c_holdBuffers require

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

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     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 {} {
    CloseCurrent
    #puts_stderr "AAA/fmt_para/START"
    return
}

# NL is an alias of PARA
# See also fmt_example_begin
c_pass 1 fmt_nl {} NOP
c_pass 2 fmt_nl {} {CloseCurrent ; 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 {}}} {
    CloseCurrent

    #puts_stderr "AAA/fmt_list_begin/Setup $what"

    ContextPush ;# push outer
    NewList $what ;# base/controller of current/new/inner
    Off
    PSave

    #puts_stderr "AAA/fmt_list_begin/Enter"
    return
}

c_pass 1 fmt_list_end {} NOP
c_pass 2 fmt_list_end {} {
    CloseCurrent

    #puts_stderr "AAA/fmt_list_end/Exit"

    ContextPop ;# return to outer
    PRestore

    #puts_stderr "AAA/fmt_list_end/Done"
    return
}

c_pass 1 fmt_lst_item {text} NOP
c_pass 2 fmt_lst_item {text} {
    if {[IsOff]} { On } else { CloseParagraph [Def] }

    #puts_stderr "AAA/fmt_lst_item/(($text))"

    Text $text
    CloseParagraph [Term]

    #puts_stderr "AAA/fmt_lst_item/Done"
    return
}

c_pass 1 fmt_bullet {} NOP
c_pass 2 fmt_bullet {} {
    if {[IsOff]} { On } else { CloseCurrent }

    #puts_stderr "AAA/fmt_bullet/START"
    PReset
    return
}

c_pass 1 fmt_enum {} NOP
c_pass 2 fmt_enum {} {
    if {[IsOff]} { On } else { CloseCurrent }

    #puts_stderr "AAA/fmt_enum/START"
    PReset
    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 {} {
    #puts_stderr "AAA/fmt_example_begin"

    CloseCurrent

    #puts_stderr "AAA/fmt_example_begin/Done"
    return
}

c_pass 1 fmt_example_end {} NOP
c_pass 2 fmt_example_end {} {
    #puts_stderr "AAA/fmt_example_end"
    #puts_stderr AAA/EIN=[string map [list \1 \\1 \t \\t { } \\s] <<[join [split [Text?] \n] >>\n<<]>>]

    # Flush markup from preceding commands into the text buffer.
    TextPlain ""
    
    TextTrimLeadingSpace

    # Look for and convert continuation lines protected from Tcl
    # substitution into a regular continuation line.
    set t [string map [list \\\\\n \\\n] [Text?]]
    TextClear
    Text $t

    #puts_stderr AAA/EFT=[string map [list \1\\1 \t \\t { } \\s] <<[join [split [Text?] \n] >>\n<<]>>]
    
    set penv [GetCurrent]
    if {$penv != {}} {
	# In a list we save the current list context, activate the
	# proper paragraph context and create its example
	# variant. After closing the paragraph using the example we
	# restore and reactivate the list context.
	ContextPush
	ContextSet $penv
	#if {[CloseParagraph [Example]]} PAdvance
	CloseParagraph [Example]
	ContextPop
    } else {
	# In a regular paragraph we simple close the example
	#if {[CloseParagraph [Example]]} PAdvance
	CloseParagraph [Example]
    }

    #puts_stderr "AAA/fmt_example_end/Done"
    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
}

# # ## ### ##### ########
## 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_image {text {label {}}} {
    # text = symbolic name of the image.

    set img [dt_imgdata $text {txt}]
    if {$img != {}} {
	if {$label == {}} {
	    return "IMAGE: $text"
	} else {
	    return "IMAGE: $text $label"
	}
    }

    return $img
}

proc fmt_term    {text} { Em     $text }
proc fmt_const   {text} { Strong $text }
proc fmt_mdash {} { return " --- " }
proc fmt_ndash {} { return " -- " }

# # ## ### ##### ########
return