Created
January 27, 2010 15:38
-
-
Save louis-wu/287936 to your computer and use it in GitHub Desktop.
使用coderay和railscasts样式进行代码高亮
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- | |
# CodeRay是一个语法高亮的Ruby库.支持的语法包括: Ruby, HTML, RHTML,YAML,SQL等。 | |
# 官方文档: http://coderay.rubychan.de/doc/ | |
# 以下示例来自于: http://hideto.javaeye.com/blog/204525 | |
#-- | |
# 1,安装coderay gem | |
gem install coderay | |
# 2,在application.rb中 | |
require 'coderay' | |
# 3,在application_helper.rb里添加一个helper方法 | |
def parse_coderay(text) | |
text.scan(/(\[code\:([a-z].+?)\](.+?)\[\/code\])/m).each do |match| | |
text.gsub!(match[0],CodeRay.scan(match[2].strip, match[1].to_sym).div( :line_numbers => :table,:css => :class)) | |
end | |
return text | |
end | |
# 这样,我们的文本输入框就支持这样的code标签了: | |
\[code:ruby\] | |
def aaa | |
puts "aaaa" | |
end | |
\[\/code\] | |
# code:ruby这样的标签中ruby可以用上面提到的支持的语言来替代 | |
# 4,借用railscasts的coderay样式 | |
http://railscasts.com/stylesheets/coderay.css | |
# 5,在html.erb页面中 | |
<div class="CodeRay"><%= parse_coderay @post.content %></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment