Class: HTMLPipeline::HighlightFilter

Inherits:
NodeFilter
  • Object
show all
Defined in:
lib/html/pipeline/highlight/filter.rb

Constant Summary collapse

DEFAULT_IGNORED_ANCESTOR_TAGS =
%w[pre code a style script].freeze
DEFAULT_CLASS_NAME =
'highlight'

Instance Method Summary collapse

Instance Method Details

#apply_filter(content) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/html/pipeline/highlight/filter.rb', line 16

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

#handle_text_chunk(text) ⇒ Object



12
13
14
# File 'lib/html/pipeline/highlight/filter.rb', line 12

def handle_text_chunk(text)
  text.replace(apply_filter(text.to_s), as: :html)
end

#selectorObject



8
9
10
# File 'lib/html/pipeline/highlight/filter.rb', line 8

def selector
  Selma::Selector.new(match_text_within: "*", ignore_text_within: DEFAULT_IGNORED_ANCESTOR_TAGS)
end

#validateObject



26
27
28
# File 'lib/html/pipeline/highlight/filter.rb', line 26

def validate
  needs(:highlight_pattern)
end