How to post source code for highliting

12:20 pm in How To by buzz_lightyear

Here’s little howto about posting source code in your posts, here at buzzdev website.

Posting Source Code

While our site 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:

#button {
font-weight: bold;
border: 2px solid #fff;
}

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):

  • bash
  • cpp
  • csharp
  • css
  • delphi
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • shell
  • 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.

Other BBCode Methods

Find [sourcecode language='css']code here[/sourcecode] too long to type? Here’s some alternative examples:

[source language='css']code here[/source]
[code language='css']code here[/code]
[sourcecode lang='css']code here[/sourcecode]
[source lang='css']code here[/source]
[code lang='css']code here[/code]
[sourcecode='css']code here[/sourcecode]
[source='css']code here[/source]
[code='css']code here[/code]
[lang='css']code here[/lang]
[css]code here[/css] (or any of the supported language)

Note: Quotation marks around the language can be omitted for even shorter and neater syntax.

FAQ

I still see the BBCode in my post. What gives?
Make sure you correctly use the BBCode with a valid language attribute. A malformed usage of it won’t result in replacement.

I use the visual editor, my code has lots of line breaks inserted, what do I do?
Use ‘shift + return’ instead of ‘return’ when changing lines.