class HTree::Location

Attributes

index[R]
node[R]
parent[R]
to_node[R]

Public Instance Methods

extract_text() click to toggle source
# File htree/extract_text.rb, line 13
def extract_text
  to_node.extract_text
end
loc_list() click to toggle source

loc_list returns an array containing from location's root to itself.

t = HTree('<a><b><c>')
l = t.make_loc.get_subnode(0, 0, 0)
pp l, l.loc_list
# =>
#<HTree::Location: doc()/a/b/c>
[#<HTree::Location: doc()>,
 #<HTree::Location: doc()/a>,
 #<HTree::Location: doc()/a/b>,
 #<HTree::Location: doc()/a/b/c>]
# File htree/loc.rb, line 255
def loc_list
  loc = self
  result = [self]
  while loc = loc.parent
    result << loc
  end
  result.reverse!
  result
end
make_loc() click to toggle source

return self.

# File htree/loc.rb, line 169
def make_loc
  self
end
path() click to toggle source

path returns the path of the location.

l = HTree.parse("<a><b>x</b><b/><a/>").make_loc
l = l.get_subnode(0, 0, 0)
p l.path # => "doc()/a/b[1]/text()"
# File htree/loc.rb, line 270
def path
  result = ''
  loc_list.each {|loc|
    if parent = loc.parent
      result << '/' << parent.node.find_loc_step(loc.index)
    else
      result << loc.node.node_test_string
    end
  }
  result
end
subst(pairs) click to toggle source

subst substitutes several subtrees at once.

t = HTree('<r><x/><y/><z/></r>')
l = t.make_loc
l2 = l.subst({
  l.root.get_subnode('k') => 'v',
  l.root.get_subnode(-1) => HTree('<a/>'),
  l.find_element('y') => nil,
  l.find_element('z').get_subnode(0) => HTree('<b/>'),
})
pp l2, l2.to_node
# =>
#<HTree::Doc::Loc: doc()>
#<HTree::Doc
 {elem <r k="v"> {emptyelem <a>} {emptyelem <x>} {elem <z> {emptyelem <b>}}}>
# File htree/loc.rb, line 239
def subst(pairs)
  subst_itself(@node.subst(pairs))
end
subst_itself(node) click to toggle source

subst_itself substitutes the node pointed by the location. It returns the location of substituted node.

t1 = HTree('<a><b><c><d>')
p t1
l1 = t1.make_loc.get_subnode(0, 0, 0, 0)
p l1
l2 = l1.subst_itself(HTree('<z/>'))
p l2
t2 = l2.top.to_node
p t2
# =>
#<HTree::Doc {elem <a> {elem <b> {elem <c> {emptyelem <d>}}}}>
#<HTree::Location: doc()/a/b/c/d>
#<HTree::Location: doc()/a/b/c/z>
#<HTree::Doc {elem <a> {elem <b> {elem <c> {emptyelem <z>}}}}>
# File htree/loc.rb, line 206
def subst_itself(node)
  if @parent
    new_index = @index
    if !@node
      if Integer === @index
        if @index < 0
          new_index = 0
        elsif @parent.to_node.children.length < @index
          new_index = @parent.to_node.children.length
        end
      end
    end
    @parent.subst_itself(@parent.to_node.subst_subnode({@index=>node})).get_subnode(new_index)
  else
    node.make_loc
  end
end
top() click to toggle source

top returns the originator location.

t = HTree('<a><b><c><d>')
l = t.make_loc.get_subnode(0, 0, 0, 0)
p l, l.top
# =>
#<HTree::Location: doc()/a/b/c/d>
#<HTree::Location: doc()>
# File htree/loc.rb, line 181
def top
  result = self
  while result.parent
    result = result.parent
  end
  result
end