Here is a start of a DOM like HTML formater I wrote while working on themes. I hope someone will improve this when I return to the themes stuff... --NirSoffer

I started the WikiDomFormatter, which is already in tla (MoinMoin/formatter/dom_xml.py). It is intended as prototype for solving the problems we have with the generation of valid XML/HTML. I don't think it makes sense to start another such project. IMHO it also makes no sense to reimplement a DOM-like datastructure if we can use DOM insted -- FlorianFesti 2004-01-28 15:18:51

   1 """
   2 HTML objects library
   3 
   4 This module provid easy to create and print html objects. You can create
   5 html without writing one line of html code.
   6 
   7 Object are created with some or all data, then you can add more data or
   8 attributes. You can print or get the html representation by calling
   9 them.
  10 
  11 """
  12 
  13 class Element:
  14     """
  15     Base class for html elements
  16 
  17     HTML elements has attributes and children. You create them with
  18     an HTML tag name, then set their attributes and add child nodes.
  19 
  20     Printing an element is done by printing the element or by calling it.
  21     """
  22     def __init__(self, tag, **attributes):
  23         self.tag = tag
  24         self.attributes = attributes
  25         self.children = []
  26         
  27     def __str__(self):
  28         s = []
  29         s.append('<%s' % self.tag)
  30         for key, value in self.attributes.items():
  31             s.append(' %s="%s"' % (key, value))
  32         s.append('>')
  33 
  34         for child in self.children:
  35             s.append(str(child))
  36         
  37         if self._close:
  38             s.append('</%s>%s' % (self.tag, self._newline))
  39         return ''.join(s)
  40 
  41     def __call__(self):
  42         return self.__str__()
  43 
  44     def __getattr__(self, attribute):
  45         return getattr(self.children, attribute)
  46 
  47 
  48 class Div(Element):
  49     def __init__(self, **attribues):
  50         self._close = 1
  51         self._newline = '\n'
  52         Element.__init__(self, 'div', **attribues)
  53 
  54 class Img(Element):
  55     def __init__(self, **attribues):
  56         self._close = 0
  57         self._newline = ''
  58         Element.__init__(self, 'img', **attribues)
  59 
  60         
  61 if __name__ == '__main__':
  62 
  63     #Test code and usage example:
  64     
  65     d = Div(id='header')
  66     d.append('\n')
  67     d.append('This is the first text of the div\n')
  68     d.append('This is the second text of the div\n')
  69     i = Img(src='../img/moinmoin.png')
  70     i.attributes['alt'] = 'MoinMoin Logo'
  71     d.append(i)
  72     d.append('\n')
  73 
  74     result = d()
  75     expected = """<div id="header">
  76 This is the first text of the div
  77 This is the second text of the div
  78 <img src="../img/moinmoin.png" alt="MoinMoin Logo">
  79 </div>
  80 """
  81 
  82     print 'Testing...'
  83     print d,
  84     if result == expected:
  85         print '... pass'
  86     else:
  87         print '... failed!'    

MoinMoin: FormatterRefactoring/DomLikeHtmlFormater (last edited 2007-10-29 19:07:18 by localhost)