Metadata-Version: 1.1 Name: html Version: 1.16 Summary: simple, elegant HTML, XHTML and XML generation Home-page: http://pypi.python.org/pypi/html Author: Richard Jones Author-email: rjones@ekit-inc.com License: UNKNOWN Description: Simple, elegant HTML, XHTML and XML generation. Constructing your HTML ---------------------- To construct HTML start with an instance of ``html.HTML()``. Add tags by accessing the tag's attribute on that object. For example: >>> from html import HTML >>> h = HTML() >>> h.p('Hello, world!') >>> print h # or print(h) in python 3+
Hello, world!
You may supply a tag name and some text contents when creating a HTML instance: >>> h = HTML('html', 'text') >>> print h text You may also append text content later using the tag's ``.text()`` method or using augmented addition ``+=``. Any HTML-specific characters (``<>&"``) in the text will be escaped for HTML safety as appropriate unless ``escape=False`` is passed. Each of the following examples uses a new ``HTML`` instance: >>> p = h.p('hello world!\n') >>> p.br >>> p.text('more → text', escape=False) >>> p += ' ... augmented' >>> h.p >>> print hhello, world!
more → text ... augmented
Note also that the top-level ``HTML`` object adds newlines between tags by default. Finally in the above you'll see an empty paragraph tag - tags with no contents get no closing tag. If the tag should have sub-tags you have two options. You may either add the sub-tags directly on the tag: >>> l = h.ol >>> l.li('item 1') >>> l.li.b('item 2 > 1') >>> print h
column 1 | column 2 |
column 1 | column 2 |
content
Unicode ------- ``HTML`` will work with either regular strings **or** unicode strings, but not **both at the same time**. Obtain the final unicode string by calling ``unicode()`` on the ``HTML`` instance: >>> h = HTML() >>> h.p(u'Some Euro: €1.14') >>> unicode(h) u'Some Euro: €1.14
' If (under Python 2.x) you add non-unicode strings or attempt to get the resultant HTML source through any means other than ``unicode()`` then you will most likely get one of the following errors raised: UnicodeDecodeError Probably means you've added non-unicode strings to your HTML. UnicodeEncodeError Probably means you're trying to get the resultant HTML using ``print`` or ``str()`` (or ``%s``). How generation works -------------------- The HTML document is generated when the ``HTML`` instance is "stringified". This could be done either by invoking ``str()`` on it, or just printing it. It may also be returned directly as the "iterable content" from a WSGI app function. You may also render any tag or sub-tag at any time by stringifying it. Tags with no contents (either text or sub-tags) will have no closing tag. There is no "special list" of tags that must always have closing tags, so if you need to force a closing tag you'll need to provide some content, even if it's just a single space character. Rendering doesn't affect the HTML document's state, so you can add to or otherwise manipulate the HTML after you've stringified it. Creating XHTML -------------- To construct XHTML start with an instance of ``html.XHTML()`` and use it as you would an ``HTML`` instance. Empty elements will now be rendered with the appropriate XHTML minimized tag syntax. For example: >>> from html import XHTML >>> h = XHTML() >>> h.p >>> h.br >>> print h