## Here is some raw Python, pasted directly into the page:## For every line...forlineinlines:# ...print it out if it's not blank:ifline!="":printlineelse:result=blank_line_handler()print"Blank line found, handler returned: %s"%result

[/pyg]

Description

This WordPress plugin is a syntax highlighter for many different computer languages, including HTML, Python, PHP, Javascript, CSS, and many others. It uses the Pygments syntax highlighter from Python. It calls the pygmentize command on the server, so most of the features come directly from that command.

You can paste raw code directly from your source code editor into your WordPress pages or posts, and by wrapping it with [pyg] shortcode tags, it will appear syntax highlighted in your pages.

Features

Line numbers are optional, using both an HTML table (which lets you copy’n'paste code from the page without any line numbers embedded), and alternatively “inline” line numbers, so that they are included when copy’n'pasting from the screen.

Tabs are preserved, and you can use a customizable tab width! (The examples here use a width of 4 spaces. Copy’n'paste it someplace to see!)

Extra “bright” highlighting of arbitrary line numbers is supported, by specifying which line numbers you want to draw attention to.

Server-side highlighting. In contrast to the older SyntaxHighlighter or SyntaxHighlighter Evolved plugins, this library does the syntax highlighting on the server, not in the web browser. It uses only CSS markup and HTML, and no Javascript.

Full Pygments support: 197 languages (incl. combos like HTML+PHP), and 18 colorful styles. Each block of code can have its own style. The older Javascript plugins mentioned above only support 23 languages, and 7 styles.

Unlike WpPygments, this plugin does not communicate with any remote web services. It runs the pygmentize command on the server instead.

This plugin may be installed in parallel with the other syntax highlighter plugins, because they don’t use the [pyg] shortcode.

Just ~320 lines of PHP code, and about a third of that is license and comments.

Works with the Visual editor enabled, however, note that the Visual editor will mess up whitespace, esp. tabs. So using the editor in HTML mode is strongly recommended.)

As a convenience feature, the shortcode options for language, style, tabwidth, linenos, and nowrap are remembered after each call. So you only need to specify them once, for the first code block. Then they become the defaults for the rest of the page.

1. Put it into `/wp-content/plugins/` directory
2. Activate it in the admin Plugin page.

Usage

Here are some examples:

[pygmentize language="python"]

# For every line...forlineinlines:# ...print it out if it's not blank:ifline!="":printlineelse:result=blank_line_handler()print"Blank line found, handler returned: %s"%result

[/pygmentize]

Typing in “pygmentize” gets tedious, so you can use [pyg] for short:

[pyg language="html+php" style="monokai" ]

<H2class="sample">Some HTML</H2><?phpif($hl_lines!=NULL){/* We split apart the passed-in arg to make sure each one is numeric: */$hl_lines_array=preg_split('/ +/',$hl_lines);$hl_lines_array_safe=array();foreach($hl_lines_arrayas$line_no){if(is_numeric($line_no)){$hl_lines_array_safe[]=$line_no;}}$option="hl_lines='".join(" ",$hl_lines_array_safe)."'";$extra_opts_array[]=$option;}

[/pyg]

You can also use the short form ‘l’ and ‘s’ to for the common options ‘language’ and ‘style’:

Linking to a named site:
[url=http://www.bbcode.org/]This be bbcode.org![/url]

[/pyg]

You can get an instant preview of various styles by using the official Pygments Demo page.

The following attributes are available. See the pygmentize documentation for more details about these options:

language, l

Which of the ~197 language syntaxes you’d like to highlight in. Common examples are “python”, “php”, “html+php”, “css”, “js”. See the full list by running “pygmentize -L” on your server, or check the list at the end of this page.

style, s

Which of the 18 color styles you’d like to highlight in. See the full list by running “pygmentize -L” on your server, or check the list at the end of this page.

tabwidth

The number of character widths to render tabs as. The default is 4 chars.

linenos

Whether or not to display line numbers. This can be empty, like linenos=”", to turn off line numbering. It can be set to “inline”, or “table”. Inline numbers are inserted into the code lines, but table uses a separate table column for the numbers. The “table” option allows users to easily copy’n'paste working code from your web page, even though it is annotated with line numbers.

linenostart

When using linenos, you can have the line numbers start with a number higher than 1. This is useful for referencing line numbers in a file. Example: linenostart=”57″

hl_lines

In addition to regular syntax highlighting, you can also “highlight” specific lines to stand out above the regular code. Just specify a space-separated list of numbers, like: hl_lines=”42 43 68 69″

nowrap

If set to True, don’t wrap the tokens in any HTML tags at all, not even inside a <pre> tag. (This has nothing to do with line wrapping. It’s for getting raw tokenized output.) See the pygmentize docs for details.

The following attribrutes are remembered each call, so you only need to specify them for your first block of code (and then they are the defaults for the rest of the page or post), which saves typing if your pages contain all the same type of highlighted code: