File: fmt.latex

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 (431 lines) | stat: -rw-r--r-- 11,175 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
# -*- tcl -*-
# (c) 2001-2019 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# [mpexpand] definitions to convert a tcl based manpage definition into
# a manpage based upon LaTeX markup.
#
################################################################

##
## This engine needs a rewrite for a better handling
## of characters special to TeX / LaTeX.
##

dt_source _common.tcl   ; # Shared code

global _in_example
set    _in_example 0

global _has_images
set    _has_images 0

global _has_examples
set    _has_examples 0

# Called to handle plain text from the input
proc fmt_plain_text {text} {
    global _in_example
    if {$_in_example} {	
	lappend map \\\\\n \1\\textbackslash\n
	lappend map \\\n   \1\\textbackslash\n	
	set text [string map $map $text]
	return $text
    }
    return [texEscape $text]
}

proc Year {} {clock format [clock seconds] -format %Y}

c_holdBuffers require

proc fmt_postprocess {text} {
    # Trim trailing whitespace, for all lines.
    regsub -all -- "\[ \t]+\n" $text "\n" text
    # Strip superfluous line breaks
    regsub -all -- "\n+" $text "\n" text
    # Normalize paragraph breaks (subsume leading and traling whitespace)
    regsub -all -- "\[\t \n]+\1@P" $text "\1@P" text
    regsub -all -- "\1@P\[\t \n]+" $text "\1@P" text
    # Insert the paragraph breaks, unify multiple adjacent breaks
    regsub -all -- "(\1@P)+" $text "\n\n" text
    # Finalize the protected special characters
    return [string trimleft [string map {\1\\ \\ \1$ $} $text]]
    #return $text
}

################################################################
## Backend for LaTeX markup

c_pass 1 fmt_manpage_begin {title section version} NOP
c_pass 2 fmt_manpage_begin {title section version} {
    global _has_images _has_examples

    set module      [dt_module]
    set shortdesc   [c_get_module]
    set description [c_get_title]
    set copyright   [c_get_copyright]

    set     hdr ""
    append  hdr [Comment [c_provenance]] \n
    if {$copyright != {}} {
	append  hdr [Comment $copyright] \n
    }
    append  hdr [Comment "$title.$section"] \n
    append  hdr \n
    append  hdr "\1\\documentclass\{article\}" \n

    if {$_has_images} {
	append hdr "\1\\usepackage{epsfig}" \n
	append hdr "\1\\usepackage{epstopdf}" \n
    }
    if {$_has_examples} {
	append hdr "\1\\usepackage{alltt}" \n
    }

    append  hdr "\1\\begin\{document\}" \n
    append  hdr "\1\\author\{[dt_user]\}" \n

    set    titletext ""
    append titletext "$module / $title -- "
    append titletext "$shortdesc : $description"

    append  hdr "\1\\title\{[texEscape $titletext]\}" \n
    append  hdr "\1\\maketitle" \n
    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 [texEscape $desc]}
c_pass 2 fmt_copyright {desc} NOP

c_pass 1 fmt_manpage_end {} NOP
c_pass 2 fmt_manpage_end {} {
    set    res ""

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

    if {[llength $sa] > 0} {
	set tmp {}
	foreach x $sa {lappend tmp [texEscape $x]}
	set sa $tmp

	append res [fmt_section {See Also} see-also] \n
	append res [join [lsort $sa] ", "] \n
    }
    if {[llength $kw] > 0} {
	set tmp {}
	foreach x $kw {lappend tmp [texEscape $x]}
	set kw $tmp

	append res [fmt_section Keywords keywords] \n
	append res [join [lsort $kw] ", "] \n
    }
    if {$ca ne ""} {
	set ca [texEscape $ca]

	append res [fmt_section Category category] \n
	append res $ca \n
    }
    if {$ct != {}} {
	append res [fmt_section Copyright copyright] \n
	append res \1\\begin\{flushleft\} \n
	append res [join [split $ct \n] \1\\linebreak\n] \1\\linebreak\n
	append res \1\\end\{flushleft\} \n
    }
    append  res "\1\\end\{document\}"
    return $res
}

