module HTree::Node

Public Instance Methods

display_html(out=$stdout, encoding=HTree::Encoder.internal_charset) click to toggle source

#display_html prints the node as HTML.

The first optional argument, out, specifies output target. It should respond to <<. If it is not specified, $stdout is used.

The second optional argument, encoding, specifies output MIME charset (character encoding). If it is not specified, HTree::Encoder.internal_charset is used.

#display_html returns out.

# File htree/display.rb, line 37
def display_html(out=$stdout, encoding=HTree::Encoder.internal_charset)
  encoder = HTree::Encoder.new(encoding)
  encoder.html_output = true
  self.output(encoder, HTree::HTMLContext)
  out << encoder.finish
  out
end
display_xml(out=$stdout, encoding=HTree::Encoder.internal_charset) click to toggle source

#display_xml prints the node as XML.

The first optional argument, out, specifies output target. It should respond to <<. If it is not specified, $stdout is used.

The second optional argument, encoding, specifies output MIME charset (character encoding). If it is not specified, HTree::Encoder.internal_charset is used.

#display_xml returns out.

# File htree/display.rb, line 17
def display_xml(out=$stdout, encoding=HTree::Encoder.internal_charset)
  encoder = HTree::Encoder.new(encoding)
  self.output(encoder, HTree::DefaultContext)
  # don't call finish_with_xmldecl because self already has a xml decl.
  out << encoder.finish
  out
end
eliminate_raw_string() click to toggle source
# File htree/raw_string.rb, line 63
def eliminate_raw_string
  raise NotImplementedError
end
extract_text() click to toggle source
# File htree/extract_text.rb, line 7
def extract_text
  raise NotImplementedError
end
make_loc() click to toggle source

creates a location object which points to self.

# File htree/loc.rb, line 8
def make_loc
  self.class::Loc.new(nil, nil, self)
end
raw_string() click to toggle source

#raw_string returns a source string recorded by parsing. It returns nil if the node is constructed not via parsing.

# File htree/raw_string.rb, line 8
def raw_string
  catch(:raw_string_tag) {
    return raw_string_internal('')
  }
  nil
end
subst(pairs) click to toggle source

subst substitutes several subtrees at once.

t = HTree('<r><x/><y/><z/></r>')
l = t.make_loc
t2 = t.subst({
  l.get_subnode(0, 'k') => 'v',
  l.get_subnode(0, -1) => HTree('<a/>'),
  l.get_subnode(0, 1) => nil,
  l.get_subnode(0, 2, 0) => HTree('<b/>'),
})
pp t2
# =>
#<HTree::Doc
 {elem <r k="v"> {emptyelem <a>} {emptyelem <x>} {elem <z> {emptyelem <b>}}}>
# File htree/loc.rb, line 31
def subst(pairs)
  pairs = pairs.map {|key, val|
    key = key.index_list(self)
    unless Array === val
      val = [val]
    end
    [key, val]
  }

  pairs_empty_key, pairs_nonempty_key =
    pairs.partition {|key, val| key.empty? }
  if !pairs_empty_key.empty?
    if !pairs_nonempty_key.empty?
      raise ArgumentError, "cannot substitute a node under substituting tree."
    end
    result = []
    pairs_empty_key.each {|key, val| result.concat val }
    result.compact!
    if result.length == 1
      return result[0]
    else
      raise ArgumentError, "cannot substitute top node by multiple nodes: #{nodes.inspect}"
    end
  end
  if pairs_nonempty_key.empty?
    return self
  end

  subst_internal(pairs)
end
to_node() click to toggle source

return self.

# File htree/loc.rb, line 13
def to_node
  self
end
to_rexml() click to toggle source

convert to REXML tree.

# File htree/rexml.rb, line 29
def to_rexml
  require 'rexml/document'
  to_rexml_internal(nil, DefaultContext)
end