Syntax highlighting with Pygments

Pygments is a python syntax highlighting library that can be used to highlight source code it outputs HTML and the output can be styled using diffrent themes with CSS. the library can be used as a command line tool or as a python library, but knowledge of python is not required to use it as a command line tool.

Installing

Inorder to use the library the python programming language needs to be installed, it can be downloaded from the python website. Then install Pygments with the command:

pip install Pygments

Using the command line tool

the command line tool with output the highlighted html code to a file, by default and will determine the language based on the file extension, for example to highlight this snippet of JSON taken from wikipedia:

pygmentize -o test.html test.json

will output this highlighted JSON:

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    },
    {
      "type": "mobile",
      "number": "123 456-7890"
    }
  ],
  "children": [],
  "spouse": null
}

the code will not be highlighted unless a CSS highlighting theme is imported the default theme can be found at http://pygments.org/_static/pygments.css

Comments