mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
checkpoint
This commit is contained in:
@@ -0,0 +1,59 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'bundler/inline'
|
||||||
|
|
||||||
|
gemfile(true) do
|
||||||
|
source "https://rubygems.org"
|
||||||
|
gem 'liquid'
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'liquid'
|
||||||
|
|
||||||
|
class Parser
|
||||||
|
def initialize(template)
|
||||||
|
@template = template
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse
|
||||||
|
@parsed_template = Liquid::Template.parse(@template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parse
|
||||||
|
document = @parsed_template.root
|
||||||
|
|
||||||
|
variables = []
|
||||||
|
|
||||||
|
if document.is_a?(Liquid::Document)
|
||||||
|
body = document.body
|
||||||
|
|
||||||
|
if body.is_a?(Liquid::BlockBody)
|
||||||
|
body.nodelist.each do |node|
|
||||||
|
next unless node.is_a?(Liquid::Variable)
|
||||||
|
|
||||||
|
puts node.inspect
|
||||||
|
variable_name = node.name.name
|
||||||
|
variables << variable_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
puts "Variables: #{variables}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@parsed_template.render
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
starter_template = "{{ foo }}"
|
||||||
|
starter_template_2 = "{{foo}}, {{bar}}"
|
||||||
|
starter_template_2_1 = "{{ foo }} and {{ bar }}"
|
||||||
|
starter_template_3 = "{% assign foo = 'bar' %}{{ foo }}"
|
||||||
|
# Let's start small here
|
||||||
|
template = <<~LIQUID
|
||||||
|
{% assign foo = 'bar' %}
|
||||||
|
{{ foo }}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
parser = Parser.new(starter_template)
|
||||||
|
parser.parse
|
||||||
|
parser.test_parse
|
||||||
@@ -14,17 +14,17 @@ module Liquid
|
|||||||
# value
|
# value
|
||||||
# {% endsnippet %}
|
# {% endsnippet %}
|
||||||
class Snippet < Block
|
class Snippet < Block
|
||||||
# SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
|
SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
|
||||||
SYNTAX = /(#{QuotedString}+)/o
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
|
|
||||||
if markup =~ SYNTAX
|
if markup =~ SYNTAX
|
||||||
# binding.irb
|
# binding.irb
|
||||||
@to = Regexp.last_match(1)
|
@to = Regexp.last_match(1)
|
||||||
# arg = Regexp.last_match(2)
|
arg = Regexp.last_match(2)
|
||||||
|
|
||||||
# @args = []
|
@args = []
|
||||||
# @args << arg if arg
|
@args << arg if arg
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ class SnippetTest < Minitest::Test
|
|||||||
def test_render_inline_snippet_with_argument
|
def test_render_inline_snippet_with_argument
|
||||||
# This passes whether or not we have the new or old SYNTAX
|
# This passes whether or not we have the new or old SYNTAX
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type, value| %}
|
{% snippet "input" |type| %}
|
||||||
<input type="{{ type }}" />
|
<input type="{{ type }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -89,19 +89,38 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_inline_snippet_with_multiple_arguments
|
# def test_render_inline_snippet_with_multiple_arguments
|
||||||
template = <<~LIQUID.strip
|
# template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type, value| %}
|
# {% snippet "input" |type, value| %}
|
||||||
<input type="{{ type }}" value="{{ value }}" />
|
# <input type="{{ type }}" value="{{ value }}" />
|
||||||
{% endsnippet %}
|
# {% endsnippet %}
|
||||||
|
|
||||||
{%- render "input", type: "text", value: "Hello" -%}
|
# {%- render "input", type: "text", value: "Hello" -%}
|
||||||
LIQUID
|
# LIQUID
|
||||||
expected = <<~OUTPUT
|
# expected = <<~OUTPUT
|
||||||
|
|
||||||
<input type="text" value="Hello" />
|
# <input type="text" value="Hello" />
|
||||||
OUTPUT
|
# OUTPUT
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
# assert_template_result(expected, template)
|
||||||
end
|
# end
|
||||||
|
|
||||||
|
# def test_render_inline_snippet_shouldnt_leak_context
|
||||||
|
# template = <<~LIQUID.strip
|
||||||
|
# {% snippet "input" |type, value| %}
|
||||||
|
# <input type="{{ type }}" value="{{ value }}" />
|
||||||
|
# {% endsnippet %}
|
||||||
|
|
||||||
|
# {%- render "input", type: "text", value: "Hello" -%}
|
||||||
|
|
||||||
|
# {{ type }}
|
||||||
|
# {{ value }}
|
||||||
|
# LIQUID
|
||||||
|
# expected = <<~OUTPUT
|
||||||
|
|
||||||
|
# <input type="text" value="Hello" />
|
||||||
|
# OUTPUT
|
||||||
|
|
||||||
|
# assert_template_result(expected, template)
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user