Every text value you pass to a py2html element method —Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nishad12323/py2html/llms.txt
Use this file to discover all available pages before exploring further.
Label, Button, Heading, Link, Frame, and so on — is automatically processed by escapeCharacters() before it is stored in the tag dictionary. This single method does two things at once: it HTML-escapes dangerous characters so your strings are safe to embed in a page, and it expands a set of lightweight bracket-based tokens into real HTML inline elements. You can write [*bold*] in a heading’s text argument and get <strong>bold</strong> in the output — no string concatenation or manual HTML required.
How escapeCharacters() Works
escapeCharacters(text) is a method on Parent. It is called internally by every element method that accepts a text value, so you rarely need to call it yourself. The processing happens in two passes:
- HTML-escape special characters — turns
&,<,>,", and'into their safe HTML entity equivalents, and converts whitespace control characters into HTML equivalents. - Expand markup tokens — replaces bracketed shorthand tokens with the corresponding HTML tags.
Special Character Escaping
| Raw character | HTML output |
|---|---|
& | & |
< | < |
> | > |
" | " |
' | ' |
\n | <br> |
\t | (4 non-breaking spaces) |
Markup Token Reference
The full set of supported tokens is listed below. Tokens follow the pattern[token content token] — an opening tag made of [ + keyword, the content, then the closing keyword + ].
| Opening token | Closing token | HTML produced | Description |
|---|---|---|---|
[* | *] | <strong>…</strong> | Bold text |
[_ | _] | <em>…</em> | Italic text |
[` | `] | <code>…</code> | Inline code span |
[``` | ```] | <pre><code>…</code></pre> | Preformatted code block |
[c | c] | <center>…</center> | Centered text |
[xxs | xxs] | <span style='font-size: 0.4rem;'>…</span> | Extra-extra-small |
[xs | xs] | <span style='font-size: 0.6rem;'>…</span> | Extra-small |
[s | s] | <span style='font-size: 0.8rem;'>…</span> | Small |
[m | m] | <span style='font-size: 1rem;'>…</span> | Medium (base size) |
[l | l] | <span style='font-size: 1.2rem;'>…</span> | Large |
[xl | xl] | <span style='font-size: 1.5rem;'>…</span> | Extra-large |
[xxl | xxl] | <span style='font-size: 2rem;'>…</span> | Extra-extra-large |
[xxxl | xxxl] | <span style='font-size: 3rem;'>…</span> | Triple-extra-large |
Font-size tokens require a trailing space on the opening token (
[xl not [xl) and a leading space on the closing token ( xl] not xl]). This avoids false matches inside ordinary words.Bold, italic, code, and center tokens ([*, *], [_, _], [`, `], [c, c]) match purely on their bracket sequences — no surrounding spaces required.Controlling Escaping with the escape Parameter
Two places in the API expose an escape flag that bypasses escapeCharacters() entirely:
TextNode(text, escape=True)
TextNode adds raw text to the tag list. Pass escape=False to inject pre-built HTML strings without double-escaping.
__call__(*args, escape=True, sep=" ")
Calling a Parent directly is shorthand for TextNode. The same escape keyword applies:
Practical Examples
Token Nesting and Ordering
escapeCharacters applies all replacements in a fixed sequence. Outer tokens are replaced independently of inner ones — tokens do not nest. For example, [*bold and [_italic_]*] will produce <strong>bold and <em>italic</em></strong> only because both patterns are replaced in the same pass; py2html does not validate bracket pairing.
Token replacement is purely textual (Python
str.replace). There is no parser, so mismatched or overlapping tokens may produce malformed HTML. Keep each token on its own level and avoid nesting the same token type.