-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-as-tcl.tcl
executable file
·277 lines (206 loc) · 5.23 KB
/
xml-as-tcl.tcl
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
#!/usr/bin/tclsh
package require Tcl 8.5
package require tdom
proc showvar v {
# block some variables
if { $v in {auto_index auto_path errorInfo env tcl_platform} } {
return "<blocked>"
}
upvar $v var
if { ![info exists var] } {
return
}
if { ![array exists var] } {
return [set var]
}
foreach k [array names var] {
lappend r "{($k)$var($k)} "
}
return $r
}
proc domAsTcl {dom} {
set doc [$dom documentElement]
set list [$doc asList]
set result [domAsTclNode $list 0]
set entlist ""
foreach e $::entities {
lappend entlist "#?ent $e"
}
puts stderr "Collected [llength $::entities] entities:\n$entlist"
#puts stderr "EF:\n"
#uplevel #0 {foreach e [info vars] { puts stderr "$e = [showvar $e]" } }
return [join $entlist \n]\n\n$result
}
proc canBeRaw {text} {
if { [catch {set no [llength $text]}] } {
return no
}
# This is smart: will say "no" for "" and "one two", but not for "one"
if { $no != 1 } {
return no
}
if { [string index $text 0] == "-" || [string match {*[(){}/#;]*} $text] } {
return no
}
if { [string trim $text] != $text } {
return no
}
return yes
}
proc enclose {text} {
if { [canBeRaw $text] } {
return $text
}
if { [string first "\"" $text] != -1 } {
return "\{$text\}"
}
set text [string map {\\ \\\\ \" \\\"} $text]
return "\"$text\""
}
proc comment-out-block {block spaces} {
set out ""
foreach ln [split $block \n] {
append out "$spaces# [string trim $ln]\n"
}
return $out
}
proc domAsTclNode {node indent} {
# Process contents of the current node
set ispaces [string repeat " " [expr {4*$indent}]]
set ispace [string repeat " " 4]
#lassign $node name attr subnodes
foreach {name attr subnodes} $node break
# puts stderr "NODE: $name ATTR: $attr"
# if { [string index $name 0] == "#" } {
# switch [string range $name 1 end] {
# comment {
# return "\n$ispaces#$attr\n"
# }
#
# text {
# return "-- \"$attr\"\n"
# }
# }
# }
if { $name == "#comment" } {
set cattr [comment-out-block $attr $ispaces]
return \n$cattr\n
}
set target $ispaces$name
# puts stderr "FIRST RESULT: $target"
if { [catch {
foreach {n v} $attr {
append target " -$n [enclose $v]"
# puts stderr "RESULT STEP: $target"
}
} result] } {
# puts stderr "ERROR: attrs:\n$attr\n----------------\n$node\n-----------"
}
# puts stderr "RESULT PARTIAL: $target"
set node1 [lindex $subnodes 0]
if { [lindex $node1 0] == "#text" } {
append target " -- {\n$ispaces$ispace[lindex $node1 1]$ispaces}\n"
set subnodes [lrange $subnodes 1 end]
}
if { [string trim $subnodes] != "" } {
append target " \{\n"
incr indent
foreach n $subnodes {
append target [domAsTclNode $n $indent]
}
append target "$ispaces\}\n\n"
} else {
#append target " \{\}\n"
append target "\n"
}
# puts stderr "RESULT: $target"
return $target
}
proc slice str {
set str [split $str " "]
set result ""
foreach s $str {
set s [string trim $s]
if { $s != "" } {
lappend result $s
}
}
return $result
}
proc requestEntityProvider {baseUri sysid pubid} {
puts stderr "Requesting entity provider: $baseUri sysid:$sysid pubid:$pubid"
set localid ""
foreach k [array names ::supath] {
if { [string match $k* $sysid] } {
set len [string length $k]
set suf [string range $sysid $len end]
set localid "$::supath($k)$suf"
break
}
}
if { $localid == "" } {
puts stderr " ... not found!"
lappend ::entities [list $sysid "<not found!>"]
return [list string $baseUri ""]
}
lappend ::entities [list $sysid [file normalize $localid]]
#puts stderr "RQ:\n"
#uplevel #0 {foreach e [info vars] { puts stderr "$e = [showvar $e]" } }
set fd [open $localid r]
set contents [read $fd]
close $fd
# Good - and now let's fake it. Clear the entity definitions, providing your own.
set fake ""
foreach line [split $contents \n] {
set splot [slice $line]
set ent [lindex $splot 0]
set name [lindex $splot 1]
if { $ent != "<!ENTITY" } {
lappend fake $line
continue
}
lappend fake "$ent $name \"\$\{$name\}\">"
}
set contents [join $fake \n]
#puts stderr $contents
return [list string $baseUri $contents]
}
set filename [lindex $argv 0]
set argv [lrange $argv 1 end]
# Defaulted
set option(type) ""
foreach e $argv {
if { [string match *=* $e] } {
lassign [split $e =] match subst
set ::supath($match) $subst
continue
}
if { [string match -*/* $e] } {
lassign [split [string range $e 1 end] /] optname value
set ::option($optname) $value
continue
}
}
set fd [open $filename r]
puts stderr "Processing $filename"
set entities ""
set typestr ""
set sourcearg "-channel $fd"
if { $option(type) != "" } {
set sourcearg [list [read $fd]] ;# [list] required against {*}
if { $option(type) == "html" } {
set typestr "-html"
} elseif { $option(type) == "simple" } {
set typestr "-simple"
} else {
puts stderr "Unknown -type/$option(type): use -type/simple or -type/html"
exit 1
}
}
set xml [dom parse -externalentitycommand requestEntityProvider {*}$sourcearg {*}$typestr]
close $fd
puts "#?tml: TUL\n"
puts "#?xml: version=\"1.0\" encoding=\"UTF-8\""
puts "# (generated by xml-as-tcl)\n"
puts [domAsTcl $xml]
puts "# vim[format ""]:ft=tcl"