liquid 5.9.0 → 5.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 112da43191fdf32af9afb1ad6322ff2fab8b22b6d658073980ba236aa1834213
4
- data.tar.gz: b7ab5c91bc2f65b91a782027eedd8374f1f46629c791d6b2c07a8ba2c07d7314
3
+ metadata.gz: 1b12ad70375f0ed9e3a2bab4a0a53ea565f3082af8d5bc06f4dea13a0a4ac574
4
+ data.tar.gz: 04c2f7494b1ab0f97a2a33ad9d39c9c573a33b42bd109df8d42bb5e26d245f01
5
5
  SHA512:
6
- metadata.gz: c941dc92d57cf97c30a6db6eb206e7f00b780070b8dc790def3a8be06a3714562813b0ca8da34d5745be4432c839cf2f5946557d103ae0e56ffe5822b4ebf1e6
7
- data.tar.gz: 9491a34a69273c3cebe65208f935007e68dbd81a8b32913ebc7d0056863c9f3a30b6dff9d1da3b37af66e472aedf60c6979e22d5f500ba6d24bd4f8637449a3f
6
+ metadata.gz: dc94bd83ae20e716eabf8ab2ce9f1fe9f8ff45dee6e0acac91f9095c32557c94d08e5a3c1b473c268c5a6a6d63a4d4ca10170406f062545545f63c1980a4d392
7
+ data.tar.gz: 0f19c1d79039fda7a6e485cf794cde252817dc43bd292351b7bc53d1adabd12919e18cc0870fcf3db3c0018e22ef91a1507ded76b27719fb30b79414e04d3d2f
data/History.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Liquid Change Log
2
2
 
3
+ ## 5.10.0
4
+ * Introduce support for Inline Snippets [Julia Boutin]
5
+ ```
6
+ {%- snippet snowdevil -%}
7
+ Snowdevil
8
+ {%- endsnippet -%}
9
+ {% render snowdevil %}
10
+ ```
11
+
3
12
  ## 5.9.0
4
13
  * Introduce `:rigid` error mode for stricter, safer parsing of all tags [CP Clermont, Guilherme Carreiro]
5
14
 
@@ -5,6 +5,7 @@
5
5
  block_tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: {% %{tag} %}{% end%{tag} %}"
6
6
  assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]"
7
7
  capture: "Syntax Error in 'capture' - Valid syntax: capture [var]"
8
+ snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [var]"
8
9
  case: "Syntax Error in 'case' - Valid syntax: case [condition]"
9
10
  case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}"
10
11
  case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) "
@@ -19,6 +20,7 @@
19
20
  invalid_delimiter: "'%{tag}' is not a valid delimiter for %{block_name} tags. use %{block_delimiter}"
20
21
  invalid_template_encoding: "Invalid template encoding"
21
22
  render: "Syntax error in tag 'render' - Template name must be a quoted string"
23
+ render_invalid_template_name: "Syntax error in tag 'render' - Expected a string or identifier, found %{found}"
22
24
  table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3"
23
25
  table_row_invalid_attribute: "Invalid attribute '%{attribute}' in tablerow loop. Valid attributes are cols, limit, offset, and range"
24
26
  tag_never_closed: "'%{block_name}' tag was never closed"
@@ -29,5 +31,6 @@
29
31
  variable_termination: "Variable '%{token}' was not properly terminated with regexp: %{tag_end}"
30
32
  argument:
31
33
  include: "Argument error in tag 'include' - Illegal template name"
34
+ render: "Argument error in tag 'render' - Dynamically chosen templates are not allowed"
32
35
  disabled:
33
36
  tag: "usage is not allowed in this context"
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ class SnippetDrop < Drop
5
+ attr_reader :body, :name, :filename
6
+
7
+ def initialize(body, name, filename)
8
+ super()
9
+ @body = body
10
+ @name = name
11
+ @filename = filename
12
+ end
13
+
14
+ def to_partial
15
+ @body
16
+ end
17
+
18
+ def to_s
19
+ 'SnippetDrop'
20
+ end
21
+ end
22
+ end
@@ -27,7 +27,7 @@ module Liquid
27
27
  # @liquid_syntax_keyword filename The name of the snippet to render, without the `.liquid` extension.
28
28
  class Render < Tag
29
29
  FOR = 'for'
