weak_xml 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28f1fcd1035b0186e9ebbfd78ad831a1d1598f29
4
- data.tar.gz: ddb2a863e7dacf566f175300f440c036289c84c1
3
+ metadata.gz: b63a7bfb1c0581a34ec1d3ea98c8b04d6ebac39f
4
+ data.tar.gz: 66661f43b772b611cc6c7a4ce81024cba1520207
5
5
  SHA512:
6
- metadata.gz: c39a32813a211c8b67ac6766841587066c7279c58f1d4614bbf2b5947d53927cc45c67c7ff0427b49cee1e6d58fbba47ebf63cd643e9ac183eff0a23cbe392e9
7
- data.tar.gz: 964bd144d6f0e55c0a9fb1b98ede708ade79e9d48c5bb3486c246e535f7ea8ea4a642799c2828dc1456ec69707dd855e91ac07f0f084dd3b978df2afd8c4c00e
6
+ metadata.gz: 9f151bb121175bc18223945e8fdb2ae96371ed1809aed14637101f1eca1ac7e1433639326682cff978d354d2936902b90e52a0268e5691584210fbfac41c723a
7
+ data.tar.gz: debfabacf78ae899d20e793894c1e2198547d406722b5eaa9615dda62b4a3889f15b53a9b453388869d74a4560ba3a783cc451a1ec8cff82440b2106c0ab7be3
@@ -1,20 +1,26 @@
1
1
  require "weak_xml/version"
2
2
 
3
3
  class WeakXml
4
- require_relative "./weak_xml/fragment"
4
+ # Compiling regular expressions is expensive, specially if one uses variable
5
+ # parts. So, in order to achive the best performance, these should be compiled
6
+ # upfront without any "runtime dependencies".
7
+ ATTRIBUTES_REGEXP = Regexp.compile(/\A<[^>\s]+\s+([^>]+)/m)
8
+ CONTENT_REGEXP = Regexp.compile(/.*?>(.*?)<[^>]+>\Z/m)
5
9
 
6
10
  def self.find(tag, xml, options = {})
7
11
  if matched_string = xml[regex_factory(tag, options)]
8
- Fragment.new(tag, matched_string)
12
+ self.new(matched_string, tag: tag)
9
13
  end
10
14
  end
11
15
 
12
16
  def self.find_all(tag, xml, options = {})
13
- xml.scan(regex_factory(tag, options)).map! do |match|
14
- Fragment.new(tag, match)
17
+ xml.scan(regex_factory(tag, options)).map! do |matched_string|
18
+ self.new(matched_string, tag: tag)
15
19
  end
16
20
  end
17
21
 
22
+ private
23
+
18
24
  def self.regex_factory(tag, options = {})
19
25
  options[:multiline] = options[:multiline].nil? ? true : !!options[:multiline]
20
26
 
@@ -28,11 +34,29 @@ class WeakXml
28
34
  Regexp.new(regexp_base, (options[:multiline] ? Regexp::MULTILINE : 0))
29
35
  end
30
36
 
37
+ public
38
+
31
39
  def initialize(xml, options = {})
32
40
  @options = options
41
+ @tag = options.delete(:tag)
33
42
  @xml = xml
34
43
  end
35
44
 
45
+ def attr(key)
46
+ if match_data = @xml.match(ATTRIBUTES_REGEXP)
47
+ if value_match_data = match_data.captures.first.match(/#{key}="(.+?)"/)
48
+ value_match_data.captures.first
49
+ end
50
+ end
51
+ end
52
+
53
+ def content
54
+ @content ||=
55
+ if match_data = @xml.match(CONTENT_REGEXP)
56
+ match_data.captures.first.tap(&:strip!)
57
+ end
58
+ end
59
+
36
60
  def find(tag, options = nil)
37
61
  self.class.find(tag, @xml, (options || @options))
38
62
  end
@@ -40,4 +64,8 @@ class WeakXml
40
64
  def find_all(tag, options = nil)
41
65
  self.class.find_all(tag, @xml, (options || @options))
42
66
  end
67
+
68
+ def to_s
69
+ @xml
70
+ end
43
71
  end
@@ -1,3 +1,3 @@
1
1
  class WeakXml
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weak_xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-28 00:00:00.000000000 Z
11
+ date: 2015-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,7 +92,6 @@ files:
92
92
  - bin/console
93
93
  - bin/setup
94
94
  - lib/weak_xml.rb
95
- - lib/weak_xml/fragment.rb
96
95
  - lib/weak_xml/version.rb
97
96
  - weak_xml.gemspec
98
97
  homepage: https://github.com/msievers/weak_xml
@@ -114,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
113
  version: '0'
115
114
  requirements: []
116
115
  rubyforge_project:
117
- rubygems_version: 2.4.7
116
+ rubygems_version: 2.4.8
118
117
  signing_key:
119
118
  specification_version: 4
120
119
  summary: A none parsing xml library
121
120
  test_files: []
122
- has_rdoc:
@@ -1,28 +0,0 @@
1
- require_relative "../weak_xml"
2
-
3
- class WeakXml::Fragment
4
- # Compiling regular expressions is expensive, specially if one uses variable
5
- # parts. So, in order to achive the best performance, these should be compiled
6
- # upfront without any "runtime dependencies".
7
- ATTRIBUTES_REGEXP = Regexp.compile(/\A<\w+\s+([^>]+)/m)
8
- CONTENT_REGEXP = Regexp.compile(/.*?>(.*?)<[^>]+>\Z/m)
9
-
10
- def initialize(tag, xml)
11
- @tag = tag
12
- @xml = xml
13
- end
14
-
15
- def attr(key)
16
- if match_data = @xml.match(ATTRIBUTES_REGEXP)
17
- if value_match_data = match_data.captures.first.match(/#{key}="(.+?)"/)
18
- value_match_data.captures.first
19
- end
20
- end
21
- end
22
-
23
- def content
24
- if match_data = @xml.match(CONTENT_REGEXP)
25
- match_data.captures.first.tap(&:strip!)
26
- end
27
- end
28
- end