Module: Hpricot::Doc::Trav

Includes:
Container::Trav
Included in:
Hpricot::Doc
Defined in:
lib/hpricot/traverse.rb,
lib/hpricot/modules.rb,
lib/hpricot/traverse.rb,
lib/hpricot/traverse.rb,
lib/hpricot/traverse.rb

Overview

:stopdoc:

Instance Method Summary collapse

Methods included from Container::Trav

#classes, #containers, #each_child, #each_child_with_index, #each_hyperlink, #each_hyperlink_uri, #each_uri, #filter, #find_element, #following_siblings, #get_element_by_id, #get_elements_by_tag_name, #insert_after, #insert_before, #next_sibling, #preceding_siblings, #previous_sibling, #replace_child, #siblings_at, #traverse_text_internal

Methods included from Traverse

#after, #at, #before, #bogusetag?, #children_of_type, #clean_path, #comment?, #doc?, #doctype?, #elem?, filter, #following, #get_subnode, #html, #index, #inner_html=, #inner_text, #make, #next, #node_position, #nodes_at, #position, #preceding, #previous, #procins?, #search, #swap, #text?, #to_html, #to_original_html, #to_plain_text, #traverse_element, #traverse_text, #xmldecl?

Instance Method Details

#authorObject

author searches author and return it as a text. It returns nil if not found.

author searchs following information.

  • <meta name=“author” content=“author-name”> in HTML

  • <link rev=“made” title=“author-name”> in HTML

  • <dc:creator>author-name</dc:creator> in RSS

  • <dc:publisher>author-name</dc:publisher> in RSS



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/hpricot/traverse.rb', line 754

def author
  traverse_element('meta',
    '{http://www.w3.org/1999/xhtml}meta') {|e|
    begin
      next unless e.fetch_attr('name').downcase == 'author'
      author = e.fetch_attribute('content').strip
      return author if !author.empty?
    rescue IndexError
    end
  }

  traverse_element('link',
    '{http://www.w3.org/1999/xhtml}link') {|e|
    begin
      next unless e.fetch_attr('rev').downcase == 'made'
      author = e.fetch_attribute('title').strip
      return author if !author.empty?
    rescue IndexError
    end
  } 

  if channel = find_element('{http://purl.org/rss/1.0/}channel')
    channel.traverse_element('{http://purl.org/dc/elements/1.1/}creator') {|e|
      begin
        author = e.extract_text.strip
        return author if !author.empty?
      rescue IndexError
      end
    }
    channel.traverse_element('{http://purl.org/dc/elements/1.1/}publisher') {|e|
      begin
        author = e.extract_text.strip
        return author if !author.empty?
      rescue IndexError
      end
    }
  end

  nil
end

#css_pathObject



634
635
636
# File 'lib/hpricot/traverse.rb', line 634

def css_path
  nil
end

#rootObject

Raises:



798
799
800
801
802
803
804
# File 'lib/hpricot/traverse.rb', line 798

def root
  es = []
  children.each {|c| es << c if c.elem? }
  raise Hpricot::Error, "no element" if es.empty?
  raise Hpricot::Error, "multiple top elements" if 1 < es.length
  es[0]
end

#titleObject

title searches title and return it as a text. It returns nil if not found.

title searchs following information.

  • <title>…</title> in HTML

  • <title>…</title> in RSS



737
738
739
740
741
742
743
# File 'lib/hpricot/traverse.rb', line 737

def title
  e = find_element('title',
    '{http://www.w3.org/1999/xhtml}title',
    '{http://purl.org/rss/1.0/}title',
    '{http://my.netscape.com/rdf/simple/0.9/}title')
  e && e.extract_text
end

#traverse_all_element(&block) ⇒ Object



628
629
630
# File 'lib/hpricot/traverse.rb', line 628

def traverse_all_element(&block)
  children.each {|c| c.traverse_all_element(&block) }
end

#traverse_some_element(name_set, &block) ⇒ Object



653
654
655
# File 'lib/hpricot/traverse.rb', line 653

def traverse_some_element(name_set, &block)
  children.each {|c| c.traverse_some_element(name_set, &block) }
end

#xpathObject



631
632
633
# File 'lib/hpricot/traverse.rb', line 631

def xpath
  "/"
end