Introduction to Pygments
Pygments is a general purpose syntax highlighting library written in Python.
(From the documentation) There are four types of components that work together highlighting a piece of code:
- A lexer splits the source into tokens, fragments of the source that have a token type that determines what the text represents semantically (e.g., keyword, string, or comment). There is a lexer for every language or markup format that Pygments supports.
- The token stream can be piped through filters, which usually modify the token types or text fragments, e.g. uppercasing all keywords.
- A formatter then takes the token stream and writes it to an output file, in a format such as HTML, LaTeX or RTF.
- While writing the output, a style determines how to highlight all the different token types. It maps them to attributes like “red and bold”.
I used the command-line program pygmentize
when setting up CSS for Python
Markdown:
python3 /usr/bin/pygmentize -S default -f html -a .codehilite
Note the -S default
parameter; that sets a style. Discovering the available
styles is easy because they’re listed with the pygmentize -L
command:
-
algol:
(no description) -
algol_nu:
(no description) -
autumn:
A colorful style, inspired by the terminal highlighting style. -
borland:
Style similar to the style used in the borland IDEs. -
bw:
(no description) -
colorful:
A colorful style, inspired by CodeRay. -
default:
The default style (inspired by Emacs 22). -
emacs:
The default style (inspired by Emacs 22). -
friendly:
A modern style based on the VIM pyte theme. -
fruity:
Pygments version of the “native” vim theme. -
igor:
Pygments version of the official colors for Igor Pro procedures. -
lovelace:
The style used in Lovelace interactive learning environment. Tries to avoid the “angry fruit salad” effect with desaturated and dim colours. -
manni:
A colorful style, inspired by the terminal highlighting style. -
monokai:
This style mimics the Monokai color scheme. -
murphy:
Murphy’s style from CodeRay. -
native:
Pygments version of the “native” vim theme. -
paraiso-dark:
(no description) -
paraiso-light:
(no description) -
pastie:
Style similar to the pastie default style. -
perldoc:
Style similar to the style used in the perldoc code blocks. -
rrt:
Minimalistic “rrt” theme, based on Zap and Emacs defaults. -
tango:
The Crunchy default Style inspired from the color palette from the Tango Icon Theme Guidelines. -
trac:
Port of the default trac highlighter design. -
vim:
Styles somewhat like vim 7.0 -
vs:
(no description) -
xcode:
Style similar to the Xcode default colouring theme.
I ran the following get an overview of the styles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/bin/bash #LEXER="sql" FILE="/usr/share/doc/koji/docs/schema-upgrade-1.4-1.5.sql" #LEXER="bash" FILE="/usr/local/bin/edit-pod-gz-base64" #LEXER="sql" FILE="/usr/share/phpMyAdmin/sql/upgrade_column_info_4_3_0+.sql" #LEXER="perl" FILE="/usr/local/bin/LinuxPartitionInfo_test.pl" LEXER="python" FILE="/home/brian/projects/python-markdown-extensions/gentoc_remove.py" for STYLE in default algol algol_nu autumn borland bw colorful emacs friendly \ fruity igor lovelace manni monokai murphy native paraiso-dark paraiso-light \ pastie perldoc rrt tango trac vim vs xcode do echo -n " $STYLE" >&2 echo "<hr><h2>$STYLE</h2>" expand -t4 $FILE | python3 /usr/bin/pygmentize -l $LEXER -f html -O style=$STYLE,noclasses=True,linenos=pre [ $? == 0 ] || break done >pygmentize.html |