Rails Plugin: TextMate Syntax Highlighting
April 30, 2008Have you been envious of my super-pretty code blocks? Well envy no more! I finally released it as a plugin. It's pretty simple, it uses the Ultraviolet gem and provides easy-to-use helpers and then you get the awesomeness you see below.
1 def create 2 @post = Post.new(params[:post]) 3 @post.user = current_user 4 5 set_hidden_based_on_commit 6 7 respond_to do |format| 8 if @post.save 9 flash[:notice] = "Your post has been successfully created!" 10 format.html { redirect_to post_path(@post)} 11 else 12 format.html { render :action => "new"} 13 end 14 end 15 end
There is a pretty clear README included with the plugin, please do read it if you're going to use it.
A few quick notes:
- Ultraviolet and/or TextPow is slow so fragment caching for syntax highlighted blocks is highly recommended.
- You need to copy over the CSS files for the themes using the syntax_css generator (described in the README).
- Please do contribute if you have any ideas or improvements.
Anyway, here it is: tm_syntax_hightlighting hosted on GitHub.
Comments
I want an RSS feed for this blog! In fact I demand it! :P
Can't keep up with blogs unless i have a feed and i really like this one so far.
Hey Fredrik,
Thanks for reading my blog. I actually do have an RSS feed. It's set up as an auto-discovery link so you browser should pick it up. I'll add an RSS icon/link somewhere soon. But here is the link for now:
http://feeds.feedburner.com/UnboundImagination
You might want to run your rss excerpt thru a markdown parser before publishing it, as it currently shows the markdown code.
Oh blushes Thank you!
Awesome, I look forward to incorporating this into my site as well, keep up the good posts!
How slow are we talking?
Alistair,
I did a quick benchmark for you:
Came out to 0.06 seconds average but probably even larger for longer code segments. It's not outrageously slow, but it makes a pretty big chunk of the render time if you don't cache it.
seems to be an improved(?) version of radiograph? ;)
Hi Fredrik,
Thanks for this! I'm sorry to be so ignorant, but I'm new to plugins - what is the command for installing this particular plugin?
Thanks, Ken
Hi universal,
Yes, radiograph also does syntax highlighting using Ultraviolet. I found it when I was making my blog and had some problems so I decided to write my own that was more customizable.
Hi Ken,
I'm not sure if you meant to be addressing me. Fredrik is just a reader of the blog, not the author. Anyway, to install this plugin you can by downloading the source through git or if you don't use git, you can download the tarball version and extract it into your vendor/plugins folder. Then you can read the README to see how to use it.
Oops - sorry for the confusion on your name. Thanks for the help on the install!!
K
Okay I got it all installed and what not, but how do you implement it on the page? Say I'm using markdown? Or is there something special I gots ta do?
Hi Zach,
To syntax highlight a block of code, you pass a string containing the code to the code view helper and it returns the HTML for syntax highlighted code (assuming you have the respective CSS included). The README's examples cover the basics.
Now if you're using it in your blogging engine like I am and you want to be able to embed code into posts like this:
You can whip up your own solution for splitting it and parsing them separately or use the one I wrote:
Update: I had to replace "code" with "c0de" in order to for it to parse itself correctly. (Clearly it's flawed, if any readers want to make any suggestions, that would be greatly appreciated.)
It's not exactly the prettiest solution but it gets the job done pretty well. The one issue I have with it right now is that I can't use Markdown's reference links if the reference and the definition is separated by a code block. I intend on fixing that when I have some free time, if you want, I can e-mail you to notify you when I've made those changes.
Anyway, feel free to ask any questions about the above code if it's unclear, I'll be glad to help.
Hey Arya,
Thanks for the fast response and taking the time to whip up some code. I included your code in my application_helper.rb file and I call it like this:
The problem I am getting is that the pre tag is not getting the a class appended to it, whether it be sunburst or twilight. Is there something I am missing? I have the CSS properly included, and if I go in there and hardcode the pre tag with the appropriate class the highlighting works correctly.
Thanks for a great plugin and I'd love to receive an email with updates. Also are you on workingwithrails.com?
Sorry to double comment like this. I believe the problem is in my initializer for the defaults, can you explain how you did that?
Thanks again.
Hey Zach,
No problem, I'm glad to help.
There are two ways to specify the theme, you can either pass it when you call the helper, like so:
Or you can specify it globally so it becomes the default, by doing this is in an initializer:
Remember that you'll need to restart your mongrel if you add/change the initializer since those only get executed on boot.
Try that and let me know how it goes.
Also, I am on WorkingWithRails.com. I have the badge on my blog index, but here is a link for your convenience.
http://www.workingwithrails.com/recommendation/new/person/10754-arya-asemanfar
Hey again,
Okay it was a number of factors that were causing the problems. You were right that I needed to restart my server for the initializer, but I went ahead and put it in the helper so that I could use it with a live preview. Which works by the way. I also had to change 'c0de' to 'code', which I think you were getting at in your update. The only thing I don't get is how you made your code areas scrollable, but I suspect that is a CSS problem. Other than that, everything works like a charm. Sweet ass plugin man. Very good job, thanks for being so helpful as well.
Hi Zach,
Yea, the "c0de" (with a zero) was my way of getting around my code parsing itself as a end-of-code block.
As for the scrolling code blocks, I added this CSS to all pre tags:
The overflow is the part that makes it have scroll bars instead of overflowing.
Also, if possible, could you link to your site? I'd like to see it in use on someone else's site :)
Overflow! That was it. And yes, I'll link to it, it's actually in active development, but as soon as it's up (Friday at the latest), I'll link to it.
Leave a Comment