rexle 0.8.4 → 0.8.5

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 +73 -60
  2. metadata +1 -1
data/lib/rexle.rb CHANGED
@@ -6,10 +6,69 @@ require 'rexml/document'
6
6
  require 'rexleparser'
7
7
  require 'dynarex-parser'
8
8
  require 'polyrex-parser'
9
- require 'pretty-xml'
10
9
  include REXML
11
10
 
11
+ module XMLhelper
12
+
13
+ def doc_print(children)
14
+
15
+ body = scan_print(children).join
16
+ a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
17
+ "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body, self.root.name]
18
+ end
19
+
20
+ def doc_pretty_print(children)
21
+
22
+ body = pretty_print(children,2).join
23
+ a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
24
+ ind = "\n "
25
+ "<%s%s>%s%s%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), ind, body, "\n", self.root.name]
26
+ end
27
+
28
+ def scan_print(nodes)
29
+
30
+ nodes.map do |x|
31
+ unless x.name == '![' then
32
+ a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
33
+ tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
34
+
35
+ out = ["<%s>" % tag]
36
+ out << x.value unless x.value.nil? || x.value.empty?
37
+ out << scan_print(x.children)
38
+ out << "</%s>" % x.name
39
+ else
40
+ "<![CDATA[%s]]>" % x.value
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ def pretty_print(nodes, indent='0')
47
+ indent = indent.to_i
48
+ nodes.map.with_index do |x, i|
49
+ unless x.name == '![' then
50
+ a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
51
+ tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
52
+
53
+ ind1 = x.children.length > 0 ? ("\n" + ' ' * indent) : ''
54
+ start = i > 0 ? ("\n" + ' ' * (indent - 1)) : ''
55
+ out = ["%s<%s>%s" % [start, tag, ind1]]
56
+
57
+ out << x.value.sub(/^[\n\s]+$/,'') unless x.value.nil? || x.value.empty?
58
+ out << pretty_print(x.children, (indent + 1).to_s.clone)
59
+ ind2 = x.children.length > 0 ? ("\n" + ' ' * (indent - 1)) : ''
60
+ out << "%s</%s>" % [ind2, x.name]
61
+ else
62
+ "<![CDATA[%s]]>" % x.value
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
12
70
  class Rexle
71
+ include XMLhelper
13
72
 
14
73
  def initialize(x=nil)
15
74
  super()
@@ -51,6 +110,8 @@ class Rexle
51
110
  end
52
111
 
53
112
  class Element
113
+ include XMLhelper
114
+
54
115
  attr_accessor :name, :value, :parent
55
116
  attr_reader :child_lookup
56
117
 
@@ -228,9 +289,17 @@ class Rexle
228
289
 
229
290
  alias text= value=
230
291
 
292
+ def xml(options={})
293
+ o = {pretty: false}.merge(options)
294
+ msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
295
+ method(msg).call(self.children)
296
+ end
297
+
298
+ # temp methods
299
+
231
300
  private
232
301
 
233
- def scan_print(nodes)
302
+ def scan_print222(nodes)
234
303
  out = []
235
304
  nodes.each do |x|
236
305
  out << "<%s>" % x.name
@@ -295,6 +364,8 @@ class Rexle
295
364
  return items.join(' ')
296
365
  end
297
366
  end
367
+
368
+
298
369
  end
299
370
 
300
371
  def scan_match(nodes, element, attr_search, condition, rlist)
@@ -444,64 +515,6 @@ class Rexle
444
515
  return element
445
516
  end
446
517
 
447
- def doc_print(children)
448
-
449
- body = scan_print(children).join
450
- a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
451
- "<%s%s>%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), body, self.root.name]
452
- end
453
-
454
-
455
- def doc_pretty_print(children)
456
-
457
- body = pretty_print(children,2).join
458
- a = self.root.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
459
- ind = "\n "
460
- "<%s%s>%s%s%s</%s>" % [self.root.name, a.empty? ? '' : ' ' + a.join(' '), ind, body, "\n", self.root.name]
461
- end
462
-
463
-
464
-
465
- def scan_print(nodes)
466
-
467
- nodes.map do |x|
468
- unless x.name == '![' then
469
- a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
470
- tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
471
-
472
- out = ["<%s>" % tag]
473
- out << x.value unless x.value.nil? || x.value.empty?
474
- out << scan_print(x.children)
475
- out << "</%s>" % x.name
476
- else
477
- "<![CDATA[%s]]>" % x.value
478
- end
479
- end
480
-
481
- end
482
-
483
- def pretty_print(nodes, indent='0')
484
- indent = indent.to_i
485
- nodes.map.with_index do |x, i|
486
- unless x.name == '![' then
487
- a = x.attributes.to_a.map{|k,v| "%s='%s'" % [k,v]}
488
- tag = x.name + (a.empty? ? '' : ' ' + a.join(' '))
489
-
490
- ind1 = x.children.length > 0 ? ("\n" + ' ' * indent) : ''
491
- start = i > 0 ? ("\n" + ' ' * (indent - 1)) : ''
492
- out = ["%s<%s>%s" % [start, tag, ind1]]
493
-
494
- out << x.value.sub(/^[\n\s]+$/,'') unless x.value.nil? || x.value.empty?
495
- out << pretty_print(x.children, (indent + 1).to_s.clone)
496
- ind2 = x.children.length > 0 ? ("\n" + ' ' * (indent - 1)) : ''
497
- out << "%s</%s>" % [ind2, x.name]
498
- else
499
- "<![CDATA[%s]]>" % x.value
500
- end
501
- end
502
-
503
- end
504
-
505
518
  def count(path) @doc.xpath(path).flatten.compact.length end
506
519
  def max(path) @doc.xpath(path).map(&:to_i).max end
507
520
 
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.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors: []
7
7