File: export_html.tcl

package info (click to toggle)
tcllib 1.21%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 69,456 kB
  • sloc: tcl: 266,493; ansic: 14,259; sh: 2,936; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 112; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (421 lines) | stat: -rw-r--r-- 10,586 bytes parent folder | download | duplicates (7)
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
# text.tcl --
#
#	The HTML export plugin. Generation of HTML markup.
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# 
# RCS: @(#) $Id: export_html.tcl,v 1.3 2009/08/07 18:53:11 andreas_kupries Exp $

# This package is a plugin for the doctools::idx v2 system.  It takes
# the list serialization of a keyword index and produces text in HTML
# format.

# ### ### ### ######### ######### #########
## Requisites

# @mdgen NODEP: doctools::idx::export::plugin

package require Tcl 8.4
package require doctools::idx::export::plugin ; # Presence of this
						# pseudo package
						# indicates execution
						# inside of a properly
						# initialized plugin
						# interpreter.
package require doctools::idx::structure ; # Verification that the
					   # input is proper.
package require doctools::html
package require doctools::html::cssdefaults

doctools::html::import ;# -> ::html::*

# ### ### ### ######### ######### #########
## API. 

proc export {serial configuration} {

    # Phase I. Check that we got a canonical index serialization. That
    #          makes the unpacking easier, as we can mix it with the
    #          generation of the output, knowing that everything is
    #          already sorted as it should be.

    ::doctools::idx::structure verify-as-canonical $serial

    # ### ### ### ######### ######### #########
    # Configuration ...
    # * Standard entries
    #   - user   = person running the application doing the formatting
    #   - format = name of this format
    #   - file   = name of the file the index came from. Optional.
    #   - map    = maps symbolic references to actual file path. Optional.

    # * HTML specific entries
    #   - newlines = boolean. tags separated by eol markers
    #   - indented = boolean. tags indented per their nesting structure.
    #   //layout   = string in { list, table }.
    #
    #   - meta   = HTML fragment for use within the document <meta> section.
    #   - header = HTML fragment used immediately after <body>
    #   - footer = HTML fragment used immediately before </body>
    #
    #   - kwid   = dictionary mapping keywords to link anchor names.
    #     <=> KeyWord IDentifier
    # 
    # Notes
    # * indented => newlines

    # Import the configuration and initialize the internal state
    #// layout    list
    array set config {
	newlines  0
	indented  0
	meta      {}
	header    {}
	footer    {}
	kwid      {}
	map       {}
	sepline   ------------------------------------------------------------
	kwidth         35
	dot            {&#183;}
	class.main     doctools
	class.header   idx-header
	class.title    idx-title
	class.navsep   idx-navsep
	class.navbar   idx-kwnav
	class.contents idx-contents
	class.leader   idx-leader
	class.row0     idx-even
	class.row1     idx-odd
	class.keyword  idx-keyword
	class.refs     idx-refs
	class.footer   idx-footer
    }
    array set config $configuration
    array set map    $config(map)
    array set kwid   $config(kwid)

    if {($config(kwidth) < 1) || ($config(kwidth) > 99)} {
	set config(kwidth) 35
    }
    set config(rwidth) [expr {100 - $config(kwidth)}]


    # Force the implications mentioned in the notes above.
    if {$config(indented)} {
	set config(newlines) 1
    }

    # Allow structuring comments iff structure is present.
    set config(comments) [expr {$config(indented) || $config(newlines)}]

    array set anchor {}
    set dot {&#183;}

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

    # Phase II. Generate the output, taking the configuration into
    #           account.

    # Unpack the serialization.
    array set idx $serial
    array set idx $idx(doctools::idx)
    unset     idx(doctools::idx)
    array set r $idx(references)
    array set k $idx(keywords)

    html::begin
    # Configure the layouting
    if {!$config(indented)} { html::indenting 0 }
    if {!$config(newlines)} { html::newlines  0 }

    html::tag* html {
	html::newline ; html::indented 4 {
	    Header
	    Provenance
	    Body
	}
    }

    return [html::done]
}

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

proc Header {} {
    upvar 1 config config idx idx
    html::tag* head {
	html::newline ; html::indented 4 {
	    html::tag= title [Title] ; html::newline
	    if {![Extend meta]} {
		html::tag* style {
		    DefaultStyle
		} ; html::newline
	    }
	}
    } ; html::newline
    return
}

proc Provenance {} {
    upvar 1 config config
    if {!$config(comments)} return
    html::comment [html::collect {
	html::indented 4 {
	    html::+  "Generated @ [clock format [clock seconds]]" ; html::newline
	    html::+  "By          $config(user)"                  ; html::newline
	    if {[info exists config(file)] && ($config(file) ne {})} {
		html::+ "From file   $config(file)" ; html::newline
	    }
	}
    }] ; html::newline
    return
}

proc Body {} {
    upvar 1 config config idx idx dot dot anchor anchor kwid kwid k k r r
    html::tag* body {
	html::newline ; html::indented 4 {
	    html::tag* div class $config(class.main) {
		html::newline ; html::indented 4 {
		    html::tag* div class $config(class.header) {
			html::newline ; html::indented 4 {
			    BodyTitle
			    UserHeader
			    html::tag1 hr class $config(class.navsep) ; html::newline
			    NavigationBar
			}
		    } ;	html::newline
		    Keywords
		    html::tag* div class $config(class.footer) {
			html::newline ; html::indented 4 {
			    html::tag1 hr class $config(class.navsep) ; html::newline
			    UserFooter
			}
		    } ; html::newline
		}
	    } ; html::newline
	}
    } ; html::newline
    return
}

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

proc BodyTitle {} {
    upvar 1 idx idx config config
    html::tag= h1 class $config(class.title) [Title] ; html::newline
    return
}

proc UserHeader {} {
    upvar 1 config config
    Extend header
    html::newline
    return
}

proc UserFooter {} {
    upvar 1 config config
    Extend footer
    html::newline
    return
}

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

proc Title {} {
    upvar 1 idx(label) label idx(title) title
    if {($label ne {}) && ($title ne {})} {
	return "$label -- $title"
    } elseif {$label ne {}} {
	return $label
    } elseif {$title ne {}} {
	return $title
    }
    return -code error {Reached the unreachable}
}

proc DefaultStyle {} {
    html::comment \n[doctools::html::cssdefaults::contents]
    return
}

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

proc NavigationBar {} {
    upvar 1 config config idx idx anchor anchor kwid kwid char char

    # No navigation bar for an empty index.

    if {![llength $idx(keywords)]} return

    # Name each keyword, if that was not done already. And sort them
    # into bins based on their first character (always taken as upper
    # case, i.e. X and x are the same).

    foreach {keyword references} $idx(keywords) {
	if {![info exists kwid($keyword)]} {
	    set kwid($keyword) KW-$keyword
	}
	lappend char([string toupper [string index $keyword 0]]) $keyword
    }

    # Now name each character

    set counter 0
    foreach c [lsort -dict [array names char]] {
	set anchor($c) KEYWORDS-$c
	incr counter
    }

    # Now we have the information we can construct the nav bar from.

    # NOTE: Should I do this as ul/ ?  Then the CSS can select the
    # location of the navbar, its orientation, and how the elements
    # are joined. Right ?!

    Separator {Navigation Bar}
    html::newline
    set sep 0
    html::tag* div class $config(class.navbar) {
	html::newline ; html::indented 4 {
	    foreach c [lsort -dict [array names char]] {
		if {$sep} {
		    html::++ " $config(dot)"
		    if {![html::newline]} { html::++ " " }
		}
		html::tag= a href #$anchor($c) $c
		set sep 1
	    }
	    html::newline
	}
    } ; html::newline
    return
}

proc Keywords {} {
    upvar 1 config config idx idx anchor anchor dot dot kwid kwid char char k k r r

    # No content for an empty index.

    if {![llength $idx(keywords)]} return

    # Process the characters and associated keywords.

    set rows [list $config(class.row0) $config(class.row1)]

    Separator Contents
    html::newline
    html::tag* table class $config(class.contents) width 100% {
	html::newline ; html::indented 4 {
	    foreach c [lsort -dict [array names char]] {
		Separator "($c)"
		html::newline
		Leader $c
		foreach kw $char($c) {
		    Keyword $kw
		}
	    }
	    Separator
	    html::newline
	}
    } ; html::newline
    return
}

proc Leader {char} {
    upvar 1 anchor anchor config config

    html::tag* tr class $config(class.leader) {
	html::tag* th colspan 2 {
	    html::tag= a name $anchor($char) "Keywords: $char"
	}
    } ; html::newline
    return
}

proc Keyword {kw} {
    upvar 1 config config rows rows kwid kwid k k r r

    html::tag* tr class [Row] {
	html::newline ; html::indented 4 {
	    html::tag* td width $config(kwidth)% class $config(class.keyword) {
		html::tag= a name $kwid($kw) $kw
	    } ; html::newline
	    html::tag* td width $config(rwidth)% class $config(class.refs) {
		if {[llength $k($kw)]} {
		    html::newline ; html::indented 4 {
			References $kw
		    }
		}
	    } ; html::newline
	}
    } ; html::newline
    return
}

proc References {kw} {
    upvar 1 config config k k r r
    # Iterate over the references of the key
    set sep 0
    foreach id $k($kw) {
	foreach {type label} $r($id) break
	if {$sep} {
	    html::++ " $config(dot)"
	    if {![html::newline]} { html::++ " " }
	}
	html::tag= a href [Map $type $id] $label
	set sep 1
    }
    html::newline
    return
}

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

proc Separator {{text {}}} {
    upvar config config
    if {!$config(comments)} return
    set str $config(sepline)
    if {$text ne {}} {
	set new " $text "
	set str [string replace $str 1 [string length $new] $new]
    }
    html::comment $str
    return
}

proc Row {} {
    upvar 1 rows rows
    foreach {a b} $rows break
    set rows [list $b $a]
    return $a
}

proc Map {type id} {
    if {$type eq "url"} { return $id }
    upvar 1 map map
    if {![info exists map($id)]} { return $id }
    return $map($id)
}

proc Extend {varname} {
    upvar 1 config config
    if {$config($varname) eq {}} {
	if {$config(comments)} {
	    html::comment "Customization Point: $varname"
	}
	return 0
    }
    html::+++ $config($varname)
    return 1
}

# ### ### ### ######### ######### #########
## Ready

package provide doctools::idx::export::html 0.2
return