Index: app/helpers/application_helper.rb =================================================================== --- app/helpers/application_helper.rb (revision 2594) +++ app/helpers/application_helper.rb (working copy) @@ -15,8 +15,6 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -require 'coderay' -require 'coderay/helpers/file_type' require 'forwardable' require 'cgi' @@ -216,8 +214,7 @@ end def syntax_highlight(name, content) - type = CodeRay::FileType[name] - type ? CodeRay.scan(content, type).html : h(content) + Redmine::SyntaxHighlighting.colorize_file(name, content) end def to_path_param(path) Index: app/views/settings/_general.rhtml =================================================================== --- app/views/settings/_general.rhtml (revision 2594) +++ app/views/settings/_general.rhtml (working copy) @@ -27,6 +27,9 @@

<%= select_tag 'settings[text_formatting]', options_for_select([[l(:label_none), "0"], *Redmine::WikiFormatting.format_names.collect{|name| [name, name]} ], Setting.text_formatting.to_sym) %>

+

+<%= select_tag 'settings[syntax_highlighter]', options_for_select( (Redmine::SyntaxHighlighting.available_highlighters.collect{|h| [h, h]}), Setting.syntax_highlighter) %>

+

<%= select_tag 'settings[wiki_compression]', options_for_select( [[l(:label_none), 0], ["gzip", "gzip"]], Setting.wiki_compression) %>

Index: app/views/attachments/file.rhtml =================================================================== --- app/views/attachments/file.rhtml (revision 2594) +++ app/views/attachments/file.rhtml (working copy) @@ -8,7 +8,7 @@   -<%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.filename} %> +<%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.disk_filename} %> <% content_for :header_tags do -%> <%= stylesheet_link_tag "scm" -%> Index: app/views/common/_file.rhtml =================================================================== --- app/views/common/_file.rhtml (revision 2594) +++ app/views/common/_file.rhtml (working copy) @@ -1,9 +1,9 @@
- +
<% line_num = 1 %> <% syntax_highlight(filename, to_utf8(content)).each_line do |line| %> - + <% line_num += 1 %> <% end %> Index: config/settings.yml =================================================================== --- config/settings.yml (revision 2594) +++ config/settings.yml (working copy) @@ -150,3 +150,5 @@ default: 0 openid: default: 0 +syntax_highlighter: + default: 'DefaultCodeRay' Index: config/locales/en.yml =================================================================== --- config/locales/en.yml (revision 2594) +++ config/locales/en.yml (working copy) @@ -666,6 +666,7 @@ label_ascending: Ascending label_descending: Descending label_date_from_to: From {{start}} to {{end}} + label_syntax_highlighter: Syntax highlighter button_login: Login button_submit: Submit Index: config/locales/fr.yml =================================================================== --- config/locales/fr.yml (revision 2594) +++ config/locales/fr.yml (working copy) @@ -698,6 +698,7 @@ label_ascending: Croissant label_descending: Décroissant label_date_from_to: Du {{start}} au {{end}} + label_syntax_highlighter: Colorateur syntaxique button_login: Connexion button_submit: Soumettre Index: lib/redmine/wiki_formatting/textile/formatter.rb =================================================================== --- lib/redmine/wiki_formatting/textile/formatter.rb (revision 2594) +++ lib/redmine/wiki_formatting/textile/formatter.rb (working copy) @@ -16,7 +16,6 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require 'redcloth3' -require 'coderay' module Redmine module WikiFormatting @@ -54,8 +53,7 @@ text.gsub!(//) do content = @pre_list[$1.to_i] if content.match(/\s?(.+)/m) - content = "" + - CodeRay.scan($2, $1.downcase).html(:escape => false, :line_numbers => :inline) + content = Redmine::SyntaxHighlighting.colorize($2, $1) end content end Index: lib/redmine/syntax_highlighting.rb =================================================================== --- lib/redmine/syntax_highlighting.rb (revision 0) +++ lib/redmine/syntax_highlighting.rb (revision 0) @@ -0,0 +1,85 @@ +# Redmine - project management software +# Copyright (C) 2006-2008 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. + +module Redmine + module SyntaxHighlighting + + class << self + # get highlighter class object + def highlighter + const_get(Setting.syntax_highlighter) + end + + # get all available highlighters + def available_highlighters + constants.sort - ["SyntaxAssets"] + end + + # used in the wiki to colorize text + def colorize(text, format="") + raise "#{highlighter} should implement 'colorize' method !" unless highlighter.respond_to?(:colorize) + highlighter.colorize(text, format) + end + + # used in attachments/repository to colorize files + def colorize_file(filename, content) + raise "#{highlighter} should implement 'colorize_file' method !" unless highlighter.respond_to?(:colorize_file) + highlighter.colorize_file(filename, content) + end + + end + + #hook to add chosen highlighter specific js and css tags in layout header + class SyntaxAssets < Redmine::Hook::ViewListener + def view_layouts_base_html_head(context) + res = [] + h = Redmine::SyntaxHighlighting.highlighter + # specific stylesheets ; just define a "stylesheets" method returning an array + h.stylesheets.each do |args| + res << stylesheet_link_tag(*args) + end if h.respond_to?(:stylesheets) + # specific javascripts ; just define a "javascripts" method returning an array + h.javascripts.each do |args| + res << javascript_include_tag(*args) + end if h.respond_to?(:javascripts) + res.join + end + end + Redmine::Hook.add_listener(SyntaxAssets) + + #default highlighter + module DefaultCodeRay + require 'coderay' + require 'coderay/helpers/file_type' + + class << self + def colorize(text, format) + "" + + CodeRay.scan(text, format.downcase).html(:escape => false, :line_numbers => :inline) + end + + def colorize_file(filename, content) + type = CodeRay::FileType[filename] + result = type ? CodeRay.scan(content, type).html : ERB::Util.h(content) + # to keep old behaviour (
 tags were in common/_file.rhtml template, disturbing eventual other highlighter)
+          result.split(/\n/).map{|l| "
#{l}
"}.join("\n") + end + end + end + + end +end \ No newline at end of file Index: lib/redmine.rb =================================================================== --- lib/redmine.rb (revision 2594) +++ lib/redmine.rb (working copy) @@ -7,6 +7,7 @@ require 'redmine/hook' require 'redmine/plugin' require 'redmine/wiki_formatting' +require 'redmine/syntax_highlighting' begin require_library_or_gem 'RMagick' unless Object.const_defined?(:Magick) Index: public/stylesheets/scm.css =================================================================== --- public/stylesheets/scm.css (revision 2594) +++ public/stylesheets/scm.css (working copy) @@ -45,6 +45,7 @@ color: inherit; } table.filecontent td.line-code pre { + margin: 0px; white-space: pre-wrap; /* CSS2.1 compliant */ white-space: -moz-pre-wrap; /* Mozilla-based browsers */ white-space: -o-pre-wrap; /* Opera 7+ */
<%= line_num %>
<%= line %>
<%= line_num %><%= line %>