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.
The installation of the Ultraviolet gem requires first installing the Onigurama library and gem. I've done all my Unix installs with MacPorts, and it turns out there's a port for Onigurama. Pick the latest: onigurama5.
For newbies like me, first do a "sudo port selfupdate" and then "sudo port install onigurama5"
Then --oops -- no success installing the plugin. Does this plugin require Rails 2.0.2?
I tried "script/plugin install" with each of the following arguments: "tm_syntax_lighlighting" "git://github.com/arya/tm_syntax_highlighting.git" "tm_syntax_highlighting --source http://github.com/arya/tm_syntax_highlighting/tree/master"
Couldn't find it. The last attempt yielded the following error:
Then I downloaded and unpacked the .tar.gz file into the /vendors/plugins directory. But then "script/generate syntax_css list" generated a NoMethodError; apparently it couldn't locate a file. Should I leave the code at the end of the directory name ("arya-tm_syntax_highlighting-e38b5c67f3e05f4cfd5b3128fbf030e1faea1124")? Deleting it didn't help.
Hi Jim,
I'm sorry to hear you're having trouble installing the plugin. I don't think there is anything specific to Rails 2.0.2 in there, it should work with 1.2 as well.
As for running "script/plugin install" on a Git repository, that unfortunately does not work (at least not in the current version of Rails).
You were right to download the tarball and extract it. It should be in a folder named "tm_syntax_highlighting" inside your "vendor/plugins" folder with no suffix or anything. Then you need to make sure you have the following 3 gems installed: oniguruma, textpow, ultraviolet. Note that the oniguruma gem is different (but depends on) the system package you installed through MacPorts.
Lastly, I'm actually not sure if the MacPorts version is compatible with the oniguruma ruby gem. Maybe that has to do with the error you are getting. After you ensure you have the gems correctly installed and if you still get the error, can you paste the NoMethodError you get so I can help debug some more?
I know it's a little off-topic, but I was wondering if you could point me at any resources for actually generating these kinds of syntax-highlighted code samples from TextMate itself. I've looked around everywhere, but Google keeps sending me here! Thanks...
Hi Greg,
If you open up the Bundles menu and go down to TextMate, there should be 3 relevant menu options:
If TextMate does not appear as a bundle, try going to Bundles > Bundle Editor > Show Bundle Editor and making sure that it's installed and not disabled in the filter list.
If you don't have the TextMate Bundle installed, you can check it out (into Library/Application Support/TextMate/Bundles) using subversion from TextMate's website: http://macromates.com/svn/Bundles/trunk/Bundles/TextMate.tmbundle/
I hope that helps. Let me know if you have any other questions.
This is nifty, thank you :)
I too had the problem of using it with Markdown. I scan for code blocks using the original Markdown.pl regex, generate the HTML with your plugin and then pipe the rest through Markdown (RDiscount).
In this way i can just write normal Markdown code blocks, with a optional parameter to choose the language:
Also viewable at http://pastie.org/288223
Probably not nearly optimal and a bit hackish, but works for me.
I use it in my Model as a :before_save to generate the html and save it in an extra db field alongside the markdown text.
very appreciate for this plugin.And I just wanna know that how did I develop a web app like this page? I mean the content maybe contains some different codes and text.Is there a demo to show this? thanks again!
Hi Lee,
There a few code examples above that show usage of the plugin. You can install the plugin on any Rails application and install the required gems and that'll provide the helper to take code segments and turn them into syntax-highlighted code.
Leave a Comment