html5tagger


Namehtml5tagger JSON
Version 1.3.0 PyPI version JSON
download
home_pagehttps://github.com/sanic-org/html5tagger
SummaryPythonic HTML generation/templating (no template files)
upload_time2023-03-28 05:59:34
maintainer
docs_urlNone
authorSanic Community
requires_python>=3.7
license
keywords html html5 templating jinja2
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # HTML5 Generation with html5tagger: Fast, Pure Python, No Dependencies

If you're looking for a more efficient and streamlined way to generate HTML5, look no further than html5tagger! This module provides a simplified HTML5 syntax, so you can create your entire document template using only Python. Say goodbye to the clunky and error-prone process of manually writing HTML tags.

With html5tagger, you can safely and quickly generate HTML5 without any dependencies, making it the perfect solution for developers who value speed and simplicity. And with its pure Python implementation, you'll never have to worry about compatibility issues or adding extra libraries to your project.

Ready to streamline your page rendering process? It is super fast to get started. Trust us, once you try html5tagger, you'll never go back to Jinja2 or manual HTML writing again!

```sh
pip install html5tagger
```

## Intro

html5tagger provides two starting points for HTML generation: `E` as an empty builder for creating HTML snippets, or `Document` for generating full HTML documents with a DOCTYPE declaration. Both produce a `Builder` object, in case you need that for type annotations.

Create a snippet and add tags by dot notation:
```python
E.p("Powered by:").br.a(href="...")("html5tagger")
```
```html
<p>Powered by:<br><a href="...">html5tagger</a>
```

A complete example with template variables and other features:

```python
from html5tagger import Document, E

# Create a document
doc = Document(
    E.TitleText_,           # The first argument is for <title>, adding variable TitleText
    lang="en",              # Keyword arguments for <html> attributes

    # Just list the resources you need, no need to remember link/script tags
    _urls=[ "style.css", "favicon.png", "manifest.json" ]
)

# Upper case names are template variables. You can modify them later.
doc.Head_
doc.h1.TitleText_("Demo")   # Goes inside <h1> and updates <title> as well

# This has been a hard problem for DOM other such generators:
doc.p("A paragraph with ").a("a link", href="/files")(" and ").em("formatting")

# Use with for complex nesting (not often needed)
with doc.table(id="data"):
    doc.tr.th("First").th("Second").th("Third")
    doc.TableRows_

# Let's add something to the template variables
doc.Head._script("console.log('</script> escaping is weird')")

table = doc.TableRows
for row in range(10):
    table.tr
    for col in range(3):
        table.td(row * col)

# Or remove the table data we just added
doc.TableRows = None
```

You can `str(doc)` to get the HTML code, and using `doc` directly usually has the desired effect as well (e.g. giving HTML responses). Jupyter Notebooks render it as HTML. For debugging, use `repr(doc)` where the templating variables are visible:

```html
>>> doc
《Document Builder》
<!DOCTYPE html><html lang=en><meta charset="utf-8">
<title>《TitleText:Demo》</title>
<link href="style.css" rel=stylesheet>
<link href="favicon.png" rel=icon type="image/png">
<link href="manifest.json" rel=manifest>
《Head:<script>console.log('<\/script> escaping is weird')</script>》
<h1>《TitleText:Demo》</h1>
<p>A paragraph with <a href="/files">a link</a> and <em>formatting</em>
<table id=data>
  <tr><th>First<th>Second<th>Third
  《TableRows》
</table>
```

The actual HTML output is similar. No whitespace is added to the document, it is all on one line unless the content contains newlines. You may notice that `body` and other familiar tags are missing and that the escaping is very minimal. This is HTML5: the document is standards-compliant with a lot less cruft.

## Templating

Use template variables to build a document once and only update the dynamic parts at render time for faster performance. Access template variables via doc.TitleText and add content in parenthesis after the tag name. The underscore at the end of a tag name indicates the tag is added to the document and can have content in parenthesis, but any further tags on the same line go to the original document, not the template.

## Nesting

In HTML5 elements such as `<p>` do not need any closing tag, so we can keep adding content without worrying of when it should close. This module does not use closing tags for any elements where those are optional or forbidden.

A tag is automatically closed when you add content to it or when another tag is added. Setting attributes alone does not close an element. Use `(None)` to close an empty element if any subsequent content is not meant to go inside it, e.g. `doc.script(None, src="...")`.

For elements like `<table>` and `<ul>`, you can use `with` blocks, pass sub-snippet arguments, or add a template variable. Unlike adding another tag, adding a template does NOT close its preceding tag but instead the variable goes inside any open element.

