Syntax highlighting on

In order to present readable and usable code here on without manually formatting it I searched the intertubes for an elegant solution – and found one here.

import math, string

def readFile(file):
    """Reads a file from disk and returns its content as a string variable
    :param file: path to a file to be read
    with open(file, 'r') as f:
        data =
    return data

def countCharsWhitespace(data):
    """Returns the number of characters in a string as an integer,
       including whitespace characters and punctuation marks, but
       not newline characters
    :param data: string variable containing the text to be analysed
    data = data.replace('\n','')
    return len(data)

def countWords(data):
    """Returns the number of words in a string as an integer
    :param data: string variable containing the text to be analysed
    return len(data.split())

The syntax highlighting works with what WordPress seems to call a shortcode: [ sourcecode ]. Presently some 25 languages are supported and various options further customise the look and feel of the code representation (for example, line numbers could easily be added to above display):

  • autolinks (true/false) — Makes all URLs in your posted code clickable. Defaults to true.
  • collapse (true/false) — If true, the code box will be collapsed when the page loads, requiring the visitor to click to expand it. Good for large code posts. Defaults to false.
  • firstline (number) — Use this to change what number the line numbering starts at. It defaults to 1.
  • gutter (true/false) — If false, the line numbering on the left side will be hidden. Defaults to true.
  • highlight (comma-seperated list of numbers) — You can list the line numbers you want to be highlighted. For example “4,7,19″.
  • htmlscript (true/false) — If true, any HTML/XML in your code will be highlighted. This is useful when you are mixing code into HTML, such as PHP inside of HTML. Defaults to false and will only work with certain code languages.
  • light (true/false) — If true, the gutter (line numbering) and toolbar (see below) will be hidden. This is helpful when posting only one or two lines of code. Defaults to false.
  • padlinenumbers (true/false/integer) — Allows you to control the line number padding. true will result in automatic padding, false will result in no padding, and entering a number will force a specific amount of padding.
  • toolbar (true/false) — If false, the toolbar containing the helpful buttons that appears when you hover over the code will not be shown. Defaults to true.
  • wraplines (true/false) — If true, line line wrapping will be disabled. This will cause a horizontal scrollbar to appear for long lines of code.

(and yes, for one of my next projects I’m looking into (very basic!) language analysis ;)


One thought on “Syntax highlighting on

  1. Pingback: wordpress.com發文技巧的記事 | Mr Tony Life

Leave a reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s