Skip to main content

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>&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

Project details


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.1.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

prettierfier-1.0.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file prettierfier-1.0.1.tar.gz.

File metadata

  • Download URL: prettierfier-1.0.1.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for prettierfier-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4e18e4367cafdcce340d1380df40062043a2e6d5d67375d98dcee4c9fed7b542
MD5 f5f35b1a2b432e69bce3068396679056
BLAKE2b-256 ab8242e946d430666ad5e11a4c7d38f386e09096308136cf1c34256b20a4ae89

See more details on using hashes here.

File details

Details for the file prettierfier-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: prettierfier-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for prettierfier-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cb4ddde058894e55cb6e1b44e72199c60ecfea5deecee4b6c0778970f5a1de9e
MD5 09c484109485c322ebb79c81178619b6
BLAKE2b-256 bea16eb2facdf9155ad83756c8b346612874892e4948d6966879a3a69913949e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page