in the city
Syntax Highlighting for Everyone!
The software I ended up using was SyntaxHighlighter
Once thats in place all you have todo is invoke:
<pre name="code" class="yourlanguage"> awesome code snippit here
Where ‘yourlanguage’ is one of ruby,perl,etc
There is a very useful option which allows you to match line numbers to the file you may be refering to (for example your code may begin on line 10).
<pre name="code" class="yourlanguage:firstline"> awesome code snippit here
Here is an example from line 35 from a rails controller (Note the line numbers on the left)
def show @owner = User.find(params[:user]) || User.find(1) @blogs = Blog.paginate :conditions => ["(user_id = ?) AND NOT disabled", @owner.id], :order => 'updated_at DESC', :per_page => 5, :page => params[:page] end
SyntaxHighlighter supports out of the box:
- XML (Which works well for xhtml files)
In addition you can grab shBrushPerl.js which adds perl support.
I would also like to point out some other ways to convert code into markup.
- Coderay integrates well with ruby on rails.
- GeSHi is a PHP based generator.
- Highlight is a command line (and gtk gui) based app.
At the very least Highlight’s console output can be grabbed and fed into your web application no matter what language. It also has a cool 256 Color Xterm output which is great for piping code into from grep or less.
Highlight also comes with a slew of existing color schemes in CSS which is nice.
There are many more highlighters out there, google is your friend.
Enjoy the shiny colors!