Ruby 的源码高亮库:Rouge

jopen 10年前

Rouge 是一个纯 Ruby 实现的代码高亮库。它支持高亮 60 多种语言的代码,可以输出 HTML、ANSI-256 色文本格式。而且它的 HTML 输出兼容 Pygments 的语法高亮样式。

示例代码:

# make some nice lexed html  source = File.read('/etc/bashrc')  formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight')  lexer = Rouge::Lexers::Shell.new  formatter.format(lexer.lex(source))    # Get some CSS  Rouge::Themes::ThankfulEyes.render(:scope => '.highlight')

它还提供了一个命令行的工具,可以这么使用:

rougify foo.rb

项目主页:http://www.open-open.com/lib/view/home/1392620282663