datagnan 0.2.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/datagnan.rb +59 -31
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61cb1a3b24ff1ca759c01d5a5934f92ed8feaf21
4
- data.tar.gz: 212dc29ae4e8d7c479b4ff13c207a5dddc4a8607
3
+ metadata.gz: 1d32649719dee617ba923822bc39757c91f8a32c
4
+ data.tar.gz: c11b7755410a0417d5b472d95e021baed11cc37e
5
5
  SHA512:
6
- metadata.gz: 39be95e5c5db5a4551ed636b9f516eb0239261286af2c2c50ca195a96094b0682f15c10367b0721d8e4310935b3ebbe48a8796453a8f86c476a2eee687e089ec
7
- data.tar.gz: 2b364f95fe17e9ff0c58943fa131b69209488df1304a31ec4024d55ee8a92de0253688b4a12c24854970ea5214ab4836a9b4b6aa7e07f6633f9b2f548bea9717
6
+ metadata.gz: 5be96919d376b2e7a54493fba841ad2eae1c00733cea685d27470b9984d072f7cd70b2a778a7720c0f7ae2fd09effda4db84ef1c57e38faec3062d82860c65f6
7
+ data.tar.gz: ac1197a9f5551fa1a14ac97495733cc4432ad62df1fb2e920ff0e997c89da095664c6300ca7c142485cdf399396ee2af3e3ee1096261e523161483d648244518
data/lib/datagnan.rb CHANGED
@@ -1,5 +1,5 @@
1
- ## Nokogiri for HTML parsing
2
- require 'nokogiri'
1
+ ## Oga for HTML parsing (Yeah, no Nokogiri!)
2
+ require 'oga'
3
3
 
4
4
  class Datagnan
5
5
 
@@ -8,11 +8,7 @@ class Datagnan
8
8
  ## Constructor
9
9
  def initialize(file_path, options = {}, locals = {})
10
10
  ## TODO: return fragment, not full html with auto-tags
11
- @doc = Nokogiri::HTML(File.read(file_path)) do |config|
12
- ## Because "Tag header invalid" and many other HTML5 tags
13
- config.noerror
14
- end
15
- puts @doc.errors if (@doc.errors.any?)
11
+ @doc = Oga.parse_html(File.read(file_path))
16
12
  ## debug
17
13
  # puts "-- @doc = #{@doc}"
18
14
  @attrs_sep = options.delete(:attrs_sep) || ";"
@@ -39,7 +35,7 @@ class Datagnan
39
35
  def read
40
36
  ## debug
41
37
  # puts "-- Datagnan.read ( @doc = #{@doc} )"
42
- @doc.to_html
38
+ @doc.to_xml
43
39
  end
44
40
 
45
41
  ## Parse and replace
@@ -52,23 +48,27 @@ class Datagnan
52
48
  root = @doc if root.nil?
53
49
  ## debug
54
50
  # puts "-- Datagnan.write ( vars = #{vars} )"
55
- # puts "-- Datagnan.write ( root = #{root} )"
51
+ # puts "-- Datagnan.write ( root = #{root.to_xml} )"
56
52
  ## replace data-each
57
53
  data_each(vars, root)
58
54
  ## debug
59
- # puts "-- Datagnan.write ( vars = #{vars} )"
60
- # puts "-- Datagnan.write ( root = #{root} )"
55
+ # puts "-- Datagnan.write after each ( vars = #{vars} )"
56
+ # puts "-- Datagnan.write after each ( root = #{root.to_xml} )"
61
57
  ## replace data-when
62
58
  data_when(vars, root)
63
59
  ## debug
64
- # puts "Datagnan.write after each ( vars = #{vars} )"
65
- # puts "Datagnan.write after each ( root = #{root} )"
60
+ # puts "-- Datagnan.write after when ( vars = #{vars} )"
61
+ # puts "-- Datagnan.write after when ( root = #{root.to_xml} )"
66
62
  ## replace data-var
67
63
  data_vars(vars, root)
68
64
  ## debug
69
- # puts "Datagnan.write after vars ( root = #{root} )"
65
+ # puts "Datagnan.write after vars ( vars = #{vars} )"
66
+ # puts "Datagnan.write after vars ( root = #{root.to_xml} )"
70
67
  ## replace data-attrs
71
68
  data_attrs(vars, root)
69
+ ## debug
70
+ # puts "Datagnan.write after attrs ( vars = #{vars} )"
71
+ # puts "Datagnan.write after attrs ( root = #{root.to_xml} )"
72
72
  ## result
73
73
  return self
74
74
  end
@@ -86,15 +86,15 @@ private
86
86
  # puts "-- Datagnan.data_vars each ( vars = #{vars} )"
87
87
  # puts "-- Datagnan.data_vars each ( node = #{node} )"
88
88
  var = vars
89
- node['data-var'].split('.').each do |key|
89
+ node.get('data-var').split('.').each do |key|
90
90
  ## var exist
91
91
  var = ( var.is_a?(Hash) ? var[key] : (var.respond_to?(key) ? var.send(key) : nil ) )
92
92
  end
93
93
  # puts "-- Datagnan.data_vars each ( var = #{var} )"
94
94
 
95
95
  unless var.nil?
96
- node.inner_html = var.to_s
97
- node.remove_attribute('data-var')
96
+ node.children = Oga.parse_html(var.to_s).children
97
+ node.unset('data-var')
98
98
  end
99
99
  ## TODO: if var not exist remember it (maybe return to model)
100
100
  end
@@ -106,12 +106,13 @@ private
106
106
  def data_attrs(vars, root)
107
107
  ## debug
108
108
  # puts "-- Datagnan.data_attrs ( vars = #{vars} )"
