{ "info": { "author": "Richard Jones", "author_email": "rjones@ekit-inc.com", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 2.5", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Topic :: Software Development :: Code Generators", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Text Processing :: Markup :: HTML" ], "description": "Simple, elegant HTML, XHTML and XML generation.\n\nConstructing your HTML\n----------------------\n\nTo construct HTML start with an instance of ``html.HTML()``. Add\ntags by accessing the tag's attribute on that object. For example:\n\n>>> from html import HTML\n>>> h = HTML()\n>>> h.p('Hello, world!')\n>>> print(h)\n
Hello, world!
\n\nYou may supply a tag name and some text contents when creating a HTML\ninstance:\n\n>>> h = HTML('html', 'text')\n>>> print(h)\ntext\n\nYou may also append text content later using the tag's ``.text()`` method\nor using augmented addition ``+=``. Any HTML-specific characters (``<>&\"``)\nin the text will be escaped for HTML safety as appropriate unless\n``escape=False`` is passed. Each of the following examples uses a new\n``HTML`` instance:\n\n>>> p = h.p('hello world!\\n')\n>>> p.br\n>>> p.text('more → text', escape=False)\n>>> p += ' ... augmented'\n>>> h.p\n>>> print(h)\nhello, world!
more → text ... augmented
\n\nNote also that the top-level ``HTML`` object adds newlines between tags by\ndefault. Finally in the above you'll see an empty paragraph tag - tags with\nno contents get no closing tag.\n\nIf the tag should have sub-tags you have two options. You may either add\nthe sub-tags directly on the tag:\n\n>>> l = h.ol\n>>> l.li('item 1')\n>>> l.li.b('item 2 > 1')\n>>> print(h)\n
| column 1 | column 2 |
| column 1 | column 2 |
content
\n\n\nUnicode\n-------\n\n``HTML`` will work with either regular strings **or** unicode strings, but\nnot **both at the same time**.\n\nObtain the final unicode string by calling ``unicode()`` on the ``HTML``\ninstance:\n\n>>> h = HTML()\n>>> h.p(u'Some Euro: \u20ac1.14')\n>>> unicode(h)\nu'Some Euro: \u20ac1.14
'\n\nIf (under Python 2.x) you add non-unicode strings or attempt to get the\nresultant HTML source through any means other than ``unicode()`` then you\nwill most likely get one of the following errors raised:\n\nUnicodeDecodeError\n Probably means you've added non-unicode strings to your HTML.\nUnicodeEncodeError\n Probably means you're trying to get the resultant HTML using ``print``\n or ``str()`` (or ``%s``).\n\n\nHow generation works\n--------------------\n\nThe HTML document is generated when the ``HTML`` instance is \"stringified\".\nThis could be done either by invoking ``str()`` on it, or just printing it.\nIt may also be returned directly as the \"iterable content\" from a WSGI app\nfunction.\n\nYou may also render any tag or sub-tag at any time by stringifying it.\n\nTags with no contents (either text or sub-tags) will have no closing tag.\nThere is no \"special list\" of tags that must always have closing tags, so\nif you need to force a closing tag you'll need to provide some content,\neven if it's just a single space character.\n\nRendering doesn't affect the HTML document's state, so you can add to or\notherwise manipulate the HTML after you've stringified it.\n\n\nCreating XHTML\n--------------\n\nTo construct XHTML start with an instance of ``html.XHTML()`` and use it\nas you would an ``HTML`` instance. Empty elements will now be rendered\nwith the appropriate XHTML minimized tag syntax. For example:\n\n>>> from html import XHTML\n>>> h = XHTML()\n>>> h.p\n>>> h.br\n>>> print(h)\n\n