Class: Html2rss::Selectors::PostProcessors::HtmlTransformers::WrapImgInA

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/selectors/post_processors/html_transformers/wrap_img_in_a.rb

Overview

Transformer that wraps <img> tags into <a> tags linking to ‘img.src`.

Instance Method Summary collapse

Instance Method Details

#already_wrapped?(node) ⇒ Boolean

Returns whether the image is already wrapped in a link.

Parameters:

  • node (Nokogiri::XML::Node)

    node currently being transformed

Returns:

  • (Boolean)

    whether the image is already wrapped in a link



32
33
34
# File 'lib/html2rss/selectors/post_processors/html_transformers/wrap_img_in_a.rb', line 32

def already_wrapped?(node)
  node.parent.name == 'a'
end

#call(node_name:, node:, **_env) ⇒ nil

Wraps <img> tags into <a> tags that link to ‘img.src`.

Parameters:

  • node_name (String)
  • node (Nokogiri::XML::Node)
  • _env (Hash)

    transformer context

Options Hash (**_env):

  • :_reserved (Object)

    reserved for transformer pipeline context

Returns:

  • (nil)


18
19
20
21
22
# File 'lib/html2rss/selectors/post_processors/html_transformers/wrap_img_in_a.rb', line 18

def call(node_name:, node:, **_env)
  return unless should_process?(node_name)

  wrap_image_in_anchor(node) unless already_wrapped?(node)
end

#should_process?(node_name) ⇒ Boolean

Returns whether this transformer should run for the node.

Parameters:

  • node_name (String)

    node name currently being transformed

Returns:

  • (Boolean)

    whether this transformer should run for the node



26
27
28
# File 'lib/html2rss/selectors/post_processors/html_transformers/wrap_img_in_a.rb', line 26

def should_process?(node_name)
  node_name == 'img'
end