30
- SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
30
+ SYNTAX = /(#{QuotedString}+|#{VariableSegment}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
31
31
 
32
32
  disable_tags "include"
33
33
 
@@ -47,21 +47,23 @@ module Liquid
47
47
  end
48
48
 
49
49
  def render_tag(context, output)
50
- # The expression should be a String literal, which parses to a String object
51
- template_name = @template_name_expr
52
- raise ::ArgumentError unless template_name.is_a?(String)
53
-
54
- partial = PartialCache.load(
55
- template_name,
56
- context: context,
57
- parse_context: parse_context,
58
- )
59
-
60
- context_variable_name = @alias_name || template_name.split('/').last
50
+ template = context.evaluate(@template_name_expr)
51
+
52
+ if template.respond_to?(:to_partial)
53
+ partial = template.to_partial
54
+ template_name = template.filename
55
+ context_variable_name = @alias_name || template.name
56
+ elsif @template_name_expr.is_a?(String)
57
+ partial = PartialCache.load(template, context: context, parse_context: parse_context)
58
+ template_name = partial.name
59
+ context_variable_name = @alias_name || template_name.split('/').last
60
+ else
61
+ raise ::ArgumentError
62
+ end
61
63
 
62
64
  render_partial_func = ->(var, forloop) {
63
65
  inner_context = context.new_isolated_subcontext
64
- inner_context.template_name = partial.name
66
+ inner_context.template_name = template_name
65
67
  inner_context.partial = true
66
68
  inner_context['forloop'] = forloop if forloop
67
69
 
@@ -101,14 +103,18 @@ module Liquid
101
103
  key = p.consume
102
104
  p.consume(:colon)
103
105
  @attributes[key] = safe_parse_expression(p)
104
- p.consume?(:comma)
106
+ p.consume?(:comma) # optional comma
105
107
  end
106
108
 
107
109
  p.consume(:end_of_string)
108
110
  end
109
111
 
110
112
  def rigid_template_name(p)
111
- p.consume(:string)
113
+ return p.consume(:string) if p.look(:string)
114
+ return p.consume(:id) if p.look(:id)
115
+
116
+ found = p.consume || "nothing"
117
+ raise SyntaxError, options[:locale].t("errors.syntax.render_invalid_template_name", found: found)
112
118
  end
113
119
 
114
120
  def strict_parse(markup)
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ # @liquid_public_docs
5
+ # @liquid_type tag
6
+ # @liquid_category variable
7
+ # @liquid_name snippet
8
+ # @liquid_summary
9
+ # Creates a new inline snippet.
10
+ # @liquid_description
11
+ # You can create inline snippets to make your Liquid code more modular.
12
+ # @liquid_syntax
13
+ # {% snippet snippet_name %}
14
+ # value
15
+ # {% endsnippet %}
16
+ class Snippet < Block
17
+ def initialize(tag_name, markup, options)
18
+ super
19
+ p = @parse_context.new_parser(markup)
20
+ if p.look(:id)
21
+ @to = p.consume(:id)
22
+ p.consume(:end_of_string)
23
+ else
24
+ raise SyntaxError, options[:locale].t("errors.syntax.snippet")
25
+ end
26
+ end
27
+
28
+ def render_to_output_buffer(context, output)
29
+ snippet_drop = SnippetDrop.new(@body, @to, context.template_name)
30
+ context.scopes.last[@to] = snippet_drop
31
+ context.resource_limits.increment_assign_score(assign_score_of(snippet_drop))
32
+ output
33
+ end
34
+
35
+ def blank?
36
+ true
37
+ end
38
+
39
+ private
40
+
41
+ def assign_score_of(snippet_drop)
42
+ snippet_drop.body.nodelist.sum { |node| node.to_s.bytesize }
43
+ end
44
+ end
45
+ end
data/lib/liquid/tags.rb CHANGED
@@ -20,6 +20,7 @@ require_relative "tags/raw"
20
20
  require_relative "tags/render"
21
21
  require_relative "tags/cycle"
22
22
  require_relative "tags/doc"
23
+ require_relative "tags/snippet"
23
24
 
24
25
  module Liquid
25
26
  module Tags
@@ -44,6 +45,7 @@ module Liquid
44
45
  'echo' => Echo,
45
46
  'tablerow' => TableRow,
46
47
  'doc' => Doc,
48
+ 'snippet' => Snippet,
47
49
  }.freeze
48
50
  end
49
51
  end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Liquid
5
- VERSION = "5.9.0"
5
+ VERSION = "5.10.0"
6
6
  end
data/lib/liquid.rb CHANGED
@@ -67,6 +67,7 @@ require 'liquid/i18n'
67
67
  require 'liquid/drop'
68
68
  require 'liquid/tablerowloop_drop'
69
69
  require 'liquid/forloop_drop'
70
+ require 'liquid/snippet_drop'
70
71
  require 'liquid/extensions'
71
72
  require 'liquid/errors'
72
73
  require 'liquid/interrupts'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquid
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.9.0
4
+ version: 5.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Lütke
@@ -105,6 +105,7 @@ files:
105
105
  - lib/liquid/range_lookup.rb
106
106
  - lib/liquid/registers.rb
107
107
  - lib/liquid/resource_limits.rb
108
+ - lib/liquid/snippet_drop.rb
108
109
  - lib/liquid/standardfilters.rb
109
110
  - lib/liquid/strainer_template.rb
110
111
  - lib/liquid/tablerowloop_drop.rb
@@ -130,6 +131,7 @@ files:
130
131
  - lib/liquid/tags/inline_comment.rb
131
132
  - lib/liquid/tags/raw.rb
132
133
  - lib/liquid/tags/render.rb
134
+ - lib/liquid/tags/snippet.rb
133
135
  - lib/liquid/tags/table_row.rb
134
136
  - lib/liquid/tags/unless.rb
135
137
  - lib/liquid/template.rb