Skip to content

Commit

Permalink
Compiler: remove ctx argument, fix ivars
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Feb 19, 2024
1 parent e62fa5b commit deb9913
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
9 changes: 4 additions & 5 deletions lib/papercraft/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def compile(template, initial_level = 0)

def to_code
pad = ' ' * @level
"#{pad}->(__buf__, __ctx__#{args}) do\n#{prelude}#{@code_buffer}#{pad} __buf__\n#{pad}end"
"#{pad}->(__buf__#{args}) do\n#{prelude}#{@code_buffer}#{pad} __buf__\n#{pad}end"
end

def args
Expand Down Expand Up @@ -199,8 +199,7 @@ def parse_iter(node)
end

def parse_ivar(node)
ivar = node.children.first.match(/^@(.+)*/)[1]
emit_literal("__ctx__[:#{ivar}]")
emit_literal(node.children.first.to_s)
end

def parse_fcall(node, block = nil)
Expand All @@ -226,7 +225,7 @@ def parse_fcall(node, block = nil)
when Papercraft::Template
@sub_templates << text
idx = @sub_templates.size - 1
emit_code("__sub_templates__[#{idx}].(__buf__, __ctx__)\n")
emit_code("__sub_templates__[#{idx}].(__buf__)\n")
when String
emit_output { emit_text(text) }
when RubyVM::AbstractSyntaxTree::Node
Expand Down Expand Up @@ -308,7 +307,7 @@ def emit_emit(node, block)
when Papercraft::Template
@sub_templates << value
idx = @sub_templates.size - 1
emit_code("__sub_templates__[#{idx}].(__buf__, __ctx__)\n")
emit_code("__sub_templates__[#{idx}].(__buf__)\n")
else
emit_output { emit_literal(value) }
end
Expand Down
51 changes: 34 additions & 17 deletions test/test_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'papercraft'

module ::Kernel
def C(**ctx, &block)
Papercraft::Template.new(**ctx, &block).compile
def C(&block)
Papercraft::Template.new(&block).compile
.tap { |c|
if ENV['DEBUG'] == '1'
puts '*' * 40; puts c.to_code; puts
Expand All @@ -13,14 +13,19 @@ def C(**ctx, &block)
.to_proc
end

def c(**ctx, &block)
Papercraft::Template.new(**ctx, &block).compile
end
def c(&block)
Papercraft::Template.new(&block).compile
.tap { |c|
if ENV['DEBUG'] == '1'
puts '*' * 40; puts c.to_code; puts
end
}
end
end

class ::Proc
def render(*args)
+''.tap { |b| call(b, {}, *args) }
+''.tap { |b| call(b, *args) }
end
end

Expand Down Expand Up @@ -58,7 +63,7 @@ def test_compiler_simple

code = compiled_template_code(templ)
expected = <<~RUBY
->(__buf__, __ctx__) do
->(__buf__) do
__buf__ << "<h1>foo</h1><h2>bar</h2>"
__buf__
end
Expand All @@ -74,7 +79,7 @@ def test_compiler_simple_with_attributes

code = compiled_template_code(templ)
expected = <<~RUBY
->(__buf__, __ctx__) do
->(__buf__) do
__buf__ << "<h1 class=\\"foot\\">foo</h1><h2 id=\\"bar\\" onclick=\\"f(&quot;abc&quot;, &quot;def&quot;)\\">bar</h2>"
__buf__
end
Expand All @@ -83,7 +88,7 @@ def test_compiler_simple_with_attributes

template_proc = compiled_template_proc(templ)
buffer = +''
template_proc.(buffer, nil)
template_proc.(buffer)
assert_equal '<h1 class="foot">foo</h1><h2 id="bar" onclick="f(&quot;abc&quot;, &quot;def&quot;)">bar</h2>', buffer
end

Expand Down Expand Up @@ -174,7 +179,7 @@ def test_compile
assert_kind_of Papercraft::Compiler, c
p = c.to_proc
b = +''
p.(b, nil)
p.(b)

assert_equal '<h1>foo</h1>', b
end
Expand Down Expand Up @@ -287,7 +292,7 @@ def test_text

c = t.compile
expected = <<~RUBY.chomp
->(__buf__, __ctx__) do
->(__buf__) do
__buf__ << "foo&amp;bar"
__buf__ << CGI.escapeHTML((__baz__).to_s)
__buf__ << "boo"
Expand Down Expand Up @@ -360,7 +365,7 @@ def test_tag_content_expr
}

expected = <<~RUBY
__buf__ << "<p>\#{CGI.escapeHTML((1 + 2).to_s)}</p>"
__buf__ << "<p>\#{CGI.escapeHTML((1 + 2).to_s)}</p>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp

Expand All @@ -374,7 +379,7 @@ def test_tag_content_var
}

expected = <<~RUBY
__buf__ << "<p>\#{CGI.escapeHTML((foo).to_s)}</p>"
__buf__ << "<p>\#{CGI.escapeHTML((foo).to_s)}</p>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp
assert_equal '<p>42</p>', t.to_proc.render
Expand All @@ -388,7 +393,7 @@ def test_tag_content_const
}

expected = <<~RUBY
__buf__ << "<p>43</p>"
__buf__ << "<p>43</p>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp
assert_equal '<p>43</p>', t.to_proc.render
Expand All @@ -401,12 +406,24 @@ def test_method_chain
}

expected = <<~RUBY
1.next.next.next
__buf__ << "<p>\#{CGI.escapeHTML((2.next.next.next).to_s)}</p>"
1.next.next.next
__buf__ << "<p>\#{CGI.escapeHTML((2.next.next.next).to_s)}</p>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp
assert_equal '<p>5</p>', t.to_proc.render
end

def test_
def test_ivar
@foo = 'bar'

t = c {
p @foo
}

expected = <<~RUBY
__buf__ << "<p>\#{CGI.escapeHTML((@foo).to_s)}</p>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp
assert_equal '<p>bar</p>', t.to_proc.render
end
end

0 comments on commit deb9913

Please sign in to comment.