syntax


Python syntax highlighter for PrismJS

Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind, developed by Lea Verou.

I recently changed to this highlighter on my website. But except the officialy supported languages I need to use it for highlight a WOL Python script. Thanks to the fact that it is easy extensible, I create some add-on code for Python syntax.

Python Highlighting Sample

# comment
import library
def main(arg=15):
    print("Hello world!") ;

 
Here you can see the source code of the addon.

Add-on source code (minified)

Prism.languages.python={comment:{pattern:/(^|[^\\])#.*?(\r?\n|$)/g,lookbehind:!0},string:/("|')(\\?.)*?\1/g,keyword:/\b(as|assert|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|pass|print|raise|return|try|while|with|yield)\b/g,boolean:/\b(True|False)\b/g,number:/\b-?(0x)?\d*\.?[\da-f]+\b/g,operator:/[-+]{1,2}|=?<|=?>|!|={1,2}|(&){1,2}|(&){1,2}|\|?\||\?|\*|\/|~|\^|%|\b(or|and|not)\b/g,ignore:/&(lt|gt|amp);/gi,punctuation:/[{}[\];(),.:]/g};

 
Until my GitHub fork merged with the original PrismJS project you can use it with the existing PrismJS script, by putting it after the the prism.min.js script definition and by using the value language-python at class property of the code element you like to highlight.

Usage

. . .
<script src="prism.min.js"></script>
<script src="prism-python.min.js"></script>
. . .
<pre><code class="language-python">
. . .

 
Download Prism Python Higlighter