| 1 | namespace eval ::MarkupHelper { |
|---|
| 2 | variable markup |
|---|
| 3 | |
|---|
| 4 | proc Init { } { |
|---|
| 5 | variable markup {} |
|---|
| 6 | } |
|---|
| 7 | |
|---|
| 8 | proc Verbatim { text } { |
|---|
| 9 | return [ string map {\n \n<br>} $text ] |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | proc Escape { text } { |
|---|
| 13 | set specialChars { |
|---|
| 14 | [ &lb; |
|---|
| 15 | ] &rb; |
|---|
| 16 | < < |
|---|
| 17 | > > |
|---|
| 18 | } |
|---|
| 19 | return [ string map $specialChars $text ] |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | proc Preprocess { text } { |
|---|
| 23 | set idx [ string first ";" $text ] |
|---|
| 24 | set ismarkup 0 |
|---|
| 25 | if { $idx != -1 } { |
|---|
| 26 | set header [ string range $text 0 [ expr { $idx -1 } ] ] |
|---|
| 27 | if { [ string range [ string trimleft $header ] 0 3 ] eq "#!NB" } { |
|---|
| 28 | set ismarkup 1 |
|---|
| 29 | set body [ string range $text [ expr { $idx +1 } ] end ] |
|---|
| 30 | } else { |
|---|
| 31 | set body $text |
|---|
| 32 | } |
|---|
| 33 | } else { |
|---|
| 34 | set body $text |
|---|
| 35 | } |
|---|
| 36 | if { $ismarkup } { |
|---|
| 37 | return $body |
|---|
| 38 | } else { |
|---|
| 39 | return [ Verbatim [ Escape $body ] ] |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | proc GetText { } { |
|---|
| 44 | variable markup |
|---|
| 45 | |
|---|
| 46 | return $markup |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | proc Text { text args } { |
|---|
| 50 | variable markup |
|---|
| 51 | |
|---|
| 52 | array set opts { -tags {} -br 0 } |
|---|
| 53 | array set opts $args |
|---|
| 54 | foreach t $opts(-tags) { |
|---|
| 55 | append markup "<$t>" |
|---|
| 56 | } |
|---|
| 57 | append markup $text |
|---|
| 58 | foreach t [ lreverse $opts(-tags) ] { |
|---|
| 59 | append markup "</$t>" |
|---|
| 60 | } |
|---|
| 61 | BR $opts(-br) |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | proc Image { image {br 0} } { |
|---|
| 65 | variable markup |
|---|
| 66 | |
|---|
| 67 | append markup "\[!image $image !\]" |
|---|
| 68 | BR $br |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | proc LabelValue { name value {br 0}} { |
|---|
| 72 | variable markup |
|---|
| 73 | |
|---|
| 74 | append markup "<b>$name:</b> " |
|---|
| 75 | append markup "$value" |
|---|
| 76 | BR $br |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | proc InlineHeader { text } { |
|---|
| 80 | Text $text -tags h |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | proc Header1 { text } { |
|---|
| 84 | variable markup |
|---|
| 85 | |
|---|
| 86 | NeedNewLine |
|---|
| 87 | append markup "= $text =\n" |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | proc Header2 { text } { |
|---|
| 91 | variable markup |
|---|
| 92 | |
|---|
| 93 | append markup "\n\n== $text ==\n" |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | proc Header3 { text } { |
|---|
| 97 | variable markup |
|---|
| 98 | |
|---|
| 99 | append markup "\n\n=== $text ===\n" |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | proc BR { { br 1 } } { |
|---|
| 103 | variable markup |
|---|
| 104 | |
|---|
| 105 | if { $br } { |
|---|
| 106 | append markup " <br>" |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | proc NeedNewLine { } { |
|---|
| 111 | variable markup |
|---|
| 112 | |
|---|
| 113 | if { [ string index $markup end ] ne "\n" } { |
|---|
| 114 | append markup "\n" |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | proc Line { } { |
|---|
| 119 | variable markup |
|---|
| 120 | |
|---|
| 121 | if { [ string index $markup end ] ne "\n" } { |
|---|
| 122 | append markup "\n" |
|---|
| 123 | } |
|---|
| 124 | append markup "#---\n" |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | proc BuildTolbaseInfo { args } { |
|---|
| 128 | foreach {icon grammar name content path desc objRef} $args {} |
|---|
| 129 | |
|---|
| 130 | if {$grammar eq "NameBlock" && [string length $objRef]} { |
|---|
| 131 | set classOf [Tol_ClassOfFromReference $objRef] |
|---|
| 132 | set insInfo [Tol_InstanceInfoFromReference $objRef] |
|---|
| 133 | set insCont [Tol_InstanceContentFromReference $objRef] |
|---|
| 134 | } else { |
|---|
| 135 | set classOf "" |
|---|
| 136 | set insInfo "" |
|---|
| 137 | set insCont "" |
|---|
| 138 | } |
|---|
| 139 | Text "Grammar: " -tags b |
|---|
| 140 | Image $icon |
|---|
| 141 | if {$classOf eq ""} { |
|---|
| 142 | Text " $grammar" |
|---|
| 143 | } else { |
|---|
| 144 | Text " $classOf" |
|---|
| 145 | } |
|---|
| 146 | BR |
|---|
| 147 | LabelValue "Name" $name |
|---|
| 148 | if {$insCont ne ""} { |
|---|
| 149 | set _content $insCont |
|---|
| 150 | } elseif {$classOf ne ""} { |
|---|
| 151 | set _content $classOf |
|---|
| 152 | } else { |
|---|
| 153 | set _content $content |
|---|
| 154 | } |
|---|
| 155 | BR |
|---|
| 156 | LabelValue "Content" [ Escape $_content ] |
|---|
| 157 | if {$path ne ""} { |
|---|
| 158 | BR |
|---|
| 159 | LabelValue "Path" $path |
|---|
| 160 | } |
|---|
| 161 | if {$desc ne ""} { |
|---|
| 162 | BR |
|---|
| 163 | Text "Description:" -tags b -br 1 |
|---|
| 164 | Text \ |
|---|
| 165 | [ Preprocess [ string trimright $desc ] ] |
|---|
| 166 | } |
|---|
| 167 | if { $insInfo ne "" } { |
|---|
| 168 | Header2 "Instance's Info" |
|---|
| 169 | foreach line [ split $insInfo \n ] { |
|---|
| 170 | set s [ string index $line 0 ] |
|---|
| 171 | set e [ string index $line end ] |
|---|
| 172 | if { $s eq "\[" && $e eq "\]" } { |
|---|
| 173 | Text "[ string range $line 1 end-1 ]:" \ |
|---|
| 174 | -tags {b i} -br 1 |
|---|
| 175 | } elseif { $s eq "{" && $e eq "}" } { |
|---|
| 176 | Text "[ string range $line 1 end-1 ]: " -tags {b i} |
|---|
| 177 | } else { |
|---|
| 178 | Text \ |
|---|
| 179 | [ Preprocess [ string trimright $line ] ] -br 1 |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | return [ GetText ] |
|---|
| 184 | } |
|---|
| 185 | } |
|---|