Class: HTML::Pipeline::RougeFilter
- Inherits:
-
Filter
- Object
- Filter
- HTML::Pipeline::RougeFilter
- Defined in:
- lib/html/pipeline/rouge_filter.rb
Instance Method Summary collapse
- #call ⇒ Object
- #default_css_class ⇒ Object
- #formatter(css_class: default_css_class) ⇒ Object
- #highlight_with(lexer, text) ⇒ Object
- #lexer_for(lang) ⇒ Object
- #line_numbers ⇒ Object
- #replace_br ⇒ Object
Instance Method Details
#call ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/html/pipeline/rouge_filter.rb', line 11 def call doc.search("pre").each do |node| default = must_str(context[:highlight]) next unless lang = node["lang"] || default next unless lexer = lexer_for(lang) node.css("br").each { |br| br.replace("\n") } if replace_br text = node.inner_text html = highlight_with(lexer, text) next if html.nil? if (node = node.replace(html).first) klass = node["class"] klass = [klass, "#{default_css_class}-#{lang}"].compact.join(" ") node["class"] = klass end end doc end |
#default_css_class ⇒ Object
35 36 37 |
# File 'lib/html/pipeline/rouge_filter.rb', line 35 def default_css_class must_str(context[:css_class]) || "highlight" end |
#formatter(css_class: default_css_class) ⇒ Object
47 48 49 50 |
# File 'lib/html/pipeline/rouge_filter.rb', line 47 def formatter(css_class: default_css_class) Rouge::Formatters::HTML.new(css_class: css_class, line_numbers: line_numbers) end |
#highlight_with(lexer, text) ⇒ Object
31 32 33 |
# File 'lib/html/pipeline/rouge_filter.rb', line 31 def highlight_with(lexer, text) formatter.format(lexer.lex(text)) end |
#lexer_for(lang) ⇒ Object
52 53 54 |
# File 'lib/html/pipeline/rouge_filter.rb', line 52 def lexer_for(lang) Rouge::Lexer.find_fancy(lang) || Rouge::Lexers::PlainText end |
#line_numbers ⇒ Object
39 40 41 |
# File 'lib/html/pipeline/rouge_filter.rb', line 39 def line_numbers context[:line_numbers] || false end |
#replace_br ⇒ Object
43 44 45 |
# File 'lib/html/pipeline/rouge_filter.rb', line 43 def replace_br context[:replace_br] || false end |