Sha256: 9adb94a17bd5440d7c0450d14b2452f3bf80312d8cc86000e27b69a3328a21f1

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

module Wovnrb
  class ImageReplacer < ReplacerBase
    def initialize(url, text_index, src_index, img_src_prefix)
      @url = url
      @text_index = text_index
      @src_index = src_index
      @img_src_prefix = img_src_prefix
    end

    def replace(dom, lang)
      dom.xpath('//img').each do |node|
        next if wovn_ignore?(node)

        # use regular expressions to support case insensitivity (right?)
        if node.to_html =~ /src=['"][^'"]*['"]/i
          src = node.to_html.match(/src=['"]([^'"]*)['"]/i)[1]
          # THIS SRC CORRECTION DOES NOT HANDLE ONE IMPORTANT CASE
          # 1) "../path/with/ellipse"
          # if this is not an absolute src
          if src !~ /:\/\//
            # if this is a path with a leading slash
            if src =~ /^\//
              src = "#{@url[:protocol]}://#{@url[:host]}#{src}"
            else
              src = "#{@url[:protocol]}://#{@url[:host]}#{@url[:path]}#{src}"
            end
          end

          # shouldn't need size check, but for now...
          if @src_index[src] && @src_index[src][lang.lang_code] && @src_index[src][lang.lang_code].size > 0
            node.attribute('src').value = "#{@img_src_prefix}#{@src_index[src][lang.lang_code][0]['data']}"
          end
        end

        if node.get_attribute('alt')
          alt = node.get_attribute('alt').strip
          if @text_index[alt] && @text_index[alt][lang.lang_code] && @text_index[alt][lang.lang_code].size > 0
            node.attribute('alt').value = replace_text(alt, @text_index[alt][lang.lang_code][0]['data'])
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
wovnrb-0.2.07.1 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.10 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.09 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.08 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.07 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.06 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.05 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.04 lib/wovnrb/html_replacers/image_replacer.rb
wovnrb-0.2.03 lib/wovnrb/html_replacers/image_replacer.rb