Oct 292009
 

Be it complex .NET/java code or a simple one line php/javascript statement, every technology blogger sooner or later faces the problem of writing code in his blog. The results are sometimes horrendous. From formatting gone awry to markup being processed, it sometimes ends in disaster. I have even had to paste screenshots in one of my blogs.

So I spent quite a bit of time searching for a good plugin to help write and format code properly in my WordPress blog. The first was a Live Writer plugin, though the WordPress online editor is quite good enough for me, I used live writer just to use the code formatting plugin which lets you copy code directly from Visual studio without losing formatting. Now it would have sufficed for my needs had it not been for the dark background color of my blog. Look at the results.
live_writer

Certainly not good, so my search continued. Then i stumbled upon the code plugin WP_Syntax which can be downloaded from here. After downloading, extract it using Winzip (or any other compression utility). Using a file transfer utility like FileZilla, copy it to your wordpress blog’s wp-content\plugins directory.

After copying, like all other plugins, you need to activate it.

activate

Once activated, your all set. Just switch to the HTML view and copy the code as text enclosed in the <pre> tag which denotes a pre formatted block of text to which no formatting is applied. The format is <pre lang=”language_name” line=”n”> where language_name is any supported language and n is the number to start numbering lines from. For e.g. <pre lang=”csharp” line=”1″> would give the following code

private void button1_Click(object sender, EventArgs e)
{
      Assembly _AsmObj = Assembly.GetExecutingAssembly();
      Type _t = _AsmObj.GetType("TestClass");
      MethodInfo _MIObj = _t.GetMethod("AddTwoNumbers", new Type[] { typeof(int), typeof(int) });
      object _TestClassObj = Activator.CreateInstance(_t);
      int _ReturnValue = (int)_MIObj.Invoke(_TestClassObj, new object[] { 12, 13 });
      MessageBox.Show(_ReturnValue.ToString());
}

And its as simple as that!!!. Here are the languages which are supported. Languages in bold are the popular ones.

abap, actionscript, actionscript3, ada, apache, applescript, aptsources, asm,asp, autoit, avisynth, bash, bf, bibtex, blitzbasic, bnf, boo, c, cmac, caddcl, cadlisp, cil, cfdg, cfm, cmake, cobol, cpp-qt, cpp, csharp, css, d, dcs, delphi, diff, div, dos, dot, eiffel, email, erlang, fo, fortran, freebasic, genero, gettext, glsl, gml, bnuplot, groovy, haskell, hq9plus, html4strict, idl, ini, inno, intercal, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, locobasic, lolcode lotusformulas, lotusscript, lscript, lsl2, lua, m68k, make, matlab, mirc, modula3, mpasm, mxml, mysql, nsis, oberon2, objc, ocaml-brief, ocaml, oobas,oracle11, oracle8, pascal, per, pic16, pixelbender, perl, php-brief, php, plsql, povray, powershell, progress, prolog, properties, providex, python, qbasic,rails, rebol, reg, robots, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, tcl, teraterm, text, thinbasic, tsql, typoscript, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xml, xorg_conf, xpp, z80