Tuesday, December 9, 2008

How to write raw code in WordPress

As a corollary to one of my previous posts, and after extensive googling to avoid one bloody fastidious problem for one trying to blog about code (the WordPress auto-formatting which will trim your indentation and convert code enclosed between html tags in.. well what's turns out to be in reality), I found this guideline by WordPress support which is definitely godsend.

While WordPress.com doesn’t allow you to use potentially dangerous code on your blog, there is a way to post source code for viewing. We have created a shortcode you can wrap around source code that preserves its formatting and even provides syntax highlighting for certain languages, like so:
Wrap your code in these tags:
[sourcecode language='css']
your code here
[/sourcecode]

Any of the following can be used for the language parameter (using one is required):
  • cpp
  • csharp
  • css
  • delphi
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • sql
  • vb
  • xml
Code in between the [sourcecode] tags will automatically be encoded for display, you don’t need to worry about HTML entities or anything.
Alex Gorbatchev’s syntaxhighligher Google Code project was used to implement this feature.

No comments:

Post a Comment