liquid2 0.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.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/.rubocop.yml +46 -0
  4. data/.ruby-version +1 -0
  5. data/.vscode/settings.json +32 -0
  6. data/CHANGELOG.md +5 -0
  7. data/LICENSE.txt +21 -0
  8. data/LICENSE_SHOPIFY.txt +20 -0
  9. data/README.md +219 -0
  10. data/Rakefile +23 -0
  11. data/Steepfile +26 -0
  12. data/lib/liquid2/context.rb +297 -0
  13. data/lib/liquid2/environment.rb +287 -0
  14. data/lib/liquid2/errors.rb +79 -0
  15. data/lib/liquid2/expression.rb +20 -0
  16. data/lib/liquid2/expressions/arguments.rb +25 -0
  17. data/lib/liquid2/expressions/array.rb +20 -0
  18. data/lib/liquid2/expressions/blank.rb +41 -0
  19. data/lib/liquid2/expressions/boolean.rb +20 -0
  20. data/lib/liquid2/expressions/filtered.rb +136 -0
  21. data/lib/liquid2/expressions/identifier.rb +43 -0
  22. data/lib/liquid2/expressions/lambda.rb +53 -0
  23. data/lib/liquid2/expressions/logical.rb +71 -0
  24. data/lib/liquid2/expressions/loop.rb +79 -0
  25. data/lib/liquid2/expressions/path.rb +33 -0
  26. data/lib/liquid2/expressions/range.rb +28 -0
  27. data/lib/liquid2/expressions/relational.rb +119 -0
  28. data/lib/liquid2/expressions/template_string.rb +20 -0
  29. data/lib/liquid2/filter.rb +95 -0
  30. data/lib/liquid2/filters/array.rb +202 -0
  31. data/lib/liquid2/filters/date.rb +20 -0
  32. data/lib/liquid2/filters/default.rb +16 -0
  33. data/lib/liquid2/filters/json.rb +15 -0
  34. data/lib/liquid2/filters/math.rb +87 -0
  35. data/lib/liquid2/filters/size.rb +11 -0
  36. data/lib/liquid2/filters/slice.rb +17 -0
  37. data/lib/liquid2/filters/sort.rb +96 -0
  38. data/lib/liquid2/filters/string.rb +204 -0
  39. data/lib/liquid2/loader.rb +59 -0
  40. data/lib/liquid2/loaders/file_system_loader.rb +76 -0
  41. data/lib/liquid2/loaders/mixins.rb +52 -0
  42. data/lib/liquid2/node.rb +113 -0
  43. data/lib/liquid2/nodes/comment.rb +18 -0
  44. data/lib/liquid2/nodes/output.rb +24 -0
  45. data/lib/liquid2/nodes/tags/assign.rb +35 -0
  46. data/lib/liquid2/nodes/tags/block_comment.rb +26 -0
  47. data/lib/liquid2/nodes/tags/capture.rb +40 -0
  48. data/lib/liquid2/nodes/tags/case.rb +111 -0
  49. data/lib/liquid2/nodes/tags/cycle.rb +63 -0
  50. data/lib/liquid2/nodes/tags/decrement.rb +29 -0
  51. data/lib/liquid2/nodes/tags/doc.rb +24 -0
  52. data/lib/liquid2/nodes/tags/echo.rb +31 -0
  53. data/lib/liquid2/nodes/tags/extends.rb +3 -0
  54. data/lib/liquid2/nodes/tags/for.rb +155 -0
  55. data/lib/liquid2/nodes/tags/if.rb +84 -0
  56. data/lib/liquid2/nodes/tags/include.rb +123 -0
  57. data/lib/liquid2/nodes/tags/increment.rb +29 -0
  58. data/lib/liquid2/nodes/tags/inline_comment.rb +28 -0
  59. data/lib/liquid2/nodes/tags/liquid.rb +29 -0
  60. data/lib/liquid2/nodes/tags/macro.rb +3 -0
  61. data/lib/liquid2/nodes/tags/raw.rb +30 -0
  62. data/lib/liquid2/nodes/tags/render.rb +137 -0
  63. data/lib/liquid2/nodes/tags/tablerow.rb +143 -0
  64. data/lib/liquid2/nodes/tags/translate.rb +3 -0
  65. data/lib/liquid2/nodes/tags/unless.rb +23 -0
  66. data/lib/liquid2/nodes/tags/with.rb +3 -0
  67. data/lib/liquid2/parser.rb +917 -0
  68. data/lib/liquid2/scanner.rb +595 -0
  69. data/lib/liquid2/static_analysis.rb +301 -0
  70. data/lib/liquid2/tag.rb +22 -0
  71. data/lib/liquid2/template.rb +182 -0
  72. data/lib/liquid2/undefined.rb +131 -0
  73. data/lib/liquid2/utils/cache.rb +80 -0
  74. data/lib/liquid2/utils/chain_hash.rb +40 -0
  75. data/lib/liquid2/utils/unescape.rb +119 -0
  76. data/lib/liquid2/version.rb +5 -0
  77. data/lib/liquid2.rb +90 -0
  78. data/performance/benchmark.rb +73 -0
  79. data/performance/memory_profile.rb +62 -0
  80. data/performance/profile.rb +71 -0
  81. data/sig/liquid2.rbs +2348 -0
  82. data.tar.gz.sig +0 -0
  83. metadata +164 -0
  84. metadata.gz.sig +0 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 179efed7b2ddc977ca19af55dbf412e8c8f4dda6b81c12d674bd1126b34c4cfa
