source: tol/tolbase/lib/toltk/markuphelper.tcl @ 02ea263

1941-docker-image-to-testdevel
Last change on this file since 02ea263 was 02ea263, checked in by Jorge Pérez <josp.jorge@…>, 15 years ago

refs #1347, factoring MarkupHelper in order to use it from MMS (outside tolbase)

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@3925 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 4.0 KB
Line 
1namespace 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      <  &lt;
17      >  &gt;
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  # esto deberia recibir un ObjectAddress
128  proc BuildTolbaseInfo {icon grammar name content path desc args } {
129    array set opts $args
130
131    foreach {classOf insInfo insCont} {{} {} {}} break
132    if { $grammar eq "NameBlock" } {
133      if { [ info exists opts(-objref) ] } {
134        set objRef $opts(-objref)
135        set classOf [Tol_ClassOfFromReference $objRef]
136        set insInfo [Tol_InstanceInfoFromReference $objRef]
137        set insCont [Tol_InstanceContentFromReference $objRef]
138      } elseif { [ info exists opts(-objaddr) ] } {
139        set objAddr $opts(-objaddr)
140        set classOf [Tol_ClassOf $objAddr]
141        set insInfo [Tol_InstanceInfo $objAddr]
142        set insCont [Tol_InstanceContent $objAddr]
143      }
144    }
145    Text "Grammar: " -tags b
146    Image $icon
147    if {$classOf eq ""} {
148      Text " $grammar"
149    } else {
150      Text " $classOf"
151    }
152    BR
153    LabelValue "Name" $name
154    if {$insCont ne ""} {
155      set _content $insCont
156    } elseif {$classOf ne ""} {
157      set _content $classOf
158    } else {
159      set _content $content
160    }
161    BR
162    LabelValue "Content" [ Escape $_content ]
163    if {$path ne ""} {
164      BR
165      LabelValue "Path" $path
166    }
167    if {$desc ne ""} {
168      BR
169      Text "Description:" -tags b -br 1
170      Text \
171          [ Preprocess [ string trimright $desc ] ]
172    }
173    if { $insInfo ne "" } {
174      Header2 "Instance's Info"
175      foreach line [ split $insInfo \n ] {
176        set s [ string index $line 0 ]
177        set e [ string index $line end ]
178        if { $s eq "\[" && $e eq "\]" } {
179          Text "[ string range $line 1 end-1 ]:" \
180              -tags {b i} -br 1
181        } elseif { $s eq "{" && $e eq "}" } {
182          Text "[ string range $line 1 end-1 ]: " -tags {b i}
183        } else {
184          Text \
185              [ Preprocess [ string trimright $line ] ] -br 1
186        }
187      }
188    }
189    return [ GetText ]
190  }
191}
Note: See TracBrowser for help on using the repository browser.