Class: Mato::HtmlFilters::SyntaxHighlight

Inherits:
Object
  • Object
show all
Defined in:
lib/mato/html_filters/syntax_highlight.rb

Instance Method Summary collapse

Instance Method Details

#call(doc) ⇒ Object

Parameters:

  • doc (Nokogiri::HTML::DocumentFragment)


10
11
12
13
14
15
16
# File 'lib/mato/html_filters/syntax_highlight.rb', line 10

def call(doc)
  doc.search("pre").each do |pre|
    if pre.at('code')
      pre.replace(highlight(pre))
    end
  end
end

#guess_lexer(language, filename, source) ⇒ Rouge::Lexer

Parameters:

  • language (String)
  • filename (String)
  • source (String)

Returns:

  • (Rouge::Lexer)


22
23
24
25
26
27
28
# File 'lib/mato/html_filters/syntax_highlight.rb', line 22

def guess_lexer(language, filename, source)
  Rouge::Lexer.find(language)&.tap do |lexer|
    return lexer.new
  end

  Rouge::Lexer.guess(filename: filename, source: source, &:first).new
end

#parse_label(class_name) ⇒ Object

Parameters:

  • CSS (String, nil)

    class names, e.g. “foo.js” “ruby:foo.rb”



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mato/html_filters/syntax_highlight.rb', line 31

def parse_label(class_name)
  a = class_name&.split(/:/) || []
  if a.empty?
    {}
  elsif a.size == 1
    token = a[0].sub(/^language-/, '')
    if Rouge::Lexer.find(token)
      { language: token }
    else
      { filename: token }
    end
  else
    { language: a[0], filename: a[1] }
  end
end