Module: XMLhelper
- Included in:
- Rexle, Rexle::Element
- Defined in:
- lib/rexle.rb
Overview
modifications: 15-Apr-2012: bug fix: New element names are typecast as string 16-Mar-2012: bug fix: Element names which contain a colon can now be selected
in the xpath.
22-Feb-2012: bug resolution: Deactivated the PolyrexParser; using RexleParser instead 14-Jan-2012: Implemented Rexle::Elements#each 21-Dec-2011: Bug fix: xpath modified to allow querying from the actual root rather than the 1st child element from the root 04-Sep-2011: Bug fix: xpath will now only return Rexle:elements when
* is used.
03-Sep-2011: Implemented deep_clone as well as modifying clone to function
similar to REXML.
28-Au-2011: New line characters between elements are now preserved 24-Jul-2011: Smybols are used for attribute keys instead of strings now 18-Jun-2011: A Rexle document can now be added to another Rexle document
e.g. Rexle.new('<root/>').add Rexle.new "<a>123</a>"
Instance Method Summary collapse
- #doc_pretty_print(children) ⇒ Object
- #doc_print(children) ⇒ Object
- #pretty_print(nodes, indent = '0') ⇒ Object
- #scan_print(nodes) ⇒ Object
Instance Method Details
#doc_pretty_print(children) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rexle.rb', line 38 def doc_pretty_print(children) body = children.empty? ? self.value : pretty_print(children,2).join a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]} ind = "\n " "<%s%s>%s%s%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), ind, body, "\n", self.root.name] end |
#doc_print(children) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/rexle.rb', line 30 def doc_print(children) body = children.empty? ? self.root.value : scan_print(children).join a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]} "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body, self.root.name] #"<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body] end |
#pretty_print(nodes, indent = '0') ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rexle.rb', line 75 def pretty_print(nodes, indent='0') indent = indent.to_i nodes.map.with_index do |x, i| if x.is_a? Rexle::Element then unless x.name == '![' then a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]} tag = x.name + (a.empty? ? '' : ' ' + a.join(' ')) ind1 = x.children.length > 0 ? ("\n" + ' ' * indent) : '' start = i > 0 ? ("\n" + ' ' * (indent - 1)) : '' out = ["%s<%s>%s" % [start, tag, ind1]] out << x.value.sub(/^[\n\s]+$/,'') unless x.value.nil? || x.value.empty? out << pretty_print(x.children, (indent + 1).to_s.clone) ind2 = x.children.length > 0 ? ("\n" + ' ' * (indent - 1)) : '' out << "%s</%s>" % [ind2, x.name] else "<![CDATA[%s]]>" % x.value end elsif x.is_a? String then x.sub(/^[\n\s]+$/,'') end end end |
#scan_print(nodes) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rexle.rb', line 46 def scan_print(nodes) nodes.map do |x| if x.is_a? Rexle::Element then if x.name.chr != '!' then a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]} tag = x.name + (a.empty? ? '' : ' ' + a.join(' ')) if x.value.length > 0 or x.children.length > 0 then out = ["<%s>" % tag] out << x.value unless x.value.nil? || x.value.empty? out << scan_print(x.children) out << "</%s>" % x.name else out = ["<%s/>" % tag] end elsif x.name == '!-' then "<!--%s-->" % x.value else "<![CDATA[%s]]>" % x.value end elsif x.is_a? String then x end end end |