]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/StructuredText/HTMLClass.py
Added some simple sample apps
[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 import re, sys, ST
88
89 class HTMLClass:
90
91 element_types={
92 '#text': '_text',
93 'StructuredTextDocument': 'document',
94 'StructuredTextParagraph': 'paragraph',
95 'StructuredTextExample': 'example',
96 'StructuredTextBullet': 'bullet',
97 'StructuredTextNumbered': 'numbered',
98 'StructuredTextDescription': 'description',
99 'StructuredTextDescriptionTitle': 'descriptionTitle',
100 'StructuredTextDescriptionBody': 'descriptionBody',
101 'StructuredTextSection': 'section',
102 'StructuredTextSectionTitle': 'sectionTitle',
103 'StructuredTextLiteral': 'literal',
104 'StructuredTextEmphasis': 'emphasis',
105 'StructuredTextStrong': 'strong',
106 'StructuredTextLink': 'link',
107 'StructuredTextXref': 'xref',
108 'StructuredTextInnerLink':'innerLink',
109 'StructuredTextNamedLink':'namedLink',
110 'StructuredTextUnderline':'underline',
111 'StructuredTextTable':'table',
112 'StructuredTextSGML':'sgml',
113 }
114
115 def dispatch(self, doc, level, output):
116 getattr(self, self.element_types[doc.getNodeName()])(doc, level, output)
117
118 def __call__(self, doc, level=1):
119 r=[]
120 self.dispatch(doc, level-1, r.append)
121 return join(r,'')
122
123 def _text(self, doc, level, output):
124 output(doc.getNodeValue())
125
126 def document(self, doc, level, output):
127 output('<html>\n')
128 children=doc.getChildNodes()
129 if (children and
130 children[0].getNodeName() == 'StructuredTextSection'):
131 output('<head>\n<title>%s</title>\n</head>\n' %
132 children[0].getChildNodes()[0].getNodeValue())
133 output('<body>\n')
134 for c in children:
135 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
136 output('</body>\n')
137 output('</html>\n')
138
139 def section(self, doc, level, output):
140 children=doc.getChildNodes()
141 for c in children:
142 getattr(self, self.element_types[c.getNodeName()])(c, level+1, output)
143
144 def sectionTitle(self, doc, level, output):
145 output('<h%d>' % (level))
146 for c in doc.getChildNodes():
147 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
148 output('</h%d>\n' % (level))
149
150 def description(self, doc, level, output):
151 p=doc.getPreviousSibling()
152 if p is None or p.getNodeName() is not doc.getNodeName():
153 output('<dl>\n')
154 for c in doc.getChildNodes():
155 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
156 n=doc.getNextSibling()
157 if n is None or n.getNodeName() is not doc.getNodeName():
158 output('</dl>\n')
159
160 def descriptionTitle(self, doc, level, output):
161 output('<dt>')
162 for c in doc.getChildNodes():
163 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
164 output('</dt>\n')
165
166 def descriptionBody(self, doc, level, output):
167 output('<dd>')
168 for c in doc.getChildNodes():
169 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
170 output('</dd>\n')
171
172 def bullet(self, doc, level, output):
173 p=doc.getPreviousSibling()
174 if p is None or p.getNodeName() is not doc.getNodeName():
175 output('<ul>\n')
176 output('<li>')
177 for c in doc.getChildNodes():
178 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
179 n=doc.getNextSibling()
180 output('</li>\n')
181 if n is None or n.getNodeName() is not doc.getNodeName():
182 output('</ul>\n')
183
184 def numbered(self, doc, level, output):
185 p=doc.getPreviousSibling()
186 if p is None or p.getNodeName() is not doc.getNodeName():
187 output('<ol>\n')
188 output('<li>')
189 for c in doc.getChildNodes():
190 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
191 n=doc.getNextSibling()
192 output('</li>\n')
193 if n is None or n.getNodeName() is not doc.getNodeName():
194 output('</ol>\n')
195
196 def example(self, doc, level, output):
197 i=0
198 for c in doc.getChildNodes():
199 if i==0:
200 output('<pre>')
201 output(html_quote(c.getNodeValue()))
202 output('</pre>\n')
203 else:
204 getattr(self, self.element_types[c.getNodeName()])(
205 c, level, output)
206
207 def paragraph(self, doc, level, output):
208 i=0
209 output('<p>')
210 for c in doc.getChildNodes():
211 if c.getNodeName() in ['StructuredTextParagraph']:
212 getattr(self, self.element_types[c.getNodeName()])(
213 c, level, output)
214 else:
215 getattr(self, self.element_types[c.getNodeName()])(
216 c, level, output)
217 output('</p>')
218
219 def link(self, doc, level, output):
220 output('<a href="%s">' % doc.href)
221 for c in doc.getChildNodes():
222 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
223 output('</a>')
224
225 def emphasis(self, doc, level, output):
226 output('<em>')
227 for c in doc.getChildNodes():
228 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
229 output('</em>')
230
231 def literal(self, doc, level, output):
232 output('<code>')
233 for c in doc.getChildNodes():
234 output(html_quote(c.getNodeValue()))
235 output('</code>')
236
237 def strong(self, doc, level, output):
238 output('<strong>')
239 for c in doc.getChildNodes():
240 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
241 output('</strong>')
242
243 def underline(self, doc, level, output):
244 output("<u>")
245 for c in doc.getChildNodes():
246 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
247 output("</u>")
248
249 def innerLink(self, doc, level, output):
250 output('<a href="#');
251 for c in doc.getChildNodes():
252 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
253 output('">[')
254 for c in doc.getChildNodes():
255 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
256 output(']</a>')
257
258 def namedLink(self, doc, level, output):
259 output('<a name="')
260 for c in doc.getChildNodes():
261 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
262 output('">[')
263 for c in doc.getChildNodes():
264 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
265 output(']</a>')
266
267 def sgml(self,doc,level,output):
268 for c in doc.getChildNodes():
269 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
270
271 def table(self,doc,level,output):
272 """
273 A StructuredTextTable holds StructuredTextRow(s) which
274 holds StructuredTextColumn(s). A StructuredTextColumn
275 is a type of StructuredTextParagraph and thus holds
276 the actual data.
277 """
278 output("<table border=1 cellpadding=2>\n")
279 for row in doc.getRows()[0]:
280 output("<tr>\n")
281 for column in row.getColumns()[0]:
282 str = "<td colspan=%s>" % column.getSpan()
283 output(str)
284 #for c in doc.getChildNodes():
285 # getattr(self, self.element_types[c.getNodeName()])(c, level, output)
286 for c in column.getChildNodes():
287 getattr(self, self.element_types[c.getNodeName()])(c, level, output)
288 output("</td>\n")
289 output("</tr>\n")
290 output("</table>\n")
291
292 def html_quote(v, name='(Unknown name)', md={},
293 character_entities=(
294 (('&'), '&amp;'),
295 (('<'), '&lt;' ),
296 (('>'), '&gt;' ),
297 (('\213'), '&lt;' ),
298 (('\233'), '&gt;' ),
299 (('"'), '&quot;'))): #"
300 text=str(v)
301 for re,name in character_entities:
302 if find(text, re) >= 0: text=join(split(text,re),name)
303 return text
304
305
306
307
308