liquid-arrays 1.0.0 → 1.1.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/CHANGELOG.md +13 -0
- data/README.md +78 -13
- data/lib/liquid-arrays/array_add_tag.rb +11 -9
- data/lib/liquid-arrays/array_block.rb +12 -5
- data/lib/liquid-arrays/array_create_tag.rb +23 -0
- data/lib/liquid-arrays/array_delete_tag.rb +19 -13
- data/lib/liquid-arrays/array_helper.rb +18 -0
- data/lib/liquid-arrays/array_insert_tag.rb +11 -7
- data/lib/liquid-arrays/array_replace_tag.rb +12 -8
- data/lib/liquid-arrays/array_variable.rb +12 -0
- data/lib/liquid-arrays/attribute_parser.rb +55 -23
- data/lib/liquid-arrays/error_handler.rb +23 -0
- data/lib/liquid-arrays/hash_block.rb +5 -2
- data/lib/liquid-arrays/hash_create_tag.rb +23 -0
- data/lib/liquid-arrays/hash_delete_tag.rb +10 -6
- data/lib/liquid-arrays/hash_helper.rb +18 -0
- data/lib/liquid-arrays/hash_set_tag.rb +11 -7
- data/lib/liquid-arrays/hash_variable.rb +11 -0
- data/lib/liquid-arrays/version.rb +1 -1
- data/lib/liquid-arrays.rb +7 -2
- metadata +10 -4
- data/lib/liquid-arrays/array_tag.rb +0 -22
- data/lib/liquid-arrays/hash_tag.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c728ff12a57cc91aae46e78cf74b5b3953beb670b55f33f65729c1b2d32eeeb8
|
4
|
+
data.tar.gz: 58979b0fa0b641ceb3e4358478de213ba29b331c90c05ac19df5aa9b9bcafa79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62e0a5c5d1dce765b25d5b446b3aafbbeeb4d4f71937f1f4948fb738fdf0b9f6ace6d3de0f7b75f60b3ca649ee0f384a5f231cfa37a62bef774052c343b72f61
|
7
|
+
data.tar.gz: 4264d2f758a4b1f978fe8e37582b5a584652997b98f21ad9d7ac9ac855e8fdfed9eb90414cdf94c3311f87c1b691efce7bc73121f6d153946d2fa780f34b8c01
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
## 1.1.0
|
2
|
+
|
3
|
+
### Features
|
4
|
+
|
5
|
+
* Added `array_create` and `hash_create` tags
|
6
|
+
* Arrays and hashes can now be defined inline to be used as attributes
|
7
|
+
* Improved error handling to now use Liquid's `error_mode`
|
8
|
+
|
9
|
+
### Bug Fixes
|
10
|
+
|
11
|
+
* Arrays and hashes will be created in the proper scope instead of the outer-most scope
|
12
|
+
* Fixed issue when using default attributes that use special characters, like array elements
|
13
|
+
* Added check to ensure a variable is an array or hash before modifying it to avoid errors
|
data/README.md
CHANGED
@@ -24,6 +24,12 @@ Or install it yourself as:
|
|
24
24
|
Ensure any other requirements the software you're using might have for loading
|
25
25
|
plugins are met.
|
26
26
|
|
27
|
+
## Error Handling
|
28
|
+
|
29
|
+
Error handling uses Liquid's error modes to determine how to handle errors.
|
30
|
+
Please see [Liquid - Error Modes](https://github.com/Shopify/liquid#error-modes)
|
31
|
+
for more information.
|
32
|
+
|
27
33
|
## Usage
|
28
34
|
|
29
35
|
There are several tags that will modify arrays and hashes in various ways.
|
@@ -41,6 +47,42 @@ attribute needs specified, like so:
|
|
41
47
|
{% tag_name value %}
|
42
48
|
```
|
43
49
|
|
50
|
+
### Types
|
51
|
+
|
52
|
+
Aside from the standard Liquid types, inline arrays and hashes may also be
|
53
|
+
defined and used for appropriate attributes. The syntax for each is as follows:
|
54
|
+
|
55
|
+
```liquid
|
56
|
+
{% tag_name array:value1,value2,value3 %}
|
57
|
+
{% tag_name hash:key1>value1,key2>value2 %}
|
58
|
+
```
|
59
|
+
|
60
|
+
**Note**: If an array is defined with a single variable and that variable contains
|
61
|
+
an array, that array will be used instead of being the first element.
|
62
|
+
|
63
|
+
### array_create tag
|
64
|
+
|
65
|
+
Creates an array with opinional initial items. If the variable is already
|
66
|
+
defined it will reassign it.
|
67
|
+
|
68
|
+
| Attribute | Required | Type | Description |
|
69
|
+
| --------- | ------------- | -------- | ------------------- |
|
70
|
+
| array | Yes (default) | Variable | The array to create |
|
71
|
+
| items | No | Array | The initial items |
|
72
|
+
|
73
|
+
|
74
|
+
#### Example
|
75
|
+
|
76
|
+
```liquid
|
77
|
+
{% array_create values1 %}
|
78
|
+
{% array_create array:values2 items:"value1",2 %}
|
79
|
+
```
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
values1 => []
|
83
|
+
values2 => ["value1", 2]
|
84
|
+
```
|
85
|
+
|
44
86
|
### array_add tag
|
45
87
|
|
46
88
|
Adds a value to the end of an array. If the array doesn't exist, it will
|
@@ -166,6 +208,29 @@ for every operation. If the array doesn't exist, it will create an empty array.
|
|
166
208
|
values => ["a", "z", "b"]
|
167
209
|
```
|
168
210
|
|
211
|
+
### hash_create tag
|
212
|
+
|
213
|
+
Creates a hash with opinional initial entries. If the variable is already
|
214
|
+
defined it will reassign it.
|
215
|
+
|
216
|
+
| Attribute | Required | Type | Description |
|
217
|
+
| --------- | ------------- | -------- | ------------------- |
|
218
|
+
| hash | Yes (default) | Variable | The hash to create |
|
219
|
+
| entries | No | Hash | The initial entries |
|
220
|
+
|
221
|
+
|
222
|
+
#### Example
|
223
|
+
|
224
|
+
```liquid
|
225
|
+
{% hash_create values1 %}
|
226
|
+
{% hash_create hash:values2 entries:"key1">"a","key2",3 %}
|
227
|
+
```
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
values1 => {}
|
231
|
+
values2 => {"key1" => "a", "key2" => 3}
|
232
|
+
```
|
233
|
+
|
169
234
|
### hash_set tag
|
170
235
|
|
171
236
|
Sets a key-value mapping in a hash, either adding it if it doesn't already
|
@@ -181,13 +246,13 @@ exist, it will create it first.
|
|
181
246
|
#### Example
|
182
247
|
|
183
248
|
```liquid
|
184
|
-
{% hash_set hash:values key:"
|
185
|
-
{% hash_set hash:values key:"
|
186
|
-
{% hash_set hash:values key:"
|
249
|
+
{% hash_set hash:values key:"key1" value:"a" %}
|
250
|
+
{% hash_set hash:values key:"key2" value:3 %}
|
251
|
+
{% hash_set hash:values key:"key3" value:false %}
|
187
252
|
```
|
188
253
|
|
189
254
|
```ruby
|
190
|
-
values => {"
|
255
|
+
values => {"key1" => "a", "key2" => 3, "key3" => false}
|
191
256
|
```
|
192
257
|
|
193
258
|
### hash_delete tag
|
@@ -202,16 +267,16 @@ Deletes a key-value mapping from a hash for a key.
|
|
202
267
|
#### Example
|
203
268
|
|
204
269
|
```ruby
|
205
|
-
values => {"
|
270
|
+
values => {"key1" => "a", "key2" => 3, "key3" => false}
|
206
271
|
```
|
207
272
|
|
208
273
|
```liquid
|
209
|
-
{% hash_delete hash:values key:"
|
210
|
-
{% hash_delete hash:values key:"
|
274
|
+
{% hash_delete hash:values key:"key1" %}
|
275
|
+
{% hash_delete hash:values key:"key2" %}
|
211
276
|
```
|
212
277
|
|
213
278
|
```ruby
|
214
|
-
values => {"
|
279
|
+
values => {"key3" => false}
|
215
280
|
```
|
216
281
|
|
217
282
|
### hash block tag
|
@@ -227,15 +292,15 @@ every operation. If the hash doesn't exist, it will create an empty hash.
|
|
227
292
|
|
228
293
|
```liquid
|
229
294
|
{% hash values %}
|
230
|
-
{% hash_set key:"
|
231
|
-
{% hash_set key:"
|
232
|
-
{% hash_set key:"
|
233
|
-
{% hash_delete "
|
295
|
+
{% hash_set key:"key1" value:"a" %}
|
296
|
+
{% hash_set key:"key2" value:"b" %}
|
297
|
+
{% hash_set key:"key3" value:"c" %}
|
298
|
+
{% hash_delete "key2" %}
|
234
299
|
{% endhash %}
|
235
300
|
```
|
236
301
|
|
237
302
|
```ruby
|
238
|
-
values => {"
|
303
|
+
values => {"key1" => "a", "key3" => "c"}
|
239
304
|
```
|
240
305
|
|
241
306
|
## License
|
@@ -1,18 +1,20 @@
|
|
1
1
|
module Arrays
|
2
|
-
class ArrayAddTag <
|
2
|
+
class ArrayAddTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, 'value', @markup)
|
9
|
+
@array_name = parser.consume_attribute('array', :id)
|
10
|
+
@value = parser.consume_required_attribute('value')
|
11
|
+
parser.finish
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
def render(context)
|
12
|
-
array = get_array(context, true)
|
13
|
-
unless array.nil?
|
14
|
-
array.push(@value.render(context))
|
15
|
-
end
|
16
|
+
array = ArrayHelper.get_array(context, @array_name, true)
|
17
|
+
array.push(@value.render(context)) unless array.nil?
|
16
18
|
''
|
17
19
|
end
|
18
20
|
end
|
@@ -1,15 +1,22 @@
|
|
1
1
|
module Arrays
|
2
2
|
class ArrayBlock < Liquid::Block
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, 'array', @markup)
|
9
|
+
@array_name = parser.consume_required_attribute('array', :id)
|
10
|
+
parser.finish
|
11
|
+
end
|
8
12
|
end
|
9
13
|
|
10
14
|
def render(context)
|
11
|
-
|
12
|
-
|
15
|
+
if context.key?(@array_name)
|
16
|
+
@array = context[@array_name]
|
17
|
+
else
|
18
|
+
context.scopes.last[@array_name] = @array = []
|
19
|
+
end
|
13
20
|
context.stack do
|
14
21
|
context['block_array'] = @array
|
15
22
|
@output = super
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayCreateTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
5
|
+
def parse(tokens)
|
6
|
+
super
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, 'array', @markup)
|
9
|
+
@array_name = parser.consume_required_attribute('array', :id)
|
10
|
+
@items = parser.consume_attribute('items', :array)
|
11
|
+
parser.finish
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def render(context)
|
16
|
+
context.scopes.last[@array_name] = @items.nil? ?
|
17
|
+
[] : @items.render(context)
|
18
|
+
''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Liquid::Template.register_tag('array_create', ArrayCreateTag)
|
23
|
+
end
|
@@ -1,27 +1,33 @@
|
|
1
1
|
module Arrays
|
2
|
-
class ArrayDeleteTag <
|
2
|
+
class ArrayDeleteTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
9
|
+
@array_name = parser.consume_attribute('array', :id)
|
10
|
+
@value = parser.consume_attribute('value')
|
11
|
+
@index = parser.consume_attribute('index', :integer)
|
12
|
+
parser.finish
|
13
|
+
if @value.nil? && @index.nil?
|
14
|
+
raise Liquid::SyntaxError, 'no value or index specified'
|
15
|
+
elsif !@value.nil? && !@index.nil?
|
16
|
+
raise Liquid::SyntaxError, 'only value or index can be specified'
|
17
|
+
end
|
18
|
+
end
|
15
19
|
end
|
16
20
|
|
17
21
|
def render(context)
|
18
|
-
array = get_array(context, false)
|
22
|
+
array = ArrayHelper.get_array(context, @array_name, false)
|
19
23
|
unless array.nil?
|
20
24
|
if @index.nil?
|
21
25
|
array.delete(@value.render(context))
|
22
26
|
else
|
23
27
|
index = @index.render(context)
|
24
|
-
|
28
|
+
if index.is_a?(Integer) && index >= 0 && index < array.length
|
29
|
+
array.delete_at(index)
|
30
|
+
end
|
25
31
|
end
|
26
32
|
end
|
27
33
|
''
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayHelper
|
3
|
+
def self.get_array(context, array_name, create)
|
4
|
+
unless array_name.nil?
|
5
|
+
if context.key?(array_name)
|
6
|
+
array = context[array_name]
|
7
|
+
elsif create
|
8
|
+
array = context.scopes.last[array_name] ||= []
|
9
|
+
end
|
10
|
+
else
|
11
|
+
if context.key?('block_array')
|
12
|
+
array = context['block_array']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
array.is_a?(Array) ? array : nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,16 +1,20 @@
|
|
1
1
|
module Arrays
|
2
|
-
class ArrayInsertTag <
|
2
|
+
class ArrayInsertTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
9
|
+
@array_name = parser.consume_attribute('array', :id)
|
10
|
+
@index = parser.consume_required_attribute('index', :integer)
|
11
|
+
@value = parser.consume_required_attribute('value')
|
12
|
+
parser.finish
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
def render(context)
|
13
|
-
array = get_array(context, false)
|
17
|
+
array = ArrayHelper.get_array(context, @array_name, false)
|
14
18
|
index = @index.render(context)
|
15
19
|
if !array.nil? && index.is_a?(Integer) && index >= 0 && index <= array.length
|
16
20
|
array.insert(index, @value.render(context))
|
@@ -1,18 +1,22 @@
|
|
1
1
|
module Arrays
|
2
|
-
class ArrayReplaceTag <
|
2
|
+
class ArrayReplaceTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
9
|
+
@array_name = parser.consume_attribute('array', :id)
|
10
|
+
@index = parser.consume_required_attribute('index', :integer)
|
11
|
+
@value = parser.consume_required_attribute('value')
|
12
|
+
parser.finish
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
def render(context)
|
13
|
-
array = get_array(context, false)
|
17
|
+
array = ArrayHelper.get_array(context, @array_name, false)
|
14
18
|
index = @index.render(context)
|
15
|
-
unless array.nil? || !index.is_a?(Integer) ||
|
19
|
+
unless array.nil? || !index.is_a?(Integer) || index < 0 || index >= array.length
|
16
20
|
array[index] = @value.render(context)
|
17
21
|
end
|
18
22
|
''
|
@@ -3,48 +3,80 @@ module Arrays
|
|
3
3
|
def initialize(parse_context, default_key = nil, markup)
|
4
4
|
super(markup)
|
5
5
|
@parse_context = parse_context
|
6
|
-
|
7
|
-
if @tokens.length == 2 && look(:end_of_string, 1)
|
8
|
-
raise Liquid::SyntaxError, 'attributes must be named' if default_key.nil?
|
9
|
-
@attributes[default_key] = get_current_value
|
10
|
-
elsif @tokens.length > 2
|
11
|
-
while look(:id) && look(:colon, 1)
|
12
|
-
key = consume
|
13
|
-
consume
|
14
|
-
@attributes[key] = get_current_value
|
15
|
-
end
|
16
|
-
unless look(:end_of_string)
|
17
|
-
raise Liquid::SyntaxError, 'attributes need to have the format key:value'
|
18
|
-
end
|
19
|
-
end
|
6
|
+
parse(default_key, markup)
|
20
7
|
end
|
21
8
|
|
22
9
|
def consume_attribute(key, type = nil)
|
23
10
|
return nil unless @attributes.key?(key)
|
24
|
-
|
11
|
+
value = @attributes.delete(key)
|
25
12
|
if type == :id
|
26
|
-
return
|
13
|
+
return value[0] == :id ? value[1].raw : nil
|
14
|
+
elsif type == :array && value[0] != :array
|
15
|
+
return ArrayVariable.new([value[1]])
|
27
16
|
end
|
28
|
-
|
29
|
-
Liquid::Variable.new(attribute[1], @parse_context) :
|
30
|
-
nil
|
17
|
+
return type.nil? || value[0] == type || value[0] == :id ? value[1] : nil
|
31
18
|
end
|
32
19
|
|
33
20
|
def consume_required_attribute(key, type = nil)
|
34
21
|
attribute = consume_attribute(key, type)
|
35
|
-
raise Liquid::
|
22
|
+
raise Liquid::SyntaxError, "#{key} not specified" if attribute == nil
|
36
23
|
attribute
|
37
24
|
end
|
38
25
|
|
39
26
|
def finish
|
40
27
|
unless @attributes.empty?
|
41
|
-
raise Liquid::
|
28
|
+
raise Liquid::SyntaxError, "invalid arguments #{@attributes.keys.join(',')}"
|
42
29
|
end
|
43
30
|
end
|
44
31
|
|
45
32
|
private
|
46
33
|
|
47
|
-
def
|
34
|
+
def parse(default_key, markup)
|
35
|
+
@attributes = {}
|
36
|
+
unless look(:end_of_string)
|
37
|
+
if look(:id) && look(:colon, 1)
|
38
|
+
while look(:id) && look(:colon, 1)
|
39
|
+
key = consume
|
40
|
+
consume
|
41
|
+
@attributes[key] = parse_value
|
42
|
+
end
|
43
|
+
unless look(:end_of_string)
|
44
|
+
raise Liquid::SyntaxError, 'attributes need to have the format key:value'
|
45
|
+
end
|
46
|
+
else
|
47
|
+
raise Liquid::SyntaxError, 'attributes must be named' if default_key.nil?
|
48
|
+
@attributes[default_key] = parse_value
|
49
|
+
unless look(:end_of_string)
|
50
|
+
raise Liquid::SyntaxError, ''
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def parse_value
|
57
|
+
expression = parse_expression
|
58
|
+
if look(:comma)
|
59
|
+
array = [expression[1]]
|
60
|
+
while look(:comma)
|
61
|
+
consume
|
62
|
+
array.push(parse_expression[1])
|
63
|
+
end
|
64
|
+
return [:array, ArrayVariable.new(array)]
|
65
|
+
elsif look(:comparison)
|
66
|
+
raise Liquid::SyntaxError, '' unless consume.eql?('>')
|
67
|
+
hash = {expression[1] => parse_expression[1]}
|
68
|
+
while look(:comma) && look(:comparison, 2)
|
69
|
+
consume
|
70
|
+
key = parse_expression[1]
|
71
|
+
raise Liquid::SyntaxError, '' unless consume.eql?('>')
|
72
|
+
hash[key] = parse_expression[1]
|
73
|
+
end
|
74
|
+
return [:hash, HashVariable.new(hash)]
|
75
|
+
end
|
76
|
+
expression
|
77
|
+
end
|
78
|
+
|
79
|
+
def parse_expression
|
48
80
|
token = @tokens[@p]
|
49
81
|
type = token[0]
|
50
82
|
value = expression
|
@@ -53,7 +85,7 @@ module Arrays
|
|
53
85
|
elsif type == :id && value.eql?('true') || value.eql?('false')
|
54
86
|
type = :boolean
|
55
87
|
end
|
56
|
-
[type, value]
|
88
|
+
[type, Liquid::Variable.new(value, @parse_context)]
|
57
89
|
end
|
58
90
|
end
|
59
91
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Arrays
|
2
|
+
module ErrorHandler
|
3
|
+
def handle(error)
|
4
|
+
error.line_number = line_number
|
5
|
+
error.markup_context = "in \"#{@markup.strip}\""
|
6
|
+
|
7
|
+
case @parse_context.error_mode
|
8
|
+
when :strict
|
9
|
+
raise error
|
10
|
+
when :warn
|
11
|
+
@parse_context.warnings << error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def catch
|
16
|
+
begin
|
17
|
+
yield
|
18
|
+
rescue => e
|
19
|
+
handle(e)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -8,8 +8,11 @@ module Arrays
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def render(context)
|
11
|
-
|
12
|
-
|
11
|
+
if context.key?(@hash_name)
|
12
|
+
@hash = context[@hash_name]
|
13
|
+
else
|
14
|
+
context.scopes.last[@hash_name] = @hash = {}
|
15
|
+
end
|
13
16
|
context.stack do
|
14
17
|
context['block_hash'] = @hash
|
15
18
|
@output = super
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Arrays
|
2
|
+
class HashCreateTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
5
|
+
def parse(tokens)
|
6
|
+
super
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, 'hash', @markup)
|
9
|
+
@hash_name = parser.consume_required_attribute('hash', :id)
|
10
|
+
@entries = parser.consume_attribute('entries', :hash)
|
11
|
+
parser.finish
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def render(context)
|
16
|
+
context.scopes.last[@hash_name] = @entries.nil? ?
|
17
|
+
{} : @entries.render(context)
|
18
|
+
''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Liquid::Template.register_tag('hash_create', HashCreateTag)
|
23
|
+
end
|
@@ -1,15 +1,19 @@
|
|
1
1
|
module Arrays
|
2
|
-
class HashDeleteTag <
|
2
|
+
class HashDeleteTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, 'key', @markup)
|
9
|
+
@hash_name = parser.consume_attribute('hash', :id)
|
10
|
+
@key = parser.consume_required_attribute('key')
|
11
|
+
parser.finish
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
def render(context)
|
12
|
-
hash = get_hash(context, false)
|
16
|
+
hash = HashHelper.get_hash(context, @hash_name, false)
|
13
17
|
unless hash.nil?
|
14
18
|
hash.delete(@key.render(context))
|
15
19
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Arrays
|
2
|
+
class HashHelper
|
3
|
+
def self.get_hash(context, hash_name, create)
|
4
|
+
unless hash_name.nil?
|
5
|
+
if context.key?(hash_name)
|
6
|
+
hash = context[hash_name]
|
7
|
+
elsif create
|
8
|
+
hash = context.scopes.last[hash_name] ||= {}
|
9
|
+
end
|
10
|
+
else
|
11
|
+
if context.key?('block_hash')
|
12
|
+
hash = context['block_hash']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
hash.is_a?(Hash) ? hash : nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,16 +1,20 @@
|
|
1
1
|
module Arrays
|
2
|
-
class HashSetTag <
|
2
|
+
class HashSetTag < Liquid::Tag
|
3
|
+
include ErrorHandler
|
4
|
+
|
3
5
|
def parse(tokens)
|
4
6
|
super
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
catch do
|
8
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
9
|
+
@hash_name = parser.consume_attribute('hash', :id)
|
10
|
+
@key = parser.consume_required_attribute('key')
|
11
|
+
@value = parser.consume_required_attribute('value')
|
12
|
+
parser.finish
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
def render(context)
|
13
|
-
hash = get_hash(context, true)
|
17
|
+
hash = HashHelper.get_hash(context, @hash_name, true)
|
14
18
|
unless hash.nil?
|
15
19
|
hash[@key.render(context)] = @value.render(context)
|
16
20
|
end
|
data/lib/liquid-arrays.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
|
-
require 'liquid-arrays/
|
1
|
+
require 'liquid-arrays/error_handler'
|
2
|
+
require 'liquid-arrays/array_helper'
|
3
|
+
require 'liquid-arrays/array_create_tag'
|
2
4
|
require 'liquid-arrays/array_add_tag'
|
3
5
|
require 'liquid-arrays/array_replace_tag'
|
4
6
|
require 'liquid-arrays/array_insert_tag'
|
5
7
|
require 'liquid-arrays/array_delete_tag'
|
6
8
|
require 'liquid-arrays/array_block'
|
7
|
-
require 'liquid-arrays/
|
9
|
+
require 'liquid-arrays/hash_helper'
|
10
|
+
require 'liquid-arrays/hash_create_tag'
|
8
11
|
require 'liquid-arrays/hash_set_tag'
|
9
12
|
require 'liquid-arrays/hash_delete_tag'
|
10
13
|
require 'liquid-arrays/hash_block'
|
11
14
|
require 'liquid-arrays/attribute_parser'
|
15
|
+
require 'liquid-arrays/array_variable'
|
16
|
+
require 'liquid-arrays/hash_variable'
|
12
17
|
require 'liquid-arrays/version'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: liquid-arrays
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frostphyr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: liquid
|
@@ -65,6 +65,7 @@ executables: []
|
|
65
65
|
extensions: []
|
66
66
|
extra_rdoc_files: []
|
67
67
|
files:
|
68
|
+
- CHANGELOG.md
|
68
69
|
- Gemfile
|
69
70
|
- LICENSE
|
70
71
|
- README.md
|
@@ -72,15 +73,20 @@ files:
|
|
72
73
|
- lib/liquid-arrays.rb
|
73
74
|
- lib/liquid-arrays/array_add_tag.rb
|
74
75
|
- lib/liquid-arrays/array_block.rb
|
76
|
+
- lib/liquid-arrays/array_create_tag.rb
|
75
77
|
- lib/liquid-arrays/array_delete_tag.rb
|
78
|
+
- lib/liquid-arrays/array_helper.rb
|
76
79
|
- lib/liquid-arrays/array_insert_tag.rb
|
77
80
|
- lib/liquid-arrays/array_replace_tag.rb
|
78
|
-
- lib/liquid-arrays/
|
81
|
+
- lib/liquid-arrays/array_variable.rb
|
79
82
|
- lib/liquid-arrays/attribute_parser.rb
|
83
|
+
- lib/liquid-arrays/error_handler.rb
|
80
84
|
- lib/liquid-arrays/hash_block.rb
|
85
|
+
- lib/liquid-arrays/hash_create_tag.rb
|
81
86
|
- lib/liquid-arrays/hash_delete_tag.rb
|
87
|
+
- lib/liquid-arrays/hash_helper.rb
|
82
88
|
- lib/liquid-arrays/hash_set_tag.rb
|
83
|
-
- lib/liquid-arrays/
|
89
|
+
- lib/liquid-arrays/hash_variable.rb
|
84
90
|
- lib/liquid-arrays/version.rb
|
85
91
|
homepage: https://github.com/Frostphyr/liquid-arrays
|
86
92
|
licenses:
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module Arrays
|
2
|
-
class ArrayTag < Liquid::Tag
|
3
|
-
def get_array(context, create)
|
4
|
-
unless @array_name.nil?
|
5
|
-
if context.key?(@array_name)
|
6
|
-
array = context[@array_name]
|
7
|
-
elsif create
|
8
|
-
array = context[@array_name] ||= []
|
9
|
-
end
|
10
|
-
else
|
11
|
-
if context.key?('block_array')
|
12
|
-
array = context['block_array']
|
13
|
-
end
|
14
|
-
end
|
15
|
-
array
|
16
|
-
end
|
17
|
-
|
18
|
-
def out_of_bounds?(array, index)
|
19
|
-
index < 0 || index >= array.length
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Arrays
|
2
|
-
class HashTag < Liquid::Tag
|
3
|
-
def get_hash(context, create)
|
4
|
-
unless @hash_name.nil?
|
5
|
-
if context.key?(@hash_name)
|
6
|
-
hash = context[@hash_name]
|
7
|
-
elsif create
|
8
|
-
hash = context[@hash_name] ||= {}
|
9
|
-
end
|
10
|
-
else
|
11
|
-
if context.key?('block_hash')
|
12
|
-
hash = context['block_hash']
|
13
|
-
end
|
14
|
-
end
|
15
|
-
hash
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|