proc fmt_section    {name id} {return    "\1\\section\{[texEscape $name]\}\1\\label\{$id\}"}
proc fmt_subsection {name id} {return "\1\\subsection\{[texEscape $name]\}\1\\label\{$id\}"}

proc fmt_para   {} { return \1@P }
proc fmt_nl     {} { return \1@P }

c_pass 2 fmt_require {pkg {version {}}} NOP
c_pass 1 fmt_require {pkg {version {}}} {
    if {$version != {}} {
	set res "package require [Bold "$pkg $version"]\n"
    } else {
	set res "package require [Bold $pkg]\n"
    }
    c_hold require $res
    return
}

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

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

c_pass 1 fmt_description {id} NOP
c_pass 2 fmt_description {id} {
    set res ""
    set req [c_held require]
    set syn [c_held synopsis]
    if {$req != {} || $syn != {}} {
	append res [fmt_section Synopsis synopsis]\n
	if {$req != {}} {
	    append res \1\\begin\{flushleft\} \n
	    append res $req \n
	    append res \1\\end\{flushleft\} \n
	}
	if {$syn != {}} {
	    append res "\1\\begin\{itemize\}" \n
	    append res ${syn} \n\n
	    append res "\1\\end\{itemize\}" \n
	}
    }
    append res [fmt_section Description $id]
    return $res
}

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

global    list_state
array set list_state {level -1}

proc fmt_list_begin  {what {hint {}}} {
    # ignoring hints
    global list_state
    incr list_state(level)
    set  list_state(l,$list_state(level)) $what
    set  list_state(l,$list_state(level),item) 0

    switch -exact -- $what {
	enumerated {
	    return \1\\begin\{enumerate\}
	}
	itemized -
	arguments -
	options -
	commands -
	tkoptions -
	definitions {
	    return \1\\begin\{itemize\}
	}
	default {
	    return -code error "Must not happen"
	}
    }
}

proc fmt_list_end {} {
    global list_state

    set what $list_state(l,$list_state(level))
    set item $list_state(l,$list_state(level),item)

    catch {unset list_state(l,$list_state(level))}
    catch {unset list_state(l,$list_state(level),item)}

    incr list_state(level) -1

    switch -exact -- $what {
	enumerated {
	    return \1\\end\{enumerate\}
	}
	itemized -
	arguments -
	options -
	commands -
	tkoptions -
	definitions {
	    return \1\\end\{itemize\}
	}
	default {
	    return -code error "Must not happen"
	}
    }
}

proc fmt_bullet {} {return "\n%\n\1\\item\n%\n"}
proc fmt_enum   {} {return "\n%\n\1\\item\n%\n"}

proc fmt_lst_item {text} {
    global list_state

    set item $list_state(l,$list_state(level),item)
    set list_state(l,$list_state(level),item) 1

    set text [texEscape $text]
    return "\n%\n\1\\item\[\] $text\n%\n"
}

proc fmt_arg_def {type name {mode {}}} {
    global list_state

    set item $list_state(l,$list_state(level),item)
    set list_state(l,$list_state(level),item) 1

    set    text ""
    append text [fmt_arg $name]
    append text " $type"
    if {$mode != {}} {append text " ($mode)"}
    return "\n%\n\1\\item\[\] $text\n%\n"
}

proc fmt_cmd_def {command} {
    global list_state

    set item $list_state(l,$list_state(level),item)
    set list_state(l,$list_state(level),item) 1

    set text [fmt_cmd $command]
    return "\n%\n\1\\item\[\] $text\n%\n"
}

proc fmt_opt_def {name {arg {}}} {
    global list_state

    set item $list_state(l,$list_state(level),item)
    set list_state(l,$list_state(level),item) 1

    set text [fmt_option $name]
    if {$arg != {}} {append text " $arg"}
    return "\n%\n\1\\item\[\] $text\n%\n"
}

