liquid-arrays 1.0.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 +7 -0
- data/Gemfile +3 -0
- data/LICENSE +176 -0
- data/README.md +255 -0
- data/Rakefile +1 -0
- data/lib/liquid-arrays.rb +12 -0
- data/lib/liquid-arrays/array_add_tag.rb +21 -0
- data/lib/liquid-arrays/array_block.rb +22 -0
- data/lib/liquid-arrays/array_delete_tag.rb +32 -0
- data/lib/liquid-arrays/array_insert_tag.rb +23 -0
- data/lib/liquid-arrays/array_replace_tag.rb +23 -0
- data/lib/liquid-arrays/array_tag.rb +22 -0
- data/lib/liquid-arrays/attribute_parser.rb +59 -0
- data/lib/liquid-arrays/hash_block.rb +22 -0
- data/lib/liquid-arrays/hash_delete_tag.rb +21 -0
- data/lib/liquid-arrays/hash_set_tag.rb +22 -0
- data/lib/liquid-arrays/hash_tag.rb +18 -0
- data/lib/liquid-arrays/version.rb +3 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f1e0a5c016d3c2b814dcb7bbcfd5af5293d5934fc44dd1215f66f49bcb4ce8af
|
4
|
+
data.tar.gz: 32eeef65c1531dc56b43cc882f617a64d6ac6d75a89d9b5b517bc1f032bdd6bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82e191d6a4cd14ca988325acce0c5fc3a38f9762f69426aed8aaa8e47a7e75407cd7716788b1b6a4aba91c4db52bf5adacfb3b978f9e7c884317fb95fcba1b22
|
7
|
+
data.tar.gz: b33d268477da626b9eecb4870000282188076a82f246a98c66091ca89f094845a75f3fc136461dc2ac9c5fc327fee56192545ccf7d946b6733bcc832c88b0fa3
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
# Liquid Arrays
|
2
|
+
|
3
|
+
Adds better support for arrays and hashes in Liquid. Liquid only comes with
|
4
|
+
support for creating arrays using `split` and iteration for arrays and hashes.
|
5
|
+
This adds support for standard array and hash operations, such as creation,
|
6
|
+
insertion, replacement, and deletion.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'liquid-arrays'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install liquid-arrays
|
23
|
+
|
24
|
+
Ensure any other requirements the software you're using might have for loading
|
25
|
+
plugins are met.
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
There are several tags that will modify arrays and hashes in various ways.
|
30
|
+
Each tag has various attributes that specify information about the operation.
|
31
|
+
The syntax for the tags and attributes is as follows:
|
32
|
+
|
33
|
+
```liquid
|
34
|
+
{% tag_name attribute:value attribute:value %}
|
35
|
+
```
|
36
|
+
|
37
|
+
Some tags will have a default attribute which can be used if only one
|
38
|
+
attribute needs specified, like so:
|
39
|
+
|
40
|
+
```liquid
|
41
|
+
{% tag_name value %}
|
42
|
+
```
|
43
|
+
|
44
|
+
### array_add tag
|
45
|
+
|
46
|
+
Adds a value to the end of an array. If the array doesn't exist, it will
|
47
|
+
create it first.
|
48
|
+
|
49
|
+
| Attribute | Required | Type | Description |
|
50
|
+
| --------- | ------------------------ | -------- | ------------------- |
|
51
|
+
| array | If outside `array` block | Variable | The array to modify |
|
52
|
+
| value | Yes (default) | Any | The value to add |
|
53
|
+
|
54
|
+
#### Example
|
55
|
+
|
56
|
+
```liquid
|
57
|
+
{% assign var = "a" %}
|
58
|
+
{% array_add array:values value:var %}
|
59
|
+
{% array_add array:values value:1 %}
|
60
|
+
{% array_add array:values value:true %}
|
61
|
+
```
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
values => ["a", 1, true]
|
65
|
+
```
|
66
|
+
|
67
|
+
### array_delete tag
|
68
|
+
|
69
|
+
Deletes an index or all occurrences of a value from an array. If an index is
|
70
|
+
used, it must be within the interval `[0, length)`.
|
71
|
+
|
72
|
+
| Attribute | Required | Type | Description |
|
73
|
+
| --------- | ------------------------ | -------- | ------------------- |
|
74
|
+
| array | If outside `array` block | Variable | The array to modify |
|
75
|
+
| index | If no `value` | Integer or Variable | The index to delete |
|
76
|
+
| value | If no `index` | Any | The value to delete |
|
77
|
+
|
78
|
+
#### Example
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
values => ["a", "b", "c", "a"]
|
82
|
+
```
|
83
|
+
|
84
|
+
```liquid
|
85
|
+
{% array_delete array:values index:1 %}
|
86
|
+
{% array_delete array:values value:"a" %}
|
87
|
+
```
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
values => ["c"]
|
91
|
+
```
|
92
|
+
|
93
|
+
### array_insert tag
|
94
|
+
|
95
|
+
Inserts a value into an array at an index. The index must be within the
|
96
|
+
interval `[0, length]`. The array must already exist and will not be created
|
97
|
+
if it does not.
|
98
|
+
|
99
|
+
| Attribute | Required | Type | Description |
|
100
|
+
| --------- | ------------------------ | -------- | ---------------------- |
|
101
|
+
| array | If outside `array` block | Variable | The array to modify |
|
102
|
+
| index | Yes | Integer or Variable | The index to insert at |
|
103
|
+
| value | Yes | Any | The value to insert |
|
104
|
+
|
105
|
+
#### Example
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
values => ["a", "b", "c"]
|
109
|
+
```
|
110
|
+
|
111
|
+
```liquid
|
112
|
+
{% array_insert array:values index:0 value:"z" %}
|
113
|
+
{% array_insert array:values index:2 value:"y" %}
|
114
|
+
```
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
values => ["z", "a", "y", "b", "c"]
|
118
|
+
```
|
119
|
+
|
120
|
+
### array_replace tag
|
121
|
+
|
122
|
+
Replaces the value at an index in an array. The index must be within the
|
123
|
+
interval `[0, length)`.
|
124
|
+
|
125
|
+
| Attribute | Required | Type | Description |
|
126
|
+
| --------- | ------------------------ | -------- | ------------------------- |
|
127
|
+
| array | If outside `array` block | Variable | The array to modify |
|
128
|
+
| index | Yes | Integer or Variable | The index to replace |
|
129
|
+
| value | Yes | Any | The value to replace with |
|
130
|
+
|
131
|
+
#### Example
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
values => ["a", "b", "c"]
|
135
|
+
```
|
136
|
+
|
137
|
+
```liquid
|
138
|
+
{% array_replace array:values index:0 value:"z" %}
|
139
|
+
{% array_replace array:values index:2 value:"y" %}
|
140
|
+
```
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
values => ["z", "b", "y"]
|
144
|
+
```
|
145
|
+
|
146
|
+
### array block tag
|
147
|
+
|
148
|
+
Allows for the modification of an array without having to specify the array
|
149
|
+
for every operation. If the array doesn't exist, it will create an empty array.
|
150
|
+
|
151
|
+
| Attribute | Required | Type | Description |
|
152
|
+
| --------- | ------------- | -------- | ------------------- |
|
153
|
+
| array | Yes (default) | Variable | The array to modify |
|
154
|
+
|
155
|
+
#### Example
|
156
|
+
|
157
|
+
```liquid
|
158
|
+
{% array values %}
|
159
|
+
{% array_add "a" %}
|
160
|
+
{% array_add "b" %}
|
161
|
+
{% array_insert index:1 value:"z" %}
|
162
|
+
{% endarray %}
|
163
|
+
```
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
values => ["a", "z", "b"]
|
167
|
+
```
|
168
|
+
|
169
|
+
### hash_set tag
|
170
|
+
|
171
|
+
Sets a key-value mapping in a hash, either adding it if it doesn't already
|
172
|
+
exist or replacing the existing mapping for the key. If the hash doesn't
|
173
|
+
exist, it will create it first.
|
174
|
+
|
175
|
+
| Attribute | Required | Type | Description |
|
176
|
+
| --------- | ----------------------- | -------- | --------------------------- |
|
177
|
+
| hash | If outside `hash` block | Variable | The hash to modify |
|
178
|
+
| key | Yes | Any | The key to map the value to |
|
179
|
+
| value | Yes | Any | The value to map to the key |
|
180
|
+
|
181
|
+
#### Example
|
182
|
+
|
183
|
+
```liquid
|
184
|
+
{% hash_set hash:values key:"value1" value:"a" %}
|
185
|
+
{% hash_set hash:values key:"value2" value:3 %}
|
186
|
+
{% hash_set hash:values key:"value3" value:false %}
|
187
|
+
```
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
values => {"value1" => "a", "value2" => 3, "value3" => false}
|
191
|
+
```
|
192
|
+
|
193
|
+
### hash_delete tag
|
194
|
+
|
195
|
+
Deletes a key-value mapping from a hash for a key.
|
196
|
+
|
197
|
+
| Attribute | Required | Type | Description |
|
198
|
+
| --------- | ----------------------- | -------- | --------------------------------- |
|
199
|
+
| hash | If outside `hash` block | Variable | The hash to modify |
|
200
|
+
| key | Yes (default) | Any | The key to remove the mapping for |
|
201
|
+
|
202
|
+
#### Example
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
values => {"value1" => "a", "value2" => 3, "value3" => false}
|
206
|
+
```
|
207
|
+
|
208
|
+
```liquid
|
209
|
+
{% hash_delete hash:values key:"value1" %}
|
210
|
+
{% hash_delete hash:values key:"value2" %}
|
211
|
+
```
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
values => {"value3" => false}
|
215
|
+
```
|
216
|
+
|
217
|
+
### hash block tag
|
218
|
+
|
219
|
+
Allows for the modification of a hash without having to specify the hash for
|
220
|
+
every operation. If the hash doesn't exist, it will create an empty hash.
|
221
|
+
|
222
|
+
| Attribute | Required | Type | Description |
|
223
|
+
| --------- | ------------- | -------- | ------------------ |
|
224
|
+
| hash | Yes (default) | Variable | The hash to modify |
|
225
|
+
|
226
|
+
#### Example
|
227
|
+
|
228
|
+
```liquid
|
229
|
+
{% hash values %}
|
230
|
+
{% hash_set key:"value1" value:"a" %}
|
231
|
+
{% hash_set key:"value2" value:"b" %}
|
232
|
+
{% hash_set key:"value3" value:"c" %}
|
233
|
+
{% hash_delete "value2" %}
|
234
|
+
{% endhash %}
|
235
|
+
```
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
values => {"value1" => "a", "value3" => "c"}
|
239
|
+
```
|
240
|
+
|
241
|
+
## License
|
242
|
+
|
243
|
+
Liquid Arrays is licensed under
|
244
|
+
[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). It is a
|
245
|
+
permissive software license with a few restrictions.
|
246
|
+
|
247
|
+
* This software may be modified, used, and distributed in open or
|
248
|
+
closed-source software.
|
249
|
+
* Redistribution requires a copy of the license and notices to any
|
250
|
+
modifications made to this software.
|
251
|
+
* Any unmodified parts of this software must retain the original license but
|
252
|
+
any modifications or derivatives may be licensed differently.
|
253
|
+
|
254
|
+
This is only a brief summary of the license. For the complete restrictions and
|
255
|
+
requirements, please refer to the full license.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'liquid-arrays/array_tag'
|
2
|
+
require 'liquid-arrays/array_add_tag'
|
3
|
+
require 'liquid-arrays/array_replace_tag'
|
4
|
+
require 'liquid-arrays/array_insert_tag'
|
5
|
+
require 'liquid-arrays/array_delete_tag'
|
6
|
+
require 'liquid-arrays/array_block'
|
7
|
+
require 'liquid-arrays/hash_tag'
|
8
|
+
require 'liquid-arrays/hash_set_tag'
|
9
|
+
require 'liquid-arrays/hash_delete_tag'
|
10
|
+
require 'liquid-arrays/hash_block'
|
11
|
+
require 'liquid-arrays/attribute_parser'
|
12
|
+
require 'liquid-arrays/version'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayAddTag < ArrayTag
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, 'value', @markup)
|
6
|
+
@array_name = parser.consume_attribute('array', :id)
|
7
|
+
@value = parser.consume_required_attribute('value')
|
8
|
+
parser.finish
|
9
|
+
end
|
10
|
+
|
11
|
+
def render(context)
|
12
|
+
array = get_array(context, true)
|
13
|
+
unless array.nil?
|
14
|
+
array.push(@value.render(context))
|
15
|
+
end
|
16
|
+
''
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Liquid::Template.register_tag('array_add', ArrayAddTag)
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayBlock < Liquid::Block
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, 'array', @markup)
|
6
|
+
@array_name = parser.consume_required_attribute('array', :id)
|
7
|
+
parser.finish
|
8
|
+
end
|
9
|
+
|
10
|
+
def render(context)
|
11
|
+
@array = context[@array_name] ||= []
|
12
|
+
context.scopes.last[@array_name] = @array
|
13
|
+
context.stack do
|
14
|
+
context['block_array'] = @array
|
15
|
+
@output = super
|
16
|
+
end
|
17
|
+
@output
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Liquid::Template.register_tag('array', ArrayBlock)
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayDeleteTag < ArrayTag
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
6
|
+
@array_name = parser.consume_attribute('array', :id)
|
7
|
+
@value = parser.consume_attribute('value')
|
8
|
+
@index = parser.consume_attribute('index', :integer)
|
9
|
+
parser.finish
|
10
|
+
if @value.nil? && @index.nil?
|
11
|
+
raise Liquid::ArgumentError, 'no value or index specified'
|
12
|
+
elsif !@value.nil? && !@index.nil?
|
13
|
+
raise Liquid::ArgumentError, 'only value or index can be specified'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def render(context)
|
18
|
+
array = get_array(context, false)
|
19
|
+
unless array.nil?
|
20
|
+
if @index.nil?
|
21
|
+
array.delete(@value.render(context))
|
22
|
+
else
|
23
|
+
index = @index.render(context)
|
24
|
+
array.delete_at(index) if index.is_a?(Integer) && !out_of_bounds?(array, index)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
''
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Liquid::Template.register_tag('array_delete', ArrayDeleteTag)
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayInsertTag < ArrayTag
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
6
|
+
@array_name = parser.consume_attribute('array', :id)
|
7
|
+
@index = parser.consume_required_attribute('index', :integer)
|
8
|
+
@value = parser.consume_required_attribute('value')
|
9
|
+
parser.finish
|
10
|
+
end
|
11
|
+
|
12
|
+
def render(context)
|
13
|
+
array = get_array(context, false)
|
14
|
+
index = @index.render(context)
|
15
|
+
if !array.nil? && index.is_a?(Integer) && index >= 0 && index <= array.length
|
16
|
+
array.insert(index, @value.render(context))
|
17
|
+
end
|
18
|
+
''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Liquid::Template.register_tag('array_insert', ArrayInsertTag)
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Arrays
|
2
|
+
class ArrayReplaceTag < ArrayTag
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
6
|
+
@array_name = parser.consume_attribute('array', :id)
|
7
|
+
@index = parser.consume_required_attribute('index', :integer)
|
8
|
+
@value = parser.consume_required_attribute('value')
|
9
|
+
parser.finish
|
10
|
+
end
|
11
|
+
|
12
|
+
def render(context)
|
13
|
+
array = get_array(context, false)
|
14
|
+
index = @index.render(context)
|
15
|
+
unless array.nil? || !index.is_a?(Integer) || out_of_bounds?(array, index)
|
16
|
+
array[index] = @value.render(context)
|
17
|
+
end
|
18
|
+
''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Liquid::Template.register_tag('array_replace', ArrayReplaceTag)
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Arrays
|
2
|
+
class AttributeParser < Liquid::Parser
|
3
|
+
def initialize(parse_context, default_key = nil, markup)
|
4
|
+
super(markup)
|
5
|
+
@parse_context = parse_context
|
6
|
+
@attributes = {}
|
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
|
20
|
+
end
|
21
|
+
|
22
|
+
def consume_attribute(key, type = nil)
|
23
|
+
return nil unless @attributes.key?(key)
|
24
|
+
attribute = @attributes.delete(key)
|
25
|
+
if type == :id
|
26
|
+
return attribute[0] == :id ? attribute[1] : nil
|
27
|
+
end
|
28
|
+
attribute[0] == :id || type.nil? || attribute[0] == type ?
|
29
|
+
Liquid::Variable.new(attribute[1], @parse_context) :
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def consume_required_attribute(key, type = nil)
|
34
|
+
attribute = consume_attribute(key, type)
|
35
|
+
raise Liquid::ArgumentError, "#{key} not specified" if attribute == nil
|
36
|
+
attribute
|
37
|
+
end
|
38
|
+
|
39
|
+
def finish
|
40
|
+
unless @attributes.empty?
|
41
|
+
raise Liquid::ArgumentError, "invalid arguments #{@attributes.keys.join(',')}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def get_current_value
|
48
|
+
token = @tokens[@p]
|
49
|
+
type = token[0]
|
50
|
+
value = expression
|
51
|
+
if type == :number
|
52
|
+
type = value.include?('.') ? :float : :integer
|
53
|
+
elsif type == :id && value.eql?('true') || value.eql?('false')
|
54
|
+
type = :boolean
|
55
|
+
end
|
56
|
+
[type, value]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Arrays
|
2
|
+
class HashBlock < Liquid::Block
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, 'hash', @markup)
|
6
|
+
@hash_name = parser.consume_required_attribute('hash', :id)
|
7
|
+
parser.finish
|
8
|
+
end
|
9
|
+
|
10
|
+
def render(context)
|
11
|
+
@hash = context[@hash_name] ||= {}
|
12
|
+
context.scopes.last[@hash_name] = @hash
|
13
|
+
context.stack do
|
14
|
+
context['block_hash'] = @hash
|
15
|
+
@output = super
|
16
|
+
end
|
17
|
+
@output
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Liquid::Template.register_tag('hash', HashBlock)
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Arrays
|
2
|
+
class HashDeleteTag < HashTag
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, 'key', @markup)
|
6
|
+
@hash_name = parser.consume_attribute('hash', :id)
|
7
|
+
@key = parser.consume_required_attribute('key')
|
8
|
+
parser.finish
|
9
|
+
end
|
10
|
+
|
11
|
+
def render(context)
|
12
|
+
hash = get_hash(context, false)
|
13
|
+
unless hash.nil?
|
14
|
+
hash.delete(@key.render(context))
|
15
|
+
end
|
16
|
+
''
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Liquid::Template.register_tag('hash_delete', HashDeleteTag)
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Arrays
|
2
|
+
class HashSetTag < HashTag
|
3
|
+
def parse(tokens)
|
4
|
+
super
|
5
|
+
parser = AttributeParser.new(@parse_context, @markup)
|
6
|
+
@hash_name = parser.consume_attribute('hash', :id)
|
7
|
+
@key = parser.consume_required_attribute('key')
|
8
|
+
@value = parser.consume_required_attribute('value')
|
9
|
+
parser.finish
|
10
|
+
end
|
11
|
+
|
12
|
+
def render(context)
|
13
|
+
hash = get_hash(context, true)
|
14
|
+
unless hash.nil?
|
15
|
+
hash[@key.render(context)] = @value.render(context)
|
16
|
+
end
|
17
|
+
''
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Liquid::Template.register_tag('hash_set', HashSetTag)
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: liquid-arrays
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frostphyr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: liquid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '13'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '13'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.10'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.10'
|
61
|
+
description:
|
62
|
+
email:
|
63
|
+
- contact@frostphyr.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- lib/liquid-arrays.rb
|
73
|
+
- lib/liquid-arrays/array_add_tag.rb
|
74
|
+
- lib/liquid-arrays/array_block.rb
|
75
|
+
- lib/liquid-arrays/array_delete_tag.rb
|
76
|
+
- lib/liquid-arrays/array_insert_tag.rb
|
77
|
+
- lib/liquid-arrays/array_replace_tag.rb
|
78
|
+
- lib/liquid-arrays/array_tag.rb
|
79
|
+
- lib/liquid-arrays/attribute_parser.rb
|
80
|
+
- lib/liquid-arrays/hash_block.rb
|
81
|
+
- lib/liquid-arrays/hash_delete_tag.rb
|
82
|
+
- lib/liquid-arrays/hash_set_tag.rb
|
83
|
+
- lib/liquid-arrays/hash_tag.rb
|
84
|
+
- lib/liquid-arrays/version.rb
|
85
|
+
homepage: https://github.com/Frostphyr/liquid-arrays
|
86
|
+
licenses:
|
87
|
+
- Apache-2.0
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.2.19
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Adds better support for arrays and hashes in Liquid
|
108
|
+
test_files: []
|