Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Rouge syntax colorizer - issue #275 #398

Merged
merged 1 commit into from
Mar 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gem 'rdiscount'
gem 'rdoc'
gem 'redcarpet'
gem 'RedCloth'
gem 'rouge'
gem 'rubypants'
gem 'sass', '~> 3.2.2'
gem 'slim'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ GEM
ref (1.0.5)
rest-client (1.6.7)
mime-types (>= 1.16)
rouge (1.3.3)
ruby-hmac (0.4.0)
rubypants (0.2.0)
sass (3.2.12)
Expand Down Expand Up @@ -190,6 +191,7 @@ DEPENDENCIES
rdiscount
rdoc
redcarpet
rouge
rubocop!
rubypants
sass (~> 3.2.2)
Expand Down
20 changes: 19 additions & 1 deletion lib/nanoc/filters/colorize_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ColorizeSyntax < Nanoc::Filter
# * `:pygmentsrb` for [pygments.rb](https://github.com/tmm1/pygments.rb),
# a Ruby interface for [Pygments](http://pygments.org/)
# * `:simon_highlight` for [Highlight](http://www.andre-simon.de/doku/highlight/en/highlight.html)
# * `:rouge` for [Rouge](https://github.com/jayferd/rouge/)
#
# Additional colorizer implementations are welcome!
#
Expand Down Expand Up @@ -292,9 +293,26 @@ def coderay_postprocess(language, element)
element.swap div_outer
end

# Runs the content through [Rouge](https://github.com/jayferd/rouge/.
#
# @api private
#
# @param [String] code The code to colorize
#
# @param [String] language The language the code is written in
#
# @return [String] The colorized output
def rouge(code, language, params = {})
require 'rouge'

formatter = Rouge::Formatters::HTML.new(:css_class => params[:css_class] || "highlight")
lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText
formatter.format(lexer.lex(code))
end

protected

KNOWN_COLORIZERS = [ :coderay, :dummy, :pygmentize, :pygmentsrb, :simon_highlight ]
KNOWN_COLORIZERS = [ :coderay, :dummy, :pygmentize, :pygmentsrb, :simon_highlight, :rouge ]

# Removes the first blank lines and any whitespace at the end.
def strip(s)
Expand Down
54 changes: 54 additions & 0 deletions test/filters/test_colorize_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,58 @@ def foo
end
end

def test_rouge
if_have 'rouge', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new

# Get input and expected output
input = <<EOS
before
<pre><code class="language-ruby">
def foo
end
</code></pre>
after
EOS
expected_output = <<EOS
before
<pre><code class=\"language-ruby\"><pre class=\"highlight\"> <span class=\"k\">def</span> <span class=\"nf\">foo</span>
<span class=\"k\">end</span></pre></code></pre>
after
EOS

# Run filter
actual_output = filter.setup_and_run(input, :default_colorizer => :rouge)
assert_equal(expected_output, actual_output)
end
end

def test_rouge_with_css_class
if_have 'rouge', 'nokogiri' do
# Create filter
filter = ::Nanoc::Filters::ColorizeSyntax.new

# Get input and expected output
input = <<EOS
before
<pre><code class="language-ruby">
def foo
end
</code></pre>
after
EOS
expected_output = <<EOS
before
<pre><code class=\"language-ruby\"><pre class=\"my-class\"> <span class=\"k\">def</span> <span class=\"nf\">foo</span>
<span class=\"k\">end</span></pre></code></pre>
after
EOS

# Run filter
actual_output = filter.setup_and_run(input, :default_colorizer => :rouge, :rouge => { :css_class => 'my-class' })
assert_equal(expected_output, actual_output)
end
end

end