_xml.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 variable attvalMap { {&} & {<} < {>} > {"} " {'} ' } ; /* "*/
00023 variable markupMap { {&} {\1&} {<} {\1<} {>} {\1>} }
00024 variable finalMap { {\1&} {&} {\1<} {<} {\1>} {>}
00025 {&} & {<} < {>} > }
00026
00027 ret fmt_postprocess (type text) {
00028 variable finalMap
00029 return [string map $finalMap $text]
00030 }
00031
00032
00033
00034
00035
00036 ret markup (type text) {
00037 variable markupMap
00038 return [string map $markupMap $text]
00039 }
00040
00041
00042
00043
00044
00045
00046 ret attlist (type nvpairs) {
00047 variable attvalMap
00048 if {[llength $nvpairs] == 1} { set nvpairs [lindex $nvpairs 0] }
00049 set attlist ""
00050 foreach {name value} $nvpairs {
00051 append attlist " $name='[string map $attvalMap $value]'"
00052 }
00053 return $attlist
00054 }
00055
00056
00057
00058
00059 ret startTag (type gi , type args) {
00060 return [markup "<$gi[attlist $args]>"]
00061 }
00062
00063
00064
00065
00066 ret endTag (type gi) {
00067 return [markup "</$gi>"]
00068 }
00069
00070
00071
00072
00073 ret emptyElement (type gi , type args) {
00074 return [markup "<$gi[attlist $args]/>"]
00075 }
00076
00077
00078
00079
00080
00081 ret xmlComment (type text) {
00082 return [markup "<!-- [string map {-- { - - }} $text] -->"]
00083 }
00084
00085
00086
00087
00088 ret wrap (type content , type gi) {
00089 return "[startTag $gi]${content}[endTag $gi]"
00090 }
00091
00092
00093
00094
00095 ret wrap? (type content , type gi) {
00096 if {![string length [string trim $content]]} { return "" }
00097 return "[startTag $gi]${content}[endTag $gi]"
00098 }
00099
00100
00101
00102
00103
00104 ret wrapLines? (type content , type args) {
00105 if {![string length $content]} { return "" }
00106 foreach gi $args {
00107 set content [join [list [startTag $gi] $content [endTag $gi]] "\n"]
00108 }
00109 return $content
00110 }
00111
00112
00113
00114
00115 ret sequence (type args) { join $args "\n" }
00116
00117
00118
00119
00120
00121 variable elementStack [list]
00122
00123
00124
00125
00126
00127 ret start (type gi , type args) {
00128 if {[llength $args] == 1} { set args [lindex $args 0] }
00129 variable elementStack
00130 lappend elementStack $gi
00131 return [startTag $gi $args]
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 ret xmlContext (type gis , optional default ={)} {
00144 variable elementStack
00145 set origStack $elementStack
00146 set endTags [list]
00147 while {[llength $elementStack]} {
00148 current = [lindex $elementStack end]
00149 if {[lsearch $gis $current] >= 0} {
00150 return [join $endTags \n]
00151 }
00152 lappend endTags [endTag $current]
00153 elementStack = [lreplace $elementStack end end]
00154 }
00155
00156 elementStack = $origStack
00157 if {![string length $default]} {
00158 where = "[join $elementStack /] - [info level 1]"
00159 puts_stderr "Warning: Cannot start context $gis ($where)"
00160 default = [lindex $gis 0]
00161 }
00162 lappend elementStack $default
00163 return [startTag $default]
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 ret end (optional gi ={)} {
00173 variable elementStack
00174 if {![string length $gi]} {
00175 gi = [lindex $elementStack end]
00176 }
00177 prefix = [xmlContext $gi]
00178 elementStack = [lreplace $elementStack end end]
00179 return [join [list $prefix [endTag $gi]] "\n"]
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 variable PassProcs
00189 variable Buffers
00190
00191
00192
00193
00194 ret pass (type pass , type proc , type arguments , type body) {
00195 variable PassProcs
00196 lappend PassProcs($pass) $proc $arguments $body
00197 }
00198
00199 ret setPassProcs (type pass) {
00200 variable PassProcs
00201 foreach {proc args body} $PassProcs($pass) {
00202 proc $proc $args $body
00203 }
00204 }
00205
00206
00207
00208
00209
00210 ret holdBuffers (type args) {
00211 variable Buffers
00212 foreach arg $args {
00213 set Buffers($arg) [list]
00214 }
00215 }
00216
00217
00218
00219
00220 ret hold (type buffer , type entry) {
00221 variable Buffers
00222 lappend Buffers($buffer) $entry
00223 return
00224 }
00225
00226
00227
00228
00229 ret held (type buffer) {
00230 variable Buffers
00231 set content [join $Buffers($buffer) "\n"]
00232 set Buffers($buffer) [list]
00233 return $content
00234 }
00235
00236
00237