datagnan 0.1.0

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 +7 -0
  2. data/lib/datagnan.rb +163 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 907b0658f4dffc41593fd94d1cf17ffb3f99026d
4
+ data.tar.gz: 3711e0c9bd3254afdd202322f8f62b64a1fffcfb
5
+ SHA512:
6
+ metadata.gz: 161992397a49b10deb721163654410eac37f99b949a55988aa52e7d4c4ea9c04e1579bc612234bf56710a4970cd21ea4d51a2d11d0934bec533d0fc1c3f2749c
7
+ data.tar.gz: 63d0e57eb1c5a1e6284644de76f8760233b1c533dec272e61c254d0b819782a33a980908cb2e4d74afd0074806ecde3c6f0cce6a809a719fb12927cef4794480
data/lib/datagnan.rb ADDED
@@ -0,0 +1,163 @@
1
+ ## Nokogiri for HTML parsing
2
+ require 'nokogiri'
3
+
4
+ class Datagnan
5
+
6
+ attr_accessor :doc, :vars
7
+
8
+ ## Constructor
9
+ def initialize(file_path, options = {}, locals = {})
10
+ ## TODO: return fragment, not full html with auto-tags
11
+ @doc = Nokogiri::HTML(File.read(file_path))
12
+ puts @doc.errors if (@doc.errors.any?)
13
+ ## debug
14
+ # puts "-- @doc = #{@doc}"
15
+ @attrs_sep = options.delete(:attrs_sep) || ";"
16
+ @scope = options.delete(:scope) || self
17
+ @vars = options.delete(:locals) || locals || {}
18
+ @vars.merge! Hash[@scope.instance_variables.map { |name| [name.to_s, @scope.instance_variable_get(name)] } ] unless @scope.nil?
19
+ ## debug
20
+ # puts "-- Datagnan.new ( @vars = #{@vars} )"
21
+ end
22
+
23
+ ## aka File.open
24
+ def self.open(file_path, options = {}, locals = {})
25
+ obj = Datagnan.new(file_path, options, locals).write
26
+ yield obj if block_given?
27
+ return obj
28
+ end
29
+
30
+ ## aka File.read(file_path)
31
+ def self.read(file_path, options = {}, locals = {})
32
+ Datagnan.new(file_path, options, locals).write.read
33
+ end
34
+
35
+ ## aka File.read
36
+ def read
37
+ ## debug
38
+ # puts "-- Datagnan.read ( @doc = #{@doc} )"
39
+ @doc.to_html
40
+ end
41
+
42
+ ## Parse and replace
43
+ def write(vars = {}, root = nil)
44
+ ## debug
45
+ # puts "Datagnan.write ( vars = #{vars} )"
46
+ # puts "Datagnan.write ( @doc = #{@doc} )"
47
+ ## use global if empty
48
+ vars = @vars if vars.is_a?(Hash) && vars.empty?
49
+ root = @doc if root.nil?
50
+ ## debug
51
+ # puts "-- Datagnan.write ( vars = #{vars} )"
52
+ # puts "-- Datagnan.write ( root = #{root} )"
53
+ ## replace data-each
54
+ data_each(vars, root)
55
+ ## debug
56
+ # puts "Datagnan.write after each ( vars = #{vars} )"
57
+ # puts "Datagnan.write after each ( root = #{root} )"
58
+ ## replace data-var
59
+ data_vars(vars, root)
60
+ ## debug
61
+ # puts "Datagnan.write after vars ( root = #{root} )"
62
+ ## replace data-attrs
63
+ data_attrs(vars, root)
64
+ ## result
65
+ return self
66
+ end
67
+
68
+ private
69
+
70
+ ## node content
71
+ def data_vars(vars, root)
72
+ ## debug
73
+ # puts "Datagnan.data_vars ( vars = #{vars} )"
74
+ # puts "Datagnan.data_vars ( root = #{root} )"
75
+ root.css('*[data-var]').each do |node|
76
+ ## recursive access to property
77
+ ## debug
78
+ # puts "-- Datagnan.data_vars each ( vars = #{vars} )"
79
+ # puts "-- Datagnan.data_vars each ( node = #{node} )"
80
+ var = vars
81
+ node['data-var'].split('.').each do |key|
82
+ ## var exist
83
+ var = ( var.is_a?(Hash) ? var[key] : (var.respond_to?(key) ? var.send(key) : nil ) )
84
+ end
85
+ # puts "-- Datagnan.data_vars each ( var = #{var} )"
86
+
87
+ unless var.nil?
88
+ node.inner_html = var.to_s
89
+ node.remove_attribute('data-var')
90
+ end
91
+ ## TODO: if var not exist remember it (maybe return to model)
92
+ end
93
+ ## debug
94
+ # puts "Datagnan.data_vars end ( root = #{root} )"
95
+ end
96
+
97
+ ## node attributes
98
+ def data_attrs(vars, root)
99
+ ## debug
100
+ # puts "-- Datagnan.data_attrs ( vars = #{vars} )"
101
+ # puts "-- Datagnan.data_attrs ( root = #{root} )"
102
+ # puts "-- Datagnan.data_attrs ( @attrs_sep = #{@attrs_sep} )"
103
+ root.css('*[data-attrs]').each do |node|
104
+ ## debug
105
+ # puts "-- Datagnan.data_attrs each ( node = #{node} )"
106
+ node['data-attrs'].split(@attrs_sep).each do |attr|
107
+ key_val = attr.split(':')
108
+ key = key_val[0]
109
+ val = key_val.size.odd? ? key_val[0] : key_val[1]
110
+ ## debug
111
+ # puts "-- Datagnan.data_attrs each attr ( key = #{key}, val = #{val} )"
112
+ var = vars
113
+ val.split('.').each do |val_part|
114
+ ## var exist
115
+ var = ( var.is_a?(Hash) ? var[val_part] : (var.respond_to?(val_part) ? var.send(val_part) : nil ) )
116
+ end
117
+ unless var.nil?
118
+ node[key] = var
119
+ end
120
+ ## TODO: if attr not exist remember it (maybe return to model)
121
+ end
122
+ node.remove_attribute('data-attrs')
123
+ end
124
+ end
125
+
126
+ ## fill template by array
127
+ def data_each(vars, root)
128
+ ## debug
129
+ # puts "Datagnan.data_each ( vars = #{vars}, root.tag = #{root.name} )"
130
+ root.css('*[data-each]').each do |node|
131
+ ## var exist
132
+ if vars.has_key? node['data-each']
133
+ vars[node['data-each']].each do |item|
134
+ node_clone = node.clone
135
+ write(item, node_clone)
136
+ node_clone.remove_attribute('data-each')
137
+ node.parent << node_clone
138
+ end
139
+ node.remove
140
+ end
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ def datagnan(template_file, options = {}, locals = {})
147
+ ## default options
148
+ views = options.delete(:views) || "./views"
149
+ scope = options.delete(:scope) || self
150
+ locals = options.delete(:locals) || locals || {}
151
+
152
+ html = Datagnan.read(views+'/'+template_file+'.html', :scope => scope, :locals => locals)
153
+ ## debug
154
+ # puts "-- datagnan ( template = #{html} )"
155
+ if File.exist?(views+'/layout.html')
156
+ locals['template'] = {'html' => html}
157
+ html = Datagnan.read(views+'/layout.html', :scope => scope, :locals => locals)
158
+ ## debug
159
+ # puts "-- datagnan ( layout = #{html} )"
160
+ end
161
+
162
+ return html
163
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datagnan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ - driedfruit
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-04-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Make HTML-templates without strange tags.
15
+ email: alex.wayfer@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/datagnan.rb
21
+ homepage: https://bitbucket.org/AlexWayfer/datagnan
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: HTML DOM-based template engine
45
+ test_files: []