4
+ data.tar.gz: 6645357f4c601ca0de255d5d6f6ab639f3bfa07709387f5e7b983f76a30a4903
5
+ SHA512:
6
+ metadata.gz: 9bb548ec7455d05fd83e8a13bf46bb3e2ccd28cdef322a0d866c0f78a111684f21c8e7b246047254f4167790501b18ff7412681a35b01a914a130f139cb819ae
7
+ data.tar.gz: '0827b948829f77dd64feee3f1dea14291d6a011dabf56dd57b448c373254a93f4017a628eed857e78fe68f3510029561b3fc7f4ef1716a87ccaf5a8a6114d312'
checksums.yaml.gz.sig ADDED
Binary file
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ plugins:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+ - rubocop-performance
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.0
8
+ NewCops: enable
9
+ Exclude:
10
+ - "test/golden_liquid/*"
11
+
12
+ Style/StringLiterals:
13
+ EnforcedStyle: double_quotes
14
+
15
+ Style/StringLiteralsInInterpolation:
16
+ EnforcedStyle: double_quotes
17
+
18
+ Layout/LineLength:
19
+ Max: 100
20
+
21
+ Metrics/ModuleLength:
22
+ Enabled: false
23
+
24
+ Metrics/MethodLength:
25
+ Enabled: false
26
+
27
+ Metrics/AbcSize:
28
+ Enabled: false
29
+
30
+ Metrics/PerceivedComplexity:
31
+ Max: 100
32
+
33
+ Metrics/CyclomaticComplexity:
34
+ Max: 50
35
+
36
+ Metrics/ParameterLists:
37
+ Enabled: false
38
+
39
+ Metrics/ClassLength:
40
+ Enabled: false
41
+
42
+ Metrics/BlockLength:
43
+ Enabled: false
44
+
45
+ Lint/UnusedMethodArgument:
46
+ AllowUnusedKeywordArguments: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.3
@@ -0,0 +1,32 @@
1
+ {
2
+ "rubocop.mode": "enableUnconditionally",
3
+ "cSpell.words": [
4
+ "bigdecimal",
5
+ "bytesize",
6
+ "byteslice",
7
+ "charpos",
8
+ "Cyclomatic",
9
+ "endcapture",
10
+ "endcase",
11
+ "endcomment",
12
+ "endraw",
13
+ "forloop",
14
+ "ivar",
15
+ "keyreq",
16
+ "kwargs",
17
+ "ljust",
18
+ "lparen",
19
+ "lstrip",
20
+ "ndigits",
21
+ "nodoc",
22
+ "parentloop",
23
+ "rparen",
24
+ "rpartition",
25
+ "rstrip",
26
+ "tablerow",
27
+ "truncatewords"
28
+ ],
29
+ "cSpell.enabledFileTypes": {
30
+ "ruby": true
31
+ }
32
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-03-18
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 James Prior
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2005, 2006 Tobias Luetke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,219 @@
1
+ <h1 align="center">Ruby Liquid2</h1>
2
+
3
+ <p align="center">
4
+ Liquid templates for Ruby, with some extra features.
5
+ </p>
6
+
7
+ ---
8
+
9
+ **Table of Contents**
10
+
11
+ - [Install](#install)
12
+ - [Example](#example)
13
+ - [Links](#links)
14
+ - [About](#about)
15
+ - [Usage](#usage)
16
+
17
+ ## Install
18
+
19
+ TODO
20
+
21
+ ## Example
22
+
23
+ ```ruby
24
+ require "liquid2"
25
+
26
+ template = Liquid2.parse("Hello, {{ you }}!")
27
+ puts template.render("you" => "World") # Hello, World!
28
+ puts template.render("you" => "Liquid") # Hello, Liquid!
29
+ ```
30
+
31
+ ## Links
32
+
33
+ - Change log: https://github.com/jg-rp/ruby-liquid2/blob/main/CHANGELOG.md
34
+ - RubyGems: TODO
35
+ - Source code: https://github.com/jg-rp/ruby-liquid2
36
+ - Issue tracker: https://github.com/jg-rp/ruby-liquid2/issues
37
+
38
+ ## About
39
+
40
+ This project aims to be mostly compatible with [Shopify/liquid](https://github.com/Shopify/liquid), but with fewer quirks and some new features.
41
+
42
+ For those already familiar with Liquid, here's a quick description of the features added in Liquid2. Also see [test/test_compliance.rb](https://github.com/jg-rp/ruby-liquid2/blob/main/test/test_compliance.rb) for a list of [golden tests](https://github.com/jg-rp/golden-liquid) that we skip.
43
+
44
+ ### Additional features
45
+
46
+ #### "Proper" string literal parsing
47
+
48
+ String literals can contain markup delimiters (`{{`, `}}`, `{%`, `%}`, `{#` and `#}`) and c-like escape sequences without interfering with template parsing. Escape sequences follow JSON string syntax and semantics, with the addition of single quoted strings and the `\'` escape sequence.
49
+
50
+ ```liquid
51
+ {% assign x = "Hi \uD83D\uDE00!" %}
52
+ {{ x }}
53
+ ```
54
+
55
+ **Output:**
56
+
57
+ ```
58
+ Hi 😀!
59
+ ```
60
+
61
+ #### String interpolation
62
+
63
+ String literals support interpolation using JavaScript-style `${` and `}`. Any single or double quoted string can use `${variable_name}` placeholders for automatic variable substitution, and `${` can be escaped with `\${` to prevent variable substitution.
64
+
65
+ Liquid template strings are effectively a shorthand alternative to `capture` tags or chains of `append` filters, which is especially useful when building short strings in `{% liquid %}` tags. These two tags are equivalent.
66
+
67
+ ```liquid2
68
+ {% capture greeting %}
69
+ Hello, {{ you | capitalize }}!
70
+ {% endcapture %}
71
+
72
+ {% assign greeting = 'Hello, ${you | capitalize}!' %}
73
+ ```
74
+
75
+ #### Array literals
76
+
77
+ Filtered expressions (those found in output statements, the `assign` tag and the `echo` tag) and `for` tag expressions support array literal syntax. We don't use the traditional `[item1, item2, ...]` syntax with square brackets because square brackets are already used for variables (`["some variable with spaces"]` is a valid variable).
78
+
79
+ ```liquid2
80
+ {% assign my_array = a, b, '42', false -%}
81
+ {% for item in my_array -%}
82
+ - {{ item }}
83
+ {% endfor %}
84
+ ```
85
+
86
+ or, using a `{% liquid %}` tag:
87
+
88
+ ```liquid2
89
+ {% liquid
90
+ for item in a, b, '42', false
91
+ echo "- ${item}\n"
92
+ endfor %}
93
+ ```
94
+
95
+ With `a` set to `"Hello"` and `b` set to `"World"`, both of the examples above produce the following output.
96
+
97
+ ```plain title="output"
98
+ - Hello
99
+ - World
100
+ - 42
101
+ - false
102
+ ```
103
+
104
+ #### Logical `not`
105
+
106
+ Logical expressions now support negation with the `not` operator and grouping terms with parentheses. Without parentheses, logical `and` takes priority over logical `or`.
107
+
108
+ In this example, `{% if not user %}` is equivalent to `{% unless user %}`, however, `not` can also be used after `and` and `or`, like `{% if user.active and not user.title %}`, potentially saving nested `if` and `unless` tags.
109
+
110
+ ```liquid2
111
+ {% if not user %}
112
+ please log in
113
+ {% else %}
114
+ hello user
115
+ {% endif %}
116
+ ```
117
+
118
+ #### Inline conditional and relational expressions
119
+
120
+ In most expressions where you'd normally provide a literal (string, integer, float, true, false, nil/null) or variable name/path (foo.bar[0]), you can now use an inline conditional or relational expression.
121
+
122
+ See [Shopify/liquid #1922](https://github.com/Shopify/liquid/pull/1922) and [jg-rp/liquid #175](https://github.com/jg-rp/liquid/pull/175).
123
+
124
+ These two templates are equivalent.
125
+
126
+ ```liquid
127
+ {{ user.name || "guest" }}
128
+ ```
129
+
130
+ ```liquid
131
+ {% if user.name %}{{ user.name }}{% else %}guest{% endif %}
132
+ ```
133
+
134
+ #### Ternary expressions
135
+
136
+ Output statements, the `{% assign %}` tag and the `{% echo %}` tag support ternary expressions.
137
+
138
+ ```liquid2
139
+ {{ a if b else c }}
140
+ {{ a | upcase if b == 'foo' else c || split }}
141
+ ```
142
+
143
+ Either branch can use filters with the usual single pipe character (`|`), like `upcase` in the examples above. Filters following a double pipe (`||`) are _tail filters_, which apply to both branches.
144
+
145
+ #### Lambda expressions
146
+
147
+ Many built-in filters that operate on arrays now accept lambda expression arguments. For example, we can use the `where` filter to select values according to an arbitrary Boolean expression.
148
+
149
+ ```liquid2
150
+ {% assign coding_pages = pages | where: page => page.tags contains 'coding' %}
151
+ ```
152
+
153
+ #### Dedicated comment syntax
154
+
155
+ Comments surrounded by `{#` and `#}` are enabled by default. Additional `#`'s can be added to comment out blocks of markup that already contain comments, as long as hashes are balanced.
156
+
157
+ ```liquid2
158
+ {## comment this out for now
159
+ {% for x in y %}
160
+ {# x could be empty #}
161
+ {{ x | default: TODO}}
162
+ {% endfor %}
163
+ ##}
164
+ ```
165
+
166
+ #### More whitespace control
167
+
168
+ Tags and the output statement support `-` and `~` for controlling whitespace in templates. By default, `~` will remove newlines but retain space and tab characters.
169
+
170
+ Here we use `~` to remove the newline after the opening `for` tag, but preserve indentation before `<li>`.
171
+
172
+ ```liquid2
173
+ <ul>
174
+ {% for x in (1..4) ~%}
175
+ <li>{{ x }}</li>
176
+ {% endfor -%}
177
+ </ul>
178
+ ```
179
+
180
+ ```plain title="output"
181
+ <ul>
182
+ <li>1</li>
183
+ <li>2</li>
184
+ <li>3</li>
185
+ <li>4</li>
186
+ </ul>
187
+ ```
188
+
189
+ #### Scientific notation
190
+
191
+ Integer and float literals can use scientific notation, like `1.2e3` or `1e-2`.
192
+
193
+ ## Usage
194
+
195
+ TODO
196
+
197
+ ## Development
198
+
199
+ TODO
200
+
201
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
202
+
203
+ ## Contributing
204
+
205
+ ### Profiling
206
+
207
+ #### CPU profile
208
+
209
+ Dump profile data with `bundle exec ruby performance/profile.rb`, then generate an HTML flame graph with, changing the file names appropriately:
210
+
211
+ ```
212
+ bundle exec stackprof --d3-flamegraph .stackprof-cpu-parse.dump > flamegraph-cpu-parse.html
213
+ ```
214
+
215
+ ## License
216
+
217
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
218
+
219
+ This project is not affiliated with Shopify, but we do reference [Shopify/liquid](https://github.com/Shopify/liquid) frequently and have used code from Shopify/liquid. See `LICENSE_SHOPIFY.txt` for a copy of the Shopify/liquid license.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new do |task|
11
+ task.plugins << "rubocop-minitest"
12
+ task.plugins << "rubocop-rake"
13
+ task.plugins << "rubocop-performance"
14
+ end
15
+
16
+ require "steep/rake_task"
17
+
18
+ Steep::RakeTask.new do |t|
19
+ t.check.severity_level = :error
20
+ t.watch.verbose
21
+ end
22
+
23
+ task default: %i[test rubocop steep]
data/Steepfile ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ D = Steep::Diagnostic
4
+
5
+ target :lib do
6
+ signature "sig"
7
+ check "lib"
8
+
9
+ library "base64"
10
+ library "bigdecimal"
11
+ library "cgi"
12
+ library "json"
13
+ library "strscan"
14
+ library "stringio"
15
+ library "time"
16
+ library "pathname"
17
+ library "monitor"
18
+
19
+ # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting
20
+ # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
21
+ # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
22
+ # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
23
+ # configure_code_diagnostics do |hash| # You can setup everything yourself
24
+ # hash[D::Ruby::NoMethod] = :information
25
+ # end
26
+ end