您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

syntax_highlighting.rb 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. module Redmine
  18. module SyntaxHighlighting
  19. class << self
  20. attr_reader :highlighter
  21. def highlighter=(name)
  22. if name.is_a?(Module)
  23. @highlighter = name
  24. else
  25. @highlighter = const_get(name)
  26. end
  27. end
  28. def highlight_by_filename(text, filename)
  29. highlighter.highlight_by_filename(text, filename)
  30. rescue
  31. ERB::Util.h(text)
  32. end
  33. def highlight_by_language(text, language)
  34. highlighter.highlight_by_language(text, language)
  35. rescue
  36. ERB::Util.h(text)
  37. end
  38. def language_supported?(language)
  39. if highlighter.respond_to? :language_supported?
  40. highlighter.language_supported? language
  41. else
  42. true
  43. end
  44. rescue
  45. false
  46. end
  47. end
  48. module Rouge
  49. require 'rouge'
  50. # Customized formatter based on Rouge::Formatters::HTMLLinewise
  51. # Syntax highlighting is completed within each line.
  52. class CustomHTMLLinewise < ::Rouge::Formatter
  53. def initialize(formatter)
  54. @formatter = formatter
  55. end
  56. def stream(tokens, &b)
  57. token_lines(tokens) do |line|
  58. line.each do |tok, val|
  59. yield @formatter.span(tok, val)
  60. end
  61. yield "\n"
  62. end
  63. end
  64. end
  65. class << self
  66. # Highlights +text+ as the content of +filename+
  67. # Should not return line numbers nor outer pre tag
  68. def highlight_by_filename(text, filename)
  69. lexer =::Rouge::Lexer.guess_by_filename(filename)
  70. formatter = ::Rouge::Formatters::HTML.new
  71. ::Rouge.highlight(text, lexer, CustomHTMLLinewise.new(formatter))
  72. end
  73. # Highlights +text+ using +language+ syntax
  74. # Should not return outer pre tag
  75. def highlight_by_language(text, language)
  76. lexer =
  77. find_lexer(language.to_s.downcase) || ::Rouge::Lexers::PlainText
  78. ::Rouge.highlight(text, lexer, ::Rouge::Formatters::HTML)
  79. end
  80. def language_supported?(language)
  81. find_lexer(language.to_s.downcase) ? true : false
  82. end
  83. private
  84. # Alias names used by CodeRay and not supported by Rouge
  85. LANG_ALIASES = {
  86. 'delphi' => 'pascal',
  87. 'cplusplus' => 'cpp',
  88. 'ecmascript' => 'javascript',
  89. 'ecma_script' => 'javascript',
  90. 'java_script' => 'javascript',
  91. 'xhtml' => 'html'
  92. }
  93. def find_lexer(language)
  94. ::Rouge::Lexer.find(language) ||
  95. ::Rouge::Lexer.find(LANG_ALIASES[language])
  96. end
  97. end
  98. end
  99. end
  100. SyntaxHighlighting.highlighter = 'Rouge'
  101. end