Nice one!
Though I don't need that syntax highlighting right now, i can definately make use of the benchmarking. Thanks again.
Thanks Ryan, I was planning to build an app with this feature, so your screencast is definitely saving me some time.
Ryan, I'm actually in the process of setting up my own blog (built on Rails of course) that will focus on tutorials, screen-casts, and my personal musings.
A few days ago I realized that I hadn't setup a syntax highlighter yet... long story short I stopped in Railscasts and ended up using CodeRay after looking at your CSS/Source HTML.
I managed to get everything setup, but not without some trouble. Funny that you just came out with this, always on target at Railscasts!
Thanks!
Another method (via vim):
http://gist.github.com/347644
It script save code highlight to html file in current dir.
Amazing how you always post something about what I just did or am about to do! I figured out your Coderay technique by checking out the source to Railscasts on Github but I always like listening to your explanation and comments.
My friend @gotascii and I wrote a rack middleware that uses the unofficial pygments api (http://pygments.appspot.com/) to highlight source code within a ruby app.
Check it out, the only lib it requires is nokogiri.
http://www.gotascii.com/2009/11/7/pygrack-pygments-highlighting-middleware
A couple of notes.
1) In the regexp, if you are not going to be using a matching group, you can mark it as a non-matching group using '?:', ie /(?:this_group_wont_match)/, so you can use $2, $1, instead of $3, and $2. In your case it would look like this: /\<code(?: lang="(.+?)")?\>(.+?)\<\/code\>/m.
2) Changing code tag into div may not be a good idea if you're planning to have both inline and block code. code tag is by itself an inline element. To use block code it may be better to wrap code in pre.
Thanks, as always you're great but there is one more thing...
The video quality of this episode seems to be slightly awful compared with the rest of them.
@Matt, using Rack middleware is an interesting solution for this. I haven't looked into it but does performance become a problem? I imagine it needs to look through every HTML page even if there is no <pre> block on it, and is it possible to cache the results?
@hakunin, thanks for the notes and very good points. I always forget the syntax for non matching parenthesis.
@grigio, I'm not very familiar with these editors, but you just need to determine what they generate for the code syntax and swap that out like I do with the <code> tag in this episode.
@Memiux, I'm experimenting with other video codecs because, unfortunately, the Animation codec which I normally use is very buggy in Snow Leopard. I hope to find a solution which is nearly as good quality as Animation.
@Rbates, Memiux
I dont have any problems at all with the quality over all. Though that could be related to me running Ubuntu 10.04.
What I do note though is the filesize. It's a bit larger than usual for a shorter length of screencast.
Every byte is worth saving, considering that not everyone is using high-performance connections.
I for example is stuck with 2mbit and love smaller stream-sizes.
damn, I hoped so much you would continue covering new features in rails3.
Ryan, thanks for another good tutorial.
It's seem a good step for highlight search result, am I right?!
I thought the method looks similar.
I was wondering if there were any reasons you chose the ruby-based libraries over javascript libraries, other than the rails-specific target of the screen-cast series?
Hey Ryan,
Thanks for the informative screencast. Looking at the benchmarks, I was wondering how much of Pygments slow performance is from shelling and how much is due to Pygments itself.
I'm not very familiar with this benchmarking stuff though (the next Railscast?:-). On my machine coderay is about the same, 0.24 total and pygmentize is 9.28.
Not to discredit Pygments, I wrote up the following snippet, which may not be that useful for Rails developers. It returns 0.496517 usec to run Pygments 50 times from Python. Not sure exactly how to get more consistent benchmarks between the two.
http://gist.github.com/349504
Is there an update on the pygments yet? Would also like to know this info.
Nice screncast, as usual :-) But one recommendation would be, instead of re-parsing the code snippet at every request and caching it in the view, it is better to add the parsing as a before_save hook in the model and store the final HTML already pre-rendered in another text column in your table. Then the view has only to display this column instead of the original and because of this, it won't matter which library is faster because you only have to run it once and not at every request.
Thanks for this one Ryan.
I've been having quite a hard time coming up with a good solution to have both textile and coderay work for one of my apps. This will definitely do the trick! :)
Great video (as they all are).
I ended up using ultraviolet since I love the textmate themes anyway (I'm actually a Emacs+Mac guy myself). My blog isn't moved over from Wordpress to my own custom software yet, but once it is it's going to use ultraviolet.
Just so others know, Heroku has the required libraries for Ultraviolet already installed which make using it a snap :)
http://mxkelsin.heroku.com/2010/04/01/installing-ultraviolet-on-my-mac/
Thank for covering CodeRay - you did an outstanding job of explaining its strengths, weaknesses, and how to use it.
Only one addition: There's a built-in option to combine called (require) "coderay/for_redcloth" which enables the textile-style @[lang]...@ and bc[lang] shortcuts for syntax highlighting.
I have not been able to make it work with Rails 3.
I tried using your suggestions from the XSS screencast with combinations of html_safe and raw, but so far no luck. I can get it to output it escaped once or twice, but never unescaped!
Anyone made it work for Rails 3?
I got it to work with Rails 3.
def coderay(text)
text.gsub!(/\<code(?: lang="(.+?)")?\>(.+?)\<\/code\>/m) do
code = CodeRay.scan($2, $1).div(:css => :class)
"<notextile>#{code}</notextile>"
end
return text.html_safe
end
And to override the scaffold pre style, added background-color: black
.CodeRay pre {
margin: 0px;
padding: 0px;
background-color: black;
}
Hi,
just to let you know that I've build a new syntax highlighting engine in Ruby. It's called Prism and you'll find it here: http://github.com/kib2/Prism.
I've also made a little pastebin with it : http://prism-pastebin.heroku.com/.
The sample you gave renders the following: http://prism-pastebin.heroku.com/13
Please report me any bug/problem, see you.
Site tanıtım araçları arasında yer alan sitler 2010 popüler ve kaliteli siteleri hizmetinize sunuyor. Siteler 2010 yılında tıtım yapıtıyor.
Leaving a comment is the biggest support to Blogger.Thanks!
Thanks for sharing the information.It is definitely going to help me some time.
this was a really quality post.I wasn't aware of the many ripples and depth to this story until I surfed here through Google! Great job.
I remember I tried this a while ago. It brings back bad memories. Nothing good seems to happen the first time. How long did it take you? I look forward to your next story.
Great site. This could probably have the refactoring tag added t it.
To have the correct output with Rails 3.0.0.rc replace the following line :
content_tag("notextile", CodeRay.scan($3, $2).div(:css => :class))
by :
content_tag('notextile', CodeRay.scan($3, $2).div(:css => :class).html_safe)
(simply add html_safe)
Oops, sorry for make it in two times, also :
<%=raw textilize(coderay(@article.content)) %>
(simply add raw)
PS : Perhaps, could you join this to my precedent comment and delete this one ?
At QS Machinery, our engineers team always work closely with you, from a idea to finished products, so We can provide Quality Steel Casting, lost wax casting, precision casting and downhole cable protector and so on.
To 53:@Batzooh
Thanks for your comment, finally I can let it run on Rails 3.0.0.rc.
But I also notice that you're using raw helper method, which I think is dangerous because any script in the article content will run. It's better to use sanitize helper method.
<%=sanitize coderay(@article.content) %>
<a href="http://www.buyonstore.com/">designer handbags</a>
The article is worth reading, I like it very much. I will keep your new articles.
Generally I do not post on blogs, but I would like to say that this post really forced me to do so, Excellent post!
love converse all star,love yourself.High quality low price.It's fit for you.
Any member of your group can post to your trip blog. This is a great way to share information with your team and your
supporters.<b><a href=http://www.hzzxdq.net>power strip</a></b> |<b><a href=http://www.jdxpwj.cn/booster-cable>booster cable</a></b> |
<b><a href=http://www.jdxpwj.cn/tow-rope>tow rope</a></b> |<b><a href=http://www.jdxpwj.cn/ratchet-tiedown>ratchet tiedown</a></b>
Well done, I admire the valuable information you offer in your articles. I will bookmark your blog
and have my children check up here often. I am quite sure they will learn lots of new stuff here than
anybody else<b><a href=http://www.hzhtdq.cn>power strip</a></b> |<b><a href=http://www.hzhtdq.cn>extension cord</a></b>
|<b><a href=http://www.hzhtdq.cn>trouble light</a></b>
Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets
I have always liked Outdoor movies, a child standing at the window, looked out from home
to the following. Will be able to see the staff busy figure, a huge white cloth has a
child hang up and soon will be able to see the movie.
Yuyao Huaneng sanitary ware factory is specialized in production of plumbing fittings.
thanks for the notes and very good points. I always forget the syntax for non matching parenthesis.
Good article! Thank you so much for sharing this post.Your views truly open my mind.






