datagnan 1.1.5 → 1.2.0
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 +4 -4
- data/lib/datagnan.rb +76 -38
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 881e2804b0516deffd9baa0fdd07a6b4dd9e5aa6
|
4
|
+
data.tar.gz: 3435fc4132a7ad4f77c84a0f8f78906bf058c527
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65388ecda133e1669acf4cfea36067b8b203ab2fe67ed15ac25c91e40300957970f5c797621bfcd8af71bd778c2f5bba37c4b8f089b119620a3f06a3201fb674
|
7
|
+
data.tar.gz: 17245b721d824eef3654a2acce9a1cb7736019329f1a8d113e4f5834564750212f618157847508a26bdabd03fd23289b04363777bd6d55e50c20b6850976f499
|
data/lib/datagnan.rb
CHANGED
@@ -10,18 +10,17 @@ class Datagnan
|
|
10
10
|
# path to .html-template file
|
11
11
|
# @param [Hash] options
|
12
12
|
# options for parsing and filling the template
|
13
|
-
# @option options [String] :attrs_sep (";") Separate string for 'data-attrs' attribute
|
14
|
-
# @option options [Object] :scope (self) Scope for variables
|
15
|
-
# @option options [Hash] :locals (locals || {}) Local variables for filling the template
|
16
13
|
# @param [Hash] locals
|
17
14
|
# local variables for filling the template
|
18
|
-
def initialize(file_path, options = {},
|
19
|
-
@doc = Oga.parse_html
|
15
|
+
def initialize(file_path, options = {}, local_vars = {})
|
16
|
+
@doc = Oga.parse_html File.read(file_path)
|
20
17
|
## debug
|
21
18
|
# puts "-- @doc = #{@doc}"
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
19
|
+
@options = {}
|
20
|
+
@options[:attrs_sep] = options[:attrs_sep] || ";"
|
21
|
+
@options[:path_filters] = options[:path_filters] || {}
|
22
|
+
@scope = options[:scope] || self
|
23
|
+
@vars = (options[:local_vars] || {}).merge! local_vars
|
25
24
|
@vars.merge! Hash[@scope.instance_variables.map { |name| [name.to_s, @scope.instance_variable_get(name)] } ] unless @scope.nil?
|
26
25
|
## debug
|
27
26
|
# puts "-- Datagnan.new ( @vars = #{@vars} )"
|
@@ -34,13 +33,10 @@ class Datagnan
|
|
34
33
|
# path to .html-template file
|
35
34
|
# @param [Hash] options
|
36
35
|
# options for parsing and filling the template
|
37
|
-
# @option options [String] :attrs_sep (";") Separate string for 'data-attrs' attribute
|
38
|
-
# @option options [Object] :scope (self) Scope for variables
|
39
|
-
# @option options [Hash] :locals (locals || {}) Local variables for filling the template
|
40
36
|
# @param [Hash] locals
|
41
37
|
# local variables for filling the template
|
42
|
-
def self.open(file_path, options = {},
|
43
|
-
obj = Datagnan.new(file_path, options,
|
38
|
+
def self.open(file_path, options = {}, local_vars = {})
|
39
|
+
obj = Datagnan.new(file_path, options, local_vars).write
|
44
40
|
yield obj if block_given?
|
45
41
|
return obj
|
46
42
|
end
|
@@ -51,13 +47,10 @@ class Datagnan
|
|
51
47
|
# path to .html-template file
|
52
48
|
# @param [Hash] options
|
53
49
|
# options for parsing and filling the template
|
54
|
-
# @option options [String] :attrs_sep (";") Separate string for 'data-attrs' attribute
|
55
|
-
# @option options [Object] :scope (self) Scope for variables
|
56
|
-
# @option options [Hash] :locals (locals || {}) Local variables for filling the template
|
57
50
|
# @param [Hash] locals
|
58
51
|
# local variables for filling the template
|
59
|
-
def self.read(file_path, options = {},
|
60
|
-
Datagnan.new(file_path, options,
|
52
|
+
def self.read(file_path, options = {}, local_vars = {})
|
53
|
+
Datagnan.new(file_path, options, local_vars).write.read
|
61
54
|
end
|
62
55
|
|
63
56
|
## Return HTML-String. As File.new.read
|
@@ -101,6 +94,7 @@ class Datagnan
|
|
101
94
|
# puts "Datagnan.write after vars ( root = #{root.to_xml} )"
|
102
95
|
## replace data-attrs
|
103
96
|
data_attrs(vars, root)
|
97
|
+
data_attr(vars, root)
|
104
98
|
## debug
|
105
99
|
# puts "Datagnan.write after attrs ( vars = #{vars} )"
|
106
100
|
# puts "Datagnan.write after attrs ( root = #{root.to_xml} )"
|
@@ -126,6 +120,14 @@ private
|
|
126
120
|
# puts "-- Datagnan.find_value ( vars = #{vars} )"
|
127
121
|
# beginning_time = Time.now
|
128
122
|
var = vars
|
123
|
+
|
124
|
+
## path_filters for replace
|
125
|
+
## Example:
|
126
|
+
## path => '/foo/bar'
|
127
|
+
## :path_filters = { /^\// => '', '/' => '.' }
|
128
|
+
## path => 'foo.bar'
|
129
|
+
@options[:path_filters].each { |key, val| path.gsub!(key, val) }
|
130
|
+
|
129
131
|
dot_keys = path.split('.')
|
130
132
|
dot_keys = dot_keys.drop_while do |dot_key|
|
131
133
|
## debug
|
@@ -142,23 +144,25 @@ private
|
|
142
144
|
# puts "-- Datagnan.find_value after drop_while ( var = #{var} )"
|
143
145
|
# puts "-- Datagnan.find_value after drop_while ( dot_keys = #{dot_keys} )"
|
144
146
|
# puts "-- Datagnan.find_value before eval ( dot_keys = #{dot_keys} )"
|
145
|
-
|
146
|
-
|
147
|
+
|
147
148
|
begin
|
148
149
|
## debug
|
150
|
+
# puts "-- Datagnan.find_value before eval var.instance_eval"
|
149
151
|
# puts "-- Datagnan.find_value before eval ( var.instance_eval(dot_keys.join('.')) = #{var.instance_eval(dot_keys.join('.'))} )"
|
150
152
|
var = var.instance_eval(dot_keys.join('.'))
|
151
153
|
rescue Exception => e
|
152
154
|
begin
|
153
155
|
## debug
|
154
|
-
# puts "-- Datagnan.find_value before eval
|
156
|
+
# puts "-- Datagnan.find_value before eval @scope.instance_eval('var.')"
|
157
|
+
# puts "-- Datagnan.find_value before eval ( @scope.instance_eval('var.'+dot_keys.join('.')) = #{@scope.instance_eval('var.'+dot_keys.join('.'))} )"
|
155
158
|
var = @scope.instance_eval('var.'+dot_keys.join('.'))
|
156
159
|
rescue Exception => e
|
157
160
|
begin
|
158
161
|
## debug
|
162
|
+
# puts "-- Datagnan.find_value before eval @scope.instance_eval"
|
159
163
|
# puts "-- Datagnan.find_value before eval ( @scope.instance_eval(dot_keys.join('.')) = #{@scope.instance_eval(dot_keys.join('.'))} )"
|
160
164
|
var = @scope.instance_eval(dot_keys.join('.'))
|
161
|
-
rescue
|
165
|
+
rescue Exception => e
|
162
166
|
var = nil
|
163
167
|
end
|
164
168
|
end
|
@@ -189,17 +193,43 @@ private
|
|
189
193
|
# puts "Datagnan.data_var end ( root = #{root} )"
|
190
194
|
end
|
191
195
|
|
196
|
+
## node attributes (DOMtempl-comaptible)
|
197
|
+
# @since 1.2.0
|
198
|
+
def data_attr(vars, root)
|
199
|
+
root.xpath("//@*[starts-with(name(), 'data-attr-')]").each do |attr|
|
200
|
+
node = attr.element
|
201
|
+
|
202
|
+
key = attr.name.sub!('data-attr-', '')
|
203
|
+
val = attr.value
|
204
|
+
|
205
|
+
var = find_value(val, vars)
|
206
|
+
|
207
|
+
unless var.nil?
|
208
|
+
node.attributes.insert(
|
209
|
+
node.attributes.index(attr),
|
210
|
+
Oga::XML::Attribute.new(
|
211
|
+
:name => key,
|
212
|
+
:value => var.to_s,
|
213
|
+
:element => node
|
214
|
+
)
|
215
|
+
)
|
216
|
+
end
|
217
|
+
node.unset(attr.name)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
192
221
|
## node attributes
|
193
222
|
def data_attrs(vars, root)
|
194
223
|
## debug
|
195
224
|
# puts "-- Datagnan.data_attrs ( vars = #{vars} )"
|
196
225
|
# puts "-- Datagnan.data_attrs ( root = #{root.to_xml} )"
|
197
|
-
# puts "-- Datagnan.data_attrs ( @attrs_sep = #{@attrs_sep} )"
|
226
|
+
# puts "-- Datagnan.data_attrs ( @options[:attrs_sep] = #{@options[:attrs_sep]} )"
|
198
227
|
root.css('*[data-attrs]').each do |node|
|
199
228
|
## debug
|
200
229
|
# puts "-- Datagnan.data_attrs each ( node = #{node} )"
|
201
230
|
# puts "-- Datagnan.data_attrs each ( node.to_xml = #{node.to_xml} )"
|
202
|
-
node.
|
231
|
+
node_data_attrs = node.attribute('data-attrs')
|
232
|
+
node_data_attrs.value.split(@options[:attrs_sep]).each do |attr|
|
203
233
|
key_val = attr.split(':')
|
204
234
|
key = key_val[0]
|
205
235
|
val = key_val.size.odd? ? key_val[0] : key_val[1]
|
@@ -212,15 +242,19 @@ private
|
|
212
242
|
|
213
243
|
unless var.nil?
|
214
244
|
node.attributes.insert(
|
215
|
-
node.attributes.index(
|
216
|
-
Oga::XML::Attribute.new(
|
245
|
+
node.attributes.index(node_data_attrs),
|
246
|
+
Oga::XML::Attribute.new(
|
247
|
+
:name => key,
|
248
|
+
:value => var.to_s,
|
249
|
+
:element => node
|
250
|
+
)
|
217
251
|
)
|
218
252
|
end
|
219
253
|
# puts "-- Datagnan.data_attrs each ( node.to_xml = #{node.to_xml} )"
|
220
254
|
## TODO: if attr not exist remember it (maybe return to model)
|
221
255
|
end
|
222
256
|
node.unset('data-attrs')
|
223
|
-
end
|
257
|
+
end
|
224
258
|
end
|
225
259
|
|
226
260
|
## fill template by array
|
@@ -306,24 +340,28 @@ end
|
|
306
340
|
# path to .html-template file
|
307
341
|
# @param [Hash] options
|
308
342
|
# options for parsing and filling the template
|
309
|
-
# @option options [Object] :scope (self) Scope for variables
|
310
|
-
# @option options [String] :views (scope.settings.views || "./views") Path to views directory
|
311
|
-
# @option options [Hash] :locals (locals || {}) Local variables for filling the template
|
312
343
|
# @param [Hash] locals
|
313
344
|
# local variables for filling the template
|
314
|
-
def datagnan(template_file, options = {},
|
345
|
+
def datagnan(template_file, options = {}, local_vars = {})
|
315
346
|
# beginning_time = Time.now
|
316
347
|
## default options
|
317
|
-
|
318
|
-
|
319
|
-
|
348
|
+
options[:scope] ||= self
|
349
|
+
options[:views_dir] = File.realpath(options[:views_dir] || (options[:scope].settings.views if options[:scope].settings) || "./views")
|
350
|
+
options[:layout_file] = File.join(options[:views_dir], options[:layout_file] || 'layout.html')
|
351
|
+
(options[:local_vars] ||= {}).merge! local_vars
|
352
|
+
|
353
|
+
## prepare template filename
|
354
|
+
template_file = File.join(options[:views_dir], template_file.to_s)
|
355
|
+
template_file += '.html' unless File.exist? template_file
|
320
356
|
|
321
|
-
|
357
|
+
## render template
|
358
|
+
html = Datagnan.read(template_file, options)
|
322
359
|
## debug
|
323
360
|
# puts "-- datagnan ( template = #{html} )"
|
324
|
-
|
325
|
-
|
326
|
-
|
361
|
+
## check for layout and wrap template if exist
|
362
|
+
if File.exist? options[:layout_file]
|
363
|
+
options[:local_vars]['template'] = { 'html' => html }
|
364
|
+
html = Datagnan.read(options[:layout_file], options)
|
327
365
|
## debug
|
328
366
|
# puts "-- datagnan ( layout = #{html} )"
|
329
367
|
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Popov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-05-
|
12
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oga
|