Patch #43643 » 0002-Use-syntax_highlight_scrubber-in-textile.patch
| lib/redmine/wiki_formatting/common_mark/syntax_highlight_scrubber.rb | ||
|---|---|---|
| 23 | 23 |
# Redmine Syntax highlighting for <pre><code class="language-foo"> |
| 24 | 24 |
# blocks as generated by commonmarker |
| 25 | 25 |
class SyntaxHighlightScrubber < Loofah::Scrubber |
| 26 |
include Redmine::WikiFormatting::SyntaxHighlight |
|
| 27 | ||
| 26 | 28 |
def scrub(node) |
| 27 | 29 |
# Equivalent to the CSS selector "pre > code". Implemented for performance |
| 28 | 30 |
return unless node.name == 'code' && node.parent.name == 'pre' |
| 29 | 31 | |
| 30 |
return unless lang = node["class"].presence
|
|
| 32 |
return unless lang = node['class'].presence
|
|
| 31 | 33 |
return unless lang =~ /\Alanguage-(\S+)\z/ |
| 32 | 34 | |
| 33 | 35 |
lang = $1 |
| 34 | 36 |
text = node.inner_text |
| 35 | ||
| 36 |
# original language for extension development |
|
| 37 |
node["data-language"] = lang unless node["data-language"] |
|
| 38 | ||
| 39 |
if Redmine::SyntaxHighlighting.language_supported?(lang) |
|
| 40 |
html = Redmine::SyntaxHighlighting.highlight_by_language(text, lang) |
|
| 41 |
return if html.nil? |
|
| 42 | ||
| 43 |
node.inner_html = html |
|
| 44 |
node["class"] = "#{lang} syntaxhl"
|
|
| 45 |
else |
|
| 46 |
# unsupported language, remove the class attribute |
|
| 47 |
node.remove_attribute("class")
|
|
| 48 |
end |
|
| 37 |
process node, text, lang |
|
| 49 | 38 |
end |
| 50 | 39 |
end |
| 51 | 40 |
end |
| lib/redmine/wiki_formatting/syntax_highlight.rb | ||
|---|---|---|
| 1 |
# frozen_string_literal: true |
|
| 2 | ||
| 3 |
# Redmine - project management software |
|
| 4 |
# Copyright (C) 2006- Jean-Philippe Lang |
|
| 5 |
# |
|
| 6 |
# This program is free software; you can redistribute it and/or |
|
| 7 |
# modify it under the terms of the GNU General Public License |
|
| 8 |
# as published by the Free Software Foundation; either version 2 |
|
| 9 |
# of the License, or (at your option) any later version. |
|
| 10 |
# |
|
| 11 |
# This program is distributed in the hope that it will be useful, |
|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
# GNU General Public License for more details. |
|
| 15 |
# |
|
| 16 |
# You should have received a copy of the GNU General Public License |
|
| 17 |
# along with this program; if not, write to the Free Software |
|
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 19 | ||
| 20 |
module Redmine |
|
| 21 |
module WikiFormatting |
|
| 22 |
module SyntaxHighlight |
|
| 23 |
def process(node, text, lang) |
|
| 24 |
# original language for extension development |
|
| 25 |
node['data-language'] = lang unless node['data-language'] |
|
| 26 | ||
| 27 |
if Redmine::SyntaxHighlighting.language_supported?(lang) |
|
| 28 |
html = Redmine::SyntaxHighlighting.highlight_by_language(text, lang) |
|
| 29 |
return if html.nil? |
|
| 30 | ||
| 31 |
node.inner_html = html |
|
| 32 |
node['class'] = "#{lang} syntaxhl"
|
|
| 33 |
else |
|
| 34 |
# unsupported language, remove the class attribute |
|
| 35 |
node.remove_attribute('class')
|
|
| 36 |
end |
|
| 37 |
end |
|
| 38 |
end |
|
| 39 |
end |
|
| 40 |
end |
|
| lib/redmine/wiki_formatting/textile/formatter.rb | ||
|---|---|---|
| 21 | 21 |
module WikiFormatting |
| 22 | 22 |
module Textile |
| 23 | 23 |
SCRUBBERS = [ |
| 24 |
SyntaxHighlightScrubber.new, |
|
| 24 | 25 |
Redmine::WikiFormatting::TablesortScrubber.new |
| 25 | 26 |
] |
| 26 | 27 | |
| ... | ... | |
| 109 | 110 |
end |
| 110 | 111 |
end |
| 111 | 112 |
sections = [before.strip, s.strip, after.strip] |
| 112 |
sections.each {|section| smooth_offtags_without_code_highlighting section}
|
|
| 113 |
sections.each {|section| smooth_offtags section}
|
|
| 113 | 114 |
sections |
| 114 | 115 |
end |
| 115 | 116 | |
| ... | ... | |
| 120 | 121 |
def hard_break(text) |
| 121 | 122 |
text.gsub!(/(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />") if hard_breaks
|
| 122 | 123 |
end |
| 123 | ||
| 124 |
alias :smooth_offtags_without_code_highlighting :smooth_offtags |
|
| 125 |
# Patch to add code highlighting support to RedCloth |
|
| 126 |
def smooth_offtags(text) |
|
| 127 |
unless @pre_list.empty? |
|
| 128 |
## replace <pre> content |
|
| 129 |
text.gsub!(/<redpre#(\d+)>/) do |
|
| 130 |
content = @pre_list[$1.to_i] |
|
| 131 |
# This regex must match any data produced by RedCloth3#rip_offtags |
|
| 132 |
if content =~ /<code\s+class=(?:"([^"]+)"|'([^']+)')>\s?(.*)/m |
|
| 133 |
language = $1 || $2 |
|
| 134 |
text = $3 |
|
| 135 |
# original language for extension development |
|
| 136 |
langattr = " data-language=\"#{CGI.escapeHTML language}\"" if language.present?
|
|
| 137 |
if Redmine::SyntaxHighlighting.language_supported?(language) |
|
| 138 |
text.gsub!("x%x%", '&')
|
|
| 139 |
content = "<code class=\"#{CGI.escapeHTML language} syntaxhl\"#{langattr}>" +
|
|
| 140 |
Redmine::SyntaxHighlighting.highlight_by_language(text, language) |
|
| 141 |
else |
|
| 142 |
content = "<code#{langattr}>#{ERB::Util.h(text)}"
|
|
| 143 |
end |
|
| 144 |
end |
|
| 145 |
content |
|
| 146 |
end |
|
| 147 |
end |
|
| 148 |
end |
|
| 149 | 124 |
end |
| 150 | 125 |
end |
| 151 | 126 |
end |
| lib/redmine/wiki_formatting/textile/syntax_highlight_scrubber.rb | ||
|---|---|---|
| 1 |
# frozen_string_literal: true |
|
| 2 | ||
| 3 |
# Redmine - project management software |
|
| 4 |
# Copyright (C) 2006- Jean-Philippe Lang |
|
| 5 |
# |
|
| 6 |
# This program is free software; you can redistribute it and/or |
|
| 7 |
# modify it under the terms of the GNU General Public License |
|
| 8 |
# as published by the Free Software Foundation; either version 2 |
|
| 9 |
# of the License, or (at your option) any later version. |
|
| 10 |
# |
|
| 11 |
# This program is distributed in the hope that it will be useful, |
|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
# GNU General Public License for more details. |
|
| 15 |
# |
|
| 16 |
# You should have received a copy of the GNU General Public License |
|
| 17 |
# along with this program; if not, write to the Free Software |
|
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 19 | ||
| 20 |
module Redmine |
|
| 21 |
module WikiFormatting |
|
| 22 |
module Textile |
|
| 23 |
# Redmine Syntax highlighting for <pre><code class="foo"> |
|
| 24 |
class SyntaxHighlightScrubber < Loofah::Scrubber |
|
| 25 |
include Redmine::WikiFormatting::SyntaxHighlight |
|
| 26 | ||
| 27 |
def scrub(node) |
|
| 28 |
return unless node.name == 'code' |
|
| 29 |
return unless lang = node['class'].presence |
|
| 30 | ||
| 31 |
text = node.inner_text |
|
| 32 |
if text.start_with?("\n")
|
|
| 33 |
text = text.sub("\n", "")
|
|
| 34 |
end |
|
| 35 | ||
| 36 |
process node, text, lang |
|
| 37 |
end |
|
| 38 |
end |
|
| 39 |
end |
|
| 40 |
end |
|
| 41 |
end |
|