Coverage for lino/utils/xmlgen/html.py : 69%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: UTF-8 -*- # Adapted copy from lxml\src\lxml\html\builder.py # -------------------------------------------------------------------- # The ElementTree toolkit is # Copyright (c) 1999-2004 by Fredrik Lundh # Modifications in this file are # Copyright (c) 2012-2016 Luc Saffre # --------------------------------------------------------------------
# This document is part of the Lino test suite. To test only this # document, run:: # # $ python setup.py test -s tests.UtilsTests.test_xmlgen_html
Usage:
>>> from lino.utils.xmlgen.html import E >>> html = E.html( ... E.head( E.title("Hello World") ), ... E.body( ... E.h1("Hello World !"), ... class_="main" ... ) ... )
>>> print E.tostring_pretty(html) <html> <head> <title>Hello World</title> </head> <body class="main"> <h1>Hello World !</h1> </body> </html>
>>> kw = dict(title=u'Ein süßes Beispiel') >>> kw.update(href="foo/bar.html") >>> btn = E.button(type='button', class_='x-btn-text x-tbar-upload') >>> html = E.a(btn, **kw) >>> print E.tostring_pretty(html) <a href="foo/bar.html" title="Ein süßes Beispiel"> <button class="x-btn-text x-tbar-upload" type="button" /> </a>
You can also do the opposite, i.e. parse HTML:
>>> html = E.raw('''<a href="foo/bar.html" ... title="Ein süßes Beispiel"> ... <button class="x-btn-text x-tbar-upload" type="button" /> ... </a>''') >>> print(E.tostring_pretty(html)) <a href="foo/bar.html" title="Ein süßes Beispiel"> <button class="x-btn-text x-tbar-upload" type="button" /> </a>
>>> print(E.tostring(E.raw( ... '<ul type="disc"><li>First</li><li>Second</li></ul>'))) <ul type="disc"><li>First</li><li>Second</li></ul>
"""
# from htmlentitydefs import name2codepoint
# ENTITIES = {} # ENTITIES.update((x, unichr(i)) for x, i in name2codepoint.iteritems())
# def CreateParser(): # """Every string that is being parsed must get its own parser instance. # This is because "Due to limitations in the Expat library used by # pyexpat, the xmlparser instance returned can only be used to parse # a single XML document. Call ParserCreate for each document to # provide unique parser instances. (`docs.python.org # <https://docs.python.org/2/library/pyexpat.html>`_)
# """ # p = ET.XMLParser() # # PARSER.entity.update(htmlentitydefs.entitydefs) # p.entity = ENTITIES # assert 'otilde' in p.entity # assert 'eacute' in p.entity # assert 'nbsp' in p.entity # return p
# class RAW_HTML_STRING(unicode): # pass
"""The HTML namespace. This is instantiated as ``E``. """
# if isinstance(v, types.GeneratorType): return "".join([self.tostring(x, *args, **kw) for x in v]) # kw.setdefault('method', 'html') return str(v)
return "".join([self.to_rst(x, stripped) for x in v]) return str(v)
# def raw(self, raw_html): # return RAW_HTML_STRING(raw_html)
"""Parses the given string into an HTML Element.""" # print 20151008, raw_html
# the lxml parser wraps `<html><body>...</body></html>` around # the snippet, but we don't want it. # try: # return self.fromstring(raw_html, parser=CreateParser()) # except ET.ParseError as e: # raise Exception("ParseError {0} in {1}".format(e, raw_html))
a abbr acronym address alt applet area b base basefont bdo big blockquote body br button caption center cite code col colgroup dd del dfn dir div dl dt em fieldset font form frame frameset h1 h2 h3 h4 h5 h6 head height hr html i iframe img input ins isindex kbd label legend li link map menu meta name noframes noscript object ol optgroup option p param pre q s samp script select small span strike strong style sub sup table tbody td textarea tfoot th thead title tr tt u ul var
class id bgcolor cellspacing width align valign href type rel target value onclick src rows data-toggle tabindex placeholder """)
"""A pythonic representation of a ``<table>`` with ``<head>``, ``<foot>`` and ``<body>``.
"""
e = table_body_row(*args, **kw) self.foot.append(e) return e
children.append(E.tfoot(*self.foot))
"""A pythonic representation of a ``<body>`` with a ``<title>`` and some ``<head>`` tags for stylesheets.
"""
self.title = title self.body = [] self.stylesheets = stylesheets or []
self.stylesheets.append(url)
t = Table() self.body.append(t) return t
ET.ElementTree(self.as_element()).write(*args, **kw)
body = [] for e in self.body: if isinstance(e, Table): body.append(e.as_element()) else: body.append(e) headers = [] for css in self.stylesheets: headers.append(E.link(rel="stylesheet", type="text/css", href=css)) headers.append(E.title(self.title))
return E.html( E.head(*headers), E.body(*body) )
"""Convert the given list of text lines `lines` into a paragraph (``<p>``) with one ``<br>`` between each line. If optional `min_height` is given, add empty lines if necessary.
Examples:
>>> print(E.tostring(lines2p(['first', 'second']))) <p>first<br />second</p>
>>> print(E.tostring(lines2p(['first', 'second'], min_height=5))) <p>first<br />second<br /><br /><br /></p>
If `min_height` is specified, and `lines` contains more items, then we don't truncate:
>>> print(E.tostring(lines2p(['a', 'b', 'c', 'd', 'e'], min_height=4))) <p>a<br />b<br />c<br />d<br />e</p>
This also works:
>>> print(E.tostring(lines2p([], min_height=5))) <p><br /><br /><br /><br /></p>
"""
import doctest doctest.testmod()
_test() |