Sha256: 00330575db57351b451c2a2cba8194c2fdb360970da08a1689398045a73fa762

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Html2rss
  module AttributePostProcessors
    module HtmlTransformers
      ##
      # Transformer that wraps <img> tags into <a> tags linking to `img.src`.
      class WrapImgInA
        ##
        # Wraps <img> tags into <a> tags that link to `img.src`.
        #
        # @param node_name [String]
        # @param node [Nokogiri::XML::Node]
        # @return [nil]
        def call(node_name:, node:, **_env)
          return unless already_wrapped?(node_name, node)

          wrap_image_in_anchor(node)
        end

        def already_wrapped?(node_name, node)
          node_name == 'img' && node.parent.name != 'a'
        end

        private

        ##
        # Wraps the <img> node in an <a> tag.
        #
        # @param node [Nokogiri::XML::Node]
        # @return [nil]
        def wrap_image_in_anchor(node)
          anchor = Nokogiri::XML::Node.new('a', node.document)
          anchor['href'] = node['src']
          node.add_next_sibling(anchor)
          anchor.add_child(node.remove)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
html2rss-0.15.0 lib/html2rss/attribute_post_processors/html_transformers/wrap_img_in_a.rb
html2rss-0.14.0 lib/html2rss/attribute_post_processors/html_transformers/wrap_img_in_a.rb
html2rss-0.13.0 lib/html2rss/attribute_post_processors/html_transformers/wrap_img_in_a.rb
html2rss-0.12.0 lib/html2rss/attribute_post_processors/html_transformers/wrap_img_in_a.rb
html2rss-0.11.0 lib/html2rss/attribute_post_processors/html_transformers/wrap_img_in_a.rb
html2rss-0.10.0 lib/html2rss/attribute_post_processors/html_transformers/wrap_img_in_a.rb