```python
with doc.ul:  # Nest using with
    doc.li("Write HTML in Python")
    doc.li("Simple syntax").ul(id="inner").InnerList_  # Nest using template
    doc.li("No need for brackets or closing tags")
    doc.ul(E.li("Easy").li("Peasy"))  # Nest using (...)
```

## Escaping

All content and attributes are automatically escaped. For instance, we can put the entire document into an iframe's srcdoc attribute where only the minimal but necessary escaping is applied. Use custom methods `_script`, `_style` and `_comment` for corresponding inline formats, to follow their custom escaping rules.

```python
doc = Document("Escaping & Context")
doc._style('h1::after {content: "</Style>"}').h1("<Escape>")
doc._comment("All-->OK")
doc.iframe(srcdoc=Document().p("&amp; is used for &"))
```

```html
<!DOCTYPE html><meta charset="utf-8"><title>Escaping &amp; Context</title>
<style>h1::after {content: "<\/Style>"}</style><h1>&lt;Escape></h1>
<!--All‒‒>OK-->
<iframe srcdoc="<!DOCTYPE html><p>&amp;amp;amp; is used for &amp;amp;"></iframe>
```

Works perfectly in browsers.

## Name mangling and boolean attributes

Underscore at the end of name is ignored so that `class_` and `for_` among other attributes may be used despite being reserved words in Python. Other underscores convert into hyphens.

⚠️ The above only is true for HTML elements and attributes, but template placeholders only use an ending underscore to denote that the it is to be placed on the document, rather than be fetched for use.

Boolean values convert into short attributes.

```python
E.input(type="checkbox", id="somebox", checked=True).label(for_="somebox", aria_role="img")("🥳")
```

```html
<input type=checkbox id=somebox checked><label for=somebox aria-role=img>🥳</label>
```

## Preformatted HTML

All content is automatically escaped, unless it provides an `__html__` method that returns a string in HTML format. Similarly, the builder objects of this module expose `__html__` and `_repr_html_` accessors that allow them to be rendered as HTML in Jupyter Notebooks and various other systems that follow this convention.

Any preformatted HTML may be wrapped in `html5tagger.HTML(string_of_html)` to avoid it being escaped when included in a document, as the HTML class has those accessors.

⚠️ Do not use `HTML()` for text, in particular not on messages sent by users, that may contain HTML that you didn't intend to execute as HTML.

## Performance

```python
%timeit str(Document("benchmarking", lang="en", _urls=("foo.js", "bar.js")))
14 µs ± 153 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
```

Jinja2 renders similar document from memory template within about 10 µs but it doesn't need to format any of the HTML. When Templating is similarly used with html5tagger, the rendering times drop to about 4 µs.

In the above benchmark html5tagger created the entire document from scratch, one element and attribute at a time. Unless you are creating very large documents dynamically, this should be quite fast enough.


## Further development

There have been no changes to the tagging API since 2018 when this module was brought to production use, and thus the interface is considered stable.

In 2023 support for templating was added, allowing documents to be preformatted for all their static parts (as long strings), with only templates filled in between. This is a work on progress and has not been optimized yet.

Additionally, `_script` and `_style` special methods were added in 2023. These may eventually replace also the non-underscored automatic versions but for now a separate method was easier to implement.

