Class: Redmine::WikiFormatting::CommonMark::SyntaxHighlightFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb

Overview

Redmine Syntax highlighting for <pre><code class=“language-foo”> blocks as generated by commonmarker

Instance Method Summary collapse

Instance Method Details

#callObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/redmine/wiki_formatting/common_mark/syntax_highlight_filter.rb', line 26

def call
  doc.search("pre > code").each do |node|
    next unless lang = node["class"].presence
    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?

      node.inner_html = html
      node["class"] = "#{lang} syntaxhl"
    else
      # unsupported language, remove the class attribute
      node.remove_attribute("class")
    end
  end
  doc
end