diff --git a/lib/papercraft/compiler.rb b/lib/papercraft/compiler.rb index 52adbba..3b7f6a9 100644 --- a/lib/papercraft/compiler.rb +++ b/lib/papercraft/compiler.rb @@ -22,7 +22,7 @@ def render_emit_call(o, *a, **b, &block) when nil # do nothing when ::Proc - raise NotImplementedError + Papercraft.html(&o).render(*a, **b, &block) else o.to_s end @@ -49,20 +49,22 @@ def inject_buffer_parameter(node) node.inject_parameters('__buffer__') end - def emit(str) - if @embed_mode - @embed_buffer << str - else - @buffer << str - end - end - def embed_visit(node, pre = '', post = '') + tmp_last_loc_start = @last_loc_start + tmp_last_loc_end = @last_loc_end + @last_loc_start = loc_start(node.location) + @last_loc_end = loc_end(node.location) + @embed_mode = true - @embed_buffer = +'' + tmp_buffer = @buffer + @buffer = +'' visit(node) @embed_mode = false - @html_buffer << "#{pre}#{@embed_buffer}#{post}" + @html_buffer << "#{pre}#{@buffer}#{post}" + @buffer = tmp_buffer + + @last_loc_start = tmp_last_loc_start + @last_loc_end = tmp_last_loc_end end def html_embed_visit(node) @@ -77,10 +79,6 @@ def tag_attr_embed_visit(node, key) end end - def adjust_whitespace(loc) - super(loc) if !@embed_mode - end - def emit_code(loc, semicolon: false) flush_html_buffer if !@embed_mode super @@ -105,7 +103,7 @@ def flush_html_buffer end def visit_call_node(node) - return super if node.receiver + return super if node.receiver || @embed_mode @html_location_start ||= node.location @@ -160,9 +158,6 @@ def emit_tag_inner_text(node) end def emit_tag_attributes(node, attrs) - # puts - # p emit_tag_attributes: node, attrs: attrs - attrs.elements.each do |e| emit_html(" ") @@ -188,17 +183,6 @@ def emit_tag_attribute_node(node, key = false) end end - def emit_html_text(node) - args = node.arguments&.arguments - return nil if !args - - emit_tag_inner_text(args[0]) - end - - def emit_html_emit(node) - embed_visit(node.arguments, '#{Papercraft.render_emit_call(', ')}') - end - def emit_html_tag(node) inner_text, attrs = tag_args(node) block = node.block @@ -216,4 +200,18 @@ def emit_html_tag(node) emit_tag_open_close(node, attrs) end end + + def emit_html_text(node) + args = node.arguments&.arguments + return nil if !args + + emit_tag_inner_text(args[0]) + end + + def emit_html_emit(node) + args = node.arguments&.arguments + return nil if !args + + embed_visit(node.arguments, '#{Papercraft.render_emit_call(', ')}') + end end diff --git a/test/fixtures/compiler/emit_proc.html b/test/fixtures/compiler/emit_proc.html new file mode 100644 index 0000000..8d55da2 --- /dev/null +++ b/test/fixtures/compiler/emit_proc.html @@ -0,0 +1 @@ +
foo
42
bar\ No newline at end of file diff --git a/test/fixtures/compiler/emit_proc_compiled.rb b/test/fixtures/compiler/emit_proc_compiled.rb new file mode 100644 index 0000000..cb64268 --- /dev/null +++ b/test/fixtures/compiler/emit_proc_compiled.rb @@ -0,0 +1,3 @@ +->(__buffer__) { + __buffer__ << "#{Papercraft.render_emit_call(pr1)}#{Papercraft.render_emit_call(pr2, 42)}