Table of Contents
Very simple HyperText Markup Language (HTML) generator.
Look to this module as the future location for making composing HTML documents easier. Currently this module only contains the building blocks, which do not really make composition easier.
The HTML Quick Reference [1] provides a useful reference for more information about what it is that this module's functions actually generated.
A lot of functions in this module have very short (and probably common) names, for instance b() may be easily confused with a variable called b. For this reason is is recommend that your import should be,
from grail.misc import htmlgen
Not the simpler,
from grail.misc.htmlgen import *
Which will pollute your namespace unnecessarily.
The following code example illustrates how you can go about composing an HyperText Markup Language (HTML) document. You have know a few little things about HTML, but basically the current incarnation of this module just saves you the effort of having to manage all the HTML tags.
print "creating 'test.html'...",
head = head(title("Test document"))
body = body(aname("top", h1("This is a test document.")),
"This is a test string",
ahref("test.html#top", "top"),
"And here is a hyper link to ",
ahref("http://www.google.ca", "google"),
bgcolor="#ebebeb")
HTMLDoc("test.html").generate(head, body)
print "...done."
Basic document generation tool.
Takes a set of strings (otherwise known as contents), and writes to the file specified by the path. The contents are wrapped like,
Note
This does not open a physical file handle to to the path. That is only done when the generate() method is executed.
- generate(*contents)
Actually writes the contents out to the file.
- Argument:
- *contents : variable argument list
- An arbitrarily long list of strings that will be put together to generate the final HTML document.
You do not want to send the output directly to a file via the HTMLDoc class, then you want to use the stream object. The stream object will write the contents out the given stream.
The stream can be another file, a cStringIO object or just sys.stdout.
Writes out the contents out to the stream.
Helps to simplify generating a HTML table of data.
As a somewhat contrived example, consider working with a pointlist, and you want to generate an html table with pointnumber, x, y, and z columns. You would do it like this,
The HTML TABLE element will have a summary attribute of "" applied to the table if no attribute is supplied. This is in accordance with the WC3 specification for TABLE elements.
Set the contents of a cell in the table.
Set the contents of the header.
Allows you set an attribute for a row.
Allows you to set attributes at the table level.
Returns <BODY key=value key=value...>components</BODY>.
Keywords are applied as parameters to the BODY tag. For example the following call,
Will return,
Returns <HEAD>components</HEAD>.
Typical components include: title, javascript, style.
Returns <HTML>components</HTML>.
Typical components include: head, body.
Note
The HTMLDoc class wraps your contents with the HTML tag.
Returns <STYLE>contents</STYLE>.
The contents are inserted in between the style tag. Contents can be multiple lines, and in the future there will be additional tags for composing contents.
Returns <TABLE key=value key=value>rows</TABLE>.
You have to compose the rows with the tr() function. This is one of the hardest ways to build a table, and is only really good for simple table layouts.
Returns <TD key=value key=value...>items</TD>.
The return value is supplied to the tr() function.
Returns <TH key=value key=value...>items</TH>.
The return value is typically supplied to the tr() function.
Returns a <TR key=value key=value...>columns</TR>.
These are supplied to the table() function. The columns are generated using the td() function.
[1] | University of Miami. HTML Quick Reference. 1998. 2 June 2004. <http://www.it.miami.edu/classes/documents/BegHTML/htmllist.html> |