Class: LibXML::XML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rmtools/xml/finders.rb,
lib/rmtools/xml/document.rb

Instance Method Summary collapse

Instance Method Details

#__find(xp = nil, ns = nil, ss = nil) ⇒ Object



67
68
69
70
# File 'lib/rmtools/xml/finders.rb', line 67

def __find(xp=nil, ns=nil, ss=nil)
  xp ||= ss.head
  context(ns).find(XPath.filter xp)
end

#at(xpath, nslist = DefaultNS) ⇒ Object



90
91
92
# File 'lib/rmtools/xml/finders.rb', line 90

def at(xpath, nslist=DefaultNS)
  find(xpath, nslist)[0]
end

#bodyObject



10
11
12
# File 'lib/rmtools/xml/document.rb', line 10

def body
  context(nil).find('body')[0] || root
end

#find(xpath, nslist = DefaultNS) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rmtools/xml/finders.rb', line 72

def find(xpath, nslist=DefaultNS)
  xpath.sub!(/^([\w*])/, '//\1')
  node = self
  ss = StringScanner.new xpath
  ss.each %r{\[-?\d+(\.\.\d+)?\]|\{[^\}]+\}}, 
    ?[ => lambda {|ss|
      if node; node = FindByIndex[node, nslist, ss]
      else     return []     end  }, 
    ?{ => lambda {|ss|
      if node; node = FindByProc[node, nslist, ss]
      else     return []     end  },
    nil => lambda {|str|
      node = node.is(Array) ?
        node.sum([]) {|n| n.__find(str, nslist).to_a} : node.__find(str, nslist) 
    }
  node ? (!ss.eos? || node.is(Array)) ? node : [node] :  []
end

#inspectObject



23
24
25
# File 'lib/rmtools/xml/document.rb', line 23

def inspect
  "<#XMLDocument #{title && "«#{title}» "}(#{to_xhtml.size.bytes})>"
end

#titleObject



6
7
8
# File 'lib/rmtools/xml/document.rb', line 6

def title
  (a = context(nil).find('head//title')[0]) && a.content.strip
end

#to_xhtmlObject



14
15
16
17
18
19
20
21
# File 'lib/rmtools/xml/document.rb', line 14

def to_xhtml
  html = to_s
  html.sub! "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n", ''
  html.gsub! %r{<a name(=\"\S+\")\s+id\1(\s*[a-z0-9="]*\s*>)}, '<a name\1\2'
  html.gsub! %r{\n?<!\[CDATA\[\s*(.+?)\s*\]\]>\n?}m, '\1'
  html.gsub! %r{<html( +xmlns=\"http://\S+\")\1}, '<html\1'
  html
end