durusmail: quixote-users: DOMish view of HTML as it's being rendered
DOMish view of HTML as it's being rendered
2002-03-02
DOMish view of HTML as it's being rendered
Joel Shprentz
2002-03-02
Everything old is new again.

On Feb 28, 2002 at 20:52 David Ascher  wrote:
>I've found that at times I need to do a fair bit of reorganizing of my
>code in order to, e.g., get the right things in the , while I'm
>actually deep in the body of the .  Given the very "sequential"
>nature of "HTML accretion" in Quixote, that can be a little tricky.
>
>Has anyone else longed for a DOM-like approach at times, where one
>could, as an _alternative_ to just returning strings, do something like:
>
>    response.head.append('bit of html')

See my paper "Creating HTML Documents with Python"
(http://www.python.org/workshops/1995-12/papers/shprentz.html) from the
Third Python Workshop in December 1995.

My approach to creating HTML documents was to build a DOM-like tree with
Python objects representing tags and their contents as tree nodes.  After
the tree was completely constructed, a recursive method could walk the tree
to generate the HTML.

When I presented my paper, this approach was criticized because no HTML
could be generated until the entire tree was built.  Some people wanted
display their page banner and advertisements while the server built the
body of the page.

Undaunted, I continued to use the same code (with some improvements and
HTML version upgrades).  It still generates thousands of HTML pages each
day.  If there is interest here, I can make the code available.

This example code shows how a table of contents (an unordered list) is
created and added to a document, but completed later.  As new sections are
started, entries are added to the table of contents.

class ...:

     def showMoreDetails (self):
         self.showTableOfContents ()
         self.showStaffList ()

     def showTableOfContents (self):
         self.toc = self.UL ()
         self.append (self.H2 ("Table of Contents"))
         self.append (self.toc)

     def.showStaffList (self):
         self.initialLetter = None
         for aStaffLine in open ("staffList.txt").readlines ():
             self.showStaffLine (aStaffLine)

     def showStaffLine (self, aStaffLine):
         firstName, lastName, phone = string.split (aStaffLine, "\t")
         self.showLetterSection (lastName)
         self.showStaffDetails (firstName, lastName, phone)

     def showLetterSection (self, lastName):
         if self.initialLetter == lastName [0]:
             return
         self.initialLetter = lastName [0]
         self.toc.append (self.LI (
                 self.anchor ("#" + self.initialLetter, self.initialLetter)))
         self.append (self.H2 (self.name (self.initialLetter,
self.initialLetter)))

     def showStaffDetails (self, firstName, lastName, phone):
         self.append (self.P ("%s %s: %s" % (firstName, lastName, phone)))

--
Joel Shprentz              (703) 478-9668
1516 Park Glen Court       jshprentz@his.com
Reston, VA 20190



reply