plesty.lib.report.html
A report as an HTML fragment, for a page that sets it in its own style.
The fragment carries class names and no styling: assets/report.css is a
stylesheet for it, and a page such as the bench’s run export brings its own.
Math is converted to MathML, which browsers draw natively, so the fragment
needs no script to show an equation — that takes the optional latex2mathml
(pip install plesty-lib[report]); without it an equation is shown as its
LaTeX source. Figures are inline SVG (plesty.lib.report.svg).
HTML from raw() is sanitised: a report is
written by a third-party analyzer package, and it is shown inside a page that
may hold other things.
Attributes
Classes
Rebuild HTML from allowed elements and attributes only. |
Functions
|
Render a report, or one block of it, as an HTML fragment. |
|
Render one block. |
|
Render inline markup (see |
|
Render parsed spans. |
|
Render one LaTeX math expression as MathML, or as its source without it. |
|
Show an expression as the LaTeX it was written in. |
|
Render a number, its uncertainty, and a shared power of ten. |
|
Render one cell, with its span, alignment and note mark. |
|
Render a section's rows: only the cells that start in each row. |
|
Render the footnotes and the defined terms below a table. |
|
Render a table: its title, grid, notes and caption. |
|
Render a figure as inline SVG with its title and caption. |
|
Keep only table, inline-text and MathML elements, with harmless attributes. |
Module Contents
- plesty.lib.report.html._DEFAULT_ALIGN
- plesty.lib.report.html.to_html(item: Any, level: int = 2) str
Render a report, or one block of it, as an HTML fragment.
- Parameters:
item (Any) – A
Reportor a single block.level (int) – Heading level of the report’s title (
2→<h2>); block titles are one level below it.
- Returns:
The fragment.
- Return type:
str
- plesty.lib.report.html._block(block: Any, level: int) str
Render one block.
- Parameters:
block (Any)
level (int)
- Return type:
str
- plesty.lib.report.html.inline(text: str) str
Render inline markup (see
plesty.lib.report.markup) as HTML.- Parameters:
text (str)
- Return type:
str
- plesty.lib.report.html._spans(spans: list[plesty.lib.report.markup.Span]) str
Render parsed spans.
- Parameters:
spans (list[plesty.lib.report.markup.Span])
- Return type:
str
- plesty.lib.report.html.math(tex: str) str
Render one LaTeX math expression as MathML, or as its source without it.
- Parameters:
tex (str)
- Return type:
str
- plesty.lib.report.html._math_source(tex: str) str
Show an expression as the LaTeX it was written in.
- Parameters:
tex (str)
- Return type:
str
- plesty.lib.report.html._value(cell: plesty.lib.report.blocks.Value) str
Render a number, its uncertainty, and a shared power of ten.
- Parameters:
- Return type:
str
- plesty.lib.report.html._cell(cell: plesty.lib.report.blocks._Spanning, tag: str) str
Render one cell, with its span, alignment and note mark.
- Parameters:
tag (str)
- Return type:
str
- plesty.lib.report.html._rows(table: plesty.lib.report.blocks.Table, section: str, tag: str) str
Render a section’s rows: only the cells that start in each row.
- Parameters:
table (plesty.lib.report.blocks.Table)
section (str)
tag (str)
- Return type:
str
- plesty.lib.report.html._notes(table: plesty.lib.report.blocks.Table) str
Render the footnotes and the defined terms below a table.
- Parameters:
table (plesty.lib.report.blocks.Table)
- Return type:
str
- plesty.lib.report.html._table(table: plesty.lib.report.blocks.Table, level: int) str
Render a table: its title, grid, notes and caption.
- Parameters:
table (plesty.lib.report.blocks.Table)
level (int)
- Return type:
str
- plesty.lib.report.html._figure(figure: plesty.lib.report.blocks.Figure, level: int) str
Render a figure as inline SVG with its title and caption.
- Parameters:
figure (plesty.lib.report.blocks.Figure)
level (int)
- Return type:
str
- plesty.lib.report.html.ALLOWED_TAGS
- plesty.lib.report.html.ALLOWED_ATTRIBUTES
- plesty.lib.report.html._VOID
- plesty.lib.report.html._DROPPED
- class plesty.lib.report.html._Sanitizer
Bases:
html.parser.HTMLParserRebuild HTML from allowed elements and attributes only.
Initialize and reset this instance.
If convert_charrefs is True (the default), all character references are automatically converted to the corresponding Unicode characters.
- out: list[str] = []
- open: list[str] = []
- dropping = 0
- handle_starttag(tag: str, attrs: list[tuple[str, str | None]]) None
- Parameters:
tag (str)
attrs (list[tuple[str, Optional[str]]])
- Return type:
None
- handle_startendtag(tag: str, attrs: list[tuple[str, str | None]]) None
- Parameters:
tag (str)
attrs (list[tuple[str, Optional[str]]])
- Return type:
None
- handle_endtag(tag: str) None
- Parameters:
tag (str)
- Return type:
None
- handle_data(data: str) None
- Parameters:
data (str)
- Return type:
None
- plesty.lib.report.html.sanitize(markup: str) str
Keep only table, inline-text and MathML elements, with harmless attributes.
Scripts, styles and embedded content are dropped with what is inside them; any other element is dropped and its text kept; event handlers, links and inline styles are dropped from the elements that stay. Every element left open is closed.
- Parameters:
markup (str) – Untrusted HTML.
- Returns:
Safe HTML.
- Return type:
str