This post is for folks who blog about Python (or any programming language for that matter) on WordPress.
Updated 2011-11-09 to make it easier to copy-and-paste the [sourcecode]
template.
My topic today is How to post source code on WordPress.
The trick is to use the WordPress [sourcecode] shortcut tag, as documented at http://en.support.wordpress.com/code/posting-source-code/.
Note that when the WordPress docs tell you to enclose the [sourcecode] shortcut tag in square — not pointy — brackets, they mean it. When you view your post as HTML, what you should see is square brackets around the shortcut tags, not pointy brackets.
Here is the tag I like to use for snippets of Python code.
[sourcecode language="python" wraplines="false" collapse="false"] your source code goes here [/sourcecode]
The default for wraplines is true, which causes long lines to be wrapped. That isn’t appropriate for Python, so I specify wraplines=”false”.
The default for collapse is false, which is what I normally want. But I code it explicitly, as a reminder that if I ever want to collapse a long code snippet, I can.
Here are some examples.
Note that
- WordPress knows how to do syntax highlighting for Python. It uses Alex Gorbatchev’s SyntaxHighlighter.
- If you hover your mouse pointer over the code, you get a pop-up toolbar that allows you to look at the original source code snippet, copy it to the clipboard, print it, etc.
(1)
First, a normal chunk of relatively short lines of Python code.
indentCount = 0 textChars = [] suffixChars = [] # convert the line into a list of characters # and feed the list to the ReadAhead generator chars = ReadAhead(list(line)) c = chars.next() # get first while c and c == INDENT_CHAR: # process indent characters indentCount += 1 c = chars.next() while c and c != SYMBOL: # process text characters textChars.append(c) c = chars.next() if c and c == SYMBOL: c = chars.next() # read past the SYMBOL while c: # process suffix characters suffixChars.append(c) c = chars.next()
(2)
Here is a different code snippet. This one has a line containing a very long comment. Note that the long line is NOT wrapped, and a horizontal scrollbar is available so that you can scroll as far to the right as you need to. That is because we have specified wraplines=”false”.
somePythonVariable = 1 # This is a long, single-line, comment. I put it here to illustrate the effect of the wraplines argument. In this code snippet, wraplines="false", so lines are NOT wrapped, but extend indefinitely, and a horizontal scrollbar is available so that you can scroll as far to the right as you need to.
(3)
This is what a similar code snippet would look like if we had specified wraplines=true. Note that line 2 wraps around and there is no horizontal scrollbar.
somePythonVariable = 1 # This is a long, single-line, comment. I put it here to illustrate the effect of the wraplines argument. In this code snippet, wraplines="true", so lines are ARE wrapped. They do NOT extend indefinitely, and a horizontal scrollbar is NOT available so that you can scroll as far to the right as you need to.
(4)
Finally, the same code snippet with collapse=true, so the code snippet initially displays as collapsed. Clicking on the collapsed code snippet will cause it to expand.
somePythonVariable = 1 # This is a long, single-line, comment. I put it here to illustrate the effect of the wraplines argument. In this code snippet, wraplines="true", so lines are ARE wrapped. They do NOT extend indefinitely, and a horizontal scrollbar is NOT available so that you can scroll as far to the right as you need to.
As far as I can tell, once a reader has expanded a snippet that was initially collapsed, there is no way for him to re-collapse it. That would be a nice enhancement for WordPress — to allow a reader to collapse and expand a code snippet.
Here is a final thought about wraplines. If you specify wraplines=”false”, and a reader prints a paper copy of your post, the printed output will not show the scrollbar, and it will show only the portion of long lines that were visible on the screen. In short, the printed output might cut off the right-hand part of long lines.
In most cases, I think, this should not be a problem. The pop-up tools allow a reader to view or print the entire source code snippet if he wants to. Still, I can imagine cases in which I might choose to specify wraplines=”true”, even for a whitespace-sensitive language such as Python. And I can understand that someone else, simply as a matter of personal taste, might prefer to specify wraplines=”true” all of the time.
Now that I think of it, another nice enhancement for WordPress would be to allow a reader to toggle wraplines on and off.
Keep on bloggin’!
Image may be NSFW.
Clik here to view.
