html (16)
ansi2html¶
ansi2html(ansi_string: str, styles: dict[int, dict[str, str]] | None = None) -> str
Convert ansi string to colored HTML.
Example
Jinja call:
Result:<span style="color: red; font_weight: bold; text_decoration: underline">Hello</span>
DocStrings
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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
clean_svg¶
clean_svg(text: str) -> str
Strip off unwanted stuff from svg text which might be added by external libs.
Example
Jinja call:
Result:<body></body>
DocStrings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to cleanup / filter |
required |
Source code in src/jinjarope/htmlfilters.py
132 133 134 135 136 137 138 139 140 141 142 |
|
format_css_rule¶
format_css_rule(dct: 'Mapping') -> 'str'
Format a nested dictionary as CSS rule.
DocStrings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dct
|
Mapping
|
The mapping to convert to CSS text |
required |
Source code in src/jinjarope/htmlfilters.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
format_js_map¶
format_js_map(mapping: dict | str, indent: int = 4) -> str
Return JS map str for given dictionary.
DocStrings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mapping
|
dict | str
|
Dictionary to dump |
required |
indent
|
int
|
The amount of indentation for the key-value pairs |
4
|
Source code in src/jinjarope/htmlfilters.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
format_xml¶
format_xml(str_or_elem: str | xml.etree.ElementTree.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.
Example
Jinja call:
Result:<a>
<b>
<c>Hello!</c>
</b>
</a>
Example
Jinja call:
Result:<a>
<b>
<c>Hello!</c>
</b>
</a>
Example
Jinja call:
Result:<br />
Example
Jinja call:
Result:<?xml version='1.0' encoding='utf-8'?>
<br />
DocStrings
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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
html_link¶
html_link(text: str | None = None, link: str | None = None, **kwargs: Any) -> str
Create a html link.
Example
Jinja call:
Result:<a href='https://google.com'>Test</a>
DocStrings
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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
inject_javascript¶
inject_javascript(html_content: ~ContentType, javascript: str, *, position: Literal['body', 'head', 'end_head', 'end_body'] = 'end_body') -> ~ContentType
Injects JavaScript code into an HTML string or bytes object.
Example
Jinja call:
Result:<body>some HTML<script>var number = 1;</script></body>
Example
Jinja call:
{{ "<head>HEAD</head><body>some HTML</body>" | inject_javascript("var number = 1;", position="head") }}
<head><script>var number = 1;</script>HEAD</head><body>some HTML</body>
DocStrings
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
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
|
normalize_url¶
normalize_url(path: str, url: str | None = None, base: str = '') -> str
Return a URL relative to the given url or using the base.
Example
Jinja call:
Result:https://phil65.github.io/test
DocStrings
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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
relative_url¶
relative_url(url_a: str, url_b: str) -> str
Compute the relative path from URL A to URL B.
Example
Jinja call:
Result:mknodes
DocStrings
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
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
relative_url_mkdocs¶
relative_url_mkdocs(url: str, other: str) -> str
Return given url relative to other (MkDocs implementation).
Example
Jinja call:
Result:..
DocStrings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
URL A. |
required |
other
|
str
|
URL B. |
required |
Source code in src/jinjarope/htmlfilters.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
svg_to_data_uri¶
svg_to_data_uri(svg: str) -> str
Wrap svg as data URL.
Example
Jinja call:
Result:url('data:image/svg+xml;charset=utf-8,<svg>...</svg>')
DocStrings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
svg
|
str
|
The svg to wrap into a data URL |
required |
Source code in src/jinjarope/htmlfilters.py
120 121 122 123 124 125 126 127 128 129 |
|
url_to_b64¶
url_to_b64(image_url: str) -> str | None
Convert an image URL to a base64-encoded string.
Example
Jinja call:
Result:/9j/4QDeRXhpZgAASUkqAAgAAAAGABIBAwABAAAAAQAAABoBBQABAAAAVgAAABsBBQABAAAAXgAAACgBAwABAAAAAgAAABMCAwABAAAAAQAAAGmHBAABAAAAZgAAAAAAAABIAAAAAQAAAEgAAAABAAAABwAAkAcABAAAADAyMTABkQcABAAAAAECAwCGkgcAFgAAAMAAAAAAoAcABAAAADAxMDABoAMAAQAAAP//AAACoAQAAQAAAGQAAAADoAQAAQAAAGQAAAAAAAAAQVNDSUkAAABQaWNzdW0gSUQ6IDM3Nv/bAEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQfJzk9ODI8LjM0Mv/bAEMBCQkJDAsMGA0NGDIhHCEyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMv/CABEIAGQAZAMBIgACEQEDEQH/xAAaAAACAwEBAAAAAAAAAAAAAAAEBQADBgIB/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/9oADAMBAAIQAxAAAAHxooagX5x+pIX7nD7dhYGWNsmCNQsjzP2ro19lUYaN0sNB6H7sBzW9wm7onSx9mMc0UuOlQKoo15qYRNjXt1atVBuRji14x2yzL/FMELQOxKG1W+mYsdQldoUB+VbQ6oDdidBHbHIOURAtndqtHCZ6VWx5HTP3cljDrWy6dBeenmIll6R10mf69lUltUVy3qnsoj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ElNDAhSJPc3kIhwQQQM5Ek2SOINnz8zi0IVIU2RRqb85KWqfMMpjhL3iA1BJTD14weArWie8nYQJAo+sk8GeNRkejNhjBwwtAt66MDGYYlApE1ILIdM2f4w9GNDRIv4DEYluYnJEyahF46MJyeN/jImhparY4VgEDcPT+cP3gTt04xVWAnARvscjgAwOIQOjoyaYYMBHQoDKxhPuTBw86dwjCFZJtk8iYaMH5uAdUwBoU/POJF3yO07nDQUddOmPMkTh4Sf2OUS2zXnHCnyxtvLK/uqsHiEyxPLhIoAEdLebKkkk9GQIXh+jC1UnuMF+xJTsx81vHh7MQoCAAlQ/8AuIHHT1H+cFrvH//Z
DocStrings
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 |
---|---|
RequestException
|
If there's an error downloading the image. |
Source code in src/jinjarope/htmlfilters.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
urlencode¶
urlencode(value: Union[str, Mapping[str, Any], Iterable[Tuple[str, Any]]]) -> str
Quote data for use in a URL path or query using UTF-8.
DocStrings
Source code in .venv/lib/python3.12/site-packages/jinja2/filters.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
urlize¶
urlize(eval_ctx: 'EvalContext', value: str, trim_url_limit: Optional[int] = None, nofollow: bool = False, target: Optional[str] = None, rel: Optional[str] = None, extra_schemes: Optional[Iterable[str]] = None) -> str
Convert URLs in text into clickable links.
Example
Jinja call:
Result:<a href="https://example.com" rel="noopener" target="_blank">https://example.com</a>
DocStrings
Source code in .venv/lib/python3.12/site-packages/jinja2/filters.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
|
wrap_in_elem¶
wrap_in_elem(text: str | None, tag: str, add_linebreaks: bool = False, **kwargs: Any) -> str
Wrap given text in an HTML/XML tag (with attributes).
DocStrings
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. |
{}
|
Source code in src/jinjarope/htmlfilters.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
xmlattr¶
xmlattr(eval_ctx: 'EvalContext', d: Mapping[str, Any], autospace: bool = True) -> str
Create an SGML/XML attribute string based on the items in a dict.
Example
Jinja call:
Result:<ul class="my_list" id="some-id">
Example
Jinja call:
Result:<div data-items="['a', 'b']" disabled="True">
DocStrings
Source code in .venv/lib/python3.12/site-packages/jinja2/filters.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|