Skip to main content

An opinionated HTML/XML soup formatter

Project description



A tool to format in a very opinionated way HTML, XML and text containing XML snippets.

It satisfies a primary need: decrease the pain of diffing HTML/XML.

For this reason zpretty formats the markup following these rules of thumb:

  • maximize the vertical space/decrease the line length
  • attributes should be sorted consistently
  • attribute sorting is first semantic and then alphabetic

This tool understands the TAL language and has some features dedicated to it.

This tool is not a linter! If you are looking for linters safe bets are Tidy and xmllint.

You may have parsing problems! zpretty will close for you some known self closing tags, like input and img, that are allowed to be open in HTML.

zpretty is not clever enough to understand correctly valueless attributes! Some work is ongoing, but it works best with "normal" attributes.

Lack of feature/slowness are a known issue. For the moment the development focused in having a working tool. So it works fast enough: less than a second to format a ~100k file. New features are planned and also huge perfomance boost can be easily obtained. Anyway zpretty is not your option for formatting large files (> 1 MB).

See TODO section to know what is forecast for the future.

The source code and the issue tracker are hosted on GitHub.


The suggested installation method is using pip:

$ pip install zpretty

The latest release of zpretty requires Python3. If you need to use Python2.7 use zpretty 0.9.x.


Basic usage:

zpretty [-h] [--encoding ENCODING] [-i] [-x] [-z] [--check] [file ...]

positional arguments:
  file                 The list of files to prettify (defaults to stdin)

optional arguments:
  -h, --help           show this help message and exit
  --encoding ENCODING  The file encoding (defaults to utf8)
  -i, --inplace        Format files in place (overwrite existing file)
  -x, --xml            Threat the input file(s) as XML
  -z, --zcml           Threat the input file(s) as XML. Follow the ZCML
  --check              Return code 0 if nothing would be changed, 1 if some
                       files would be reformatted

Without parameters constraining the file type (e.g. [-x]{.title-ref}, [-z]{.title-ref}, ...) zpretty will try to guess the right options for you.


zpretty hello_world.html


$ git clone ...
$ cd zpretty
$ make


$ make test


  • Valueless attributes are not allowed in XML
  • Attributes are aligned in a strange way if previous sibling has no spaces
  • TBD: Style attributes should be multiline


2.2.0 (2021-12-06)

  • Add a --check command line parameter (Fixes #49) [ale-rt]
  • Now the package is pre-commit compatibile (Fixes #50) [ale-rt]

2.1.0 (2021-02-12)

  • Remove unused autofix method [ale-rt]
  • Do not render a spurious ="" when new lines appear inside a tag (Refs. #35) [ale-rt]
  • The attributes renderer knows about the element indentation and for indents the attributes consequently [ale-rt]
  • The ZCML element has now its custom tag templates, this simplifies the code [ale-rt]
  • Attributes content spanning multiple lines is not indented anymore (Refs. #17) [ale-rt]
  • Improved sorting for zcml attributes (Refs. #11) [ale-rt]
  • Code is compliant with black 20.8b1 [ale-rt]
  • Switch to pytest for running tests [ale-rt]
  • Upgrade dev requirements [ale-rt]
  • Support Python 3.9 [ale-rt]

2.0.0 (2020-05-28)

  • Updated the list of self closing elements and boolean attributes [ale-rt]

1.0.3 (2020-05-22)

  • Fix unwanted newlines (#20)

1.0.2 (2019-11-03)

  • In Python3.8 quotes in attributes were escaped
  • Fix output again on file and stdout [ale-rt]

1.0.1 (2019-10-28)

  • Fix output on file [ale-rt]

1.0.0 (2019-10-27)

  • Support Python3 only [ale-rt]

0.9.3 (2017-05-06)

  • Last release that supports Python2.7
  • Fix text method
  • Preserve entities in text
  • Added an --encoding parameter
  • Added an --xml parameter to force xml parsing
  • Choose the better parser according to the given filename if no parser is forced
  • Process stdin if - is in the arguments or no arguments are passed [ale-rt]

0.9.2 (2017-02-27)

  • Small modification for the order of the zcml attributes
  • Auto add a new line to the end of the prettified files
  • Self heal open self closing tag. [ale-rt] (2017-02-18)

  • Fixed bad release. [ale-rt]

0.9.1 (2017-02-18)

  • Initial support for zcml style guide (#3). [ale-rt]

0.9.0 (2017-02-11)

  • Initial release. [ale-rt]

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

zpretty-2.2.0.tar.gz (26.9 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page