@@ -1,55 +1,66 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
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 | 18 | module Redmine |
|
19 | 19 | module SyntaxHighlighting |
|
20 | 20 | |
|
21 | 21 | class << self |
|
22 | 22 | attr_reader :highlighter |
|
23 | delegate :highlight_by_filename, :highlight_by_language, :to => :highlighter | |
|
24 | 23 | |
|
25 | 24 | def highlighter=(name) |
|
26 | 25 | if name.is_a?(Module) |
|
27 | 26 | @highlighter = name |
|
28 | 27 | else |
|
29 | 28 | @highlighter = const_get(name) |
|
30 | 29 | end |
|
31 | 30 | end |
|
31 | ||
|
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 | |
|
32 | 43 | end |
|
33 | 44 | |
|
34 | 45 | module CodeRay |
|
35 | 46 | require 'coderay' |
|
36 | 47 | |
|
37 | 48 | class << self |
|
38 | 49 | # Highlights +text+ as the content of +filename+ |
|
39 | 50 | # Should not return line numbers nor outer pre tag |
|
40 | 51 | def highlight_by_filename(text, filename) |
|
41 | 52 | language = ::CodeRay::FileType[filename] |
|
42 | 53 | language ? ::CodeRay.scan(text, language).html(:break_lines => true) : ERB::Util.h(text) |
|
43 | 54 | end |
|
44 | 55 | |
|
45 | 56 | # Highlights +text+ using +language+ syntax |
|
46 | 57 | # Should not return outer pre tag |
|
47 | 58 | def highlight_by_language(text, language) |
|
48 | 59 | ::CodeRay.scan(text, language).html(:wrap => :span) |
|
49 | 60 | end |
|
50 | 61 | end |
|
51 | 62 | end |
|
52 | 63 | end |
|
53 | 64 | |
|
54 | 65 | SyntaxHighlighting.highlighter = 'CodeRay' |
|
55 | 66 | end |
General Comments 0
You need to be logged in to leave comments.
Login now