Skip to content

Pygments filter for highlighting code ranges based on spor anchors

License

Notifications You must be signed in to change notification settings

sixty-north/highlight-ranges

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

highlight-ranges

Highlight ranges of code in pygments output.

This provides a filter that uses spor anchors to determine which ranges of code to highlight in the pygmentized output. You specify the ranges by creating anchors with metadata where the key "highlight" is true, e.g.:

{"highlight": true}

Quick start

First, install highlight-ranges. See the "Installation" section for details.

Create a directory somewhere and create a file named example.py with the following contents:

def func(x):
    y = x * 4
    return y

Now initialize a spor repository and add an anchor that will highlight the second line in the file:

spor init
echo '{"highlight": true}' | spor add example.py 17 9 5

Finally, use the highlight-ranges filter to generate the pygmentized HTML of the code:

pygmentize -l python3 -f html -O full,style=highlight-ranges-black-on-red -F highlight-ranges:file=example.py example.py > example.html

If you open example.html in a browser, you should see the second line in the code emphasized.

Installation

You can install from PyPI with pip::

pip install highlight-ranges

Or you can install from source:

pip install .

Styling

The filter marks highlighted ranges of code with the Generic.Emph type. From a CSS point of view, this means you can style the highlights via the ge class.

Filter options

file
The file being pygmentized.

About

Pygments filter for highlighting code ranges based on spor anchors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages