Class: HTML::Pipeline::ImageLinkFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/html/pipeline/image_link/filter.rb

Constant Summary collapse

IGNORE_PARENTS =
%w[pre code a style script].to_set

Instance Method Summary collapse

Instance Method Details

#apply_filter(content) ⇒ Object



21
22
23
24
25
26
# File 'lib/html/pipeline/image_link/filter.rb', line 21

def apply_filter(content)
  protocols = https_only? ? %w[https] : %w[https http]
  content.gsub(/#{Regexp.union(protocols)}:\/\/.+\.#{Regexp.union(image_extensions)}(\?\S+)?/i) do |match|
    %(<a href="#{match}"><img src="#{match}" alt=""></a>)
  end
end

#callObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/html/pipeline/image_link/filter.rb', line 8

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