class HTML::Pipeline::HighlightFilter

Constants

DEFAULT_CLASS_NAME
IGNORE_PARENTS

Public Instance Methods

apply_filter(content) click to toggle source
# File lib/html/pipeline/highlight/filter.rb, line 22
def apply_filter(content)
  content.gsub(@context[:highlight_pattern]) do |text|
    if converter
      converter.call(Regexp.last_match)
    else
      %(<span class="#{class_name}">#{ERB::Util.html_escape(text)}</span>)
    end
  end
end
call() click to toggle source
# File lib/html/pipeline/highlight/filter.rb, line 9
def call
  doc.xpath('.//text()').each do |node|
    next if has_ancestor?(node, IGNORE_PARENTS)

    content = node.to_html
    html = apply_filter(content)
    next if html == content

    node.replace(html)
  end
  doc
end
validate() click to toggle source
# File lib/html/pipeline/highlight/filter.rb, line 32
def validate
  needs(:highlight_pattern)
end

Private Instance Methods

class_name() click to toggle source
# File lib/html/pipeline/highlight/filter.rb, line 38
def class_name
  @context[:highlight_class_name] || DEFAULT_CLASS_NAME
end
converter() click to toggle source
# File lib/html/pipeline/highlight/filter.rb, line 42
def converter
  @context[:highlight_converter]
end