Class: Mato::HtmlFilters::SyntaxHighlight
- Inherits:
-
Object
- Object
- Mato::HtmlFilters::SyntaxHighlight
- Defined in:
- lib/mato/html_filters/syntax_highlight.rb
Instance Method Summary collapse
- #call(doc) ⇒ Object
- #guess_lexer(language, filename, source) ⇒ Rouge::Lexer
- #parse_label(class_name) ⇒ Object
Instance Method Details
#call(doc) ⇒ Object
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
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
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 |