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