Module: Hpricot::Doc::Trav

Includes:
Container::Trav
Included in:
Hpricot::Doc
Defined in:
lib/ext/hpricot/traverse.rb,
lib/ext/hpricot/modules.rb,
lib/ext/hpricot/traverse.rb,
lib/ext/hpricot/traverse.rb,
lib/ext/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



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
794
795
796
797
798
799
# File 'lib/ext/hpricot/traverse.rb', line 760

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



640
641
642
# File 'lib/ext/hpricot/traverse.rb', line 640

def css_path
  nil
end

#rootObject

Raises:



804
805
806
807
808
809
810
# File 'lib/ext/hpricot/traverse.rb', line 804

def root
  es = []
  children.each {|c| es << c if c.elem? } if children
  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



743
744
745
746
747
748
749
# File 'lib/ext/hpricot/traverse.rb', line 743

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



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

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

#traverse_some_element(name_set, &block) ⇒ Object



659
660
661
# File 'lib/ext/hpricot/traverse.rb', line 659

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

#xpathObject



637
638
639
# File 'lib/ext/hpricot/traverse.rb', line 637

def xpath
  "/"
end