DESCRIPTION.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. Simple, elegant HTML, XHTML and XML generation.
  2. Constructing your HTML
  3. ----------------------
  4. To construct HTML start with an instance of ``html.HTML()``. Add
  5. tags by accessing the tag's attribute on that object. For example:
  6. >>> from html import HTML
  7. >>> h = HTML()
  8. >>> h.p('Hello, world!')
  9. >>> print h # or print(h) in python 3+
  10. <p>Hello, world!</p>
  11. You may supply a tag name and some text contents when creating a HTML
  12. instance:
  13. >>> h = HTML('html', 'text')
  14. >>> print h
  15. <html>text</html>
  16. You may also append text content later using the tag's ``.text()`` method
  17. or using augmented addition ``+=``. Any HTML-specific characters (``<>&"``)
  18. in the text will be escaped for HTML safety as appropriate unless
  19. ``escape=False`` is passed. Each of the following examples uses a new
  20. ``HTML`` instance:
  21. >>> p = h.p('hello world!\n')
  22. >>> p.br
  23. >>> p.text('more &rarr; text', escape=False)
  24. >>> p += ' ... augmented'
  25. >>> h.p
  26. >>> print h
  27. <p>hello, world!<br>more &rarr; text ... augmented</p>
  28. <p>
  29. Note also that the top-level ``HTML`` object adds newlines between tags by
  30. default. Finally in the above you'll see an empty paragraph tag - tags with
  31. no contents get no closing tag.
  32. If the tag should have sub-tags you have two options. You may either add
  33. the sub-tags directly on the tag:
  34. >>> l = h.ol
  35. >>> l.li('item 1')
  36. >>> l.li.b('item 2 > 1')
  37. >>> print h
  38. <ol>
  39. <li>item 1</li>
  40. <li><b>item 2 &gt; 1</b></li>
  41. </ol>
  42. Note that the default behavior with lists (and tables) is to add newlines
  43. between sub-tags to generate a nicer output. You can also see in that
  44. example the chaining of tags in ``l.li.b``.
  45. Tag attributes may be passed in as well:
  46. >>> t = h.table(border='1')
  47. >>> for i in range(2):
  48. >>> r = t.tr
  49. >>> r.td('column 1')
  50. >>> r.td('column 2')
  51. >>> print t
  52. <table border="1">
  53. <tr><td>column 1</td><td>column 2</td></tr>
  54. <tr><td>column 1</td><td>column 2</td></tr>
  55. </table>
  56. A variation on the above is to use a tag as a context variable. The
  57. following is functionally identical to the first list construction but
  58. with a slightly different sytax emphasising the HTML structure:
  59. >>> with h.ol as l:
  60. ... l.li('item 1')
  61. ... l.li.b('item 2 > 1')
  62. You may turn off/on adding newlines by passing ``newlines=False`` or
  63. ``True`` to the tag (or ``HTML`` instance) at creation time:
  64. >>> l = h.ol(newlines=False)
  65. >>> l.li('item 1')
  66. >>> l.li('item 2')
  67. >>> print h
  68. <ol><li>item 1</li><li>item 2</li></ol>
  69. Since we can't use ``class`` as a keyword, the library recognises ``klass``
  70. as a substitute:
  71. >>> print h.p(content, klass="styled")
  72. <p class="styled">content</p>
  73. Unicode
  74. -------
  75. ``HTML`` will work with either regular strings **or** unicode strings, but
  76. not **both at the same time**.
  77. Obtain the final unicode string by calling ``unicode()`` on the ``HTML``
  78. instance:
  79. >>> h = HTML()
  80. >>> h.p(u'Some Euro: €1.14')
  81. >>> unicode(h)
  82. u'<p>Some Euro: €1.14</p>'
  83. If (under Python 2.x) you add non-unicode strings or attempt to get the
  84. resultant HTML source through any means other than ``unicode()`` then you
  85. will most likely get one of the following errors raised:
  86. UnicodeDecodeError
  87. Probably means you've added non-unicode strings to your HTML.
  88. UnicodeEncodeError
  89. Probably means you're trying to get the resultant HTML using ``print``
  90. or ``str()`` (or ``%s``).
  91. How generation works
  92. --------------------
  93. The HTML document is generated when the ``HTML`` instance is "stringified".
  94. This could be done either by invoking ``str()`` on it, or just printing it.
  95. It may also be returned directly as the "iterable content" from a WSGI app
  96. function.
  97. You may also render any tag or sub-tag at any time by stringifying it.
  98. Tags with no contents (either text or sub-tags) will have no closing tag.
  99. There is no "special list" of tags that must always have closing tags, so
  100. if you need to force a closing tag you'll need to provide some content,
  101. even if it's just a single space character.
  102. Rendering doesn't affect the HTML document's state, so you can add to or
  103. otherwise manipulate the HTML after you've stringified it.
  104. Creating XHTML
  105. --------------
  106. To construct XHTML start with an instance of ``html.XHTML()`` and use it
  107. as you would an ``HTML`` instance. Empty elements will now be rendered
  108. with the appropriate XHTML minimized tag syntax. For example:
  109. >>> from html import XHTML
  110. >>> h = XHTML()
  111. >>> h.p
  112. >>> h.br
  113. >>> print h
  114. <p></p>
  115. <br />
  116. Creating XML
  117. ------------
  118. A slight tweak to the ``html.XHTML()`` implementation allows us to generate
  119. arbitrary XML using ``html.XML()``:
  120. >>> from html import XML
  121. >>> h = XML('xml')
  122. >>> h.p
  123. >>> h.br('hi there')
  124. >>> print h
  125. <xml>
  126. <p />
  127. <br>hi there</br>
  128. </xml>
  129. Tags with difficult names
  130. -------------------------
  131. If your tag name isn't a valid Python identifier name, or if it's called
  132. "text" or "raw_text" you can add your tag slightly more manually:
  133. >>> from html import XML
  134. >>> h = XML('xml')
  135. >>> h += XML('some-tag', 'some text')
  136. >>> h += XML('text', 'some text')
  137. >>> print h
  138. <xml>
  139. <some-tag>some text</some-tag>
  140. <text>some text</text>
  141. </xml>
  142. Version History (in Brief)
  143. --------------------------
  144. - 1.16 detect and raise a more useful error when some WSGI frameworks
  145. attempt to call HTML.read(). Also added ability to add new content using
  146. the += operator.
  147. - 1.15 fix Python 3 compatibility (unit tests)
  148. - 1.14 added plain XML support
  149. - 1.13 allow adding (X)HTML instances (tags) as new document content
  150. - 1.12 fix handling of XHTML empty tags when generating unicode
  151. output (thanks Carsten Eggers)
  152. - 1.11 remove setuptools dependency
  153. - 1.10 support plain ol' distutils again
  154. - 1.9 added unicode support for Python 2.x
  155. - 1.8 added Python 3 compatibility
  156. - 1.7 added Python 2.5 compatibility and escape argument to tag
  157. construction
  158. - 1.6 added .raw_text() and and WSGI compatibility
  159. - 1.5 added XHTML support
  160. - 1.3 added more documentation, more tests
  161. - 1.2 added special-case klass / class attribute
  162. - 1.1 added escaping control
  163. - 1.0 was the initial release
  164. ----
  165. I would be interested to know whether this module is useful - if you use it
  166. please indicate so at https://www.ohloh.net/p/pyhtml
  167. This code is copyright 2009-2011 eKit.com Inc (http://www.ekit.com/)
  168. See the end of the source file for the license of use.
  169. XHTML support was contributed by Michael Haubenwallner.