Sha256: 4be70c8e8516b7dfc36c74b9595e615485fb580cf4752348ee7674d961eefc1c

Contents?: true

Size: 916 Bytes

Versions: 4

Compression:

Stored size: 916 Bytes

Contents

module Convert
  module Converters

    SKIP = ['a', 'pre', 'code', 'kbd', 'script', 'iframe', 'img', 'link']

    # Scan a string with Nokogiri and convert if match string
    def scan(string, options = {})
      return string if !options[:converters] or options[:converters].empty?

      doc = Nokogiri::HTML.fragment(string)

      doc.search('.//text()').each do |el|
        t = el.text
        if t.strip.size > 0
          t = convert(t, options) if convertable?(el)
        end
        el.replace(t)
      end

      doc.to_html
    end

    # Loop converters and convert
    def convert(string, options = {})
      options[:converters].each{|c| string = send(c, string)}
      string
    end

    # Checks if a node is convertable by scanning the parents
    def convertable?(node)
      while(node = node.parent) do
        return false if SKIP.include?(node.name)
      end
      true
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convert-0.1.7 lib/converters/nokogiri.rb
convert-0.1.6 lib/converters/nokogiri.rb
convert-0.1.5 lib/converters/nokogiri.rb
convert-0.1.4 lib/converters/nokogiri.rb