| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2016-03-13 10:30:10 +00:00
										 |  |  | # Copyright (C) 2006-2016  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  | # 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 | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |     class << self | 
					
						
							|  |  |  |       attr_reader :highlighter | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |       def highlighter=(name) | 
					
						
							|  |  |  |         if name.is_a?(Module) | 
					
						
							|  |  |  |           @highlighter = name | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           @highlighter = const_get(name) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-12-23 12:39:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def highlight_by_filename(text, filename) | 
					
						
							|  |  |  |         highlighter.highlight_by_filename(text, filename) | 
					
						
							|  |  |  |       rescue | 
					
						
							|  |  |  |         ERB::Util.h(text) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def highlight_by_language(text, language) | 
					
						
							|  |  |  |         highlighter.highlight_by_language(text, language) | 
					
						
							|  |  |  |       rescue | 
					
						
							|  |  |  |         ERB::Util.h(text) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-04-06 16:37:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def language_supported?(language) | 
					
						
							|  |  |  |         if highlighter.respond_to? :language_supported? | 
					
						
							|  |  |  |           highlighter.language_supported? language | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           true | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       rescue | 
					
						
							|  |  |  |         false | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |     module CodeRay | 
					
						
							|  |  |  |       require 'coderay' | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |       class << self | 
					
						
							|  |  |  |         # Highlights +text+ as the content of +filename+ | 
					
						
							|  |  |  |         # Should not return line numbers nor outer pre tag | 
					
						
							|  |  |  |         def highlight_by_filename(text, filename) | 
					
						
							|  |  |  |           language = ::CodeRay::FileType[filename] | 
					
						
							| 
									
										
										
										
											2012-04-12 17:54:55 +00:00
										 |  |  |           language ? ::CodeRay.scan(text, language).html(:break_lines => true) : ERB::Util.h(text) | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |         # Highlights +text+ using +language+ syntax | 
					
						
							|  |  |  |         # Should not return outer pre tag | 
					
						
							|  |  |  |         def highlight_by_language(text, language) | 
					
						
							| 
									
										
										
										
											2012-07-29 14:52:57 +00:00
										 |  |  |           ::CodeRay.scan(text, language).html(:wrap => :span) | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-04-06 16:37:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def language_supported?(language) | 
					
						
							|  |  |  |           ::CodeRay::Scanners.list.include?(language.to_s.downcase.to_sym) | 
					
						
							|  |  |  |         rescue | 
					
						
							|  |  |  |           false | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:44:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-14 12:57:08 +00:00
										 |  |  |   SyntaxHighlighting.highlighter = 'CodeRay' | 
					
						
							|  |  |  | end |