To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / syntax_highlighting.rb @ 1568:bc47b68a9487

History | View | Annotate | Download (2.01 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1494:e248c7af89ec Chris
# Copyright (C) 2006-2014  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
21 0:513646585e45 Chris
    class << self
22
      attr_reader :highlighter
23 909:cbb26bc654de Chris
24 0:513646585e45 Chris
      def highlighter=(name)
25
        if name.is_a?(Module)
26
          @highlighter = name
27
        else
28
          @highlighter = const_get(name)
29
        end
30
      end
31 1517:dffacf8a6908 Chris
32
      def highlight_by_filename(text, filename)
33
        highlighter.highlight_by_filename(text, filename)
34
      rescue
35
        ERB::Util.h(text)
36
      end
37
38
      def highlight_by_language(text, language)
39
        highlighter.highlight_by_language(text, language)
40
      rescue
41
        ERB::Util.h(text)
42
      end
43 0:513646585e45 Chris
    end
44 909:cbb26bc654de Chris
45 0:513646585e45 Chris
    module CodeRay
46
      require 'coderay'
47 909:cbb26bc654de Chris
48 0:513646585e45 Chris
      class << self
49
        # Highlights +text+ as the content of +filename+
50
        # Should not return line numbers nor outer pre tag
51
        def highlight_by_filename(text, filename)
52
          language = ::CodeRay::FileType[filename]
53 1115:433d4f72a19b Chris
          language ? ::CodeRay.scan(text, language).html(:break_lines => true) : ERB::Util.h(text)
54 0:513646585e45 Chris
        end
55 909:cbb26bc654de Chris
56 0:513646585e45 Chris
        # Highlights +text+ using +language+ syntax
57
        # Should not return outer pre tag
58
        def highlight_by_language(text, language)
59 1115:433d4f72a19b Chris
          ::CodeRay.scan(text, language).html(:wrap => :span)
60 0:513646585e45 Chris
        end
61
      end
62
    end
63
  end
64 909:cbb26bc654de Chris
65 0:513646585e45 Chris
  SyntaxHighlighting.highlighter = 'CodeRay'
66
end