html5/html-author/utils elements-template.html,NONE,1.1 elements.py,NONE,1.1

Update of /sources/public/html5/html-author/utils
In directory hutz:/tmp/cvs-serv17941/utils

Added Files:
	elements-template.html elements.py 
Log Message:
Script to generate element summaries

--- NEW FILE: elements-template.html ---
<!DOCTYPE html>
<title>Template</title>
<div class="element">
	<h2></h2>
	<div class="summary">
		<p class="desc"></p>

		<dl class="html-tags">
			<dt>Start tag:</dt> <dd></dd>
			<dt>End tag:</dt> <dd></dd>
		</dl>

		<table class="kinds-of-content">
			<tr>
				<th>Categories:</th>
				<td></td>
			</tr>
			<tr>
				<th>Contained By:</th>
				<td></td>
			</tr>
			<tr>
				<th>Content Model:</th>
				<td></td>
			</tr>
		</table>

		<div class="properties">
			<div class="attributes">
				<h3 class="no-num no-toc">Attributes</h3>
			</div>
			<div class="dom">
				<h3 class="no-num no-toc">DOM Interface</h3>
			</div>
		</div>
	</div>
</div>

--- NEW FILE: elements.py ---
# Requires a copy of the HTML5 source
# http://www.whatwg.org/specs/web-apps/current-work/source
# and elements-template.html to be in the same directory.
#
# Usage:
# python elements.py > output.html

import sys
import html5lib
from html5lib import treebuilders, treewalkers, serializer

sourceFile = open("source")
templateFile = open("elements-template.html")
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom"))
sourceDocument = parser.parse(sourceFile)
templateDocument = parser.parse(templateFile)
template = templateDocument.getElementsByTagName("div")[0]

def findElementSummaries(headings):
    summaries = []
    for h in headings:
        next = getNextSiblingElement(h)
        if next.hasAttribute("class") and next.getAttribute("class") == "element":
            summaries.append({'h': h, 's': next})
    return summaries

def getNextSiblingElement(element):
    test = element.nextSibling
    while test != None and test.nodeType != 1:
        test = test.nextSibling
    return test

def createFragmentFromChildren(element):
    clone = element.cloneNode(True)
    fragment = sourceDocument.createDocumentFragment()
    while clone.hasChildNodes():
        fragment.appendChild(clone.firstChild)
    return fragment

def getDescriptions(dt):
    dd = getNextSiblingElement(dt)
    frag = None
    desc = []
    while dd != None and dd.tagName == "dd":
        desc.append(createFragmentFromChildren(dd))
        dd = getNextSiblingElement(dd)
    return desc

def markUpList(items):
    ul = templateDocument.createElement("ul");
    for item in items:
        li = templateDocument.createElement("li")
        li.appendChild(item)
        ul.appendChild(li)
    return ul

def markUpSummary(summary):
    markup = template.cloneNode(True)
    h2 = markup.getElementsByTagName("h2")[0]
    h2.appendChild(createFragmentFromChildren(summary['h']))

    contentTableRows = markup.getElementsByTagName("table")[0].getElementsByTagName("tr")
    contentTableRows[0].getElementsByTagName("td")[0].appendChild(markUpList(summary['categories']))
    contentTableRows[1].getElementsByTagName("td")[0].appendChild(markUpList(summary['containedby']))
    contentTableRows[2].getElementsByTagName("td")[0].appendChild(markUpList(summary['contentmodel']))

    attributes = markup.getElementsByTagName("div")[2]
    attributes.appendChild(markUpList(summary['attributes']))

    dom = markup.getElementsByTagName("div")[3]
    dom.appendChild(markUpList(summary['dom']))
    return markup

def serialise(markup):
    walker = treewalkers.getTreeWalker("dom")
    stream = walker(markup)
    s = serializer.htmlserializer.HTMLSerializer(omit_optional_tags=False)
    output_generator = s.serialize(stream)
    for item in output_generator:
        sys.stdout.write(item)

def generateElementSummaries():
    headings = sourceDocument.getElementsByTagName("h4")
    summaries = findElementSummaries(headings)
    for summary in summaries:
        dt = summary['s'].getElementsByTagName("dt")
        summary['categories'] = getDescriptions(dt[0])
        summary['containedby'] = getDescriptions(dt[1])
        summary['contentmodel'] = getDescriptions(dt[2])
        summary['attributes'] = getDescriptions(dt[3])
        summary['dom'] = getDescriptions(dt[4])
        markup = markUpSummary(summary)
        serialise(markup)

generateElementSummaries()

Received on Sunday, 1 February 2009 21:57:21 UTC