109
- # puts "-- Datagnan.data_attrs ( root = #{root} )"
109
+ # puts "-- Datagnan.data_attrs ( root = #{root.to_xml} )"
110
110
  # puts "-- Datagnan.data_attrs ( @attrs_sep = #{@attrs_sep} )"
111
111
  root.css('*[data-attrs]').each do |node|
112
112
  ## debug
113
113
  # puts "-- Datagnan.data_attrs each ( node = #{node} )"
114
- node['data-attrs'].split(@attrs_sep).each do |attr|
114
+ # puts "-- Datagnan.data_attrs each ( node.to_xml = #{node.to_xml} )"
115
+ node.get('data-attrs').split(@attrs_sep).each do |attr|
115
116
  key_val = attr.split(':')
116
117
  key = key_val[0]
117
118
  val = key_val.size.odd? ? key_val[0] : key_val[1]
@@ -122,27 +123,54 @@ private
122
123
  ## var exist
123
124
  var = ( var.is_a?(Hash) ? var[val_part] : (var.respond_to?(val_part) ? var.send(val_part) : nil ) )
124
125
  end
126
+ # puts "-- Datagnan.data_attrs each ( var = #{var} )"
125
127
  unless var.nil?
126
- node[key] = var
128
+ node.set(key, var.to_s)
127
129
  end
130
+ # puts "-- Datagnan.data_attrs each ( node.to_xml = #{node.to_xml} )"
128
131
  ## TODO: if attr not exist remember it (maybe return to model)
129
132
  end
130
- node.remove_attribute('data-attrs')
133
+ node.unset('data-attrs')
131
134
  end
132
135
  end
133
136
 
134
137
  ## fill template by array
135
138
  def data_each(vars, root)
136
139
  ## debug
137
- # puts "Datagnan.data_each ( vars = #{vars}, root.tag = #{root.name} )"
140
+ # puts "-- Datagnan.data_each ( vars = #{vars} )"
141
+ # puts "-- Datagnan.data_each ( root = #{root.to_xml} )"
142
+ # puts "-- Datagnan.data_each ( root.css('[data-each]') = #{root.css('[data-each]').inspect} )"
138
143
  root.css('*[data-each]').each do |node|
139
- ## var exist
140
- if vars.has_key? node['data-each']
141
- vars[node['data-each']].each do |item|
142
- node_clone = node.clone
143
- write(item, node_clone)
144
- node_clone.remove_attribute('data-each')
145
- node.parent << node_clone
144
+ # puts "-- Datagnan.data_each each"
145
+ # --------
146
+ var = vars
147
+ node.get('data-each').split('.').each do |key|
148
+ ## var exist
149
+ var = ( var.is_a?(Hash) ? var[key] : (var.respond_to?(key) ? var.send(key) : nil ) )
150
+ end
151
+ # puts "-- Datagnan.data_each each ( var = #{var} )"
152
+ # puts "-- Datagnan.data_each each ( node = #{node.to_xml} )"
153
+ unless var.nil?
154
+ # --------
155
+ node.unset('data-each')
156
+ var.each do |item|
157
+ # puts "-- Datagnan.data_each each ( item = #{item} )"
158
+ # puts "-- Datagnan.data_each each ( item.inspect = #{item.inspect} )"
159
+ new_node = Oga.parse_html(node.to_xml).children.pop
160
+ # puts "-- Datagnan.data_each each ( node after clone = #{node} )"
161
+ # puts "-- Datagnan.data_each each ( node.to_xml after clone = #{node.to_xml} )"
162
+ # puts "-- Datagnan.data_each each ( node.parent.to_xml = #{node.parent.to_xml} )"
163
+ # puts "-- Datagnan.data_each each ( new_node = #{new_node} )"
164
+ # puts "-- Datagnan.data_each each ( new_node.to_xml = #{new_node.to_xml} )"
165
+ write(item, new_node)
166
+ # puts "-- Datagnan.data_each each ( new_node after write = #{new_node} )"
167
+ # puts "-- Datagnan.data_each each ( new_node.to_xml after write = #{new_node.to_xml} )"
168
+ # puts "-- Datagnan.data_each each ( node after write= #{node} )"
169
+ # puts "-- Datagnan.data_each each ( node.to_xml after write= #{node.to_xml} )"
170
+ node.before(new_node)
171
+ # puts "-- Datagnan.data_each each ( node = #{node} )"
172
+ # puts "-- Datagnan.data_each each ( node.to_xml after insert = #{node.to_xml} )"
173
+ # puts "-- Datagnan.data_each each ( node.parent = #{node.parent.to_xml} )"
146
174
  end
147
175
  node.remove
148
176
  end
@@ -160,14 +188,14 @@ private
160
188
  # puts "-- Datagnan.data_when each ( vars = #{vars} )"
161
189
  # puts "-- Datagnan.data_when each ( node = #{node} )"
162
190
  var = vars
163
- node['data-when'].split('.').each do |key|
191
+ node.get('data-when').split('.').each do |key|
164
192
  ## var exist
165
193
  var = ( var.is_a?(Hash) ? var[key] : (var.respond_to?(key) ? var.send(key) : nil ) )
166
194
  end
167
195
  # puts "-- Datagnan.data_when each ( var = #{var} )"
168
196
 
169
197
  unless var.nil?
170
- var ? node.remove_attribute('data-when') : node.remove
198
+ var ? node.unset('data-when') : node.remove
171
199
  end
172
200
  ## TODO: if var not exist remember it (maybe return to model)
173
201
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagnan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
@@ -9,20 +9,20 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-23 00:00:00.000000000 Z
12
+ date: 2015-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: nokogiri
15
+ name: oga
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  description: Make HTML-templates without strange tags.