Attachment 'no-pythonxml_with_4suite_v1.1.patch'

Download

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

You are not allowed to attach a file to this page.