W3C home > Mailing lists > Public > public-html-diffs@w3.org > February 2009

utils/elements-template.html 1.1 Script to generate element summaries

From: poot <cvsmail@w3.org>
Date: Mon, 2 Feb 2009 06:58:22 +0900 (JST)
To: public-html-diffs@w3.org
Message-Id: <20090201215822.60C322BC83@toro.w3.mag.keio.ac.jp>

Script to generate element summaries

http://people.w3.org/mike/diffs/html5/html-author/utils/elements-template.1.1.html

http://dev.w3.org/html5/html-author/utils/elements-template.html
http://people.w3.org/mike/diffs/html5/html-author/utils/elements-template.diff.html
http://dev.w3.org/cvsweb/html5/html-author/utils/elements-template.html?r1=NONE&r2=1.1&f=h

<!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:58:59 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Saturday, 18 December 2010 06:13:58 GMT