Attachment 'no-pythonxml_with_minidom+BeautifulSoup_v1.patch'
Download 1 --- /home/fpiat/deb/moin/moin.git/MoinMoin/formatter/text_docbook.py 2009-10-10 19:10:03.000000000 +0200
2 +++ /usr/share/pyshared/MoinMoin/formatter/text_docbook.py+minidom 2009-10-10 20:26:35.000000000 +0200
3 @@ -12,8 +12,8 @@
4 import os
5
6 from xml.dom import getDOMImplementation
7 -from xml.dom.ext.reader import Sax
8 -from xml.dom.ext import Node
9 +import xml.dom.minidom as Reader
10 +from xml.dom import Node
11
12 from MoinMoin.formatter import FormatterBase
13 from MoinMoin import wikiutil
14 @@ -28,10 +28,7 @@
15 class InternalError(CompositeError):
16 pass
17
18 -try:
19 - dom = getDOMImplementation("4DOM")
20 -except ImportError:
21 - raise InternalError("You need to install 4suite to use the DocBook formatter.")
22 +dom = getDOMImplementation()
23
24
25 class Formatter(FormatterBase):
26 @@ -72,15 +69,15 @@
27 self.cur = None
28
29 def startDocument(self, pagename):
30 - self.doc = dom.createDocument(None, self.doctype, dom.createDocumentType(
31 - self.doctype, "-//OASIS//DTD DocBook XML V4.4//EN",
32 - "http://www.docbook.org/xml/4.4/docbookx.dtd"))
33 + self.doc = dom.createDocument(None, unicode(self.doctype), dom.createDocumentType(
34 + unicode(self.doctype), u"-//OASIS//DTD DocBook XML V4.4//EN",
35 + u"http://www.docbook.org/xml/4.4/docbookx.dtd"))
36
37 self.title = pagename
38 self.root = self.doc.documentElement
39
40 if not self.include_kludge and self.doctype == "article":
41 - info = self.doc.createElement("articleinfo")
42 + info = self.doc.createElement(u"articleinfo")
43 self.root.appendChild(info)
44 self._addTitleElement(self.title, targetNode=info)
45 self._addRevisionHistory(targetNode=info)
46 @@ -101,14 +98,7 @@
47 return ""
48
49 def endDocument(self):
50 - from xml.dom.ext import PrettyPrint, Print
51 - import StringIO
52 -
53 - f = StringIO.StringIO()
54 - Print(self.doc, f)
55 - txt = f.getvalue()
56 - f.close()
57 -
58 + txt = self.doc.toxml()
59 self.cur = None
60 return txt
61
62 @@ -125,7 +115,7 @@
63 self.cur.lastChild.nodeValue = self.cur.lastChild.nodeValue + srcText
64 else:
65 # We create a new cdata section
66 - self.cur.appendChild(self.doc.createCDATASection(srcText))
67 + self.cur.appendChild(self.doc.createCDATASection(unicode(srcText)))
68
69 elif self.cur.nodeName in self.wrap_text_in_para:
70 """
71 @@ -133,13 +123,13 @@
72 and not create a new one. Another question is if we should add a space?
73 """
74 if self.cur.lastChild is not None and self.cur.lastChild.nodeName == 'para':
75 - self.cur.lastChild.appendChild(self.doc.createTextNode(srcText))
76 + self.cur.lastChild.appendChild(self.doc.createTextNode(unicode(srcText)))
77 else:
78 self.paragraph(1)
79 self.text(text)
80 self.paragraph(0)
81 else:
82 - self.cur.appendChild(self.doc.createTextNode(srcText))
83 + self.cur.appendChild(self.doc.createTextNode(unicode(srcText)))
84 return ""
85
86 def heading(self, on, depth, **kw):
87 @@ -159,11 +149,11 @@
88 if self.cur.nodeName == "section":
89 self.cur = self.cur.parentNode
90
91 - section = self.doc.createElement("section")
92 + section = self.doc.createElement(u"section")
93 self.cur.appendChild(section)
94 self.cur = section
95
96 - title = self.doc.createElement("title")
97 + title = self.doc.createElement(u"title")
98 self.cur.appendChild(title)
99 self.cur = title
100 self.curdepth = depth
101 @@ -203,6 +193,16 @@
102 self.text('\n')
103 return ""
104
105 + def SanitizeXml(self, text=""):
106 + from Ft.Xml.Domlette import Print, PrettyPrint, CanonicalPrint
107 + from BeautifulSoup import BeautifulSoup
108 + soup = BeautifulSoup(text, fromEncoding="utf-8")
109 + if soup.find('') == None:
110 + st = "<span>%s</span>" % str(soup)
111 + else:
112 + st = str(soup)
113 + return st
114 +
115 ### Inline ##########################################################
116
117 def strong(self, on, **kw):
118 @@ -446,22 +449,22 @@
119 def image(self, src=None, **kw):
120 if src:
121 kw['src'] = src
122 - media = self.doc.createElement('inlinemediaobject')
123 + media = self.doc.createElement(u"inlinemediaobject")
124
125 - imagewrap = self.doc.createElement('imageobject')
126 + imagewrap = self.doc.createElement(u"imageobject")
127 media.appendChild(imagewrap)
128
129 - image = self.doc.createElement('imagedata')
130 + image = self.doc.createElement(u"imagedata")
131 if kw.has_key('src'):
132 src = kw['src']
133 if src.startswith("/"):
134 # convert to absolute path:
135 src = self.request.url_root + src
136 - image.setAttribute('fileref', src)
137 + image.setAttribute(u"fileref", unicode(src))
138 if kw.has_key('width'):
139 - image.setAttribute('width', str(kw['width']))
140 + image.setAttribute(u"width", unicode(kw['width']))
141 if kw.has_key('height'):
142 - image.setAttribute('depth', str(kw['height']))
143 + image.setAttribute(u"depth", unicode(kw['height']))
144 imagewrap.appendChild(image)
145
146 # Look for any suitable title, order is important.
147 @@ -471,7 +474,7 @@
148 title = kw[a]
149 break
150 if title:
151 - txtcontainer = self.doc.createElement('textobject')
152 + txtcontainer = self.doc.createElement(u"textobject")
153 self._addTextElem(txtcontainer, "phrase", title)
154 media.appendChild(txtcontainer)
155
156 @@ -480,7 +483,7 @@
157
158 def transclusion(self, on, **kw):
159 # TODO, see text_html formatter
160 - self._emitComment('transclusion is not implemented in DocBook formatter')
161 + self._emitComment(u"transclusion is not implemented in DocBook formatter")
162 return ""
163
164 def transclusion_param(self, **kw):
165 @@ -543,7 +546,7 @@
166
167 def code_line(self, on):
168 if on:
169 - self.cur.appendChild(self.doc.createTextNode('\n'))
170 + self.cur.appendChild(self.doc.createTextNode(u"\n"))
171 return ''
172
173 def code_token(self, on, tok_type):
174 @@ -603,7 +606,7 @@
175 self._emitComment("The macro %s doesn't work with the DocBook formatter." % name)
176
177 elif name == "FootNote":
178 - footnote = self.doc.createElement('footnote')
179 + footnote = self.doc.createElement(u"footnote")
180 self._addTextElem(footnote, "para", str(args))
181 self.cur.appendChild(footnote)
182
183 @@ -613,7 +616,7 @@
184 self.paragraph(0)
185 text = FormatterBase.macro(self, macro_obj, name, args)
186 if text.strip():
187 - self._copyExternalNodes(Sax.FromXml(text).documentElement.childNodes, exclude=excludes)
188 + self._copyExternalNodes(Reader.parseString(text).documentElement.childNodes, exclude=excludes)
189 if was_in_para:
190 self.paragraph(1)
191
192 @@ -621,8 +624,9 @@
193 text = FormatterBase.macro(self, macro_obj, name, args)
194 if text:
195 from xml.parsers.expat import ExpatError
196 + st = self.SanitizeXml(text)
197 try:
198 - xml_dom = Sax.FromXml(text).documentElement.childNodes
199 + xml_dom = Reader.parseString(st).documentElement.childNodes
200 self._copyExternalNodes(xml_dom, exclude=excludes)
201 except ExpatError:
202 self._emitComment("The macro %s caused an error and should be blacklisted. It returned the data '%s' which caused the docbook-formatter to choke. Please file a bug." % (name, text))
203 @@ -646,15 +650,15 @@
204
205 def _emitComment(self, text):
206 text = text.replace("--", "- -") # There cannot be "--" in XML comment
207 - self.cur.appendChild(self.doc.createComment(text))
208 + self.cur.appendChild(self.doc.createComment(unicode(text)))
209
210 def _handleNode(self, name, on, attributes=()):
211 if on:
212 - node = self.doc.createElement(name)
213 + node = self.doc.createElement(unicode(name))
214 self.cur.appendChild(node)
215 if len(attributes) > 0:
216 for name, value in attributes:
217 - node.setAttribute(name, value)
218 + node.setAttribute(unicode(name), unicode(value))
219 self.cur = node
220 else:
221 """
222 @@ -744,8 +748,8 @@
223 with the nodeValue of text. The new element is then added as a child
224 to the element target.
225 """
226 - newElement = self.doc.createElement(elemName)
227 - newElement.appendChild(self.doc.createTextNode(text))
228 + newElement = self.doc.createElement(unicode(elemName))
229 + newElement.appendChild(self.doc.createTextNode(unicode(text)))
230 target.appendChild(newElement)
231
232
233 @@ -769,14 +773,14 @@
234 log = editlog.EditLog(self.request, rootpagename=self.title)
235 user_cache = {}
236
237 - history = self.doc.createElement("revhistory")
238 + history = self.doc.createElement(u"revhistory")
239
240 # read in the complete log of this page
241 for line in log.reverse():
242 if not line.action in ('SAVE', 'SAVENEW', 'SAVE/REVERT', 'SAVE/RENAME', ):
243 #Let's ignore adding of attachments
244 continue
245 - revision = self.doc.createElement("revision")
246 + revision = self.doc.createElement(u"revision")
247
248 # Revision number (without preceeding zeros)
249 self._addTextElem(revision, "revnumber", line.rev.lstrip('0'))
250 @@ -953,19 +957,19 @@
251 self.formatter = formatter
252 self.doc = doc
253
254 - self.tableNode = self.doc.createElement('informaltable')
255 + self.tableNode = self.doc.createElement(u"informaltable")
256 parent.appendChild(self.tableNode)
257 self.colWidths = {}
258 - self.tgroup = self.doc.createElement('tgroup')
259 + self.tgroup = self.doc.createElement(u"tgroup")
260 # Bug in yelp, the two lines below don't affect rendering
261 - #self.tgroup.setAttribute('rowsep', '1')
262 - #self.tgroup.setAttribute('colsep', '1')
263 + #self.tgroup.setAttribute(u"rowsep", u"1")
264 + #self.tgroup.setAttribute(u"colsep", u"1")
265 self.curColumn = 0
266 self.maxColumn = 0
267 self.row = None
268 self.tableNode.appendChild(self.tgroup)
269
270 - self.tbody = self.doc.createElement('tbody') # Note: This gets appended in finalizeTable
271 + self.tbody = self.doc.createElement(u"tbody") # Note: This gets appended in finalizeTable
272
273 def finalizeTable(self):
274 """Calculates the final width of the whole table and the width of each
275 @@ -976,21 +980,21 @@
276 A lot of the information is gathered from the style attributes passed
277 to the functions
278 """
279 - self.tgroup.setAttribute('cols', str(self.maxColumn))
280 + self.tgroup.setAttribute(u"cols", unicode(self.maxColumn))
281 for colnr in range(0, self.maxColumn):
282 - colspecElem = self.doc.createElement('colspec')
283 - colspecElem.setAttribute('colname', 'col_%s' % str(colnr))
284 + colspecElem = self.doc.createElement(u"colspec")
285 + colspecElem.setAttribute(u"colname", u"col_%s" % unicode(colnr))
286 if self.colWidths.has_key(str(colnr)) and self.colWidths[str(colnr)] != "1*":
287 - colspecElem.setAttribute('colwidth', self.colWidths[str(colnr)])
288 + colspecElem.setAttribute(u"colwidth", unicode(self.colWidths[str(colnr)]))
289 self.tgroup.appendChild(colspecElem)
290 self.tgroup.appendChild(self.tbody)
291 return self.tableNode.parentNode
292
293 def addRow(self, argsdict={}):
294 self.curColumn = 0
295 - self.row = self.doc.createElement('row')
296 + self.row = self.doc.createElement(u"row")
297 # Bug in yelp, doesn't affect the outcome.
298 - self.row.setAttribute("rowsep", "1") #Rows should have lines between them
299 + self.row.setAttribute(u"rowsep", u"1") #Rows should have lines between them
300 self.tbody.appendChild(self.row)
301 return self.row
302
303 @@ -998,9 +1002,9 @@
304 if 'style' in argsdict:
305 argsdict.update(self.formatter._convertStylesToDict(argsdict['style'].strip('"')))
306
307 - cell = self.doc.createElement('entry')
308 - cell.setAttribute('rowsep', '1')
309 - cell.setAttribute('colsep', '1')
310 + cell = self.doc.createElement(u"entry")
311 + cell.setAttribute(u"rowsep", u"1")
312 + cell.setAttribute(u"colsep", u"1")
313
314 self.row.appendChild(cell)
315 self._handleSimpleCellAttributes(cell, argsdict)
316 @@ -1027,8 +1031,8 @@
317 return 1
318 assert(element.nodeName == "entry")
319 extracols = int(argsdict['colspan'].strip('"')) - 1
320 - element.setAttribute('namest', "col_" + str(self.curColumn))
321 - element.setAttribute('nameend', "col_" + str(self.curColumn + extracols))
322 + element.setAttribute(u"namest", u"col_" + unicode(self.curColumn))
323 + element.setAttribute(u"nameend", u"col_" + unicode(self.curColumn + extracols))
324 return 1 + extracols
325
326 def _handleSimpleCellAttributes(self, element, argsdict={}):
327 @@ -1042,12 +1046,12 @@
328
329 if argsdict.has_key('rowspan'):
330 extrarows = int(argsdict['rowspan'].strip('"')) - 1
331 - element.setAttribute('morerows', str(extrarows))
332 + element.setAttribute(u"morerows", unicode(extrarows))
333
334 if argsdict.has_key('align'):
335 value = argsdict['align'].strip('"')
336 if value in safe_values_for['align']:
337 - element.setAttribute('align', value)
338 + element.setAttribute(u"align", unicode(value))
339 else:
340 self.formatter._emitComment("Alignment %s not supported" % value)
341 pass
342 @@ -1055,7 +1059,7 @@
343 if argsdict.has_key('valign'):
344 value = argsdict['valign'].strip('"')
345 if value in safe_values_for['valign']:
346 - element.setAttribute('valign', value)
347 + element.setAttribute(u"valign", unicode(value))
348 else:
349 self.formatter._emitComment("Vertical alignment %s not supported" % value)
350 pass
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.