Skip to content

Commit

Permalink
Compiler: add support for #emit (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed May 8, 2024
1 parent 6df970a commit 29fd60e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
69 changes: 53 additions & 16 deletions lib/papercraft/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@
require 'sirop'

class Papercraft::Compiler < Sirop::Sourcifier
module AuxMethods
def format_html_attr(tag)
tag.to_s.tr('_', '-')
end

def format_html_attrs(attrs)
attrs.reduce(+'') do |html, (k, v)|
html << ' ' if !html.empty?
html << "#{format_html_attr(k)}=\"#{v}\""
end
end

def render_emit_call(o, *a, **b, &block)
case o
when nil
# do nothing
when ::Proc
raise NotImplementedError
else
o.to_s
end
end
end

Papercraft.extend(AuxMethods)

def initialize
super
@html_buffer = +''
Expand Down Expand Up @@ -85,24 +111,13 @@ def visit_call_node(node)

case node.name
when :text
return emit_html_text(node)
end

inner_text, attrs = tag_args(node)
block = node.block

if inner_text
emit_tag_open(node, attrs)
emit_tag_inner_text(inner_text)
emit_tag_close(node)
elsif block
emit_tag_open(node, attrs)
visit(block.body)
@html_location_start ||= node.block.closing_loc
emit_tag_close(node)
emit_html_text(node)
when :emit
emit_html_emit(node)
else
emit_tag_open_close(node, attrs)
emit_html_tag(node)
end

@html_location_end = node.location
end

Expand Down Expand Up @@ -179,4 +194,26 @@ def emit_html_text(node)

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

if inner_text
emit_tag_open(node, attrs)
emit_tag_inner_text(inner_text)
emit_tag_close(node)
elsif block
emit_tag_open(node, attrs)
visit(block.body)
@html_location_start ||= node.block.closing_loc
emit_tag_close(node)
else
emit_tag_open_close(node, attrs)
end
end
end
11 changes: 0 additions & 11 deletions lib/papercraft/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@
require 'cgi'

module Papercraft
def self.format_html_attr(tag)
tag.to_s.tr('_', '-')
end

def self.format_html_attrs(attrs)
attrs.reduce(+'') do |html, (k, v)|
html << ' ' if !html.empty?
html << "#{format_html_attr(k)}=\"#{v}\""
end
end

# HTML Markup extensions
module HTML
include Tags
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/compiler/emit_text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo&bar<br/>bar&baz
3 changes: 3 additions & 0 deletions test/fixtures/compiler/emit_text_compiled.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
->(__buffer__) {
__buffer__ << "#{Papercraft.render_emit_call('foo&bar')}<br/>#{Papercraft.render_emit_call(x)}"
}
7 changes: 7 additions & 0 deletions test/fixtures/compiler/emit_text_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x = 'bar&baz'

->() {
emit 'foo&bar'
br
emit x
}

0 comments on commit 29fd60e

Please sign in to comment.