| 1 | namespace eval ::MarkupHelper { |
|---|
| 2 | variable markup {} |
|---|
| 3 | variable backup {} |
|---|
| 4 | variable indent 0 |
|---|
| 5 | |
|---|
| 6 | proc Init { } { |
|---|
| 7 | variable markup {} |
|---|
| 8 | variable restore {} |
|---|
| 9 | variable indent 0 |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | proc SetIndent { n } { |
|---|
| 13 | variable indent |
|---|
| 14 | |
|---|
| 15 | if { $n >= 0 && n <= 9 } { |
|---|
| 16 | set indent n |
|---|
| 17 | } |
|---|
| 18 | return $indent |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | proc IncrIndent { d } { |
|---|
| 22 | variable indent |
|---|
| 23 | |
|---|
| 24 | set n [ incr indent $d ] |
|---|
| 25 | if { $indent < 0 } { |
|---|
| 26 | set indent 0 |
|---|
| 27 | } elseif { $indent > 9 } { |
|---|
| 28 | set indent 9 |
|---|
| 29 | } |
|---|
| 30 | return $indent |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | proc Indent { } { |
|---|
| 34 | variable indent |
|---|
| 35 | variable markup |
|---|
| 36 | |
|---|
| 37 | append markup [ string repeat ":" $indent ] |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | proc Backup { } { |
|---|
| 41 | variable markup |
|---|
| 42 | variable backup $markup |
|---|
| 43 | |
|---|
| 44 | return $backup |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | proc Restore { } { |
|---|
| 48 | variable markup |
|---|
| 49 | variable backup |
|---|
| 50 | |
|---|
| 51 | set old $markup |
|---|
| 52 | set markup $backup |
|---|
| 53 | return $old |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | proc Verbatim { text } { |
|---|
| 57 | return [ string map {\n \n<br>} $text ] |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | proc Preformatted { text } { |
|---|
| 61 | variable markup |
|---|
| 62 | |
|---|
| 63 | append markup "#pre\n" |
|---|
| 64 | append markup "${text}\n" |
|---|
| 65 | append markup "#unpre\n" |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | proc Escape { text } { |
|---|
| 69 | set specialChars { |
|---|
| 70 | [ &lb; |
|---|
| 71 | ] &rb; |
|---|
| 72 | < < |
|---|
| 73 | > > |
|---|
| 74 | ' ≈ |
|---|
| 75 | & & |
|---|
| 76 | } |
|---|
| 77 | return [ string map $specialChars $text ] |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | proc Preprocess { text } { |
|---|
| 81 | set idx [ string first ";" $text ] |
|---|
| 82 | set ismarkup 0 |
|---|
| 83 | if { $idx != -1 } { |
|---|
| 84 | set header [ string range $text 0 [ expr { $idx -1 } ] ] |
|---|
| 85 | if { [ string range [ string trimleft $header ] 0 3 ] eq "#!NB" } { |
|---|
| 86 | set ismarkup 1 |
|---|
| 87 | set body [ string range $text [ expr { $idx +1 } ] end ] |
|---|
| 88 | } else { |
|---|
| 89 | set body $text |
|---|
| 90 | } |
|---|
| 91 | } else { |
|---|
| 92 | set body $text |
|---|
| 93 | } |
|---|
| 94 | if { $ismarkup } { |
|---|
| 95 | return $body |
|---|
| 96 | } else { |
|---|
| 97 | return [ Verbatim [ Escape $body ] ] |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | proc GetText { } { |
|---|
| 102 | variable markup |
|---|
| 103 | |
|---|
| 104 | return $markup |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | proc Text { text args } { |
|---|
| 108 | variable markup |
|---|
| 109 | |
|---|
| 110 | if { $markup eq "" } { |
|---|
| 111 | Indent |
|---|
| 112 | } |
|---|
| 113 | array set opts { -tags {} -br 0 } |
|---|
| 114 | array set opts $args |
|---|
| 115 | foreach t $opts(-tags) { |
|---|
| 116 | append markup "<$t>" |
|---|
| 117 | } |
|---|
| 118 | append markup $text |
|---|
| 119 | foreach t [ lreverse $opts(-tags) ] { |
|---|
| 120 | append markup "</$t>" |
|---|
| 121 | } |
|---|
| 122 | BR $opts(-br) |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | proc Image { image {br 0} } { |
|---|
| 126 | variable markup |
|---|
| 127 | |
|---|
| 128 | append markup "\[!image $image !\]" |
|---|
| 129 | BR $br |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | proc LabelValue { name value {br 0}} { |
|---|
| 133 | variable markup |
|---|
| 134 | |
|---|
| 135 | append markup "<b>$name:</b> " |
|---|
| 136 | append markup "$value" |
|---|
| 137 | BR $br |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | proc InlineHeader { text } { |
|---|
| 141 | Text $text -tags h |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | proc Header1 { text } { |
|---|
| 145 | variable markup |
|---|
| 146 | |
|---|
| 147 | NeedNewLine |
|---|
| 148 | append markup "= $text =\n" |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | proc Header2 { text } { |
|---|
| 152 | variable markup |
|---|
| 153 | |
|---|
| 154 | append markup "\n\n== $text ==\n" |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | proc Header3 { text } { |
|---|
| 158 | variable markup |
|---|
| 159 | |
|---|
| 160 | append markup "\n\n=== $text ===\n" |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | proc CR { } { |
|---|
| 164 | variable markup |
|---|
| 165 | |
|---|
| 166 | append markup "\n" |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | proc BR { { br 1 } } { |
|---|
| 170 | variable markup |
|---|
| 171 | variable indent |
|---|
| 172 | |
|---|
| 173 | if { $br } { |
|---|
| 174 | if { $indent } { |
|---|
| 175 | append markup "\n" |
|---|
| 176 | Indent |
|---|
| 177 | } else { |
|---|
| 178 | append markup " <br>\n" |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | proc NeedNewLine { } { |
|---|
| 184 | variable markup |
|---|
| 185 | |
|---|
| 186 | if { [ string index $markup end ] ne "\n" } { |
|---|
| 187 | append markup "\n" |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | proc Line { } { |
|---|
| 192 | variable markup |
|---|
| 193 | |
|---|
| 194 | if { [ string index $markup end ] ne "\n" } { |
|---|
| 195 | append markup "\n" |
|---|
| 196 | } |
|---|
| 197 | append markup "#---\n" |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | proc AppendHT { htBuffer } { |
|---|
| 201 | variable markup |
|---|
| 202 | |
|---|
| 203 | append markup $htBuffer |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | # esto deberia recibir un ObjectAddress |
|---|
| 207 | proc BuildTolbaseInfo {icon grammar name content path desc args } { |
|---|
| 208 | array set opts $args |
|---|
| 209 | |
|---|
| 210 | set extraHTInfo {} |
|---|
| 211 | foreach {classOf insInfo insCont} {{} {} {}} break |
|---|
| 212 | if { $grammar eq "NameBlock" } { |
|---|
| 213 | if { [ info exists opts(-objref) ] } { |
|---|
| 214 | set objRef $opts(-objref) |
|---|
| 215 | set classOf [Tol_ClassOfFromReference $objRef] |
|---|
| 216 | set insInfo [Tol_InstanceInfoFromReference $objRef] |
|---|
| 217 | set insCont [Tol_InstanceContentFromReference $objRef] |
|---|
| 218 | } elseif { [ info exists opts(-objaddr) ] } { |
|---|
| 219 | set objAddr $opts(-objaddr) |
|---|
| 220 | set classOf [Tol_ClassOf $objAddr] |
|---|
| 221 | set insInfo [Tol_InstanceInfo $objAddr] |
|---|
| 222 | set insCont [Tol_InstanceContent $objAddr] |
|---|
| 223 | } |
|---|
| 224 | if { $insInfo ne "" } { |
|---|
| 225 | Backup |
|---|
| 226 | Init |
|---|
| 227 | Header2 [ mc "Instance's Info" ] |
|---|
| 228 | foreach line [ split $insInfo \n ] { |
|---|
| 229 | set s [ string index $line 0 ] |
|---|
| 230 | set e [ string index $line end ] |
|---|
| 231 | if { $s eq "\[" && $e eq "\]" } { |
|---|
| 232 | Text "[ string range $line 1 end-1 ]:" \ |
|---|
| 233 | -tags {b} -br 1 |
|---|
| 234 | } elseif { $s eq "{" && $e eq "}" } { |
|---|
| 235 | Text "[ string range $line 1 end-1 ]: " -tags {b} |
|---|
| 236 | } else { |
|---|
| 237 | Text \ |
|---|
| 238 | [ Preprocess [ string trimright $line ] ] -br 1 |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | set extraHTInfo [ Restore ] |
|---|
| 242 | } |
|---|
| 243 | } elseif { $grammar eq "Set" } { |
|---|
| 244 | if { [ info exists opts(-objref) ] } { |
|---|
| 245 | set objRef $opts(-objref) |
|---|
| 246 | set classOf [Tol_StructOfFromReference $objRef] |
|---|
| 247 | } elseif { [ info exists opts(-objaddr) ] } { |
|---|
| 248 | set objAddr $opts(-objaddr) |
|---|
| 249 | set classOf [Tol_StructOf $objAddr] |
|---|
| 250 | } |
|---|
| 251 | if { $classOf ne "" } { |
|---|
| 252 | Backup |
|---|
| 253 | Init |
|---|
| 254 | BR |
|---|
| 255 | Text [ mc "Definition:" ] -tags b |
|---|
| 256 | CR |
|---|
| 257 | IncrIndent 1 |
|---|
| 258 | BR |
|---|
| 259 | Text "Struct" -tags b |
|---|
| 260 | Text " $classOf {" |
|---|
| 261 | IncrIndent 1 |
|---|
| 262 | BR |
|---|
| 263 | set fields [ tol::info struct $classOf ] |
|---|
| 264 | set ll0 [ llength $fields ] |
|---|
| 265 | set ll1 [ expr { $ll0 -1 } ] |
|---|
| 266 | for { set fi 0 } { $fi < $ll0 } { incr fi } { |
|---|
| 267 | foreach {n t} [ lindex $fields $fi ] break |
|---|
| 268 | Text "$t" -tags b |
|---|
| 269 | Text " ${n};" |
|---|
| 270 | if { $fi == $ll1 } { |
|---|
| 271 | IncrIndent -1 |
|---|
| 272 | } |
|---|
| 273 | BR |
|---|
| 274 | } |
|---|
| 275 | Text "}" |
|---|
| 276 | IncrIndent -1 |
|---|
| 277 | BR |
|---|
| 278 | set extraHTInfo [ Restore ] |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | Text "Grammar: " -tags b |
|---|
| 282 | Image $icon |
|---|
| 283 | if {$classOf eq ""} { |
|---|
| 284 | Text " $grammar" |
|---|
| 285 | } else { |
|---|
| 286 | Text " $classOf" |
|---|
| 287 | } |
|---|
| 288 | BR |
|---|
| 289 | LabelValue "Name" $name |
|---|
| 290 | if {$insCont ne ""} { |
|---|
| 291 | set _content $insCont |
|---|
| 292 | } elseif {$classOf ne ""} { |
|---|
| 293 | set _content $classOf |
|---|
| 294 | } else { |
|---|
| 295 | set _content $content |
|---|
| 296 | } |
|---|
| 297 | BR |
|---|
| 298 | if { $grammar eq "Text" } { |
|---|
| 299 | Text "Content:" -tags b |
|---|
| 300 | CR |
|---|
| 301 | Preformatted $_content |
|---|
| 302 | } else { |
|---|
| 303 | LabelValue "Content" [ Verbatim [ Escape $_content ] ] |
|---|
| 304 | BR |
|---|
| 305 | } |
|---|
| 306 | if {$path ne ""} { |
|---|
| 307 | LabelValue "Path" $path |
|---|
| 308 | BR |
|---|
| 309 | } |
|---|
| 310 | if {$desc ne ""} { |
|---|
| 311 | Text "Description:" -tags b -br 1 |
|---|
| 312 | Text \ |
|---|
| 313 | [ Preprocess [ string trimright $desc ] ] |
|---|
| 314 | } |
|---|
| 315 | AppendHT $extraHTInfo |
|---|
| 316 | return [ GetText ] |
|---|
| 317 | } |
|---|
| 318 | } |
|---|