Syntax highlighter: replace CodeRay with Rouge (#24681).

Patch by Go MAEDA.

git-svn-id: http://svn.redmine.org/redmine/trunk@17532 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2018-09-29 06:57:40 +00:00
parent bd2ee802c0
commit eb1e6d8c26
10 changed files with 141 additions and 166 deletions

View File

@@ -5,7 +5,7 @@ if Gem::Version.new(Bundler::VERSION) < Gem::Version.new('1.5.0')
end end
gem "rails", "5.2.1" gem "rails", "5.2.1"
gem "coderay", "~> 1.1.1" gem "rouge", "~> 3.2.1"
gem "request_store", "1.0.5" gem "request_store", "1.0.5"
gem "mini_mime", "~> 1.0.1" gem "mini_mime", "~> 1.0.1"
gem "actionpack-xml_parser" gem "actionpack-xml_parser"
@@ -89,7 +89,7 @@ group :test do
gem "mocha" gem "mocha"
gem "simplecov", "~> 0.14.1", :require => false gem "simplecov", "~> 0.14.1", :require => false
# For running system tests # For running system tests
gem 'puma', '~> 3.7' #gem 'puma', '~> 3.7'
gem "capybara", '~> 2.13' gem "capybara", '~> 2.13'
gem "selenium-webdriver" gem "selenium-webdriver"
end end

View File

@@ -52,42 +52,65 @@ module Redmine
end end
end end
module CodeRay module Rouge
require 'coderay' require 'rouge'
def self.retrieve_supported_languages # Customized formatter based on Rouge::Formatters::HTMLLinewise
::CodeRay::Scanners.list + # Syntax highlighting is completed within each line.
# Add CodeRay scanner aliases class CustomHTMLLinewise < ::Rouge::Formatter
::CodeRay::Scanners.plugin_hash.keys.map(&:to_sym) - def initialize(formatter)
# Remove internal CodeRay scanners @formatter = formatter
%w(debug default raydebug scanner).map(&:to_sym) end
def stream(tokens, &b)
token_lines(tokens) do |line|
line.each do |tok, val|
yield @formatter.span(tok, val)
end
yield "\n"
end
end
end end
private_class_method :retrieve_supported_languages
SUPPORTED_LANGUAGES = retrieve_supported_languages
class << self class << self
# Highlights +text+ as the content of +filename+ # Highlights +text+ as the content of +filename+
# Should not return line numbers nor outer pre tag # Should not return line numbers nor outer pre tag
def highlight_by_filename(text, filename) def highlight_by_filename(text, filename)
language = ::CodeRay::FileType[filename] lexer =::Rouge::Lexer.guess_by_filename(filename)
language ? ::CodeRay.scan(text, language).html(:break_lines => true) : ERB::Util.h(text) formatter = ::Rouge::Formatters::HTML.new
::Rouge.highlight(text, lexer, CustomHTMLLinewise.new(formatter))
end end
# Highlights +text+ using +language+ syntax # Highlights +text+ using +language+ syntax
# Should not return outer pre tag # Should not return outer pre tag
def highlight_by_language(text, language) def highlight_by_language(text, language)
::CodeRay.scan(text, language).html(:wrap => :span) lexer =
find_lexer(language.to_s.downcase) || ::Rouge::Lexers::PlainText
::Rouge.highlight(text, lexer, ::Rouge::Formatters::HTML)
end end
def language_supported?(language) def language_supported?(language)
SUPPORTED_LANGUAGES.include?(language.to_s.downcase.to_sym) find_lexer(language.to_s.downcase) ? true : false
rescue end
false
private
# Alias names used by CodeRay and not supported by Rouge
LANG_ALIASES = {
'delphi' => 'pascal',
'cplusplus' => 'cpp',
'ecmascript' => 'javascript',
'ecma_script' => 'javascript',
'java_script' => 'javascript',
'xhtml' => 'html'
}
def find_lexer(language)
::Rouge::Lexer.find(language) ||
::Rouge::Lexer.find(LANG_ALIASES[language])
end end
end end
end end
end end
SyntaxHighlighting.highlighter = 'CodeRay' SyntaxHighlighting.highlighter = 'Rouge'
end end

View File

@@ -125,6 +125,7 @@ module Redmine
language = $1 language = $1
text = $2 text = $2
if Redmine::SyntaxHighlighting.language_supported?(language) if Redmine::SyntaxHighlighting.language_supported?(language)
text.gsub!(/x%x%/, '&')
content = "<code class=\"#{language} syntaxhl\">" + content = "<code class=\"#{language} syntaxhl\">" +
Redmine::SyntaxHighlighting.highlight_by_language(text, language) Redmine::SyntaxHighlighting.highlight_by_language(text, language)
else else

View File

@@ -453,10 +453,10 @@ jsToolBar.prototype.resizeDragStop = function(event) {
/* Code highlighting menu */ /* Code highlighting menu */
jsToolBar.prototype.precodeMenu = function(fn){ jsToolBar.prototype.precodeMenu = function(fn){
var codeRayLanguages = ["c", "clojure", "cpp", "css", "delphi", "diff", "erb", "go", "groovy", "haml", "html", "java", "javascript", "json", "lua", "php", "python", "ruby", "sass", "sql", "taskpaper", "text", "xml", "yaml"]; var hlLanguages = ["c", "clojure", "cpp", "css", "diff", "erb", "go", "groovy", "haml", "html", "java", "javascript", "json", "lua", "pascal", "php", "python", "ruby", "sass", "sql", "text", "xml", "yaml"];
var menu = $("<ul style='position:absolute;'></ul>"); var menu = $("<ul style='position:absolute;'></ul>");
for (var i = 0; i < codeRayLanguages.length; i++) { for (var i = 0; i < hlLanguages.length; i++) {
$("<li></li>").text(codeRayLanguages[i]).appendTo(menu).mousedown(function(){ $("<li></li>").text(hlLanguages[i]).appendTo(menu).mousedown(function(){
fn($(this).text()); fn($(this).text());
}); });
} }

View File

@@ -1407,102 +1407,77 @@ img.filecontent.image {background-image: url(../images/transparent.png);}
.ui-datepicker-title select {width:70px !important; margin-top:-2px !important; margin-right:4px !important;} .ui-datepicker-title select {width:70px !important; margin-top:-2px !important; margin-right:4px !important;}
/************* CodeRay styles *************/ /************* Rouge styles *************/
.syntaxhl div {display: inline;} /* generated by: pygmentize -f html -a .syntaxhl -S colorful */
.syntaxhl .code pre { overflow: auto } .syntaxhl .hll { background-color: #ffffcc }
.syntaxhl { background: #fafafa; }
.syntaxhl .annotation { color:#007 } .syntaxhl .c { color: #888888 } /* Comment */
.syntaxhl .attribute-name { color:#b48 } .syntaxhl .err { color: #FF0000; background-color: #FFAAAA } /* Error */
.syntaxhl .attribute-value { color:#700 } .syntaxhl .k { color: #008800; font-weight: bold } /* Keyword */
.syntaxhl .binary { color:#549 } .syntaxhl .o { color: #333333 } /* Operator */
.syntaxhl .binary .char { color:#325 } .syntaxhl .ch { color: #888888 } /* Comment.Hashbang */
.syntaxhl .binary .delimiter { color:#325 } .syntaxhl .cm { color: #888888 } /* Comment.Multiline */
.syntaxhl .char { color:#D20 } .syntaxhl .cp { color: #557799 } /* Comment.Preproc */
.syntaxhl .char .content { color:#D20 } .syntaxhl .cpf { color: #888888 } /* Comment.PreprocFile */
.syntaxhl .char .delimiter { color:#710 } .syntaxhl .c1 { color: #888888 } /* Comment.Single */
.syntaxhl .class { color:#B06; font-weight:bold } .syntaxhl .cs { color: #cc0000; font-weight: bold } /* Comment.Special */
.syntaxhl .class-variable { color:#369 } .syntaxhl .gd { color: #A00000 } /* Generic.Deleted */
.syntaxhl .color { color:#0A0 } .syntaxhl .ge { font-style: italic } /* Generic.Emph */
.syntaxhl .comment { color:#777 } .syntaxhl .gr { color: #FF0000 } /* Generic.Error */
.syntaxhl .comment .char { color:#444 } .syntaxhl .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.syntaxhl .comment .delimiter { color:#444 } .syntaxhl .gi { color: #00A000 } /* Generic.Inserted */
.syntaxhl .constant { color:#036; font-weight:bold } .syntaxhl .go { color: #888888 } /* Generic.Output */
.syntaxhl .decorator { color:#B0B } .syntaxhl .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.syntaxhl .definition { color:#099; font-weight:bold } .syntaxhl .gs { font-weight: bold } /* Generic.Strong */
.syntaxhl .delimiter { color:black } .syntaxhl .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.syntaxhl .directive { color:#088; font-weight:bold } .syntaxhl .gt { color: #0044DD } /* Generic.Traceback */
.syntaxhl .docstring { color:#D42; } .syntaxhl .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.syntaxhl .doctype { color:#34b } .syntaxhl .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.syntaxhl .done { text-decoration: line-through; color: gray } .syntaxhl .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.syntaxhl .entity { color:#800; font-weight:bold } .syntaxhl .kp { color: #003388; font-weight: bold } /* Keyword.Pseudo */
.syntaxhl .error { color:#F00; background-color:#FAA } .syntaxhl .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.syntaxhl .escape { color:#666 } .syntaxhl .kt { color: #333399; font-weight: bold } /* Keyword.Type */
.syntaxhl .exception { color:#C00; font-weight:bold } .syntaxhl .m { color: #6600EE; font-weight: bold } /* Literal.Number */
.syntaxhl .float { color:#60E } .syntaxhl .s { background-color: #fff0f0 } /* Literal.String */
.syntaxhl .function { color:#06B; font-weight:bold } .syntaxhl .na { color: #0000CC } /* Name.Attribute */
.syntaxhl .function .delimiter { color:#059 } .syntaxhl .nb { color: #007020 } /* Name.Builtin */
.syntaxhl .function .content { color:#037 } .syntaxhl .nc { color: #BB0066; font-weight: bold } /* Name.Class */
.syntaxhl .global-variable { color:#d70 } .syntaxhl .no { color: #003366; font-weight: bold } /* Name.Constant */
.syntaxhl .hex { color:#02b } .syntaxhl .nd { color: #555555; font-weight: bold } /* Name.Decorator */
.syntaxhl .id { color:#33D; font-weight:bold } .syntaxhl .ni { color: #880000; font-weight: bold } /* Name.Entity */
.syntaxhl .include { color:#B44; font-weight:bold } .syntaxhl .ne { color: #FF0000; font-weight: bold } /* Name.Exception */
.syntaxhl .inline { background-color: hsla(0,0%,0%,0.07); color: black } .syntaxhl .nf { color: #0066BB; font-weight: bold } /* Name.Function */
.syntaxhl .inline-delimiter { font-weight: bold; color: #666 } .syntaxhl .nl { color: #997700; font-weight: bold } /* Name.Label */
.syntaxhl .instance-variable { color:#33B } .syntaxhl .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.syntaxhl .integer { color:#00D } .syntaxhl .nt { color: #007700 } /* Name.Tag */
.syntaxhl .imaginary { color:#f00 } .syntaxhl .nv { color: #996633 } /* Name.Variable */
.syntaxhl .important { color:#D00 } .syntaxhl .ow { color: #000000; font-weight: bold } /* Operator.Word */
.syntaxhl .key { color: #606 } .syntaxhl .w { color: #bbbbbb } /* Text.Whitespace */
.syntaxhl .key .char { color: #60f } .syntaxhl .mb { color: #6600EE; font-weight: bold } /* Literal.Number.Bin */
.syntaxhl .key .delimiter { color: #404 } .syntaxhl .mf { color: #6600EE; font-weight: bold } /* Literal.Number.Float */
.syntaxhl .keyword { color:#080; font-weight:bold } .syntaxhl .mh { color: #005588; font-weight: bold } /* Literal.Number.Hex */
.syntaxhl .label { color:#970; font-weight:bold } .syntaxhl .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.syntaxhl .local-variable { color:#950 } .syntaxhl .mo { color: #4400EE; font-weight: bold } /* Literal.Number.Oct */
.syntaxhl .map .content { color:#808 } .syntaxhl .sa { background-color: #fff0f0 } /* Literal.String.Affix */
.syntaxhl .map .delimiter { color:#40A} .syntaxhl .sb { background-color: #fff0f0 } /* Literal.String.Backtick */
.syntaxhl .map { background-color:hsla(200,100%,50%,0.06); } .syntaxhl .sc { color: #0044DD } /* Literal.String.Char */
.syntaxhl .namespace { color:#707; font-weight:bold } .syntaxhl .dl { background-color: #fff0f0 } /* Literal.String.Delimiter */
.syntaxhl .octal { color:#40E } .syntaxhl .sd { color: #DD4422 } /* Literal.String.Doc */
.syntaxhl .operator { } .syntaxhl .s2 { background-color: #fff0f0 } /* Literal.String.Double */
.syntaxhl .predefined { color:#369; font-weight:bold } .syntaxhl .se { color: #666666; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */
.syntaxhl .predefined-constant { color:#069 } .syntaxhl .sh { background-color: #fff0f0 } /* Literal.String.Heredoc */
.syntaxhl .predefined-type { color:#0a8; font-weight:bold } .syntaxhl .si { background-color: #eeeeee } /* Literal.String.Interpol */
.syntaxhl .preprocessor { color:#579 } .syntaxhl .sx { color: #DD2200; background-color: #fff0f0 } /* Literal.String.Other */
.syntaxhl .pseudo-class { color:#00C; font-weight:bold } .syntaxhl .sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */
.syntaxhl .regexp { background-color:hsla(300,100%,50%,0.06); } .syntaxhl .s1 { background-color: #fff0f0 } /* Literal.String.Single */
.syntaxhl .regexp .content { color:#808 } .syntaxhl .ss { color: #AA6600 } /* Literal.String.Symbol */
.syntaxhl .regexp .delimiter { color:#404 } .syntaxhl .bp { color: #007020 } /* Name.Builtin.Pseudo */
.syntaxhl .regexp .modifier { color:#C2C } .syntaxhl .fm { color: #0066BB; font-weight: bold } /* Name.Function.Magic */
.syntaxhl .reserved { color:#080; font-weight:bold } .syntaxhl .vc { color: #336699 } /* Name.Variable.Class */
.syntaxhl .shell { background-color:hsla(120,100%,50%,0.06); } .syntaxhl .vg { color: #dd7700; font-weight: bold } /* Name.Variable.Global */
.syntaxhl .shell .content { color:#2B2 } .syntaxhl .vi { color: #3333BB } /* Name.Variable.Instance */
.syntaxhl .shell .delimiter { color:#161 } .syntaxhl .vm { color: #996633 } /* Name.Variable.Magic */
.syntaxhl .string { background-color:hsla(0,100%,50%,0.05); } .syntaxhl .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
.syntaxhl .string .char { color: #b0b }
.syntaxhl .string .content { color: #D20 }
.syntaxhl .string .delimiter { color: #710 }
.syntaxhl .string .modifier { color: #E40 }
.syntaxhl .symbol { color:#A60 }
.syntaxhl .symbol .content { color:#A60 }
.syntaxhl .symbol .delimiter { color:#740 }
.syntaxhl .tag { color:#070; font-weight:bold }
.syntaxhl .type { color:#339; font-weight:bold }
.syntaxhl .value { color: #088 }
.syntaxhl .variable { color:#037 }
.syntaxhl .insert { background: hsla(120,100%,50%,0.12) }
.syntaxhl .delete { background: hsla(0,100%,50%,0.12) }
.syntaxhl .change { color: #bbf; background: #007 }
.syntaxhl .head { color: #f8f; background: #505 }
.syntaxhl .head .filename { color: white; }
.syntaxhl .delete .eyecatcher { background-color: hsla(0,100%,50%,0.2); border: 1px solid hsla(0,100%,45%,0.5); margin: -1px; border-bottom: none; border-top-left-radius: 5px; border-top-right-radius: 5px; }
.syntaxhl .insert .eyecatcher { background-color: hsla(120,100%,50%,0.2); border: 1px solid hsla(120,100%,25%,0.5); margin: -1px; border-top: none; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; }
.syntaxhl .insert .insert { color: #0c0; background:transparent; font-weight:bold }
.syntaxhl .delete .delete { color: #c00; background:transparent; font-weight:bold }
.syntaxhl .change .change { color: #88f }
.syntaxhl .head .head { color: #f4f }
/***** Media print specific styles *****/ /***** Media print specific styles *****/
@media print { @media print {

View File

@@ -373,9 +373,6 @@ td.username img.gravatar {margin:0 0 0 0.5em; }
/* Custom JQuery styles */ /* Custom JQuery styles */
.ui-datepicker-title select {margin-left:4px !important; margin-right:0 !important;} .ui-datepicker-title select {margin-left:4px !important; margin-right:0 !important;}
/************* CodeRay styles *************/
.syntaxhl .line-numbers {margin:0px 0px 0px 5px;}
/***** Media print specific styles *****/ /***** Media print specific styles *****/
@media print { @media print {
} }

View File

@@ -1094,19 +1094,35 @@ EXPECTED
def test_syntax_highlight def test_syntax_highlight
raw = <<-RAW raw = <<-RAW
<pre><code class="ruby"> <pre><code class="ECMA_script">
# Some ruby code here /* Hello */
document.write("Hello World!");
</code></pre> </code></pre>
RAW RAW
expected = <<-EXPECTED expected = <<-EXPECTED
<pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="comment"># Some ruby code here</span></span> <pre><code class=\"ECMA_script syntaxhl\"><span class=\"cm\">/* Hello */</span><span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">write</span><span class=\"p\">(</span><span class=\"s2\">\"Hello World!\"</span><span class=\"p\">);</span></code></pre>
</code></pre>
EXPECTED EXPECTED
assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
end end
def test_syntax_highlight_ampersand_in_textile
raw = <<-RAW
<pre><code class="ruby">
x = a & b
</code></pre>
RAW
expected = <<-EXPECTED
<pre><code class=\"ruby syntaxhl\"><span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"n\">a</span> <span class=\"o\">&amp;</span> <span class=\"n\">b</span></code></pre>
EXPECTED
with_settings :text_formatting => 'textile' do
assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
end
end
def test_to_path_param def test_to_path_param
assert_equal 'test1/test2', to_path_param('test1/test2') assert_equal 'test1/test2', to_path_param('test1/test2')
assert_equal 'test1/test2', to_path_param('/test1/test2/') assert_equal 'test1/test2', to_path_param('/test1/test2/')

View File

@@ -1,37 +0,0 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../../../../test_helper', __FILE__)
class Redmine::SyntaxHighlighting::CodeRayTest < ActiveSupport::TestCase
def test_retrieve_supported_languages_should_return_array_of_symbols
assert_kind_of Array, Redmine::SyntaxHighlighting::CodeRay.send(:retrieve_supported_languages)
assert_kind_of Symbol, Redmine::SyntaxHighlighting::CodeRay.send(:retrieve_supported_languages).first
end
def test_retrieve_supported_languages_should_return_array_of_symbols_holding_languages
assert_includes Redmine::SyntaxHighlighting::CodeRay.send(:retrieve_supported_languages), :ruby
end
def test_retrieve_supported_languages_should_return_array_of_symbols_holding_languages_aliases
assert_includes Redmine::SyntaxHighlighting::CodeRay.send(:retrieve_supported_languages), :javascript
end
def test_retrieve_supported_languages_should_return_array_of_symbols_not_holding_internal_languages
refute_includes Redmine::SyntaxHighlighting::CodeRay.send(:retrieve_supported_languages), :default
end
end

View File

@@ -66,7 +66,7 @@ end
~~~ ~~~
STR STR
assert_select_in @formatter.new(text).to_html, 'pre code.ruby.syntaxhl' do assert_select_in @formatter.new(text).to_html, 'pre code.ruby.syntaxhl' do
assert_select 'span.keyword', :text => 'def' assert_select 'span.k', :text => 'def'
end end
end end

View File

@@ -547,9 +547,9 @@ STR
def test_should_allow_valid_language_class_attribute_on_code_tags def test_should_allow_valid_language_class_attribute_on_code_tags
# language name is double-quoted # language name is double-quoted
assert_html_output({"<code class=\"ruby\">test</code>" => "<code class=\"ruby syntaxhl\"><span class=\"CodeRay\">test</span></code>"}, false) assert_html_output({"<code class=\"ruby\">test</code>" => "<code class=\"ruby syntaxhl\"><span class=\"nb\">test</span></code>"}, false)
# language name is single-quoted # language name is single-quoted
assert_html_output({"<code class='ruby'>test</code>" => "<code class=\"ruby syntaxhl\"><span class=\"CodeRay\">test</span></code>"}, false) assert_html_output({"<code class='ruby'>test</code>" => "<code class=\"ruby syntaxhl\"><span class=\"nb\">test</span></code>"}, false)
end end
def test_should_not_allow_valid_language_class_attribute_on_non_code_offtags def test_should_not_allow_valid_language_class_attribute_on_non_code_offtags