Skip to main content

Better HTML formatter for Pygments

Project description

Better line numbers for Pygments HTML

This library provides improved line numbers for the Pygments HTML formatter. The BetterHtmlFormatter supports two styles:

  • linenos='table' (the default) — every line of the code is a separate table row (a 2xN table, as opposed to Pygments’ standard 2x1 table) This improves the appearance if the code contains characters with unusual line-height, and allows for the code to be word-wrapped with the numbers kept in the right places.
  • linenos='ol' — lines are <li> elements in an <ol> list.

Both styles allow for copy-pasting into a code editor. Directly copy-pasting into Microsoft Word (or similar) might produce something ugly. The first style is inspired by GitHub, and the second can be seen at pastebin.com.

Usage

In most cases, it’s a drop-in replacement for HtmlFormatter. Just add the import:

from pygments_better_html import BetterHtmlFormatter

and when calling highlight(), instead of HtmlFormatter, pass the BetterHtmlFormatter class:

BetterHtmlFormatter(linenos="table", …other options…)
BetterHtmlFormatter(linenos="ol", …other options…)

You can see a simple demo in demo.py.

Required CSS

To make this work, you will need to add the following CSS:

.highlight { white-space: pre-wrap; }
.highlight table, .highlight tr, .highlight td { border-spacing: 0; border-collapse: collapse; }
.highlight pre { white-space: pre-wrap; line-height: normal; }
.highlighttable td.linenos { vertical-align: top; padding-left: 10px; user-select: none; }
.highlighttable td.code { overflow-wrap: normal; border-collapse: collapse; }
.highlight .lineno.nonumber { list-style: none; }

If you’re using get_style_defs, those will be included for you.

Known limitations

  • The anchorlinenos option is not supported for linenos='ol'.

License

Copyright © 2020, Chris Warrick. Licensed under the 3-clause BSD license.

Many parts of the code are taken from Pygments’ original HTMLFormatter, which is copyright © 2006-2019 by the Pygments team, and is licensed under the 2-clause BSD license.

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

pygments_better_html-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

pygments_better_html-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pygments_better_html-0.1.0.tar.gz.

File metadata

  • Download URL: pygments_better_html-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for pygments_better_html-0.1.0.tar.gz
Algorithm Hash digest
SHA256 16ea025060453add08cbbc9b9c3dc30434fb7957b6f21c674787239d60cd3a79
MD5 b951111f7ae838aae08ac1546a551a06
BLAKE2b-256 3a2a6ab2af20913a438edb9e5ca8fc347883e3166e67bd059f4def35e6c14418

See more details on using hashes here.

File details

Details for the file pygments_better_html-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pygments_better_html-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for pygments_better_html-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5706140aa42f58446c67d4d910afe4a0f3dee723b9ecee717252ac1e0e30a38a
MD5 7d5b7258fa8fc68a4442ad0fbee8e0d4
BLAKE2b-256 c2984c322adc32836290f7f6b95cd7936ef9a7032ce0122b34a82262122a875f

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