Intelligently pretty-print HTML/XML with inline tags.
Project description
prettierfier
While I love Beautiful Soup as a parser, BeautifulSoup.prettify()
adds a linebreak between every tag.
This results in unwanted white space between tags that should be inline, like <sup>
, <a>
, <span>
, etc:
<p>Introducing GitHub<sup>®</sup></p>
Introducing GitHub®
vs.
<p>
Introducing GitHub
<sup>
®
</sup>
</p>
Introducing GitHub ®
This module parses HTML/XML as a raw string to more intelligently format tags.
Installation
You have two options:
pip install prettierfier
in your command line- Copy the contents of prettierfier.py to your own module.
This module is built with just the Python Standard Library and contains no external third-party dependencies.
Functions
prettify_xml(xml_string, indent=2, debug=False)
- Can be used with no prior formatting.
Args:
xml_string (str): XML text to prettify.
indent (int, optional): Set size of XML tag indents.
Test-only args:
debug (bool, optional): Show results of each regexp application.
Returns:
str: Prettified XML.
prettify_html(html_string, debug=False)
- Originally created to process
BeautifulSoup.prettify()
output. - Does not add or remove regular line breaks. Can be used with regular HTML if it already has the newlines you want to keep.
Args:
html_string (str): HTML string to parse.
Test-only args:
debug (bool, optional): Show results of each regexp application.
Returns:
str: Prettified HTML.
Example
import prettierfier
ugly_html = """<p>
Introducing GitHub
<sup>
®
</sup>
</p>"""
pretty_html = prettierfier.prettify_html(ugly_html)
print(pretty_html)
# Output
>>> <p>Introducing GitHub<sup>®</sup></p>
Links
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
prettierfier-1.0.3.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for prettierfier-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dd22b1141b333c02df87b81062d18c5194e811675fa1c656627f39494431a3d |
|
MD5 | d91b93de74e21cfd282ae5b52339c664 |
|
BLAKE2b-256 | 4626876576c766bb77bcdadbdeb69bc3c6850f12f01bf8a9c1bf96da9b7f07db |