If you blog using MarsEdit and publish to your WordPress-based site and you sometimes need to intersperse your articles with code snippets, you’ll find that it’s a real pain to use MarsEdit’s rich editor. It was for me. You see, MarsEdit (as of version 3.5.6) often tries to be too helpful and incorrectly reformat the code block that I’ve pasted into its rich text editor. I’ve tried pasting already HTML-formatted code block from Xcode, and that didn’t work very well. I’ve tried various WordPress plugins that does syntax highlighting (triggered by special pseudo-tags) but wasn’t satisfied with the results.
I’m not a big fan of fiddling around with HTML code —especially not in blog posts — and that’s exactly why I bought MarsEdit in the first place. Writing good content is hard enough and I don’t want to be burdened by fiddling with yet another computer language just to express my thoughts to other people. That is until I find this special technique and plugin combination which works quite well.
What do you need
- A WordPress-based blog that you can install plugins to.
- The SyntaxHighlighter Evolved plugin installed (the one by Alex Mills a.k.a Viper007Bond).
- MarsEdit or comparable blog editor.
Go to your blog administration page, select the SyntaxHighlighter plugin and tick the “Load All Brushes” check box. This will enable syntax highlighting using the
<pre> tag and not the
[code]…[/code] custom markup that it normally wants. Many decent WYSIWYG blog editors (including MarsEdit) will recognize that this is a pre-formatted text block and not mess it up.
Whenever you want to paste-in a source code block, flip to MarsEdit’s HTML view, insert a
</pre> section and paste your code block in there between those
<pre> tags. Then switch over to the rich view and verify that your code doesn’t get messed up. You’ll need to take care to escape some special characters within those block:
<(less-than sign) with
>(greater-than sign) with
&” and ends with a semicolon “
;“. Also refrain from entering any characters that you can’t type on a standard US keyboard in that block — MarsEdit may choke on it. This happened to me when I have the ellipsis “…” character in my
<pre>block, MarsEdit incorrectly escaped and unescaped as I move back-and-forth between the rich and HTML editing modes.
On that same tone, you’ll want to switch back and forth a few times between the rich and HTML modes to ensure that your code block remains intact. Just make sure that there are no extra “
&” get inserted or other weird symbols mysteriously manifest in your code block. Also pay special attention on the syntax highlighting when in HTML mode before switching to rich text mode, an unescaped
> may cause your article to become corrupted when you switch to rich text mode.
When you’re satisfied with your code block, it’s time to add the special sauce to make your code syntax highlighted. Add
class="brush: x" at your opening
<pre> tag where “x” says what is the programming language of the snippet inside the
<pre> block. Just take a look at the example below:
These are the available syntax highlighting languages that are supported by the plugin. If you can’t find your favorite language, try selecting something close to it. For example, I use “c” as the syntax highlighting language although it’s a code snippet for Objective-C.
- ActionScript3 –
- Bash shell –
- ColdFusion –
- C# –
- C++ –
- CSS –
- Dephi –
- Diff –
- Erlang –
- Groovy –
- Java –
- Perl –
- Plain Text –
- PowerShell –
- Ruby –
- Scala –
- SQL –
- Visual Basic –
- XML –
SyntaxHighlighter does a pretty good job at coloring my code snippets. It also adds line numbers, which may be convenient when you need to explain longer and more complicated code blocks.
So that’s just about it for now, happy blogging!