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, #get_element_by_id, #get_elements_by_tag_name, #insert_after, #insert_before, #next_sibling, #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, #get_subnode, #inner_html, #inner_html=, #inner_text, #next_node, #node_position, #nodes_at, #position, #previous_node, #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



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/hpricot/traverse.rb', line 686

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



566
567
568
# File 'lib/hpricot/traverse.rb', line 566

def css_path
  nil
end

#rootObject

Raises:



730
731
732
733
734
735
736
# File 'lib/hpricot/traverse.rb', line 730

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



669
670
671
672
673
674
675
# File 'lib/hpricot/traverse.rb', line 669

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



560
561
562
# File 'lib/hpricot/traverse.rb', line 560

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

#traverse_some_element(name_set, &block) ⇒ Object



585
586
587
# File 'lib/hpricot/traverse.rb', line 585

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

#xpathObject



563
564
565
# File 'lib/hpricot/traverse.rb', line 563

def xpath
  "/"
end