]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/StructuredText/HTMLClass.py
[start of] a wxUniversal implementation of wxToolBar
[wxWidgets.git] / wxPython / samples / stxview / StructuredText / HTMLClass.py
1 ##############################################################################
2 #
3 # Zope Public License (ZPL) Version 1.0
4 # -------------------------------------
5 #
6 # Copyright (c) Digital Creations. All rights reserved.
7 #
8 # This license has been certified as Open Source(tm).
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are
12 # met:
13 #
14 # 1. Redistributions in source code must retain the above copyright
15 # notice, this list of conditions, and the following disclaimer.
16 #
17 # 2. Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions, and the following disclaimer in
19 # the documentation and/or other materials provided with the
20 # distribution.
21 #
22 # 3. Digital Creations requests that attribution be given to Zope
23 # in any manner possible. Zope includes a "Powered by Zope"
24 # button that is installed by default. While it is not a license
25 # violation to remove this button, it is requested that the
26 # attribution remain. A significant investment has been put
27 # into Zope, and this effort will continue if the Zope community
28 # continues to grow. This is one way to assure that growth.
29 #
30 # 4. All advertising materials and documentation mentioning
31 # features derived from or use of this software must display
32 # the following acknowledgement:
33 #
34 # "This product includes software developed by Digital Creations
35 # for use in the Z Object Publishing Environment
36 # (http://www.zope.org/)."
37 #
38 # In the event that the product being advertised includes an
39 # intact Zope distribution (with copyright and license included)
40 # then this clause is waived.
41 #
42 # 5. Names associated with Zope or Digital Creations must not be used to
43 # endorse or promote products derived from this software without
44 # prior written permission from Digital Creations.
45 #
46 # 6. Modified redistributions of any form whatsoever must retain
47 # the following acknowledgment:
48 #
49 # "This product includes software developed by Digital Creations
50 # for use in the Z Object Publishing Environment
51 # (http://www.zope.org/)."
52 #
53 # Intact (re-)distributions of any official Zope release do not
54 # require an external acknowledgement.
55 #
56 # 7. Modifications are encouraged but must be packaged separately as
57 # patches to official Zope releases. Distributions that do not
58 # clearly separate the patches from the original work must be clearly
59 # labeled as unofficial distributions. Modifications which do not
60 # carry the name Zope may be packaged in any form, as long as they
61 # conform to all of the clauses above.
62 #
63 #
64 # Disclaimer
65 #
66 # THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
67 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
70 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
71 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
72 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
73 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
74 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
76 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 # SUCH DAMAGE.
78 #
79 #
80 # This software consists of contributions made by Digital Creations and
81 # many individuals on behalf of Digital Creations. Specific
82 # attributions are listed in the accompanying credits file.
83 #
84 ##############################################################################
85
86 from string import join, split, find
87 from cgi import escape
88 import re, sys, ST
89
90 class HTMLClass:
91
92 element_types={
93 '#text': '_text',
94 'StructuredTextDocument': 'document',
95 'StructuredTextParagraph': 'paragraph',
96 'StructuredTextExample': 'example',
97 'StructuredTextBullet': 'bullet',
98 'StructuredTextNumbered': 'numbered',
99 'StructuredTextDescription': 'description',
100 'StructuredTextDescriptionTitle': 'descriptionTitle',
101 'StructuredTextDescriptionBody': 'descriptionBody',
102 'StructuredTextSection': 'section',
103 'StructuredTextSectionTitle': 'sectionTitle',
104 'StructuredTextLiteral': 'literal',
105 'StructuredTextEmphasis': 'emphasis',
106 'StructuredTextStrong': 'strong',
107 'StructuredTextLink': 'link',
108 'StructuredTextXref': 'xref',
109 'StructuredTextInnerLink':'innerLink',
110 'StructuredTextNamedLink':'namedLink',
111 'StructuredTextUnderline':'underline',
112 'StructuredTextTable':'table',
113 'StructuredTextSGML':'sgml',
114 }
115
116 def dispatch(self, doc, level, output):
117 getattr(self, self.element_types[doc.getNodeName()])(doc, level, output)
118
119 def __call__(self, doc, level=1):
120 r=[]
121 self.dispatch(doc, level-1, r.append)
122 return join(r,'')
123
124 def _text(self, doc, level, output):
125 output(doc.getNodeValue())
126
127 def document(self, doc, level, output):
128 output('<html>\n')
129 children=doc.getChildNodes()
130 if (children and
131 children[0].getNodeName() == 'StructuredTextSection'):
132 output('<head>\n<title>%s</title>\n</head>\n' %
133 children[0].getChildNodes()[0].getNodeValue())
134 output('<body>\n')
135 for c in children:
136 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
137 output('</body>\n')
138 output('</html>\n')
139
140 def section(self, doc, level, output):
141 children=doc.getChildNodes()
142 for c in children:
143 getattr(self, self.element_types[c.getNodeName()])(c, level+1, output)
144
145 def sectionTitle(self, doc, level, output):
146 output('<h%d>' % (level))
147 for c in doc.getChildNodes():
148 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
149 output('</h%d>\n' % (level))
150
151 def description(self, doc, level, output):
152 p=doc.getPreviousSibling()
153 if p is None or p.getNodeName() is not doc.getNodeName():
154 output('<dl>\n')
155 for c in doc.getChildNodes():
156 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
157 n=doc.getNextSibling()
158 if n is None or n.getNodeName() is not doc.getNodeName():
159 output('</dl>\n')
160
161 def descriptionTitle(self, doc, level, output):
162 output('<dt>')
163 for c in doc.getChildNodes():
164 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
165 output('</dt>\n')
166
167 def descriptionBody(self, doc, level, output):
168 output('<dd>')
169 for c in doc.getChildNodes():
170 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
171 output('</dd>\n')
172
173 def bullet(self, doc, level, output):
174 p=doc.getPreviousSibling()
175 if p is None or p.getNodeName() is not doc.getNodeName():
176 output('\n<ul>\n')
177 output('<li>')
178 for c in doc.getChildNodes():
179 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
180 n=doc.getNextSibling()
181 output('</li>\n')
182 if n is None or n.getNodeName() is not doc.getNodeName():
183 output('\n</ul>\n')
184
185 def numbered(self, doc, level, output):
186 p=doc.getPreviousSibling()
187 if p is None or p.getNodeName() is not doc.getNodeName():
188 output('\n<ol>\n')
189 output('<li>')
190 for c in doc.getChildNodes():
191 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
192 n=doc.getNextSibling()
193 output('</li>\n')
194 if n is None or n.getNodeName() is not doc.getNodeName():
195 output('\n</ol>\n')
196
197 def example(self, doc, level, output):
198 i=0
199 for c in doc.getChildNodes():
200 if i==0:
201 output('\n<pre>\n')
202 output(escape(c.getNodeValue()))
203 output('\n</pre>\n')
204 else:
205 getattr(self, self.element_types[c.getNodeName()])(
206 c, level, output)
207
208 def paragraph(self, doc, level, output):
209 i=0
210 output('<p>')
211 for c in doc.getChildNodes():
212 if c.getNodeName() in ['StructuredTextParagraph']:
213 getattr(self, self.element_types[c.getNodeName()])(
214 c, level, output)
215 else:
216 getattr(self, self.element_types[c.getNodeName()])(
217 c, level, output)
218 output('</p>\n')
219
220 def link(self, doc, level, output):
221 output('<a href="%s">' % doc.href)
222 for c in doc.getChildNodes():
223 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
224 output('</a>')
225
226 def emphasis(self, doc, level, output):
227 output('<em>')
228 for c in doc.getChildNodes():
229 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
230 output('</em>')
231
232 def literal(self, doc, level, output):
233 output('<code>')
234 for c in doc.getChildNodes():
235 output(escape(c.getNodeValue()))
236 output('</code>')
237
238 def strong(self, doc, level, output):
239 output('<strong>')
240 for c in doc.getChildNodes():
241 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
242 output('</strong>')
243
244 def underline(self, doc, level, output):
245 output("<u>")
246 for c in doc.getChildNodes():
247 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
248 output("</u>")
249
250 def innerLink(self, doc, level, output):
251 output('<a href="#');
252 for c in doc.getChildNodes():
253 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
254 output('">[')
255 for c in doc.getChildNodes():
256 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
257 output(']</a>')
258
259 def namedLink(self, doc, level, output):
260 output('<a name="')
261 for c in doc.getChildNodes():
262 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
263 output('">[')
264 for c in doc.getChildNodes():
265 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
266 output(']</a>')
267
268 def sgml(self,doc,level,output):
269 for c in doc.getChildNodes():
270 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
271
272 def xref(self, doc, level, output):
273 val = doc.getNodeValue()
274 output('<a href="#%s">[%s]</a>' % (val, val) )
275
276 def table(self,doc,level,output):
277 """
278 A StructuredTextTable holds StructuredTextRow(s) which
279 holds StructuredTextColumn(s). A StructuredTextColumn
280 is a type of StructuredTextParagraph and thus holds
281 the actual data.
282 """
283 output("<table border=1 cellpadding=2>\n")
284 for row in doc.getRows()[0]:
285 output("<tr>\n")
286 for column in row.getColumns()[0]:
287 if hasattr(column,"getAlign"):
288 str = "<%s colspan=%s align=%s valign=%s>" % (column.getType(),
289 column.getSpan(),
290 column.getAlign(),
291 column.getValign())
292 else:
293 str = "<td colspan=%s>" % column.getSpan()
294 output(str)
295 for c in column.getChildNodes():
296 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
297 if hasattr(column,"getType"):
298 output("</"+column.getType()+">\n")
299 else:
300 output("</td>\n")
301 output("</tr>\n")
302 output("</table>\n")
303
304
305
306
307