diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2018-09-29 06:57:40 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2018-09-29 06:57:40 +0000 |
commit | eb1e6d8c260e272fdee28ea1df5f034b2731215d (patch) | |
tree | faa172085f3fee950a46cc092296fc6c23f2df6c /lib | |
parent | bd2ee802c067f4e4880958c10b442a699f509431 (diff) | |
download | redmine-eb1e6d8c260e272fdee28ea1df5f034b2731215d.tar.gz redmine-eb1e6d8c260e272fdee28ea1df5f034b2731215d.zip |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/redmine/syntax_highlighting.rb | 59 | ||||
-rw-r--r-- | lib/redmine/wiki_formatting/textile/formatter.rb | 1 |
2 files changed, 42 insertions, 18 deletions
diff --git a/lib/redmine/syntax_highlighting.rb b/lib/redmine/syntax_highlighting.rb index 49d9f4110..e2d47f277 100644 --- a/lib/redmine/syntax_highlighting.rb +++ b/lib/redmine/syntax_highlighting.rb @@ -52,42 +52,65 @@ module Redmine end end - module CodeRay - require 'coderay' + module Rouge + require 'rouge' - def self.retrieve_supported_languages - ::CodeRay::Scanners.list + - # Add CodeRay scanner aliases - ::CodeRay::Scanners.plugin_hash.keys.map(&:to_sym) - - # Remove internal CodeRay scanners - %w(debug default raydebug scanner).map(&:to_sym) - end - private_class_method :retrieve_supported_languages + # Customized formatter based on Rouge::Formatters::HTMLLinewise + # Syntax highlighting is completed within each line. + class CustomHTMLLinewise < ::Rouge::Formatter + def initialize(formatter) + @formatter = formatter + end - SUPPORTED_LANGUAGES = retrieve_supported_languages + 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 class << self # Highlights +text+ as the content of +filename+ # Should not return line numbers nor outer pre tag def highlight_by_filename(text, filename) - language = ::CodeRay::FileType[filename] - language ? ::CodeRay.scan(text, language).html(:break_lines => true) : ERB::Util.h(text) + lexer =::Rouge::Lexer.guess_by_filename(filename) + formatter = ::Rouge::Formatters::HTML.new + ::Rouge.highlight(text, lexer, CustomHTMLLinewise.new(formatter)) end # Highlights +text+ using +language+ syntax # Should not return outer pre tag 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 def language_supported?(language) - SUPPORTED_LANGUAGES.include?(language.to_s.downcase.to_sym) - rescue - false + find_lexer(language.to_s.downcase) ? true : false + end + + 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 - SyntaxHighlighting.highlighter = 'CodeRay' + SyntaxHighlighting.highlighter = 'Rouge' end diff --git a/lib/redmine/wiki_formatting/textile/formatter.rb b/lib/redmine/wiki_formatting/textile/formatter.rb index b0b65bbbc..89e6105a8 100644 --- a/lib/redmine/wiki_formatting/textile/formatter.rb +++ b/lib/redmine/wiki_formatting/textile/formatter.rb @@ -125,6 +125,7 @@ module Redmine language = $1 text = $2 if Redmine::SyntaxHighlighting.language_supported?(language) + text.gsub!(/x%x%/, '&') content = "<code class=\"#{language} syntaxhl\">" + Redmine::SyntaxHighlighting.highlight_by_language(text, language) else |