checkpoint

This commit is contained in:
Josh Faigan
2024-09-30 11:25:48 -04:00
parent 98a69c80ef
commit 404d71613c
3 changed files with 96 additions and 18 deletions
+59
View File
@@ -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
+5 -5
View File
@@ -14,17 +14,17 @@ module Liquid
# value
# {% endsnippet %}
class Snippet < Block
# SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
SYNTAX = /(#{QuotedString}+)/o
SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
def initialize(tag_name, markup, options)
super
if markup =~ SYNTAX
# binding.irb
@to = Regexp.last_match(1)
# arg = Regexp.last_match(2)
arg = Regexp.last_match(2)
# @args = []
# @args << arg if arg
@args = []
@args << arg if arg
else
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
end
+32 -13
View File
@@ -75,7 +75,7 @@ class SnippetTest < Minitest::Test
def test_render_inline_snippet_with_argument
# This passes whether or not we have the new or old SYNTAX
template = <<~LIQUID.strip
{% snippet "input" |type, value| %}
{% snippet "input" |type| %}
<input type="{{ type }}" />
{% endsnippet %}
@@ -89,19 +89,38 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_inline_snippet_with_multiple_arguments
template = <<~LIQUID.strip
{% snippet "input" |type, value| %}
<input type="{{ type }}" value="{{ value }}" />
{% endsnippet %}
# def test_render_inline_snippet_with_multiple_arguments
# template = <<~LIQUID.strip
# {% snippet "input" |type, value| %}
# <input type="{{ type }}" value="{{ value }}" />
# {% endsnippet %}
{%- render "input", type: "text", value: "Hello" -%}
LIQUID
expected = <<~OUTPUT
# {%- render "input", type: "text", value: "Hello" -%}
# LIQUID
# expected = <<~OUTPUT
<input type="text" value="Hello" />
OUTPUT
# <input type="text" value="Hello" />
# OUTPUT
assert_template_result(expected, template)
end
# assert_template_result(expected, template)
# 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