Pull requests are still welcome.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sanic-org/html5tagger",
    "name": "html5tagger",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "HTML,HTML5,templating,Jinja2",
    "author": "Sanic Community",
    "author_email": "tronic@noreply.users.github.com",
    "download_url": "https://files.pythonhosted.org/packages/9e/02/2ae5f46d517a2c1d4a17f2b1e4834c2c7cc0fb3a69c92389172fa16ab389/html5tagger-1.3.0.tar.gz",
    "platform": null,
    "description": "# HTML5 Generation with html5tagger: Fast, Pure Python, No Dependencies\n\nIf you're looking for a more efficient and streamlined way to generate HTML5, look no further than html5tagger! This module provides a simplified HTML5 syntax, so you can create your entire document template using only Python. Say goodbye to the clunky and error-prone process of manually writing HTML tags.\n\nWith html5tagger, you can safely and quickly generate HTML5 without any dependencies, making it the perfect solution for developers who value speed and simplicity. And with its pure Python implementation, you'll never have to worry about compatibility issues or adding extra libraries to your project.\n\nReady to streamline your page rendering process? It is super fast to get started. Trust us, once you try html5tagger, you'll never go back to Jinja2 or manual HTML writing again!\n\n```sh\npip install html5tagger\n```\n\n## Intro\n\nhtml5tagger provides two starting points for HTML generation: `E` as an empty builder for creating HTML snippets, or `Document` for generating full HTML documents with a DOCTYPE declaration. Both produce a `Builder` object, in case you need that for type annotations.\n\nCreate a snippet and add tags by dot notation:\n```python\nE.p(\"Powered by:\").br.a(href=\"...\")(\"html5tagger\")\n```\n```html\n<p>Powered by:<br><a href=\"...\">html5tagger</a>\n```\n\nA complete example with template variables and other features:\n\n```python\nfrom html5tagger import Document, E\n\n# Create a document\ndoc = Document(\n    E.TitleText_,           # The first argument is for <title>, adding variable TitleText\n    lang=\"en\",              # Keyword arguments for <html> attributes\n\n    # Just list the resources you need, no need to remember link/script tags\n    _urls=[ \"style.css\", \"favicon.png\", \"manifest.json\" ]\n)\n\n# Upper case names are template variables. You can modify them later.\ndoc.Head_\ndoc.h1.TitleText_(\"Demo\")   # Goes inside <h1> and updates <title> as well\n\n# This has been a hard problem for DOM other such generators:\ndoc.p(\"A paragraph with \").a(\"a link\", href=\"/files\")(\" and \").em(\"formatting\")\n\n# Use with for complex nesting (not often needed)\nwith doc.table(id=\"data\"):\n    doc.tr.th(\"First\").th(\"Second\").th(\"Third\")\n    doc.TableRows_\n\n# Let's add something to the template variables\ndoc.Head._script(\"console.log('</script> escaping is weird')\")\n\ntable = doc.TableRows\nfor row in range(10):\n    table.tr\n    for col in range(3):\n        table.td(row * col)\n\n# Or remove the table data we just added\ndoc.TableRows = None\n```\n\nYou can `str(doc)` to get the HTML code, and using `doc` directly usually has the desired effect as well (e.g. giving HTML responses). Jupyter Notebooks render it as HTML. For debugging, use `repr(doc)` where the templating variables are visible:\n\n```html\n>>> doc\n\u300aDocument Builder\u300b\n<!DOCTYPE html><html lang=en><meta charset=\"utf-8\">\n<title>\u300aTitleText:Demo\u300b</title>\n<link href=\"style.css\" rel=stylesheet>\n<link href=\"favicon.png\" rel=icon type=\"image/png\">\n<link href=\"manifest.json\" rel=manifest>\n\u300aHead:<script>console.log('<\\/script> escaping is weird')</script>\u300b\n<h1>\u300aTitleText:Demo\u300b</h1>\n<p>A paragraph with <a href=\"/files\">a link</a> and <em>formatting</em>\n<table id=data>\n  <tr><th>First<th>Second<th>Third\n  \u300aTableRows\u300b\n</table>\n```\n\nThe actual HTML output is similar. No whitespace is added to the document, it is all on one line unless the content contains newlines. You may notice that `body` and other familiar tags are missing and that the escaping is very minimal. This is HTML5: the document is standards-compliant with a lot less cruft.\n\n## Templating\n\nUse template variables to build a document once and only update the dynamic parts at render time for faster performance. Access template variables via doc.TitleText and add content in parenthesis after the tag name. The underscore at the end of a tag name indicates the tag is added to the document and can have content in parenthesis, but any further tags on the same line go to the original document, not the template.\n\n## Nesting\n\nIn HTML5 elements such as `<p>` do not need any closing tag, so we can keep adding content without worrying of when it should close. This module does not use closing tags for any elements where those are optional or forbidden.\n\nA tag is automatically closed when you add content to it or when another tag is added. Setting attributes alone does not close an element. Use `(None)` to close an empty element if any subsequent content is not meant to go inside it, e.g. `doc.script(None, src=\"...\")`.\n\nFor elements like `<table>` and `<ul>`, you can use `with` blocks, pass sub-snippet arguments, or add a template variable. Unlike adding another tag, adding a template does NOT close its preceding tag but instead the variable goes inside any open element.\n\n```python\nwith doc.ul:  # Nest using with\n    doc.li(\"Write HTML in Python\")\n    doc.li(\"Simple syntax\").ul(id=\"inner\").InnerList_  # Nest using template\n    doc.li(\"No need for brackets or closing tags\")\n    doc.ul(E.li(\"Easy\").li(\"Peasy\"))  # Nest using (...)\n```\n\n## Escaping\n\nAll content and attributes are automatically escaped. For instance, we can put the entire document into an iframe's srcdoc attribute where only the minimal but necessary escaping is applied. Use custom methods `_script`, `_style` and `_comment` for corresponding inline formats, to follow their custom escaping rules.\n\n```python\ndoc = Document(\"Escaping & Context\")\ndoc._style('h1::after {content: \"</Style>\"}').h1(\"<Escape>\")\ndoc._comment(\"All-->OK\")\ndoc.iframe(srcdoc=Document().p(\"&amp; is used for &\"))\n```\n\n```html\n<!DOCTYPE html><meta charset=\"utf-8\"><title>Escaping &amp; Context</title>\n<style>h1::after {content: \"<\\/Style>\"}</style><h1>&lt;Escape></h1>\n<!--All\u2012\u2012>OK-->\n<iframe srcdoc=\"<!DOCTYPE html><p>&amp;amp;amp; is used for &amp;amp;\"></iframe>\n```\n\nWorks perfectly in browsers.\n\n## Name mangling and boolean attributes\n\nUnderscore at the end of name is ignored so that `class_` and `for_` among other attributes may be used despite being reserved words in Python. Other underscores convert into hyphens.\n\n\u26a0\ufe0f The above only is true for HTML elements and attributes, but template placeholders only use an ending underscore to denote that the it is to be placed on the document, rather than be fetched for use.\n\nBoolean values convert into short attributes.\n\n```python\nE.input(type=\"checkbox\", id=\"somebox\", checked=True).label(for_=\"somebox\", aria_role=\"img\")(\"\ud83e\udd73\")\n```\n\n```html\n<input type=checkbox id=somebox checked><label for=somebox aria-role=img>\ud83e\udd73</label>\n```\n\n## Preformatted HTML\n\nAll content is automatically escaped, unless it provides an `__html__` method that returns a string in HTML format. Similarly, the builder objects of this module expose `__html__` and `_repr_html_` accessors that allow them to be rendered as HTML in Jupyter Notebooks and various other systems that follow this convention.\n\nAny preformatted HTML may be wrapped in `html5tagger.HTML(string_of_html)` to avoid it being escaped when included in a document, as the HTML class has those accessors.\n\n\u26a0\ufe0f Do not use `HTML()` for text, in particular not on messages sent by users, that may contain HTML that you didn't intend to execute as HTML.\n\n## Performance\n\n```python\n%timeit str(Document(\"benchmarking\", lang=\"en\", _urls=(\"foo.js\", \"bar.js\")))\n14 \u00b5s \u00b1 153 ns per loop (mean \u00b1 std. dev. of 7 runs, 100,000 loops each)\n```\n\nJinja2 renders similar document from memory template within about 10 \u00b5s but it doesn't need to format any of the HTML. When Templating is similarly used with html5tagger, the rendering times drop to about 4 \u00b5s.\n\nIn the above benchmark html5tagger created the entire document from scratch, one element and attribute at a time. Unless you are creating very large documents dynamically, this should be quite fast enough.\n\n\n## Further development\n\nThere have been no changes to the tagging API since 2018 when this module was brought to production use, and thus the interface is considered stable.\n\nIn 2023 support for templating was added, allowing documents to be preformatted for all their static parts (as long strings), with only templates filled in between. This is a work on progress and has not been optimized yet.\n\nAdditionally, `_script` and `_style` special methods were added in 2023. These may eventually replace also the non-underscored automatic versions but for now a separate method was easier to implement.\n\nPull requests are still welcome.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Pythonic HTML generation/templating (no template files)",
    "version": "1.3.0",
    "split_keywords": [
        "html",
        "html5",
        "templating",
        "jinja2"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9b122f5d43ee912ea14a6baba4b3db6d309b02d932e3b7074c3339b4aded98ff",
                "md5": "e6764c478646323260e2471dd710f46f",
                "sha256": "ce14313515edffec8ed8a36c5890d023922641171b4e6e5774ad1a74998f5351"
            },
            "downloads": -1,
            "filename": "html5tagger-1.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e6764c478646323260e2471dd710f46f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 10956,
            "upload_time": "2023-03-28T05:59:32",
            "upload_time_iso_8601": "2023-03-28T05:59:32.524328Z",
            "url": "https://files.pythonhosted.org/packages/9b/12/2f5d43ee912ea14a6baba4b3db6d309b02d932e3b7074c3339b4aded98ff/html5tagger-1.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9e022ae5f46d517a2c1d4a17f2b1e4834c2c7cc0fb3a69c92389172fa16ab389",
                "md5": "5f2b0c139ecd9d00f314e9c8c2ad99b3",
                "sha256": "84fa3dfb49e5c83b79bbd856ab7b1de8e2311c3bb46a8be925f119e3880a8da9"
            },
            "downloads": -1,
            "filename": "html5tagger-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5f2b0c139ecd9d00f314e9c8c2ad99b3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 14196,
            "upload_time": "2023-03-28T05:59:34",
            "upload_time_iso_8601": "2023-03-28T05:59:34.642616Z",
            "url": "https://files.pythonhosted.org/packages/9e/02/2ae5f46d517a2c1d4a17f2b1e4834c2c7cc0fb3a69c92389172fa16ab389/html5tagger-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-28 05:59:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "sanic-org",
    "github_project": "html5tagger",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "html5tagger"
}
        
Elapsed time: 0.51669s