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