Project

General

Profile

Feature #2985 » syntax_highlighting.diff

Jean-Baptiste Barth, 2009-03-16 17:38

View differences:

app/helpers/application_helper.rb (working copy)
15 15
# along with this program; if not, write to the Free Software
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18
require 'coderay'
19
require 'coderay/helpers/file_type'
20 18
require 'forwardable'
21 19
require 'cgi'
22 20

  
......
216 214
  end
217 215

  
218 216
  def syntax_highlight(name, content)
219
    type = CodeRay::FileType[name]
220
    type ? CodeRay.scan(content, type).html : h(content)
217
    Redmine::SyntaxHighlighting.colorize_file(name, content)
221 218
  end
222 219

  
223 220
  def to_path_param(path)
app/views/settings/_general.rhtml (working copy)
27 27
<p><label><%= l(:setting_text_formatting) %></label>
28 28
<%= 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) %></p>
29 29

  
30
<p><label><%= l(:label_syntax_highlighter) %></label>
31
<%= select_tag 'settings[syntax_highlighter]', options_for_select( (Redmine::SyntaxHighlighting.available_highlighters.collect{|h| [h, h]}), Setting.syntax_highlighter) %></p>
32

  
30 33
<p><label><%= l(:setting_wiki_compression) %></label>
31 34
<%= select_tag 'settings[wiki_compression]', options_for_select( [[l(:label_none), 0], ["gzip", "gzip"]], Setting.wiki_compression) %></p>
32 35

  
app/views/attachments/file.rhtml (working copy)
8 8

  
9 9
</div>
10 10
&nbsp;
11
<%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.filename} %>
11
<%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.disk_filename} %>
12 12

  
13 13
<% content_for :header_tags do -%>
14 14
    <%= stylesheet_link_tag "scm" -%>
app/views/common/_file.rhtml (working copy)
1 1
<div class="autoscroll">
2
<table class="filecontent CodeRay">
2
<table class="filecontent">
3 3
<tbody>
4 4
<% line_num = 1 %>
5 5
<% syntax_highlight(filename, to_utf8(content)).each_line do |line| %>
6
<tr><th class="line-num" id="L<%= line_num %>"><a href="#L<%= line_num %>"><%= line_num %></a></th><td class="line-code"><pre><%= line %></pre></td></tr>
6
<tr><th class="line-num" id="L<%= line_num %>"><a href="#L<%= line_num %>"><%= line_num %></a></th><td class="line-code"><%= line %></td></tr>
7 7
<% line_num += 1 %>
8 8
<% end %>
9 9
</tbody>
config/settings.yml (working copy)
150 150
  default: 0
151 151
openid:
152 152
  default: 0
153
syntax_highlighter:
154
  default: 'DefaultCodeRay'
config/locales/en.yml (working copy)
666 666
  label_ascending: Ascending
667 667
  label_descending: Descending
668 668
  label_date_from_to: From {{start}} to {{end}}
669
  label_syntax_highlighter: Syntax highlighter
669 670
  
670 671
  button_login: Login
671 672
  button_submit: Submit
config/locales/fr.yml (working copy)
698 698
  label_ascending: Croissant
699 699
  label_descending: Décroissant
700 700
  label_date_from_to: Du {{start}} au {{end}}
701
  label_syntax_highlighter: Colorateur syntaxique
701 702
  
702 703
  button_login: Connexion
703 704
  button_submit: Soumettre
lib/redmine/wiki_formatting/textile/formatter.rb (working copy)
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18 18
require 'redcloth3'
19
require 'coderay'
20 19

  
21 20
module Redmine
22 21
  module WikiFormatting
......
54 53
            text.gsub!(/<redpre#(\d+)>/) do
55 54
              content = @pre_list[$1.to_i]
56 55
              if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
57
                content = "<code class=\"#{$1} CodeRay\">" + 
58
                  CodeRay.scan($2, $1.downcase).html(:escape => false, :line_numbers => :inline)
56
                content = Redmine::SyntaxHighlighting.colorize($2, $1)
59 57
              end
60 58
              content
61 59
            end
lib/redmine/syntax_highlighting.rb (revision 0)
1
# Redmine - project management software
2
# Copyright (C) 2006-2008  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
module Redmine
19
  module SyntaxHighlighting
20
    
21
    class << self
22
      # get highlighter class object
23
      def highlighter
24
        const_get(Setting.syntax_highlighter)
25
      end
26

  
27
      # get all available highlighters
28
      def available_highlighters
29
        constants.sort - ["SyntaxAssets"]
30
      end
31
      
32
      # used in the wiki to colorize text
33
      def colorize(text, format="")
34
        raise "#{highlighter} should implement 'colorize' method !" unless highlighter.respond_to?(:colorize)
35
        highlighter.colorize(text, format)
36
      end
37
      
38
      # used in attachments/repository to colorize files
39
      def colorize_file(filename, content)
40
        raise "#{highlighter} should implement 'colorize_file' method !" unless highlighter.respond_to?(:colorize_file)
41
        highlighter.colorize_file(filename, content)
42
      end
43
      
44
    end
45

  
46
    #hook to add chosen highlighter specific js and css tags in layout header
47
    class SyntaxAssets < Redmine::Hook::ViewListener
48
      def view_layouts_base_html_head(context)
49
        res = []
50
        h = Redmine::SyntaxHighlighting.highlighter
51
        # specific stylesheets ; just define a "stylesheets" method returning an array
52
        h.stylesheets.each do |args|
53
          res << stylesheet_link_tag(*args)
54
        end if h.respond_to?(:stylesheets)
55
        # specific javascripts ; just define a "javascripts" method returning an array
56
        h.javascripts.each do |args|
57
          res << javascript_include_tag(*args)
58
        end if h.respond_to?(:javascripts)
59
        res.join
60
      end
61
    end
62
    Redmine::Hook.add_listener(SyntaxAssets)
63
    
64
    #default highlighter
65
    module DefaultCodeRay
66
      require 'coderay'
67
      require 'coderay/helpers/file_type'
68
      
69
      class << self
70
        def colorize(text, format)
71
          "<code class=\"#{format} CodeRay\">" + 
72
            CodeRay.scan(text, format.downcase).html(:escape => false, :line_numbers => :inline)
73
        end
74
        
75
        def colorize_file(filename, content)
76
          type = CodeRay::FileType[filename]
77
          result = type ? CodeRay.scan(content, type).html : ERB::Util.h(content)
78
          # to keep old behaviour (<pre> tags were in common/_file.rhtml template, disturbing eventual other highlighter)
79
          result.split(/\n/).map{|l| "<pre>#{l}</pre>"}.join("\n")
80
        end
81
      end
82
    end
83
    
84
  end
85
end
lib/redmine.rb (working copy)
7 7
require 'redmine/hook'
8 8
require 'redmine/plugin'
9 9
require 'redmine/wiki_formatting'
10
require 'redmine/syntax_highlighting'
10 11

  
11 12
begin
12 13
  require_library_or_gem 'RMagick' unless Object.const_defined?(:Magick)
public/stylesheets/scm.css (working copy)
45 45
	color: inherit;
46 46
}
47 47
table.filecontent td.line-code pre {
48
    margin: 0px;
48 49
    white-space: pre-wrap; /* CSS2.1 compliant */
49 50
    white-space: -moz-pre-wrap; /* Mozilla-based browsers */
50 51
    white-space: -o-pre-wrap; /* Opera 7+ */
(1-1/4)