mirror of
https://github.com/meineerde/redmine.git
synced 2026-01-31 19:47:14 +00:00
Add "data-language" attribute to code block with the user-supplied language for CommonMark formater (#35104, #32424).
Patch by Martin Cizek. git-svn-id: http://svn.redmine.org/redmine/trunk@21182 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
12b2dd3098
commit
5699253da6
@ -48,7 +48,7 @@ module Redmine
|
||||
return unless node.name == "code"
|
||||
return unless node.has_attribute?("class")
|
||||
|
||||
unless /\Alanguage-(\w+)\z/.match?(node["class"])
|
||||
unless /\Alanguage-(\S+)\z/.match?(node["class"])
|
||||
node.remove_attribute("class")
|
||||
end
|
||||
}
|
||||
|
||||
@ -26,11 +26,14 @@ module Redmine
|
||||
def call
|
||||
doc.search("pre > code").each do |node|
|
||||
next unless lang = node["class"].presence
|
||||
next unless lang =~ /\Alanguage-(\w+)\z/
|
||||
next unless lang =~ /\Alanguage-(\S+)\z/
|
||||
|
||||
lang = $1
|
||||
text = node.inner_text
|
||||
|
||||
# original language for extension development
|
||||
node["data-language"] = lang unless node["data-language"]
|
||||
|
||||
if Redmine::SyntaxHighlighting.language_supported?(lang)
|
||||
html = Redmine::SyntaxHighlighting.highlight_by_language(text, lang)
|
||||
next if html.nil?
|
||||
|
||||
@ -94,24 +94,31 @@ class Redmine::WikiFormatting::CommonMark::FormatterTest < ActionView::TestCase
|
||||
end
|
||||
|
||||
def test_should_support_syntax_highlight
|
||||
text = <<-STR
|
||||
~~~ruby
|
||||
def foo
|
||||
end
|
||||
~~~
|
||||
text = <<~STR
|
||||
~~~ruby
|
||||
def foo
|
||||
end
|
||||
~~~
|
||||
STR
|
||||
assert_select_in format(text), 'pre code.ruby.syntaxhl' do
|
||||
assert_select 'span.k', :text => 'def'
|
||||
assert_select "[data-language='ruby']"
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_not_allow_invalid_language_for_code_blocks
|
||||
text = <<-STR
|
||||
~~~foo
|
||||
test
|
||||
~~~
|
||||
def test_should_support_syntax_highlight_for_language_with_special_chars
|
||||
text = <<~STR
|
||||
~~~c++
|
||||
int main() {
|
||||
}
|
||||
~~~
|
||||
STR
|
||||
assert_equal "<pre>test\n</pre>", format(text)
|
||||
|
||||
assert_select_in format(text), 'pre' do
|
||||
assert_select 'code[class=?]', "c++ syntaxhl"
|
||||
assert_select 'span.kt', :text => 'int'
|
||||
assert_select "[data-language=?]", "c++"
|
||||
end
|
||||
end
|
||||
|
||||
def test_external_links_should_have_external_css_class
|
||||
|
||||
@ -38,7 +38,7 @@ if Object.const_defined?(:CommonMarker)
|
||||
</code></pre>
|
||||
HTML
|
||||
expected = <<~HTML
|
||||
<pre><code class="ruby syntaxhl">
|
||||
<pre><code class="ruby syntaxhl" data-language="ruby">
|
||||
<span class="k">def</span> <span class="nf">foo</span>
|
||||
<span class="k">end</span>
|
||||
</code></pre>
|
||||
@ -46,7 +46,21 @@ if Object.const_defined?(:CommonMarker)
|
||||
assert_equal expected, filter(input)
|
||||
end
|
||||
|
||||
def test_should_strip_code_for_unknown_lang
|
||||
def test_should_highlight_supported_language_with_special_chars
|
||||
input = <<~HTML
|
||||
<pre><code class="language-c-k&r">
|
||||
int i;
|
||||
</code></pre>
|
||||
HTML
|
||||
expected = <<~HTML
|
||||
<pre><code data-language="c-k&r">
|
||||
int i;
|
||||
</code></pre>
|
||||
HTML
|
||||
assert_equal expected, filter(input)
|
||||
end
|
||||
|
||||
def test_should_strip_code_class_and_preserve_data_language_attr_for_unknown_language
|
||||
input = <<~HTML
|
||||
<pre><code class="language-foobar">
|
||||
def foo
|
||||
@ -54,10 +68,10 @@ if Object.const_defined?(:CommonMarker)
|
||||
</code></pre>
|
||||
HTML
|
||||
expected = <<~HTML
|
||||
<pre>
|
||||
<pre><code data-language="foobar">
|
||||
def foo
|
||||
end
|
||||
</pre>
|
||||
</code></pre>
|
||||
HTML
|
||||
assert_equal expected, filter(input)
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user