A library for building HTML documents with a simple and learnable syntax
Project description
PyHTML Enhanced
A library for building HTML documents with a simple and learnable syntax, inspired by, and similar to the original PyHTML library, but with improved documentation and type safety.
Features
-
Inline documentation and type safety for all tags. Documentation for tags is pulled from MDN Web Docs.
-
A modern and readable codebase.
-
No dependencies.
-
100% test coverage
Usage
import pyhtml as h
my_website = h.html(
h.head(
h.title("Hello, world!"),
h.script(src="http://example.com/script.js"),
),
h.body(
h.h1("Hello, world!"),
h.p("This is my amazing website rendered with PyHTML Enhanced!"),
),
)
# Converting it to a string is all you need to do to render it!
print(str(my_website))
This will produce the following HTML code:
<html>
<head>
<title>
Hello, world!
</title>
<script src="http://example.com/script.js" type="text/javascript"></script>
</head>
<body>
<h1>
Hello, world!
</h1>
<p>
This is my amazing website rendered with PyHTML Enhanced!
</p>
</body>
</html>
Differences to PyHTML
There are some minor usage differences compared to the original PyHTML library.
Uninstantiated classes are only rendered if they are given as the child of an instantiated element.
>>> br
<class 'pyhtml.__tags.generated.br'>
>>> html(body(br))
<html>
<body>
<br>
</body>
</html>
Calling an instance of a Tag
will return a new tag containing all elements of
the original tag combined with the new attributes and children, but will not
modify the original instance, as I found the old behaviour confusing and
bug-prone.
>>> para = p("Base paragraph")
>>> para2 = para("Extra text")
>>> para2
<p>
Base paragraph
Extra text
</p>
>>> para
<p>
Base paragraph
</p>
Known issues
There are a couple of things I haven't gotten round to sorting out yet
- Add default attributes to more tags
- Some tags (eg
<pre>
,<script>
) currently aren't properly implemented and escape their contents.
How it works
Since there are so many HTML tags, it would be extremely tedious to document
them all manually. In the meta
directory, you will find code that solves this
problem with the following steps:
-
Download the Markdown source for MDN's documentation of all HTML elements.
-
Parse the markdown to gather all tag names and descriptions, discarding garbage data and obsolete tags.
-
Use data from a YAML configuration file (
meta/tags.yml
) to gather information on suggested attributes and base classes to use for each tag. -
Generate Python code to represent all of these tags, including their documentation.
Credits
Cenkalti/PyHTML
Cenk Altı's work was used as a source of inspiration and reference. Although
all the code in pyhtml-enhanced
was written by me, I want to thank them for
the significant help their hard work provided while creating this project.
MDN Web Docs
Almost all of the documentation was gathered from the MDN Web Docs. It's super neat that all their documentation is open (licensed as CC-BY-SA-2.5 if you're interested).
License
All code in this project is licensed under the MIT License.
However, the documentation found in
pyhtml/__tags/generated.py
was copied from
MDN Web Docs, and is licensed under
CC-BY-SA-2.5. See
LICENSE_DOCS.md for a copy of the license.
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
Built Distribution
File details
Details for the file pyhtml_enhanced-1.0.4.tar.gz
.
File metadata
- Download URL: pyhtml_enhanced-1.0.4.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.11.6 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d901a69351a936708cb0b6bde1e1ead2cf8648c92713a850bbf9a21e11ca6f34 |
|
MD5 | ca47c96f25edd193ed8d7f067673a2dd |
|
BLAKE2b-256 | 056c9c554585e5c64e6da90e2b0ce12ef89db1529966a08ba6dab777164d93d6 |
File details
Details for the file pyhtml_enhanced-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: pyhtml_enhanced-1.0.4-py3-none-any.whl
- Upload date:
- Size: 25.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.11.6 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94da2b91ccf724e835926ac1bf0422701c31eee308a75b23bcf04f96f94ac136 |
|
MD5 | 361a8abc82e51ec9355dc00e98551959 |
|
BLAKE2b-256 | 621bcc2ee51a8741df6b37f59016ac317ca61a3c4b34ca53492f85c5dc48e044 |