mrkup
Just marking things up...
Compose HTML (and some XML) using Python.
mrkup can be used to compose html programatically which can then be converted to string but cannot parse html from external sources.
Installation
You need Python>=3.6 to use mrkup.
It can be installed from PyPI with pip using
pip install mrkup
Usage
mrkup consists of three classes:
TagCommentPI
which may be imported like
from mrkup import Comment, PI, Tag
Tags
Used to compose tags.
Tag(name: str,
attrs: dict = None,
children: List[Union[Node, str]] = None,
close: Optional[bool] = True)
Tag objects have the following attributes:
- name: tag's name
- attrs: dictionary with attributes of tag.
- children: list of children tags and strings of the tag
- close: value determining whether the tag should be closed separately or self-closed or unclosed.
If an attribute need to be specified without value, it should be present in the attrs dictionary as a key but with its value as None. Like
>>> text = Tag("input", attrs={"type": "text", "required": None},
... close=None)
>>> text.dumps(indent=None)
'<input type="text" required />'
children would have the list of Tags and strss that comes under the tag object.
close value can be used to control the manner in which the tag is closed as follows:
- True: Allow a separate closing tag (default)
- None: Self-closed tag
- False: Don't close tag
Like, for tags with a distinct closing tag
# close=True (separate closing tag)
>>> text = Tag("p", children=["Hello!"])
>>> print(text.dumps())
<p>
Hello!
</p>
or self-closing tags like
# close=None (self-closed tag)
>>> text = Tag("img", attrs={"src": "server/img.jpg"}, close=None)
>>> text.dumps(indent=None)
'<img src="server/img.jpg" />'
or tags that are not closed like
# close=False (open tag)
>>> text = Tag("br", close=False)
>>> text.dumps(indent=None)
'<br>'
Note: children attribute of a Tag object is ignored by dump() and dumps() if the close value is not True.
Note: attrs and children attributes of a Tag object can be accessed and modified like a normal dict and list respectively.
Note: In mrkup, the <!DOCTYPE html> declaration is meant to be implemented with the Tag class itself like
>>> doctype = Tag("!DOCTYPE", attrs={"html": None})
>>> doctype.dumps(indent=None)
'<!DOCTYPE html>'
Comments
Used to compose HTML comments.
Comment(text: str)
Like
>>> comment = Comment("Just a comment")
>>> comment.dumps(indent=None)
'<!--Just a comment-->'
Processing instructions
Can be used for composing the XML version declaration like
PI(name: str,
attrs: dict = None)
Like
>>> xml_pi = PI("xml", attrs={"version": "1.0", "encoding": "UTF-8"})
>>> xml_pi.dumps(indent=None)
'<?xml version="1.0" encoding="UTF-8"?>'
Converting to string
The composed HTML can be converted to an equivalent string using the dumps() method of the objects.
Indentation
Indentation level and number of spaces per indentation level used by the dumps() method can be specified using the level and indent argument respectively.
By default dumps() does pretty-printing with indent value 2.
If indent is None, pretty-printing is disabled and value of level is ignored.
Style and script data
JavaScript contents of <script> and CSS of <style> are simply treated as plain text in mrkup.
Like
>>> content = "p { text-align: center; }"
>>> style = Tag("style", children=[content])
>>> print(style.dumps())
<style>
p { text-align: center; }
</style>
No HTML validation
mrkup doesn't perform any validation to be sure that the tags are valid HTML.So we could also use it to create some XML..
Example
from mrkup import Tag, Comment
# doctype is not part of the html tag
doctype = Tag("!DOCTYPE", attrs={"html": None}, close=False)
comment = Comment("Here comes the list!")
ol = Tag("ol")
for loc in ['home', 'about', 'contact']:
a = Tag("a", attrs={"href": f"/{loc}.html"}, children=[loc.title()])
li = Tag("li", children=[a])
ol.children.append(li)
h1 = Tag("h1", children=["Hey there!"])
img = Tag("img", attrs={"src": "server/img.jpg"}, close=False)
br = Tag("br", close=None)
body = Tag("body", children=[h1, img, br, "\n", comment, ol])
title = Tag("title", children=["Mrkup your markup"])
link = Tag("link", attrs={"href": "style.css", "rel": "stylesheet"},
close=None)
script = Tag("script", attrs={"src": "script.js", "type": "text/javascript"})
head = Tag("head", children=[title, link, script])
html = Tag("html", children=[head, body])
# Generate string version
doc = doctype.dumps() + html.dumps()
print(doc)
This would generate the following:
<!DOCTYPE html>
<html>
<head>
<title>
Mrkup your markup
</title>
<link href="style.css" rel="stylesheet" />
<script src="script.js" type="text/javascript">
</script>
</head>
<body>
<h1>
Hey there!
</h1>
<img src="server/img.jpg">
<br />
<!--Here comes the list!-->
<ol>
<li>
<a href="/home.html">
Home
</a>
</li>
<li>
<a href="/about.html">
About
</a>
</li>
<li>
<a href="/contact.html">
Contact
</a>
</li>
</ol>
</body>
</html>
Metadata
Release files for mrkup 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mrkup-0.2.tar.gz | 8.6 kB | Without provenance | Details for mrkup-0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mrkup-0.2-py3-none-any.whl | Python 3 | none | any | Details for mrkup-0.2-py3-none-any.whl |
Total release size: 15.1 kB
Release files / mrkup-0.2.tar.gz
| Download URL | mrkup-0.2.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5e8d4b18cd28cedf38355271e6b95432382e961ef72178013b563725ede55d43
|
|
BLAKE2b-256 checksum How to use checksums |
b8a4bdf069caa98a8a92076caecd96257008303a624bf6f3ae9ba30d002260cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.9
|
Release files / mrkup-0.2-py3-none-any.whl
| Download URL | mrkup-0.2-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb60373640e0f2ef4b21f5abf1f8945953c98c88a3d154e11f99ca4199f8cf9a
|
|
BLAKE2b-256 checksum How to use checksums |
f01e0f82454eddeeb2b3fb4316423704b90ba090753578437d35b8cfa84be967
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.9
|
Security for mrkup 0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.