Tags
webcam vindaloo version vegan unix unicef trojan todo thinkpad textmate testing tagging syntax svn subversion sphinx spaces solaris sitemap sinatra sheet security search schema_info SchemaInfo ruby rinari relationships refresh rdiff-backup ramaze railsconf08 railsconf07 rails protools production power placeboeffect pink floyd PIC perl overheat outbreak osx os x NYHS NYC netbeans nanophotonics mysql music MPEG-4 model migration microvolunteer macbook mac log linux less leopard keynote JAX javascript java imunizator highlighting Handbrake haml hacks google geocoding genghistron gem gaming gabrielle's funny functional fun friends food fixes fixed firefox FF3 ferret fantasy' emacs DV donate datarecovery database D&D converter conference computing cheat capistrano business bribesThe Hacker said on Fri Dec 14 13:41:22 +0000 2007 | permalink
Tagged: syntax highlighting javascript ruby perl rails
Syntax Highlighting for Everyone!
I recently integrated a javascript based syntax highlighter into this blog. Its very easy to do and quiet useful. Here is a quick rundown. I also go over some alternative methods afterwards.
The software I ended up using was SyntaxHighlighter
Instructions can be found here just include some files, run the javascript and your gold.
Once thats in place all you have todo is invoke:
<pre name="code" class="yourlanguage"> awesome code snippit here </pre>
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[10]"> awesome code snippit here </pre>
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:
- Csharp
- C++
- CSS
- Delphi
- JavaScript
- Java
- Php
- Python
- Ruby
- SQL
- VisualBasic
- XML (Which works well for xhtml files)
In addition you can grab shBrushPerl.js which adds perl support.
And thats all there is to it! Syntax Highlighting with Client Side Javascript.
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!
The Fixer said on Tue Apr 15 15:46:52 +0000 2008 | permalink
Tagged: perl
Perl use lib
I recently had occasion to break up an overly-large Perl script into a couple of modules, but I didn’t particularly want to install the modules anyplace. For one thing, I’m still working on them, but I also want to use a current (working!) version in production. Keeping the modules next to the script seemed like a nice straightforward thing to do, and it even worked, so long as the current working directory was the same as the directory the script and its modules were located in, but didn’t work so well from a different directory.
The solution is the FindBin module, which has some heuristics to find where the currently running script is actually located, and make that directory available to the rest of the program as a nice neat variable. Here’s what bit me; the syntax to actually make use of this looks like this:
use FindBin; use lib "$FindBin::Bin";
What I failed to notice is that the second line is NOT a fill-in-the-name-of-your-library-here thing, it’s a literal thing. It is supposed to be followed by code that actually loads the libraries you want, only now it will look in the same directory as where the script is loaded from as well.
use FindBin; use lib "$FindBin::Bin"; use MyLibrary; use MyOtherLibrary;
“use lib” is not a command to load a library, it’s a “pragma”, which alters the way Perl works. Yet another bit of the whale guts in Perl, I suppose. Since I don’t write in Perl all that often, I suppose I really ought to dig out my copy of the camel book and keep it in a more accessible place so I don’t embarrass myself like this every time I try.