htmlfilters
Class info¶
🛈 DocStrings¶
ansi2html
¶
Convert ansi string to colored HTML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ansi_string
|
str
|
text with ANSI color codes. |
required |
styles
|
dict[int, dict[str, str]] | None
|
A mapping from ANSI codes to a dict with css |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string |
Source code in src/jinjarope/htmlfilters.py
clean_svg
¶
Strip off unwanted stuff from svg text which might be added by external libs.
Removes xml headers and doctype declarations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to cleanup / filter |
required |
Source code in src/jinjarope/htmlfilters.py
format_css_rule
¶
Format a nested dictionary as CSS rule.
Mapping must be of shape {".a": {"b": "c"}}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dct
|
Mapping[str, Any]
|
The mapping to convert to CSS text |
required |
Source code in src/jinjarope/htmlfilters.py
format_js_map
¶
Return JS map str for given dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
dict[str, Any] | str
|
Dictionary to dump |
required |
indent
|
int
|
The amount of indentation for the key-value pairs |
4
|
Source code in src/jinjarope/htmlfilters.py
format_xml
cached
¶
format_xml(
str_or_elem: str | Element,
indent: str | int = " ",
level: int = 0,
method: Literal["xml", "html", "text", "c14n"] = "html",
short_empty_elements: bool = True,
add_declaration: bool = False,
) -> str
(Pretty)print given XML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str_or_elem
|
str | Element
|
XML to prettyprint |
required |
indent
|
str | int
|
Amount of spaces to use for indentation |
' '
|
level
|
int
|
Initial indentation level |
0
|
method
|
Literal['xml', 'html', 'text', 'c14n']
|
Output method |
'html'
|
short_empty_elements
|
bool
|
Whether empty elements should get printed in short form (applies when mode is "xml") |
True
|
add_declaration
|
bool
|
whether a XML declaration should be printed (applies when mode is "xml") |
False
|
Source code in src/jinjarope/htmlfilters.py
html_link
¶
Create a html link.
If link is empty string or None, just the text will get returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | None
|
Text to show for the link |
None
|
link
|
str | None
|
Target url |
None
|
kwargs
|
Any
|
additional key-value pairs to be inserted as attributes. Key strings will have "_" stripped from the end to allow using keywords. |
{}
|
Source code in src/jinjarope/htmlfilters.py
inject_javascript
¶
inject_javascript(
html_content: ContentType, javascript: str, *, position: Position = "end_body"
) -> ContentType
Injects JavaScript code into an HTML string or bytes object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html_content
|
ContentType
|
The HTML content to inject the JavaScript into |
required |
javascript
|
str
|
The JavaScript code to inject |
required |
position
|
Position
|
The position to inject the JavaScript ('body' by default) |
'end_body'
|
Returns:
| Type | Description |
|---|---|
ContentType
|
The modified HTML content with the same type as the input |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the specified position tag is not found in the HTML content |
TypeError
|
If the input type is neither str nor bytes |
Source code in src/jinjarope/htmlfilters.py
normalize_url
¶
Return a URL relative to the given url or using the base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to normalize |
required |
url
|
str | None
|
Optional relative url |
None
|
base
|
str
|
Base path |
''
|
Source code in src/jinjarope/htmlfilters.py
relative_url
¶
Compute the relative path from URL A to URL B.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_a
|
str
|
URL A. |
required |
url_b
|
str
|
URL B. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The relative URL to go from A to B. |
Source code in src/jinjarope/htmlfilters.py
relative_url_mkdocs
¶
Return given url relative to other (MkDocs implementation).
Both are operated as slash-separated paths, similarly to the 'path' part of a URL.
The last component of other is skipped if it contains a dot (considered a file).
Actual URLs (with schemas etc.) aren't supported. The leading slash is ignored.
Paths are normalized ('..' works as parent directory), but going higher than the
root has no effect ('foo/../../bar' ends up just as 'bar').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL A. |
required |
other
|
str
|
URL B. |
required |
Source code in src/jinjarope/htmlfilters.py
split_url
¶
split_url(value: str, query: QueryStr | None = None) -> str | dict[str, str]
Split a URL into its parts (and optionally return a specific part).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The URL to split |
required |
query
|
QueryStr | None
|
Optional URL part to extract |
None
|
Source code in src/jinjarope/htmlfilters.py
svg_to_data_uri
¶
Wrap svg as data URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg
|
str
|
The svg to wrap into a data URL |
required |
Source code in src/jinjarope/htmlfilters.py
url_to_b64
¶
Convert an image URL to a base64-encoded string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_url
|
str
|
The URL of the image to convert. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The base64-encoded string of the image. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If there's an error downloading the image. |
Source code in src/jinjarope/htmlfilters.py
wrap_in_elem
¶
Wrap given text in an HTML/XML tag (with attributes).
If text is empty, just return an empty string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | None
|
Text to wrap |
required |
tag
|
str
|
Tag to wrap text in |
required |
add_linebreaks
|
bool
|
Adds a linebreak before and after the text |
False
|
kwargs
|
Any
|
additional key-value pairs to be inserted as attributes for tag. Key strings will have "_" stripped from the end to allow using keywords. |
{}
|