Class: RSX::Codegen

Inherits:
Object
  • Object
show all
Defined in:
lib/rsx/codegen.rb

Overview

Turns an RSX node tree into Ruby source.

Markup compiles to a single interpolated string literal: static text is baked in at compile time and only the ruby parts remain as interpolations. Nested elements are spliced into their parent's literal, so an entire static subtree collapses to one frozen string with no intermediate objects.

  • name

becomes

::RSX::SafeString.new("<ul class=\"list\"><li>#{::RSX.child(name)}</li></ul>")

Constant Summary collapse

LITERAL_ESCAPES =
{
  "\\" => "\\\\",
  '"' => '\"',
  "\n" => '\n',
  "\t" => '\t',
  "\r" => '\r',
  "\e" => '\e',
  "\0" => '\0'
}.freeze
LITERAL_PATTERN =
/[\\"\n\t\r\e\0]|#(?=[{$@])/
INNER_HTML =
%w[dangerouslySetInnerHTML dangerously_set_inner_html].freeze

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, prefix: nil) ⇒ Codegen

Returns a new instance of Codegen.



31
32
33
34
35
# File 'lib/rsx/codegen.rb', line 31

def initialize(path: nil, prefix: nil)
  @path = path
  @prefix = prefix || "rsx"
  @statics = 0
end

Instance Method Details

#compile(node, start_line:, end_line:) ⇒ Object

Returns [ruby_source, static?]



38
39
40
41
42
43
44
# File 'lib/rsx/codegen.rb', line 38

def compile(node, start_line:, end_line:)
  parts = []
  emit(node, parts)
  parts = merge(parts)
  static = parts.all? { |part| part[0] == :static }
  [assemble(parts, start_line, end_line, static), static]
end