Class: Shax
- Inherits:
-
Object
- Object
- Shax
- Defined in:
- lib/shax.rb
Defined Under Namespace
Classes: Node
Constant Summary collapse
- Tags =
%r` <(/?[-\w:.]+) # 1: tag (loose, but probably ok) ([^>]+)?> # 2: atts ((?:<!\[CDATA\[[\s\S]*?\]\]>|[^<]+)+)? # 3: text `mx
- Atts =
%r` ([-\w:.]+)\s*=\s* # 1: name (['"]) # 2: quote ([\s\S]*?)\2 # 3: value `x
- Data =
%r` <!\[CDATA\[([\s\S]*?)\]\]> `x
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
-
#xml ⇒ Object
Returns the value of attribute xml.
Class Method Summary collapse
Instance Method Summary collapse
- #convert(node = @root) ⇒ Object
-
#initialize(xml) ⇒ Shax
constructor
A new instance of Shax.
- #parse(str) ⇒ Object
- #parse_atts(str) ⇒ Object
- #skip_ns(str) ⇒ Object
- #value(str) ⇒ Object
Constructor Details
#initialize(xml) ⇒ Shax
Returns a new instance of Shax.
76 77 78 79 |
# File 'lib/shax.rb', line 76 def initialize(xml) @xml = xml @root = parse xml end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
2 3 4 |
# File 'lib/shax.rb', line 2 def root @root end |
#xml ⇒ Object
Returns the value of attribute xml.
2 3 4 |
# File 'lib/shax.rb', line 2 def xml @xml end |
Class Method Details
.load(str) ⇒ Object
31 32 33 |
# File 'lib/shax.rb', line 31 def self.load(str) new(str).convert end |
.load!(str, deep = 4) ⇒ Object
35 36 37 |
# File 'lib/shax.rb', line 35 def self.load!(str, deep=4) (+load(str))[['*'] * deep * '/'] end |
.show(obj, lev = 0) ⇒ Object
39 40 41 42 43 44 45 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 74 |
# File 'lib/shax.rb', line 39 def self.show(obj, lev=0) min = ' ' * (lev + 0) pre = ' ' * (lev + 1) puts "{" if lev == 0 case obj when Hash obj.each do |k, v| case v when Hash puts "#{pre}#{k.inspect} => {" show(v, lev + 1) puts "#{pre}}," when Array puts "#{pre}#{k.inspect} => [" show(v, lev + 2) puts "#{pre}]," else puts "#{pre}#{k.inspect} => #{v.inspect}," end end when Array obj.each do |v| case v when Hash puts "#{pre}{" show(v, lev + 1) puts "#{pre}}," when Array abort "ERROR: arrays of arrays not yet supported" else puts "#{min}#{v.inspect}," end end end puts "}" if lev == 0 end |
Instance Method Details
#convert(node = @root) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/shax.rb', line 139 def convert(node=@root) return node.val if node.val out = {} for kid in node.kids obj = convert kid tag = kid.tag tag = skip_ns tag if out.key?(tag) val = out[tag] out[tag] = [val] unless val.is_a?(Array) out[tag].push obj else out[tag] = obj end end out end |
#parse(str) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/shax.rb', line 81 def parse(str) node = root = Node.new "!xml" = []; str.scan(Tags).each {|hits| .push(hits)} skip = nil .each_with_index do |(tag, atts, text), i| next if i == skip # closing tag if tag[0] == '/' node = node.parent # create baby else node.kids.push(baby = Node.new(tag, node)) # baby.atts = @parse_atts atts if atts? # if config.atts # self-closing tag if atts && atts[-1] == '/' baby.val = '' # text node elsif ((skip = i + 1) < .size) and [skip][0] == "/#{tag}" # baby.val = @value text?... # baby.val = !text ? '' : text.scan(Data).each {|hits| hits[0]} baby.val = text # starting tag else node = baby skip = nil end end end root end |
#parse_atts(str) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/shax.rb', line 118 def parse_atts(str) if str and (str = str.strip!).length > 3 atts = {} str.scan(Atts).each {|hits| atts["#{skip_ns(hits[1])}"] = value hits[3]} atts end end |
#skip_ns(str) ⇒ Object
126 127 128 129 130 |
# File 'lib/shax.rb', line 126 def skip_ns(str) return str unless str.include?(':') pre = str[0] == '/' ? '/' : '' pre = str.split(':', 2)[-1] end |
#value(str) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/shax.rb', line 132 def value(str) return '' unless str return '' + str # if isNan str ??? return str.to_f if str.include? '.' return str.to_i end |