etgen.html2rst

Convert an etgen.html element to a reStructuredText string.

If stripped is True, output will be more concise and optimized for console output, but possibly not valid reStructuredText.

Usage examples:

>>> from etgen.html import E
>>> e = E.p("This is a ", E.b("first"), " test.")
>>> print(html2rst(e, True))
This is a **first** test.
>>> e = E.p(E.b("This")," is another test.")
>>> print(html2rst(e, True))
**This** is another test.
>>> e = E.p(E.b("This")," is ",E.em("another")," test.")
>>> print(html2rst(e, True))
**This** is *another* test.
>>> url = "http://example.com"
>>> e = E.p(E.b("This")," is ",E.a("a link",href=url),".")
>>> print(html2rst(e, True))
**This** is `a link <http://example.com>`__.
>>> e = E.p("An empty bold text:",E.b(""))
>>> print(html2rst(e, True))
An empty bold text:
>>> e = E.ul(E.li("First"), E.li("Second"))
>>> print(html2rst(e, True))

First
Second
>>> e = E.h1("A header")
>>> print(html2rst(e, True))
========
A header
========

For images we render the alt text between brackets:

>>> e = E.img(src="http://example.com/images/1.jpg", alt="1")
>>> print(html2rst(e, True))
[img 1]

If there is no alt text, render the content of src:

>>> e = E.img(src="http://example.com/images/1.jpg")
>>> print(html2rst(e, True))
[img http://example.com/images/1.jpg]

(This module’s source code is available here.)

Functions

html2rst(e[, stripped])

Convert an element tree to reStructuredText.

Classes

RstTable(headers[, show_headers])

A table containing elementtree HTML:

Exceptions

UnsupportedHtmlTag

etgen.html2rst.html2rst(e, stripped=False)

Convert an element tree to reStructuredText.

class etgen.html2rst.RstTable(headers, show_headers=True)

Bases: rstgen.Table

A table containing elementtree HTML:

Code

Result

>>> from etgen.html import E
>>> headers = [E.p("A ", E.b("formatted"), " header"), "A plain header"]
>>> rows = [[1,2], [3,4]]
>>> print(RstTable(headers).to_rst(rows))
======================== ================
 A **formatted** header   A plain header
------------------------ ----------------
 1                        2
 3                        4
======================== ================

A formatted header

A plain header

1

2

3

4