-
Notifications
You must be signed in to change notification settings - Fork 0
/
tclml
executable file
·443 lines (348 loc) · 8.53 KB
/
tclml
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
#!/usr/bin/tclsh8.5
# For testing
#set argc 1
#set argv "C:/sektor/papiery/trial.tclml"
set ::g_shoot 0
proc shoot {{arg 1}} {
puts stderr "----------- SHOOT [incr ::g_shoot $arg]-----------"
}
if { $argc != 1 } {
puts "Invalid usage (please pass tclml filename)"
exit 1
}
set fd [open $argv r]
set text [read $fd]
close $fd
# For further outpu file option
set fd stdout
set g_options(title) "(No title)"
set g_options(enc) iso-8859-2
set g_options(link) ""
set g_options(vlink) ""
set g_options(bgcolor) ""
set g_options(icon) ""
# Heading functions
proc settings args {
foreach {opt arg} $args {
set found 0
foreach option [array names ::g_options] {
if { [string match "-$option*" $opt] } {
set ::g_options($option) $arg
set found 1
break
}
}
if { !$found } {
error "Unknown option in 'settings' tag: '$opt'"
}
}
return "" ;# must return something to replace text!
}
# Private utilities
proc untangle {arg} {
if { [llength $arg] == 1 } {
return [lindex $arg 0]
}
return $arg
}
# Utility formatting functions:
# b (bold), u (underline), i (italic), em (emphasize)
# hr (horizontal line), h (heading), n ( <BR> )
proc b args {
set args [eval "concat $args"]
return "<B>[untangle $args]</B>"
}
proc u args {
set args [eval "concat $args"]
return "<U>[untangle $args]</U>"
}
proc i args {
set args [eval "concat $args"]
return "<I>[untangle $args]</I>"
}
proc em args {
set args [eval "concat $args"]
return "<EM>[untangle $args]</EM>"
}
proc n {} { return "<BR>\n" }
proc link {target args} {
set args [eval "concat $args"]
if { [string length $args] > 0 } {
return "<A HREF=\"$target\">[untangle $args]</A>"
} else {
set parts [split $target :]
set protocol [lindex $parts 0]
if { $protocol == "http" || $protocol == "https" } {
return "<A HREF=\"$target\">$target</A>"
}
return "<A HREF=\"$target\">[lindex $parts 1]</A>"
}
}
proc autolink {address} { return [link $address $address] }
#Non-paragraph
proc nopar {} {
set ::parconf(par) 0
return ""
}
proc hr {} {
return "\n<HR>"
}
proc --- {} { return [hr] }
proc h {level args} {
nopar
set opts ""
while { [string index $args 0] == "-" } {
set args [lassign $args opt arg]
append opts " [string range $opt 1 end]=\"$arg\""
}
set args [eval "concat $args"]
return "<H$level$opts>$args</H$level>"
}
set pardeflt(align) "" ; #defaults to left
set pardeflt(font) ""; # defaults to default
set pardeflt(par) 1 ; # is a paragraph, that is, requires <P>
proc reset_par {} {
array set ::parconf [array get ::pardeflt]
}
# Paragraph configuration: these functions set appropriate
# settings for the next paragrapgh
proc p {args} {
foreach {opt arg} $args {
if { [string index $opt 0] != "-" } {
error "Malformed 'p' - use -opt val -opt val ..."
}
set opt [string range $opt 1 end]
set ::parconf($opt) $arg
}
}
namespace eval int {}
# Variables
proc $ args {
set n [llength $args]
if { $n < 1 || $n > 2 } {
error "Malformed $: should have 1 or 2 arguments"
}
if { $n == 1 } {
set var "int::$args"
variable $var
return [set $var]
}
foreach {var val} $args {}
set var "int::$var"
variable $var
set $var $val
return ""
}
proc raw< {} { return "!rawhtml!" }
proc >raw {} { return "!endrawhtml!" }
proc raw {args} {
return "[raw<]\n[string map {%amp% & %lt% < %gt% >} $args][>raw]"
}
proc rawbegin {} {
set ::g_process_raw 1
return
}
proc rawend {} {
set ::g_process_raw 0
return
}
# Allright. In 'text' there is the text to be parsed.
# Split for paragraphs.
set text [string map {| !pipereplacedhere! } $text]
set text [string map {\n\n |} $text]
set parlist [split $text |]
set parlist [string map {!pipereplacedhere! |} $parlist]
#puts $parlist
#exit
set htmlparlist {}
set g_state 0
set g_state_option ""
set g_process_raw 0
proc switch_state {type {option {}} } {
set previous $::g_state
set current $type
set direction [expr {$current-$previous}]
if { $direction == 0 } return
set descending [expr {$direction > 0}]
if { $descending && $direction != 1 } {
error "Invalid level in descending!"
}
set ::g_state $current
set addition {}
if { $descending } {
lappend ::g_state_option $option
while 1 {
switch -- $option {
- { append addition "<UL>\n" }
1 { append addition "<OL type=1>\n" }
a { append addition "<OL type=a>\n" }
default { append addition "<OL>\n" }
}
incr previous
if { $previous == $current } {
break
}
}
} else {
while 1 {
set state [lindex $::g_state_option end]
set ::g_state_option [lrange $::g_state_option 0 end-1]
if { $state == "-" } {
append addition "</UL>\n"
} else {
append addition "</OL>\n"
}
incr previous -1
if { $previous == $current } {
break
}
}
}
return $addition
}
proc processlines text {
set lines [split $text \n]
switch_state 0
set addition {}
set linesout {}
foreach line $lines {
# Cut off line with comment
if { [string index $line 0] == "#" } {
continue
}
set continue_last 0
if { [string range $line 0 2] == " " } {
set line [string range $line 3 end]
set base 1
while 1 {
set probe [string range $line 0 1]
set line [string range $line 2 end]
switch -exact -- $probe {
"* " {
set addition [switch_state $base -]
}
"1." {
set addition [switch_state $base 1]
}
"a." {
set addition [switch_state $base a]
}
"A." {
set addition [switch_state $base A]
}
" " {
incr base
continue
}
}
break
}
} else {
#set addition [switch_state 0]
set continue_last 1
}
if { $::g_state > 0 && !$continue_last} {
set line "<LI>$line\n"
}
lappend linesout $addition$line
}
set finalline [switch_state 0]
if { $finalline != "" } {
lappend linesout $finalline
}
return $linesout
}
foreach par $parlist {
if { $::g_process_raw } {
# if so, do not process anything.
# in raw mode all commands must not break a line
# and original EOLs are saved
set fail [catch {subst -novariables $par} result]
if { $fail } {
puts stderr "***ERROR; Going to substitute:"
puts stderr "---------"
puts stderr $par
puts stderr "---------"
puts stderr $result
continue
}
append result \n ;# add the original end-of-line
lappend ::htmlparlist $result
continue
}
# First, replace illegal HTML characters
set par [string map {& %amp%} $par]
set par [string map {< %lt% > %gt% ; %scolon%} $par]
set linesout [processlines $par]
set par [join $linesout " "] ; # to prevent errors due to \n in command
reset_par
# Then, evaulate commands in the paragraph
# set result [subst -novariables $par]
set fail [catch {subst -novariables $par} result]
if { $fail } {
puts stderr "***ERROR"
puts stderr "Going to substitute:"
puts stderr "---------"
puts stderr $par
puts stderr "---------"
puts stderr $result
continue
}
set par [string trim $result]
# Skip empty paragraphs
if { $par == "" } continue
set par [string map {%amp% & %lt% < %gt% > %scolon% ; { - } { – }} $par]
set paropt {}
if { $::parconf(par) } {
foreach opt [array names ::parconf] {
if { $opt == "par" } continue
if { $::parconf($opt) != "" } {
append paropt " $opt=\"$::parconf($opt)\""
}
}
set rawpos [string first [raw<] $par]
set rawonly 0
if { $rawpos != -1 } {
set endrawpos [string first [>raw] $par]
# shift the endrawpos to the end of match string
# to verify if it would be end in this case (PAST-THE-END!)
incr endrawpos [string length [>raw]]
set rawonly [expr $rawpos == 0 && $endrawpos == [string length $par]]
set par [string map [list [raw<] "" [>raw] ""] $par]
}
if { $rawonly } {
lappend ::htmlparlist "\n$par\n"
} else {
lappend ::htmlparlist "<P$paropt>\n$par"
}
} else {
lappend ::htmlparlist $par
}
}
# Produce output html text
puts $fd "<HTML>\n<HEAD>"
# Title
puts $fd "<TITLE>$g_options(title)</TITLE>"
# Encoding
puts $fd "<META HTTP-EQUIV=Content-type CONTENT=\"text/html; charset=$g_options(enc)\">"
# Icon path
if { $::g_options(icon) != "" } {
puts $fd "<LINK REL=\"shortcut icon\" HREF=\"$::g_options(icon)\">"
}
# Build body options
proc addoption {where which} {
upvar $where optlist
if { $::g_options($which) != "" } {
append optlist " [string toupper $which]=\"$::g_options($which)\""
}
}
set bodyoptions ""
addoption bodyoptions link
addoption bodyoptions vlink
addoption bodyoptions bgcolor
puts $fd "</HEAD><BODY$bodyoptions>"
foreach par $htmlparlist {
puts $fd $par
}
puts $fd "</BODY></HTML>"
# vim: ft=tcl