Skip to content

Commit

Permalink
Compiler: rename __buf__ to __buffer__
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Feb 19, 2024
1 parent 6e836d3 commit 169717c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
20 changes: 10 additions & 10 deletions lib/papercraft/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def self.__cache_compiled_template__(value)
end
end

def self.__emit__(value, __buf__, *args)
def self.__emit__(value, __buffer__, *args)
case value
when Proc, Papercraft::Template
compiled = __cache_compiled_template__(value)
compiled.(__buf__, *args)
compiled.(__buffer__, *args)
else
__buf__ << CGI.escapeHTML(value.to_s)
__buffer__ << CGI.escapeHTML(value.to_s)
end
end

Expand Down Expand Up @@ -72,7 +72,7 @@ def emit_text_fcall(node)
value = node.children.first.to_s
emit_text(value, encoding: :html)
when :VCALL
emit_code("__buf__ << CGI.escapeHTML((#{node.children.first}).to_s)\n")
emit_code("__buffer__ << CGI.escapeHTML((#{node.children.first}).to_s)\n")
when :CONST
name = node.children.first.to_s
value = get_const(name)
Expand Down Expand Up @@ -124,7 +124,7 @@ def emit_expression
def flush_emit_buffer
return if !@emit_buffer

@code_buffer << "#{' ' * @level}__buf__ << \"#{@emit_buffer}\"\n"
@code_buffer << "#{' ' * @level}__buffer__ << \"#{@emit_buffer}\"\n"
@emit_buffer = nil
true
end
Expand Down Expand Up @@ -158,7 +158,7 @@ def compile(template, initial_level = 0)

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

def args
Expand Down Expand Up @@ -245,7 +245,7 @@ def parse_fcall(node, block = nil)
when Papercraft::Template
@sub_templates << text
idx = @sub_templates.size - 1
emit_code("__sub_templates__[#{idx}].(__buf__)\n")
emit_code("__sub_templates__[#{idx}].(__buffer__)\n")
when String
emit_output { emit_text(text) }
when RubyVM::AbstractSyntaxTree::Node
Expand Down Expand Up @@ -320,9 +320,9 @@ def emit_emit(args, block)
value = value.children.first.to_s
emit_output { emit_literal(value) }
when :VCALL
emit_code("__buf__ << #{value.children.first}\n")
emit_code("__buffer__ << #{value.children.first}\n")
when :DVAR
emit_code("Papercraft.__emit__(#{value.children.first}, __buf__")
emit_code("Papercraft.__emit__(#{value.children.first}, __buffer__")
if !rest.empty?
emit_code(", ")
parse_list(args, false, 1..-1)
Expand All @@ -335,7 +335,7 @@ def emit_emit(args, block)
when Papercraft::Template
@sub_templates << value
idx = @sub_templates.size - 1
emit_code("__sub_templates__[#{idx}].(__buf__)\n")
emit_code("__sub_templates__[#{idx}].(__buffer__)\n")
else
emit_output { emit_literal(value) }
end
Expand Down
58 changes: 29 additions & 29 deletions test/test_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def test_compiler_simple

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

code = compiled_template_code(templ)
expected = <<~RUBY
->(__buf__) do
__buf__ << "<h1 class=\\"foot\\">foo</h1><h2 id=\\"bar\\" onclick=\\"f(&quot;abc&quot;, &quot;def&quot;)\\">bar</h2>"
__buf__
->(__buffer__) do
__buffer__ << "<h1 class=\\"foot\\">foo</h1><h2 id=\\"bar\\" onclick=\\"f(&quot;abc&quot;, &quot;def&quot;)\\">bar</h2>"
__buffer__
end
RUBY
assert_equal template_body(expected), code
Expand All @@ -99,7 +99,7 @@ def test_compiler_conditional_1
}

code = compiled_template_body(template)
assert_equal " __buf__ << \"<h1>\#{CGI.escapeHTML((a ? \"foo\" : \"bar\").to_s)}</h1>\"\n", code
assert_equal " __buffer__ << \"<h1>\#{CGI.escapeHTML((a ? \"foo\" : \"bar\").to_s)}</h1>\"\n", code
end

def test_compiler_conditional_2
Expand All @@ -112,13 +112,13 @@ def test_compiler_conditional_2

code = compiled_template_body(template)
expected = <<~RUBY
__buf__ << "<header>hi</header>"
__buffer__ << "<header>hi</header>"
if a
__buf__ << "<p>foo</p><p>bar</p>"
__buffer__ << "<p>foo</p><p>bar</p>"
else
__buf__ << "<h3>baz</h3>"
__buffer__ << "<h3>baz</h3>"
end
__buf__ << "<footer>bye</footer>"
__buffer__ << "<footer>bye</footer>"
RUBY
assert_equal template_body(expected), code.chomp
end
Expand All @@ -133,10 +133,10 @@ def test_compiler_conditional_3
code = compiled_template_body(template)
expected = <<~RUBY
if a
__buf__ << "<h1>hi</h1>"
__buffer__ << "<h1>hi</h1>"
end
unless a
__buf__ << "<h2>bye</h2>"
__buffer__ << "<h2>bye</h2>"
end
RUBY
assert_equal template_body(expected), code.chomp
Expand All @@ -158,12 +158,12 @@ def test_compiler_conditional_4
code = compiled_template_body(template)
expected = <<~RUBY
if a
__buf__ << "<h1>foo</h1>"
__buffer__ << "<h1>foo</h1>"
else
if b
__buf__ << "<h2>bar</h2>"
__buffer__ << "<h2>bar</h2>"
else
__buf__ << "<h3>baz</h3>"
__buffer__ << "<h3>baz</h3>"
end
end
RUBY
Expand Down Expand Up @@ -292,11 +292,11 @@ def test_text

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

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

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

expected = <<~RUBY
__buf__ << "<p>\#{CGI.escapeHTML((foo).to_s)}</p>"
__buffer__ << "<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 @@ -393,7 +393,7 @@ def test_tag_content_const
}

expected = <<~RUBY
__buf__ << "<p>43</p>"
__buffer__ << "<p>43</p>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp
assert_equal '<p>43</p>', t.to_proc.render
Expand All @@ -407,7 +407,7 @@ def test_method_chain

expected = <<~RUBY
1.next.next.next
__buf__ << "<p>\#{CGI.escapeHTML((2.next.next.next).to_s)}</p>"
__buffer__ << "<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
Expand All @@ -421,7 +421,7 @@ def test_ivar
}

expected = <<~RUBY
__buf__ << "<p>\#{CGI.escapeHTML((@foo).to_s)}</p>"
__buffer__ << "<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
Expand All @@ -438,9 +438,9 @@ def test_sub_template_with_args
}

expected = <<~RUBY
__buf__ << "<div>"
Papercraft.__emit__(sub, __buf__, 40 + 2)
__buf__ << "</div>"
__buffer__ << "<div>"
Papercraft.__emit__(sub, __buffer__, 40 + 2)
__buffer__ << "</div>"
RUBY
assert_equal template_body(expected), t.code_buffer.chomp
assert_equal '<div><p>42</p></div>', t.to_proc.render
Expand Down

0 comments on commit 169717c

Please sign in to comment.