A simple HTML5 generator based-on Python's OOP
Project description
HTML5 generator from your Python code
Features
- HTML5 tag creation with classes such as
A()for<a></a>,Hr()for<hr />, etc. - Pretty printing the html content with
pretty()method. - Create multiple html pages using
HTMLPageclass. - Custom tag creation by inheriting
Elementclass which declared atmarkupify.tags
Goals
- Generate
*.htmlfiles after creating html content. - Create an
CSSPageclass to create some css contents. - Generate
*.cssfiles. - Show tag structure like
<body> -> <div> -> h1 -> "This is heading one!"or something. - Create a page object with specific head and body.
Installation
pip install markupify
Usage
from markupify.page import HTMLPage
from markupify.tags import Meta, Link, Title, Div, H, Comment
# Alternatively, you can import these classes from markupify directly:
# from markupify import HTMLPage, Meta, Link, Title, Div, H, Comment
page = HTMLPage()
meta = Meta(charset="UTF-8")
link = Link(href="css/styles.css", rel="stylesheet")
title = Title("My first website")
div = Div(
tag_content=H(
tag_content="Greetings text"
)
)
comment = Comment("This is a comment")
page.add_tag_to_head(meta, link, title)
page.add_tag_to_body(comment, div)
print(page)
Output:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><link href="css/styles.css" rel="stylesheet" /><title>My first website</title></head><body><!-- This is a comment --><div><h1>Greetings text</h1></div></body></html>
To prettify that, use the pretty() method of the page object:
print(page.pretty())
Output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link href="css/styles.css" rel="stylesheet"/>
<title>
My first website
</title>
</head>
<body>
<!-- This is a comment -->
<div>
<h1>
Greetings text
</h1>
</div>
</body>
</html>
Customizing
Devs! Maybe I forgot to create some tags you need. Feel free to create them yourself by inheriting from the
Elementclass.
For example, create a double tag:
from typing import Optional, Union
from markupify.tags import Element
class MyCustomTag(Element):
def __init__(self, tag_content: Optional[Union[str, Element]], **props):
"""
A tag class to represent <my_tag> tag.
"""
super().__init__(tag_name="my_tag", tag_content=tag_content, **props)
my_tag = MyCustomTag("This my custom tag! 🥳")
print(my_tag)
Output:
<my_tag>This my custom tag! 🥳</my_tag>
Add class property with add_properties
my_tag.add_properties(_class="custom my-tag")
print(my_tag)
Output:
<my_tag class="custom my-tag">This my custom tag! 🥳</my_tag>
[!NOTE] Some keywords like
classare built-in names in Python. So you need to use them with underscore before them. For example, instead ofclass, write_classand everything will be fine.
Create a single tag:
from markupify.tags import Element
class Vl(Element):
def __init__(self, **props):
"""
A tag class to represent <vl> (Vertical Line) tag.
"""
super().__init__(tag_name="vl", has_end_tag=False, **props)
vl = Vl()
print(vl)
Output:
<vl />
Add width property with add_property
vl.add_property("width", "5px")
print(vl)
Output:
<vl width="5px" />
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markupify-1.1.0.tar.gz.
File metadata
- Download URL: markupify-1.1.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.1 CPython/3.9.18 Linux/6.5.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a19bf4a76b15970e751a216420689b3546643b199582297268e77d7c5814688
|
|
| MD5 |
bc536687b5e4b7a39da89d9db2d44ecb
|
|
| BLAKE2b-256 |
897ff71f508d658464a96309b5ad593344aad36fd62498b4a68a25db46a37377
|
File details
Details for the file markupify-1.1.0-py3-none-any.whl.
File metadata
- Download URL: markupify-1.1.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.1 CPython/3.9.18 Linux/6.5.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5063e2b3195ded7d1e7029b7777c585f93dca500fbe7367ef28da27e6696e731
|
|
| MD5 |
6982b6b235e8fd5e979fa66bfa9ca6a5
|
|
| BLAKE2b-256 |
16f40b0d6f8a9dbb9fce700e25371277f1ba4bfa9d5b35e379961a4f6766b2ab
|