METADATA 7.1 KB

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