proc fmt_tkoption_def {name dbname dbclass} {
    global list_state

    set item $list_state(l,$list_state(level),item)
    set list_state(l,$list_state(level),item) 1

    set    text ""
    append text "Command-Line Switch:	[Bold $name]\\\\\n"
    append text "Database Name:	[Bold $dbname]\\\\\n"
    append text "Database Class:	[Bold $dbclass]\\\\\n"
    return "\n%\n\1\\item\[\] $text\n%\n"
}

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

proc fmt_example_begin {} {
    global _has_examples _in_example
    set _has_examples 1
    set    _in_example 1
    return "\1\\begin{alltt}\n"
}
proc fmt_example_end   {} {
    global _in_example
    set    _in_example 0
    return "\1\\end{alltt}\n"
}
# No mapping of special characters
proc fmt_example {code} {
    return [fmt_example_begin][fmt_plain_text $code][fmt_example_end]
}

proc fmt_arg    {text} {Underline $text}
proc fmt_cmd    {text} {Bold      $text}
proc fmt_emph   {text} {Italic    $text}
proc fmt_opt    {text} {return   ?$text?}

proc fmt_comment {text} {
    set res [list]
    foreach l [split $text \n] {
	lappend res [Comment $l]
    }
    return [join $res \n]
}
proc fmt_sectref {text {label {}}} {
    if {![string length $label]} {set label $text}
    Bold "$text (\1\\ref\{$label\})"
}
proc fmt_syscmd  {text} {Bold $text}
proc fmt_method  {text} {Bold $text}
proc fmt_option  {text} {Bold $text}
proc fmt_widget  {text} {Bold $text}
proc fmt_fun     {text} {Bold $text}
proc fmt_type    {text} {Bold $text}
proc fmt_package {text} {Bold $text}
proc fmt_class   {text} {Bold $text}
proc fmt_var     {text} {Bold $text}
proc fmt_file    {text} {return "\"[Italic $text]\""}
proc fmt_namespace     {text} {Bold $text]}
proc fmt_uri     {text {label {}}} {
    if {$label == {}} {
	# Without label we use the link directly as part of the text.
	return [Underline $text]
    } else {
	# Label is used in the text, referred link is delegated into a
	# footnote.
	return "[Underline $label] \1\\footnote\{[texEscape $text]\}"
    }
}
proc fmt_image {text {label {}}} {
    global _has_images
    # text = symbolic name of the image.

    set img [dt_imgsrc $text {eps ps}]
    if {$img eq {}} {
	if {$label eq {}} {
	    return [Underline "IMAGE: $text"]
	} else {
	    return [Underline "IMAGE: $text $label"]
	}
    }

    set _has_images 1

    return "\1\\begin{figure}\[htp\]\1\\includegraphics\[width=0.9\1\\textwidth\]{$img}\1\\end{figure}"
}
proc fmt_term    {text} {Italic $text}
proc fmt_const   {text} {Bold $text}
proc fmt_mdash {} { return " --- " }
proc fmt_ndash {} { return " -- " }

################################################################
# latex specific commands

proc Comment   {text} {return "% [join [split $text \n] "\n% "]"}
proc Bold      {text} {return "\{\1\\bf [texEscape $text]\}"}
proc Italic    {text} {return "\{\1\\it [texEscape $text]\}"}
proc Underline {text} {return "\1\\underline\{[texEscape $text]\}"}

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

proc texEscape {text} {
    set x 0
    if {[string match *%* $text]} {
	#puts_stderr '$text'
	set x 1
    }

    # Important: \1 protected sequences are left unchanged, they are already escaped.
    set text [string map {
        \1$\\backslash$ \1$\\backslash$ \1$<$ \1$<$ \1$>$ \1$>$
	\1\\_ \1\\_
	\1\\% \1\\%
	\1\\^ \1\\^
	\1\\$ \1\\$
	\1\\# \1\\#
	\1\\& \1\\&
	\1\\ \1\\
	\\ \1$\\backslash$
	_ \1\\_
	% \1\\%
	^ \1\\^
	$ \1\\$
	< \1$<$
	> \1$>$
	# \1\\#
	& \1\\&
    } $text]
    if {$x} {
	#puts_stderr "==> '$text'"
    }
    return $text
}

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