tzispa_rig 0.2.6 → 0.2.7
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/CHANGELOG.md +3 -0
- data/README.md +75 -16
- data/lib/tzispa/rig/binder.rb +1 -1
- data/lib/tzispa/rig/parsernext.rb +26 -22
- data/lib/tzispa/rig/syntax.rb +27 -0
- data/lib/tzispa/rig/template.rb +40 -22
- data/lib/tzispa/rig/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1aa4a9522b55f88d715802bab683693ae3ac267a
|
4
|
+
data.tar.gz: f1c4923b96b5e45a5198c4274386062d03c9dace
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 776cfe3e0b6655dc2f0133c65b10b0914e1d4f94397a83862f34aa4c7c64cba9548c752586a3cf9735142074c1186b5d1c3df60800782c2a3b3ec42633bd9695
|
7
|
+
data.tar.gz: 88e84342bf54817050b45a06fe4226d2179ca5fee0ee5062ee6bac73aecf96d5410e2b6c73c0f45344cbd73770a1f4bff9d491cc7cd5d0f89c4b27d5ae18e11a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
A general purpose template engine
|
4
4
|
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```shell
|
8
|
+
% gem install tzispa
|
9
|
+
```
|
10
|
+
|
5
11
|
## Rig template types
|
6
12
|
|
7
13
|
There are 3 template types: layout, static and block:
|
@@ -31,8 +37,8 @@ And metavariables with:
|
|
31
37
|
```
|
32
38
|
{%name%}
|
33
39
|
```
|
34
|
-
metavariables are used to make runtime template tags replacements
|
35
40
|
|
41
|
+
metavariables are used to make runtime template tags replacements
|
36
42
|
```html
|
37
43
|
<fieldset>
|
38
44
|
<div class='row'>
|
@@ -42,15 +48,13 @@ metavariables are used to make runtime template tags replacements
|
|
42
48
|
</div>
|
43
49
|
<div class='column column-20'>
|
44
50
|
<label for='skey'>Código</label>
|
45
|
-
<input type='text' name='skey' id='skey' maxlength="16" value='
|
51
|
+
<input type='text' name='skey' id='skey' maxlength="16" value='<var:skey/>'/>
|
46
52
|
</div>
|
53
|
+
<a href='<purl:site[layout=brand_edit,title=edit-brand,id0={%idb%}]/>'><i class='fa fa-edit'></i></a>
|
47
54
|
</div>
|
48
|
-
....
|
49
|
-
|
50
55
|
```
|
51
56
|
|
52
|
-
|
53
|
-
|
57
|
+
in the template binder
|
54
58
|
```ruby
|
55
59
|
def bind!
|
56
60
|
@idb = context.router_params[:id0]
|
@@ -67,18 +71,21 @@ def load_brand
|
|
67
71
|
data(
|
68
72
|
idb: brand.id,
|
69
73
|
name: brand.name,
|
70
|
-
skey: brand.skey
|
71
|
-
long_name: brand.long_name,
|
72
|
-
web: brand.web,
|
73
|
-
manual_order: brand.manual_order,
|
74
|
-
notes: brand.notes
|
74
|
+
skey: brand.skey
|
75
75
|
)
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
79
79
|
### Conditionals
|
80
80
|
|
81
|
-
You can make decisions in your templates using
|
81
|
+
You can make decisions in your templates using template ife tag:
|
82
|
+
```
|
83
|
+
<ife:test> ..... <else:test/> .... </ife:test>
|
84
|
+
|
85
|
+
or without the else part
|
86
|
+
|
87
|
+
<ife:test> ..... </ife:test>
|
88
|
+
```
|
82
89
|
|
83
90
|
```html
|
84
91
|
<ife:customer_exist>
|
@@ -98,19 +105,23 @@ You can make decisions in your templates using conditionals:
|
|
98
105
|
```
|
99
106
|
|
100
107
|
In the binder you must define customer_exist
|
101
|
-
|
102
108
|
```ruby
|
109
|
+
|
103
110
|
def bind!
|
104
111
|
idc = context.router_params[:id0]
|
105
112
|
context.repository.use :ecomm_shop
|
106
113
|
customer = context.repository[:customer][idb]
|
107
114
|
data.customer_exist = !customer.nil?
|
108
115
|
end
|
116
|
+
|
109
117
|
```
|
110
118
|
|
111
119
|
## Repeating
|
112
120
|
|
113
|
-
To repeat a part in the template
|
121
|
+
To repeat a part in the template use loop tag
|
122
|
+
```
|
123
|
+
<loop:ltag> ... </loop:ltag>
|
124
|
+
```
|
114
125
|
|
115
126
|
```html
|
116
127
|
<loop:lbrands>
|
@@ -127,7 +138,6 @@ To repeat a part in the template
|
|
127
138
|
```
|
128
139
|
|
129
140
|
In the binder you must use the 'loop_binder' method
|
130
|
-
|
131
141
|
```ruby
|
132
142
|
|
133
143
|
def bind!
|
@@ -150,7 +160,7 @@ def load_brands
|
|
150
160
|
end
|
151
161
|
```
|
152
162
|
|
153
|
-
##
|
163
|
+
## Template URLs
|
154
164
|
|
155
165
|
Rig templates can build urls for you. There are 2 url types:
|
156
166
|
|
@@ -162,6 +172,7 @@ Site urls: used to provide links to site pages
|
|
162
172
|
<purl:route_id/>
|
163
173
|
<purl:route_id[param1=value,param2=value]/>
|
164
174
|
```
|
175
|
+
|
165
176
|
```html
|
166
177
|
<purl:site[layout=brands,title=brand-list]/>
|
167
178
|
<purl:index/>
|
@@ -176,8 +187,56 @@ Api urls: used to provide urls to the application Api
|
|
176
187
|
<api:handler:verb/>
|
177
188
|
<api:handler:verb:predicate/>
|
178
189
|
```
|
190
|
+
|
179
191
|
```html
|
180
192
|
<api:customer:add:address/>
|
181
193
|
|
182
194
|
<api:brand:{%verb%}/>
|
183
195
|
```
|
196
|
+
|
197
|
+
## Building templates
|
198
|
+
|
199
|
+
You can include block and static rig templates using these tags:
|
200
|
+
```
|
201
|
+
<blk:name[param1=value,param2=value, ... ]/>
|
202
|
+
|
203
|
+
<static:name/>
|
204
|
+
```
|
205
|
+
|
206
|
+
As you can see, template parameters can be passed in the block tag.
|
207
|
+
These parameters will be available in the binder.
|
208
|
+
You can also use template subdomains using dot notation in the name
|
209
|
+
```html
|
210
|
+
<!DOCTYPE html>
|
211
|
+
<html lang="es">
|
212
|
+
<head>
|
213
|
+
<meta charset="utf-8" />
|
214
|
+
<blk:metasense/>
|
215
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
216
|
+
<static:comassert/>
|
217
|
+
</head>
|
218
|
+
<body>
|
219
|
+
<blk:header/>
|
220
|
+
<div class="container m-bot-35 m-top-25 clearfix">
|
221
|
+
<div class="row">
|
222
|
+
<div class="three columns m-bot-25">
|
223
|
+
<blk:sidebar/>
|
224
|
+
</div>
|
225
|
+
<div class="nine columns m-bot-25">
|
226
|
+
<blk:folder.edit[doc=dokum]/>
|
227
|
+
</div>
|
228
|
+
</div>
|
229
|
+
</div>
|
230
|
+
<static:footer/>
|
231
|
+
<static:footscripts/>
|
232
|
+
</body>
|
233
|
+
</html>
|
234
|
+
```
|
235
|
+
|
236
|
+
In the folder binder you can access template parameters
|
237
|
+
```ruby
|
238
|
+
def bind!
|
239
|
+
context.repository.use :quality_dok
|
240
|
+
@doctype = params[:doc]
|
241
|
+
end
|
242
|
+
```
|
data/lib/tzispa/rig/binder.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
4
|
require 'tzispa/utils/string'
|
5
|
+
require_relative 'syntax'
|
5
6
|
|
6
7
|
|
7
8
|
module Tzispa
|
@@ -27,8 +28,10 @@ module Tzispa
|
|
27
28
|
ParsedMeta.new parser, type, match[1]
|
28
29
|
when :var
|
29
30
|
ParsedVar.new parser, type, match[1], match[2]
|
30
|
-
when :
|
31
|
+
when :url
|
31
32
|
ParsedUrl.new parser, type, match[1], match[3]
|
33
|
+
when :purl
|
34
|
+
ParsedPurl.new parser, type, match[1], match[3]
|
32
35
|
when :api
|
33
36
|
ParsedApi.new parser, type, match[1], match[2], match[3], match[4]
|
34
37
|
when :loop
|
@@ -100,7 +103,7 @@ module Tzispa
|
|
100
103
|
end
|
101
104
|
|
102
105
|
|
103
|
-
class
|
106
|
+
class ParsedPurl < ParsedEntity
|
104
107
|
|
105
108
|
def initialize(parser, type, path_id, params)
|
106
109
|
super(parser, type)
|
@@ -118,6 +121,25 @@ module Tzispa
|
|
118
121
|
end
|
119
122
|
|
120
123
|
|
124
|
+
class ParsedUrl < ParsedEntity
|
125
|
+
|
126
|
+
def initialize(parser, type, path_id, params)
|
127
|
+
super(parser, type)
|
128
|
+
@path_id = path_id.to_sym
|
129
|
+
@params = params
|
130
|
+
end
|
131
|
+
|
132
|
+
def render(binder)
|
133
|
+
b_params = @params.dup.gsub(RE_ANCHOR) { |match|
|
134
|
+
parser.the_parsed.select { |p| p.anchor == match}.first.render(binder)
|
135
|
+
} if @params
|
136
|
+
binder.context.canonical_url @path_id, Parameters.new(b_params).data
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
|
121
143
|
class ParsedApi < ParsedEntity
|
122
144
|
|
123
145
|
attr_reader :handler, :verb, :predicate
|
@@ -313,24 +335,7 @@ module Tzispa
|
|
313
335
|
|
314
336
|
EMPTY_STRING = ''
|
315
337
|
|
316
|
-
|
317
|
-
:flags => /<flags:(\[(\w+=[^,\]]+(,\w+=[^,\]]+)*?)\])\/>/
|
318
|
-
}.freeze
|
319
|
-
|
320
|
-
RIG_EXPRESSIONS = {
|
321
|
-
:meta => /\{%([^%]+?)%\}/.freeze,
|
322
|
-
:var => /<var(\[%[A-Z]?[0-9]*[a-z]\])?:(\w+)\/>/,
|
323
|
-
:purl => /<purl:(\w+(?:\.\w+)?)(\[(\w+=[^,\]]+(,\w+=[^,\]]+)*?)\])?\/>/,
|
324
|
-
:api => /<api:([^:\.]+(?:\.[^:]+)?):([^:\/]+)(?::([^:\/]+))?(?::([^\/]+))?\/>/
|
325
|
-
}.freeze
|
326
|
-
|
327
|
-
RIG_STATEMENTS = /(<(loop):(\w+)>(.*?)<\/loop:\3>)|(<(ife):(\w+)>(.*?)(<else:\7\/>(.*?))?<\/ife:\7>)/m
|
328
|
-
|
329
|
-
RIG_TEMPLATES = {
|
330
|
-
:blk => /<(blk):(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?\/>/,
|
331
|
-
:iblk => /<(iblk):(\w+):(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?:(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?\/>/,
|
332
|
-
:static => /<(static):(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?\/>/
|
333
|
-
}.freeze
|
338
|
+
include Tzispa::Rig::Syntax
|
334
339
|
|
335
340
|
attr_reader :flags, :template, :the_parsed
|
336
341
|
|
@@ -343,7 +348,7 @@ module Tzispa
|
|
343
348
|
def parse!
|
344
349
|
@tags = nil
|
345
350
|
parse_flags
|
346
|
-
if @template.
|
351
|
+
if @template.bindable?
|
347
352
|
parse_statements
|
348
353
|
parse_expressions
|
349
354
|
end
|
@@ -394,7 +399,6 @@ module Tzispa
|
|
394
399
|
|
395
400
|
def parse_statements
|
396
401
|
@inner_text.gsub!(RIG_STATEMENTS) { |match|
|
397
|
-
#puts Regexp.last_match.inspect
|
398
402
|
type = (Regexp.last_match[2] || String.new) << (Regexp.last_match[6] || String.new)
|
399
403
|
pe = ParsedEntity.instance(self, type.to_sym, Regexp.last_match )
|
400
404
|
@the_parsed << pe.parse!
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Tzispa
|
2
|
+
module Rig
|
3
|
+
module Syntax
|
4
|
+
|
5
|
+
RIG_EMPTY = {
|
6
|
+
:flags => /<flags:(\[(\w+=[^,\]]+(,\w+=[^,\]]+)*?)\])\/>/
|
7
|
+
}.freeze
|
8
|
+
|
9
|
+
RIG_EXPRESSIONS = {
|
10
|
+
:meta => /\{%([^%]+?)%\}/,
|
11
|
+
:var => /<var(\[%[A-Z]?[0-9]*[a-z]\])?:(\w+)\/>/,
|
12
|
+
:purl => /<purl:(\w+(?:\.\w+)?)(\[(\w+=[^,\]]+(,\w+=[^,\]]+)*?)\])?\/>/,
|
13
|
+
:url => /<url:(\w+(?:\.\w+)?)(\[(\w+=[^,\]]+(,\w+=[^,\]]+)*?)\])?\/>/,
|
14
|
+
:api => /<api:([^:\.]+(?:\.[^:]+)?):([^:\/]+)(?::([^:\/]+))?(?::([^\/]+))?\/>/
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
RIG_STATEMENTS = /(<(loop):(\w+)>(.*?)<\/loop:\3>)|(<(ife):(\w+)>(.*?)(<else:\7\/>(.*?))?<\/ife:\7>)/m
|
18
|
+
|
19
|
+
RIG_TEMPLATES = {
|
20
|
+
:blk => /<(blk):(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?\/>/,
|
21
|
+
:iblk => /<(iblk):(\w+):(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?:(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?\/>/,
|
22
|
+
:static => /<(static):(\w+(?:\.\w+)?)(?:\[(\w+=[^,\]]+(?:,\w+=[^,\]]+)*)\])?\/>/
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/tzispa/rig/template.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'forwardable'
|
4
4
|
require 'fileutils'
|
5
5
|
require 'tzispa/utils/string'
|
6
|
+
require 'tzispa/utils/indenter'
|
7
|
+
require 'tzispa/utils/cache'
|
6
8
|
require 'tzispa/rig'
|
7
9
|
|
8
10
|
module Tzispa
|
@@ -26,7 +28,7 @@ module Tzispa
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def modified?
|
29
|
-
@modified != File.mtime(@file)
|
31
|
+
@modified != ::File.mtime(@file)
|
30
32
|
end
|
31
33
|
|
32
34
|
def exist?
|
@@ -110,18 +112,22 @@ module Tzispa
|
|
110
112
|
create_binder
|
111
113
|
end
|
112
114
|
|
113
|
-
def
|
115
|
+
def block?
|
114
116
|
@type == :block
|
115
117
|
end
|
116
118
|
|
117
|
-
def
|
119
|
+
def layout?
|
118
120
|
@type == :layout
|
119
121
|
end
|
120
122
|
|
121
|
-
def
|
123
|
+
def static?
|
122
124
|
@type == :static
|
123
125
|
end
|
124
126
|
|
127
|
+
def bindable?
|
128
|
+
block? || layout?
|
129
|
+
end
|
130
|
+
|
125
131
|
def params=(value)
|
126
132
|
@params = Parameters.new(value)
|
127
133
|
end
|
@@ -156,21 +162,27 @@ module Tzispa
|
|
156
162
|
|
157
163
|
def create_binder
|
158
164
|
::File.open("#{domain.path}/#{binder_require}.rb", "w") { |f|
|
159
|
-
|
160
|
-
|
165
|
+
f.puts new_binder_code
|
166
|
+
} if @type == :block
|
167
|
+
end
|
168
|
+
|
169
|
+
def new_binder_code
|
170
|
+
Tzispa::Utils::Indenter.new(2).tap { |binder_code|
|
171
|
+
binder_code << "require 'tzispa/rig/binder'\n\n"
|
161
172
|
level = 0
|
162
173
|
binder_namespace.split('::').each { |ns|
|
163
|
-
|
174
|
+
binder_code.indent if level > 0
|
175
|
+
binder_code << "module #{ns}\n"
|
164
176
|
level += 1
|
165
177
|
}
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
178
|
+
binder_code.indent << "\nclass #{binder_class_name} < Tzispa::Rig::TemplateBinder\n\n"
|
179
|
+
binder_code.indent << "def bind!\n"
|
180
|
+
binder_code << "end\n\n"
|
181
|
+
binder_code.unindent << "end\n"
|
170
182
|
binder_namespace.split('::').each { |ns|
|
171
|
-
|
183
|
+
binder_code.unindent << "end\n"
|
172
184
|
}
|
173
|
-
}
|
185
|
+
}.to_s
|
174
186
|
end
|
175
187
|
|
176
188
|
end
|
@@ -180,9 +192,9 @@ module Tzispa
|
|
180
192
|
|
181
193
|
attr_reader :app
|
182
194
|
|
183
|
-
def initialize(app)
|
195
|
+
def initialize(app, cache_enabled, cache_size)
|
184
196
|
@app = app
|
185
|
-
@
|
197
|
+
@cache = Tzispa::Utils::LRUCache.new(cache_size) if cache_enabled
|
186
198
|
@mutex = Mutex.new
|
187
199
|
end
|
188
200
|
|
@@ -199,14 +211,20 @@ module Tzispa
|
|
199
211
|
end
|
200
212
|
|
201
213
|
def rig_template(name, type, format, params, parent)
|
202
|
-
if @
|
203
|
-
|
204
|
-
|
214
|
+
if @cache
|
215
|
+
ktpl = "#{type}__#{name}".to_sym
|
216
|
+
if @mutex.owned?
|
217
|
+
tpl = @cache[ktpl] || Template.new(name: name, type: type, format: format, domain: @app.domain, params: params, parent: parent, engine: self)
|
218
|
+
@cache[ktpl] = tpl.loaded? && !tpl.modified? ? tpl : tpl.load!.parse!
|
219
|
+
else
|
220
|
+
@mutex.synchronize {
|
221
|
+
tpl = @cache[ktpl] || Template.new(name: name, type: type, format: format, domain: @app.domain, params: params, parent: parent, engine: self)
|
222
|
+
@cache[ktpl] = tpl.loaded? && !tpl.modified? ? tpl : tpl.load!.parse!
|
223
|
+
}
|
224
|
+
end
|
205
225
|
else
|
206
|
-
@
|
207
|
-
|
208
|
-
tpl.loaded? && !tpl.modified? ? tpl : tpl.load!.parse!
|
209
|
-
}
|
226
|
+
tpl = Template.new(name: name, type: type, format: format, domain: @app.domain, params: params, parent: parent, engine: self)
|
227
|
+
tpl.load!.parse!
|
210
228
|
end
|
211
229
|
end
|
212
230
|
|
data/lib/tzispa/rig/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tzispa_rig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Antonio Piñero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tzispa_helpers
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/tzispa/rig/parameters.rb
|
53
53
|
- lib/tzispa/rig/parser.rb
|
54
54
|
- lib/tzispa/rig/parsernext.rb
|
55
|
+
- lib/tzispa/rig/syntax.rb
|
55
56
|
- lib/tzispa/rig/template.rb
|
56
57
|
- lib/tzispa/rig/version.rb
|
57
58
|
- lib/tzispa_rig.rb
|