rexle 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rexle.rb +43 -5
  2. metadata +2 -2
data/lib/rexle.rb CHANGED
@@ -393,10 +393,8 @@ class Rexle
393
393
 
394
394
  def xml(options={})
395
395
  o = {pretty: false}.merge(options)
396
- body = scan_print(self.root.children).join
397
- a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
398
- out = "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body, self.root.name]
399
- o[:pretty] == false ? out : (PrettyXML::write out)
396
+ msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
397
+ "<?xml version='1.0' encoding='UTF-8'?>\n" + method(msg).call(self.root.children)
400
398
  end
401
399
 
402
400
  private
@@ -430,6 +428,24 @@ class Rexle
430
428
  return element
431
429
  end
432
430
 
431
+ def doc_print(children)
432
+
433
+ body = scan_print(children).join
434
+ a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
435
+ "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body, self.root.name]
436
+ end
437
+
438
+
439
+ def doc_pretty_print(children)
440
+
441
+ body = pretty_print(children,2).join
442
+ a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
443
+ ind = "\n "
444
+ "<%s%s>%s%s%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), ind, body, "\n", self.root.name]
445
+ end
446
+
447
+
448
+
433
449
  def scan_print(nodes)
434
450
 
435
451
  nodes.map do |x|
@@ -448,6 +464,28 @@ class Rexle
448
464
 
449
465
  end
450
466
 
467
+ def pretty_print(nodes, indent='0')
468
+ indent = indent.to_i
469
+ nodes.map.with_index do |x, i|
470
+ unless x.name == '![' then
471
+ a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
472
+ tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
473
+
474
+ ind1 = x.children.length > 0 ? ("\n" + ' ' * indent) : ''
475
+ start = i > 0 ? ("\n" + ' ' * (indent - 1)) : ''
476
+ out = ["%s<%s>%s" % [start, tag, ind1]]
477
+
478
+ out << x.value.sub(/^[\n\s]+$/,'') unless x.value.nil? || x.value.empty?
479
+ out << pretty_print(x.children, (indent + 2).to_s.clone)
480
+ ind2 = x.children.length > 0 ? ("\n" + ' ' * (indent - 1)) : ''
481
+ out << "%s</%s>" % [ind2, x.name]
482
+ else
483
+ "<![CDATA[%s]]>" % x.value
484
+ end
485
+ end
486
+
487
+ end
488
+
451
489
  def count(path) @doc.xpath(path).flatten.compact.length end
452
490
  def max(path) @doc.xpath(path).map(&:to_i).max end
453
491
 
@@ -459,4 +497,4 @@ class Rexle
459
497
  [node.name, node.text.to_s, attributes, *children]
460
498
  end
461
499
 
462
- end
500
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rexle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2011-01-11 00:00:00 +00:00
12
+ date: 2011-01-12 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency