Sha256: 753b3aaacccce2193cc8eda810e4880ec709bcffcf21c134495c08af41f7c51a

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

# Author::    TAC (tac@tac42.net)

require_relative 'yasuri_node'

module Yasuri
  class TextNode
    include Node

    def initialize(xpath, name, children = [], hash = {})
      super(xpath, name, children)

      truncate = hash[:truncate]
      proc     = hash[:proc]

      truncate = Regexp.new(truncate) if not truncate.nil? # regexp or nil
      @truncate = truncate
      @truncate = Regexp.new(@truncate.to_s) if not @truncate.nil?

      @proc = proc.nil? ? nil : proc.to_sym

    end

    def inject(agent, page, opt = {})
      node = page.search(@xpath)
      text = node.text.to_s

      if @truncate
        matches = @truncate.match(text)
        text = matches ? matches[1] || matches[0] || text : ""
      end

      text = text.__send__(@proc) if @proc && text.respond_to?(@proc)
      text
    end

    def opts
      {truncate:@truncate, proc:@proc}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yasuri-1.9.11 lib/yasuri/yasuri_text_node.rb