hotcell 0.1.0 → 0.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/README.md +102 -84
- data/Rakefile +2 -2
- data/ext/lexerc/lexerc.c +350 -308
- data/ext/lexerc/lexerc.h +2 -7
- data/ext/lexerc/lexerc.rl +9 -90
- data/lib/hotcell/commands/for.rb +7 -2
- data/lib/hotcell/config.rb +2 -1
- data/lib/hotcell/extensions.rb +13 -1
- data/lib/hotcell/lexer.rb +5 -10
- data/lib/hotcell/lexer.rl +95 -0
- data/lib/hotcell/lexerr.rb +256 -215
- data/lib/hotcell/lexerr.rl +7 -91
- data/lib/hotcell/manipulator.rb +20 -2
- data/lib/hotcell/node/{calculator.rb → expression.rb} +4 -2
- data/lib/hotcell/node/tag.rb +5 -3
- data/lib/hotcell/node.rb +1 -1
- data/lib/hotcell/parser.rb +565 -514
- data/lib/hotcell/parser.y +46 -24
- data/lib/hotcell/template.rb +3 -2
- data/lib/hotcell/version.rb +1 -1
- data/lib/hotcell.rb +1 -1
- data/spec/lib/hotcell/commands/for_spec.rb +3 -0
- data/spec/lib/hotcell/config_spec.rb +6 -0
- data/spec/lib/hotcell/lexer_spec.rb +28 -17
- data/spec/lib/hotcell/manipulator_spec.rb +16 -11
- data/spec/lib/hotcell/node/block_spec.rb +2 -2
- data/spec/lib/hotcell/parser_spec.rb +113 -30
- data/spec/lib/hotcell/template_spec.rb +51 -1
- data/spec/lib/hotcell_spec.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 891615844b788d5a97cd05732e62ccfea7fa9677
|
4
|
+
data.tar.gz: ce382e5b004e762cbb7bfecae1993cb29a62f408
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a23dfdd6e27f2d288d4ef3f8eab2b193a0ca72a6d7214bf3e3bd497587a4c22073906e79abdce32727188f31db5f5d71aa76435bc53bddaa12845cec202240b4
|
7
|
+
data.tar.gz: a677eb7af6252857c6e0210101676c93702e6b33a65174234d01b7e571dbf2700261bd0f49f035da806ee73bf9ff9c48d19243195489c6bbeb7675ec94f61373
|
data/README.md
CHANGED
@@ -26,6 +26,90 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
$ gem install hotcell
|
28
28
|
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
### Basic usage:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Hotcell::Template.parse('Hello, {{ name }}!').render name: 'Hulk'
|
35
|
+
```
|
36
|
+
|
37
|
+
### Additional `render` options:
|
38
|
+
|
39
|
+
* `:variables` - variables hash
|
40
|
+
* `:environment` - environment variables hash
|
41
|
+
* `:scope` - variables and environment variables together
|
42
|
+
|
43
|
+
The main difference between environment and ordinary variables is: ordinary variables
|
44
|
+
are accessible from the template and environment variables are not. Environment variables
|
45
|
+
are user for official purposes, in tag, for example. At the options level, variables have
|
46
|
+
string keys and environment variables have symbol keys.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Hotcell::Template.parse('Hello, {{ name }}!').render(
|
50
|
+
variables: { name: 'Hulk' },
|
51
|
+
environments: { some_access_token: '1234567890' },
|
52
|
+
scope: { 'foo' => 42, bar: 43 },
|
53
|
+
moo: 'Hello'
|
54
|
+
)
|
55
|
+
```
|
56
|
+
|
57
|
+
So if you will use something like above, all three options will be merged, but `:variables`
|
58
|
+
hash keys will be stringified, `:environment` hash keys will be symbolized, `:scope`
|
59
|
+
hash will be lived as is and the rest non-official options will be stringified and used as
|
60
|
+
variables. The result of this algorithm will be:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
{
|
64
|
+
'name' => 'Hulk', 'foo' => 42, 'moo' => 'Hello',
|
65
|
+
some_access_token: '1234567890', bar: 43
|
66
|
+
}
|
67
|
+
```
|
68
|
+
|
69
|
+
Remaining allowed options are:
|
70
|
+
|
71
|
+
* `:rescuer` - a lambda for error rescuing logic. The result of lambda call will be joined to
|
72
|
+
the template. The default lambda just returns error message to the template.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Hotcell::Template.parse('Hello, {{ name }}!').render(
|
76
|
+
name: 'Hulk',
|
77
|
+
rescuer: ->(e) { Rollbar.report_exception(e) }
|
78
|
+
)
|
79
|
+
```
|
80
|
+
|
81
|
+
* `:reraise` - raise exception after occurence or not. Error raises after `:rescuer` execution and
|
82
|
+
doesn't affect it. Accepts true or false.
|
83
|
+
* `:helpers` - array of modules with fuctions accessible from template. `Hotcell.helpers` config
|
84
|
+
option is used by default. Works similar to ActionController's helpers.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
Hotcell::Template.parse('Hello, {{ name }}!').render(
|
88
|
+
name: 'Hulk',
|
89
|
+
helpers: MyHelper # or array [MyHelper1, MyHelper2]
|
90
|
+
)
|
91
|
+
```
|
92
|
+
* `:shared` - just hash of shared variables, for internal usage
|
93
|
+
|
94
|
+
### Configuring Hotcell
|
95
|
+
|
96
|
+
Hotcell has several configuration methods, which provide default internals for
|
97
|
+
template processor proper work.
|
98
|
+
|
99
|
+
* `commands` accessor returns a hash of default commands
|
100
|
+
* `blocks` - same is for blocks
|
101
|
+
* `helpers` - default helper modules array
|
102
|
+
* `resolver` - default resolver instance for `include` command
|
103
|
+
* `escape_tags` - erb-like behavior. Escape tags, but not commands output.
|
104
|
+
False by default.
|
105
|
+
|
106
|
+
Also there are methods to setup configuration options:
|
107
|
+
|
108
|
+
* `register_command` adds command or block to the list of default commands or blocks
|
109
|
+
* `register_helpers` used for adding module to the list of helpers
|
110
|
+
* `resolver=` setups new default resolver (any Hotcell::Resolver-like class instance)
|
111
|
+
* `escape_tags=` sets tags scaping behavior (true or false)
|
112
|
+
|
29
113
|
## Language reference
|
30
114
|
|
31
115
|
Hotcell template consists of template parts and tags. Tags are enclosed in
|
@@ -43,6 +127,7 @@ Hotcell has several basic types:
|
|
43
127
|
Strings support escaping `{{ 'tlhab \'oS \'Iw HoHwI\' So\' batlh\' }}`.
|
44
128
|
Double-quoted strings also support escape sequences {{ "\n\r\s\t" }}
|
45
129
|
* Regular expressions `{{ /Foo/i }}`. Simple. Expression plus options (imx)
|
130
|
+
* Ranges `{{ 'a'..var_z }}` or `{{ 1...9 }}` for end value exclusion.
|
46
131
|
* Arrays `{{ [42, 'hello', /regex/m] }}`
|
47
132
|
* Hashes `{{ { a: 42, b: 'bar' } }}` has js-like syntax, so only string
|
48
133
|
can be a key for a hash
|
@@ -113,9 +198,24 @@ really flexible expressions syntax.
|
|
113
198
|
There is some tag modificators to set tag mode.
|
114
199
|
|
115
200
|
* `!` - silence modificator. Prevents tag value to be concatenated with
|
116
|
-
template, so `{{! 42 }}` will return
|
201
|
+
template, so `{{! 42 }}` will return `` - empty string. This modificator
|
117
202
|
is useful for pre-calculation: `{{! var = 'foo' * 3 }}`.
|
118
203
|
|
204
|
+
* `^` or `e` - html escape modificators. `{{^ '<article>' }}` will output
|
205
|
+
`<article>`, `{{e '<article>' }}` will do the same.
|
206
|
+
This also works with comants and blocks: `{{^ scope }}<article>{{ end }}`
|
207
|
+
will render `<article>`.
|
208
|
+
|
209
|
+
* `~` or `r` means raw output, opposite to escape. `{{~ '<article>' }}`
|
210
|
+
will produce `<article>`.
|
211
|
+
|
212
|
+
#### escape_tags config option
|
213
|
+
|
214
|
+
Expression tags are not escaped by default, but if you will perform
|
215
|
+
`Hotcell.escape_tags = true` - they will. But command and blocks still
|
216
|
+
will not. This means: `{{ '<article>' }}` will be rendered as `<article>`,
|
217
|
+
but `{{ scope }}<article>{{ end }}` will produce `<article>`.
|
218
|
+
|
119
219
|
### Comments
|
120
220
|
|
121
221
|
There is two comments types in Hotcell: in-tag line comment and block comment.
|
@@ -250,7 +350,7 @@ Case command. Like in most programming languages
|
|
250
350
|
##### For
|
251
351
|
|
252
352
|
Loop command, first argument - variable to put next value, `in` option
|
253
|
-
takes
|
353
|
+
takes any enumerable (i.e. array, range or hash).
|
254
354
|
|
255
355
|
```
|
256
356
|
{{ for post, in: posts }}
|
@@ -297,88 +397,6 @@ Or for template capturing:
|
|
297
397
|
{{ title }}
|
298
398
|
```
|
299
399
|
|
300
|
-
## Usage
|
301
|
-
|
302
|
-
### Basic usage:
|
303
|
-
|
304
|
-
```ruby
|
305
|
-
Hotcell::Template.parse('Hello, {{ name }}!').render name: 'Pyromaniac'
|
306
|
-
```
|
307
|
-
|
308
|
-
### Additional `render` options:
|
309
|
-
|
310
|
-
* `:variables` - variables hash
|
311
|
-
* `:environment` - environment variables hash
|
312
|
-
* `:scope` - variables and environment variables together
|
313
|
-
|
314
|
-
The main difference between environment and ordinary variables is: ordinary variables
|
315
|
-
are accessible from the template and environment variables are not. Environment variables
|
316
|
-
are user for official purposes, in tag, for example. At the options level, variables have
|
317
|
-
string keys and environment variables have symbol keys.
|
318
|
-
|
319
|
-
```ruby
|
320
|
-
Hotcell::Template.parse('Hello, {{ name }}!').render(
|
321
|
-
variables: { name: 'Pyromaniac' },
|
322
|
-
environments: { some_access_token: '1234567890' },
|
323
|
-
scope: { 'foo' => 42, bar: 43 },
|
324
|
-
moo: 'Hello'
|
325
|
-
)
|
326
|
-
```
|
327
|
-
|
328
|
-
So if you will use something like above, all three options will be merged, but `:variables`
|
329
|
-
hash keys will be stringified, `:environment` hash keys will be symbolized, `:scope`
|
330
|
-
hash will be lived as is and the rest non-official options will be stringified and used as
|
331
|
-
variables. The result of this algorithm will be:
|
332
|
-
|
333
|
-
```ruby
|
334
|
-
{
|
335
|
-
'name' => 'Pyromaniac', 'foo' => 42, 'moo' => 'Hello',
|
336
|
-
some_access_token: '1234567890', bar: 43
|
337
|
-
}
|
338
|
-
```
|
339
|
-
|
340
|
-
Remaining allowed options are:
|
341
|
-
|
342
|
-
* `:rescuer` - a lambda for error rescuing logic. The result of lambda call will be joined to
|
343
|
-
the template. The default lambda just returns error message to the template.
|
344
|
-
|
345
|
-
```ruby
|
346
|
-
Hotcell::Template.parse('Hello, {{ name }}!').render(
|
347
|
-
name: 'Pyromaniac',
|
348
|
-
rescuer: ->(e) { Rollbar.report_exception(e) }
|
349
|
-
)
|
350
|
-
```
|
351
|
-
|
352
|
-
* `:reraise` - raise exception after occurence or not. Error raises after `:rescuer` execution and
|
353
|
-
doesn't affect it. Accepts true or false.
|
354
|
-
* `:helpers` - array of modules with fuctions accessible from template. `Hotcell.helpers` config
|
355
|
-
option is used by default. Works similar to ActionController's helpers.
|
356
|
-
|
357
|
-
```ruby
|
358
|
-
Hotcell::Template.parse('Hello, {{ name }}!').render(
|
359
|
-
name: 'Pyromaniac',
|
360
|
-
helpers: MyHelper # or array [MyHelper1, MyHelper2]
|
361
|
-
)
|
362
|
-
```
|
363
|
-
* `:shared` - just hash of shared variables, for internal usage
|
364
|
-
|
365
|
-
### Configuring Hotcell
|
366
|
-
|
367
|
-
Hotcell has several configuration methods, which provide default internals for
|
368
|
-
template processor proper work.
|
369
|
-
|
370
|
-
* `commands` accessor returns a hash of default commands
|
371
|
-
* `blocks` - same is for blocks
|
372
|
-
* `helpers` - default helper modules array
|
373
|
-
* `resolver` - default resolver for `include` command
|
374
|
-
|
375
|
-
Also there are methods to setup configuration options:
|
376
|
-
|
377
|
-
* `register_command` adds command or block to the list of default commands or blocks
|
378
|
-
* `register_helpers` used for adding module to the list of helpers
|
379
|
-
* `resolver=` setups new default resolver
|
380
|
-
|
381
|
-
|
382
400
|
## Contributing
|
383
401
|
|
384
402
|
1. Fork it
|
data/Rakefile
CHANGED
@@ -19,12 +19,12 @@ end
|
|
19
19
|
namespace :project do
|
20
20
|
desc 'Build lexer'
|
21
21
|
task :lexerr do
|
22
|
-
`ragel -R -F1 lib/hotcell/lexerr.rl`
|
22
|
+
`ragel -R -F1 -I lib/hotcell lib/hotcell/lexerr.rl`
|
23
23
|
end
|
24
24
|
|
25
25
|
desc 'Build lexer'
|
26
26
|
task :lexerc do
|
27
|
-
`ragel -C -G2 ext/lexerc/lexerc.rl`
|
27
|
+
`ragel -C -G2 -I lib/hotcell ext/lexerc/lexerc.rl`
|
28
28
|
end
|
29
29
|
|
30
30
|
task :dot do
|