# ptag
[![ci-badge]][ci-url] [![pypi-badge]][pypi-url] [![MIT-badge]][MIT-url] [![black-badge]][black-url]
> generate html/svg tags hierarchy with context manager
- use ⭐️ context manager ⭐️ to create tag hierarchy
- create value-less(boolean) attributes with positional argument
- handy for using with [UnoCSS] attributify mode
- all standard html and svg elements are exported as functions
- pure python, no external dependencies
- high test coverage
## Quick Start
- Installation: `pip install ptag`
- base signature
- `element(content = None, *args, **kwargs) -> Tag`
```python
# common elements
from ptag import div, img, p, ul, li, label, input_,
# for creating custom element
from ptag import Tag
# for pretty print
from ptag import prettify
# empty tag
print(div())
# <div />
# None content is ignored
print(div(None))
# <div />
# empty string content creates closing tag
print(div(""))
# <div></div>
# tag as content
print(div(img(src="url"), id="bar"))
# <div id="bar"><img src="url"/></div>
# content mix with strings and tags
print(div(["foo", img(src="url"), "bar")])
# <div>foo<img src="url"/>bar</div>
```
- use with context manager
```python
with ul() as bullets:
li("foo")
li("bar")
print(bullets)
# <ul><li>foo</li><li>bar</li></ul>
```
- pretty print
```python
print(bullets.prettify())
# <ul>
# <li>foo</li>
# <li>bar</li>
# </ul>
```
- use trailing underscore to work around python keyword and built-in functions
- attributes:
- `class_` -> `class`
- `for_` -> `for`
- elements:
- `del_` -> `del`
- `input_` -> `input`
- `map_` -> `map`
- `object_` -> `object`
```python
print(label("foo", for_="bar"))
# <label for="bar">foo</label>
print(input_(None, class_="foo", name="bar", type="checkbox", value="baz"))
# <input name="bar" type="checkbox" value="baz"/>
```
- position args -> value-less attribute.
- boolean attribute: eg. `checked`, `disabled`, `selected`
- assign tailwind classes with [UnoCSS] attributify mode
```python
print(div("foo", "clear-both", "m-2", "rounded", id="baz"))
# <div clear-both m-2 rounded id="baz">foo</div>
```
- keyword argument with value None is ignored
```python
tag = div(None, "m-2", "rounded", id="baz", style=None)
print(tag)
# <div m-2 rounded id="baz" />
```
- append content and attributes to existing tag
```python
tag = div()
tag.affix(p("bar"), "m-2", "rounded", id="baz")
print(tag)
# <div m-2 rounded id="baz"><p>bar</p></div>
```
- create custom element
- signature:
- `Tag(name: str, content = None, *args, **kwargs) -> str`
```python
my_tag = Tag("MyTag", "foo", "bar", "corge", id="baz", class_="qux")
print(my_tag)
# <MyTag bar corge id="baz" class="qux">foo</MyTag>
```
- more examples could be found in [tests] package
## Limitations
- `prettify()` method doesn't support attribute without value
- use kwargs instead of positional args if prettifying is needed
- eg. `selected` -> `selected=""`
## Need Help?
- [github issue]
- [x.com posts]
- [contact the author]
[black-badge]: https://img.shields.io/badge/code%20style-black-000000.svg
[black-url]: https://github.com/psf/black
[ci-badge]: https://github.com/hoishing/ptag/actions/workflows/ci.yml/badge.svg
[ci-url]: https://github.com/hoishing/ptag/actions/workflows/ci.yml
[contact the author]: https://hoishing.github.io
[github issue]: https://github.com/hoishing/ptag/issues
[MIT-badge]: https://img.shields.io/github/license/hoishing/ptag
[MIT-url]: https://opensource.org/licenses/MIT
[pypi-badge]: https://img.shields.io/pypi/v/ptag
[pypi-url]: https://pypi.org/project/ptag/
[tests]: https://github.com/hoishing/ptag/tree/main/tests
[UnoCSS]: https://github.com/unocss/unocss
[x.com posts]: https://x.com/hoishing
Raw data
{
"_id": null,
"home_page": "https://github.com/hoishing/ptag",
"name": "ptag",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.12",
"maintainer_email": null,
"keywords": "template, html, xml, svg, tag",
"author": "Kelvin Ng",
"author_email": "hoishing@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/13/46/7ed2323bf564157bfd9fccf443b9721a42cca57bb890e8e80c41cb41023e/ptag-0.2.3.tar.gz",
"platform": null,
"description": "# ptag\n\n[![ci-badge]][ci-url] [![pypi-badge]][pypi-url] [![MIT-badge]][MIT-url] [![black-badge]][black-url]\n\n> generate html/svg tags hierarchy with context manager\n\n- use \u2b50\ufe0f context manager \u2b50\ufe0f to create tag hierarchy\n- create value-less(boolean) attributes with positional argument\n - handy for using with [UnoCSS] attributify mode\n- all standard html and svg elements are exported as functions\n- pure python, no external dependencies\n- high test coverage\n\n## Quick Start\n\n- Installation: `pip install ptag`\n- base signature\n - `element(content = None, *args, **kwargs) -> Tag`\n\n```python\n# common elements\nfrom ptag import div, img, p, ul, li, label, input_,\n# for creating custom element\nfrom ptag import Tag \n# for pretty print\nfrom ptag import prettify \n\n# empty tag\nprint(div())\n# <div />\n\n# None content is ignored\nprint(div(None))\n# <div />\n\n# empty string content creates closing tag\nprint(div(\"\"))\n# <div></div>\n\n# tag as content\nprint(div(img(src=\"url\"), id=\"bar\")) \n# <div id=\"bar\"><img src=\"url\"/></div>\n\n# content mix with strings and tags\nprint(div([\"foo\", img(src=\"url\"), \"bar\")])\n# <div>foo<img src=\"url\"/>bar</div>\n```\n\n- use with context manager\n\n```python\nwith ul() as bullets:\n li(\"foo\")\n li(\"bar\")\n\nprint(bullets)\n# <ul><li>foo</li><li>bar</li></ul>\n```\n\n- pretty print\n\n```python\nprint(bullets.prettify())\n# <ul>\n# <li>foo</li>\n# <li>bar</li>\n# </ul>\n```\n\n- use trailing underscore to work around python keyword and built-in functions\n- attributes:\n - `class_` -> `class`\n - `for_` -> `for`\n- elements:\n - `del_` -> `del`\n - `input_` -> `input`\n - `map_` -> `map`\n - `object_` -> `object`\n\n```python\nprint(label(\"foo\", for_=\"bar\"))\n# <label for=\"bar\">foo</label>\n\nprint(input_(None, class_=\"foo\", name=\"bar\", type=\"checkbox\", value=\"baz\"))\n# <input name=\"bar\" type=\"checkbox\" value=\"baz\"/>\n```\n\n- position args -> value-less attribute.\n - boolean attribute: eg. `checked`, `disabled`, `selected`\n - assign tailwind classes with [UnoCSS] attributify mode\n\n```python\nprint(div(\"foo\", \"clear-both\", \"m-2\", \"rounded\", id=\"baz\"))\n# <div clear-both m-2 rounded id=\"baz\">foo</div>\n```\n\n- keyword argument with value None is ignored\n\n```python\ntag = div(None, \"m-2\", \"rounded\", id=\"baz\", style=None) \nprint(tag) \n# <div m-2 rounded id=\"baz\" />\n```\n\n- append content and attributes to existing tag\n\n```python\ntag = div()\ntag.affix(p(\"bar\"), \"m-2\", \"rounded\", id=\"baz\") \nprint(tag) \n# <div m-2 rounded id=\"baz\"><p>bar</p></div>\n```\n\n- create custom element\n- signature:\n - `Tag(name: str, content = None, *args, **kwargs) -> str`\n\n```python\nmy_tag = Tag(\"MyTag\", \"foo\", \"bar\", \"corge\", id=\"baz\", class_=\"qux\")\nprint(my_tag) \n# <MyTag bar corge id=\"baz\" class=\"qux\">foo</MyTag>\n```\n\n- more examples could be found in [tests] package\n\n## Limitations\n\n- `prettify()` method doesn't support attribute without value\n - use kwargs instead of positional args if prettifying is needed\n - eg. `selected` -> `selected=\"\"`\n\n## Need Help?\n\n- [github issue]\n- [x.com posts]\n- [contact the author]\n\n[black-badge]: https://img.shields.io/badge/code%20style-black-000000.svg\n[black-url]: https://github.com/psf/black\n[ci-badge]: https://github.com/hoishing/ptag/actions/workflows/ci.yml/badge.svg\n[ci-url]: https://github.com/hoishing/ptag/actions/workflows/ci.yml\n[contact the author]: https://hoishing.github.io\n[github issue]: https://github.com/hoishing/ptag/issues\n[MIT-badge]: https://img.shields.io/github/license/hoishing/ptag\n[MIT-url]: https://opensource.org/licenses/MIT\n[pypi-badge]: https://img.shields.io/pypi/v/ptag\n[pypi-url]: https://pypi.org/project/ptag/\n[tests]: https://github.com/hoishing/ptag/tree/main/tests\n[UnoCSS]: https://github.com/unocss/unocss\n[x.com posts]: https://x.com/hoishing\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "generate html/svg tags hierarchy with context manager",
"version": "0.2.3",
"project_urls": {
"Homepage": "https://github.com/hoishing/ptag",
"Repository": "https://github.com/hoishing/ptag"
},
"split_keywords": [
"template",
" html",
" xml",
" svg",
" tag"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f1e53ae4393f561e1ed6e37c97ba11d886e514cb95f9711a43674bcc1e700027",
"md5": "4ff96e831169c3d21bc72e34a7f98ad4",
"sha256": "10e7e237e71b31989a6abab84bdad968c2b9f2c0248448855f1d5fe136b3c5d0"
},
"downloads": -1,
"filename": "ptag-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4ff96e831169c3d21bc72e34a7f98ad4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.12",
"size": 8667,
"upload_time": "2024-10-06T18:01:03",
"upload_time_iso_8601": "2024-10-06T18:01:03.692396Z",
"url": "https://files.pythonhosted.org/packages/f1/e5/3ae4393f561e1ed6e37c97ba11d886e514cb95f9711a43674bcc1e700027/ptag-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "13467ed2323bf564157bfd9fccf443b9721a42cca57bb890e8e80c41cb41023e",
"md5": "6c5bc924f61ec049d07ba44821369952",
"sha256": "872039eb3b5c007315bca77491b410b56aa807bb70036eb134bff4efc7ad31c4"
},
"downloads": -1,
"filename": "ptag-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "6c5bc924f61ec049d07ba44821369952",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 7605,
"upload_time": "2024-10-06T18:01:05",
"upload_time_iso_8601": "2024-10-06T18:01:05.292745Z",
"url": "https://files.pythonhosted.org/packages/13/46/7ed2323bf564157bfd9fccf443b9721a42cca57bb890e8e80c41cb41023e/ptag-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-06 18:01:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hoishing",
"github_project": "ptag",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ptag"
}