Skip to main content

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>&reg;</sup></p>

Introducing GitHub®

vs.

<p>
    Introducing GitHub
    <sup>
        &reg;
    </sup>
</p>

Introducing GitHub ®

This module parses HTML/XML as a raw string to more intelligently format tags.

Installation

You have two options:

  1. pip install prettierfier in your command line
  2. 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>
        &reg;
    </sup>
</p>"""

pretty_html = prettierfier.prettify_html(ugly_html)
print(pretty_html) 

# Output
>>> <p>Introducing GitHub<sup>&reg;</sup></p>

Links

Metadata

Release files for prettierfier 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prettierfier 1.0.3
File Size Uploaded
prettierfier-1.0.3.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prettierfier 1.0.3
File Interpreter ABI Platform
prettierfier-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.2 kB

Release files / prettierfier-1.0.3.tar.gz

Download URL prettierfier-1.0.3.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
15a5b3b46776bb6173c447f2b33bf5002d2d6d219edccd9402ea64fff9c25f68
BLAKE2b-256 checksum
How to use checksums
3a80c1bb6cb21ef8a7b228394c8fdadec6c064074852c3374a4204285d9e3f11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Release files / prettierfier-1.0.3-py3-none-any.whl

Download URL prettierfier-1.0.3-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5dd22b1141b333c02df87b81062d18c5194e811675fa1c656627f39494431a3d
BLAKE2b-256 checksum
How to use checksums
4626876576c766bb77bcdadbdeb69bc3c6850f12f01bf8a9c1bf96da9b7f07db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page