A small, pythonic, HTML/XML writer
Project description
tagout
A small, pythonic, HTML/XML writer.
Installation
$ pip install tagout
Usage
This example shows how to use component functions to easily compose a document.
import contextlib
import sys
import io
from tagout import Document, class_names
@contextlib.contextmanager
def layout(doc):
"""Site layout component that allows transclusion of content."""
doc.write('<!DOCTYPE html>')
with doc.tag('html', lang='en'):
with doc.tag('head'):
with doc.tag('meta', charset='utf-8'):
pass
with doc.tag('meta', name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no'):
pass
with doc.tag(
'link',
rel='stylesheet',
href='https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css',
integrity='sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh',
crossorigin='anonymous'
):
pass
with doc.tag('title'):
doc.text('tagout')
with doc.tag('body'):
with doc.tag('div', _class='container'):
# transclude content
yield
def module_component(doc, module):
"""Site module name list item component."""
is_private = module.startswith('_')
# the `class_names` utility function can be used to conditionally add class names
with doc.tag('li', _class=class_names({'text-danger': is_private})):
doc.text(module)
def write_doc(stream):
doc = Document(stream, self_closing=True)
with layout(doc):
# the layout component is a context manager and allows for transclusion
# the leading underscore will be removed from keyword attributes
# this is useful for the `class` attribute since it happens to be a python keyword
with doc.tag('h1', _class='text-primary'):
doc.text('<tagout>')
with doc.tag('p'):
doc.text('A small, pythonic, HTML/XML writer.')
with doc.tag('h2'):
doc.text('Modules')
with doc.tag('ul'):
# loops can be used to generate multiple components
for module in sys.modules:
# the module component takes in data
module_component(doc, module)
if __name__ == '__main__':
# the document can be written to a file-like object
with open('index.html', 'w') as output_file:
write_doc(output_file)
# the document can be written to an io.StringIO instance to get a string
output_string = io.StringIO()
write_doc(output_string)
print(output_string.getvalue())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tagout-1.0.0.tar.gz
(3.8 kB
view details)
Built Distribution
File details
Details for the file tagout-1.0.0.tar.gz
.
File metadata
- Download URL: tagout-1.0.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb8e869080852622a07c40f84d2cde7a5aec9bd0e875cacf8b03080235727728 |
|
MD5 | 8b6bc775c658168fec8577b2fda6576a |
|
BLAKE2b-256 | 60b40b16e483e57cc42b0ea597ccf23528ef6b725f788e6c05a75ea614ff2dab |
File details
Details for the file tagout-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: tagout-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75649ac256ca516e3d03329612bc56a1561409c42a64e00cc6b37d0c8dc42ad3 |
|
MD5 | 38b65cfdbd8d99357ff0bee80740d908 |
|
BLAKE2b-256 | ebb6edaa7a60c49761aa10b3d39de64b2d7423444a62c10e3f0c5a261389602c |