Class: CompEx::Template::Assembler
- Inherits:
-
Object
- Object
- CompEx::Template::Assembler
- Defined in:
- lib/compex/template/assembler.rb
Constant Summary collapse
- HTML_VOID_TAG =
%w[area base br col embed hr img input link meta param source track wbr].freeze
- ELEMENTS_WITH_SELF_CLOSING_CHILDREN =
%w[svg math].freeze
- COMPONENT_NAME_PATTERN =
/^(::)?[A-Z]\w*+(::[A-Z]\w*)*$/
Instance Attribute Summary collapse
-
#components ⇒ Object
Returns the value of attribute components.
-
#context_id ⇒ Object
Returns the value of attribute context_id.
-
#embeddings ⇒ Object
Returns the value of attribute embeddings.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #assemble ⇒ Object
- #assemble_attr(at) ⇒ Object
- #assemble_children_self_closing(node) ⇒ Object
- #assemble_interpolation(int) ⇒ Object
- #assemble_one(node) ⇒ Object
- #component?(node) ⇒ Boolean
-
#initialize(ast, context_id) ⇒ Assembler
constructor
A new instance of Assembler.
- #make_component(node) ⇒ Object
- #process_custom_events(node) ⇒ Object
- #push_literal ⇒ Object
Constructor Details
#initialize(ast, context_id) ⇒ Assembler
Returns a new instance of Assembler.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/compex/template/assembler.rb', line 11 def initialize(ast, context_id) @ast = ast = {} @components = {} @result = [] @literal = +"" @context_id = context_id = 0 @component_id = 0 end |
Instance Attribute Details
#components ⇒ Object
Returns the value of attribute components.
9 10 11 |
# File 'lib/compex/template/assembler.rb', line 9 def components @components end |
#context_id ⇒ Object
Returns the value of attribute context_id.
9 10 11 |
# File 'lib/compex/template/assembler.rb', line 9 def context_id @context_id end |
#embeddings ⇒ Object
Returns the value of attribute embeddings.
9 10 11 |
# File 'lib/compex/template/assembler.rb', line 9 def end |
#result ⇒ Object
Returns the value of attribute result.
9 10 11 |
# File 'lib/compex/template/assembler.rb', line 9 def result @result end |
Instance Method Details
#assemble ⇒ Object
22 23 24 25 26 |
# File 'lib/compex/template/assembler.rb', line 22 def assemble @ast.each { assemble_one(it) } push_literal @result end |
#assemble_attr(at) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/compex/template/assembler.rb', line 90 def assemble_attr(at) @literal << at.name if at.value.nil? @literal << " " return end @literal << "=" case at.value when MiniHTML::AST::String @literal << at.value.quote @literal << at.value.literal @literal << at.value.quote when MiniHTML::AST::Executable push_literal id = "__cx_embedding_quoted_#{@context_id}_#{@embedding_id}__" += 1 [id] = at.value.source @result << { kind: :executable, id: id, source: at.value.source } when MiniHTML::AST::Interpolation assemble_interpolation(at.value) when MiniHTML::AST::Literal @literal << at.value.value else raise "Unexpected attr value node type #{at.value.class}" end end |
#assemble_children_self_closing(node) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/compex/template/assembler.rb', line 121 def assemble_children_self_closing(node) old_self_closing = @allow_self_closing @allow_self_closing = true node.children.each { assemble_one(it) } ensure @allow_self_closing = old_self_closing end |
#assemble_interpolation(int) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/compex/template/assembler.rb', line 129 def assemble_interpolation(int) @literal << int.values.first.quote @literal << int.values.first.literal int.values[1...].each do |v| case v when MiniHTML::AST::String @literal << v.literal when MiniHTML::AST::Executable push_literal id = "__cx_embedding_#{@context_id}_#{@embedding_id}__" += 1 [id] = v.source @result << { kind: :executable, id: id, source: v.source } end end @literal << int.values.first.quote end |
#assemble_one(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/compex/template/assembler.rb', line 46 def assemble_one(node) case node when MiniHTML::AST::PlainText @literal << node.literal when MiniHTML::AST::Tag name = component?(node) ? make_component(node) : node.name @literal << "<#{name}" has_attrs = !node.attributes.empty? if has_attrs @literal << " " process_custom_events(node) node.attributes.each { assemble_attr(it) } end if node.self_closing? && (@allow_self_closing || HTML_VOID_TAG.include?(name)) @literal << " " if has_attrs @literal << "/>" return elsif node.self_closing? @literal << "></#{name}>" return end @literal << ">" if ELEMENTS_WITH_SELF_CLOSING_CHILDREN.include?(name) assemble_children_self_closing(node) else node.children.each { assemble_one(it) } end @literal << "</#{name}>" when MiniHTML::AST::Executable push_literal id = "__cx_embedding_#{@context_id}_#{@embedding_id}__" += 1 [id] = node.source @result << { kind: :executable, id:, source: node.source } else raise "Unexpected node type #{node.class}" end end |
#component?(node) ⇒ Boolean
37 |
# File 'lib/compex/template/assembler.rb', line 37 def component?(node) = COMPONENT_NAME_PATTERN.match?(node.name) |
#make_component(node) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/compex/template/assembler.rb', line 39 def make_component(node) name = "compex-component-#{@component_id}" @component_id += 1 @components[name] = node.name name end |
#process_custom_events(node) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/compex/template/assembler.rb', line 149 def process_custom_events(node) events = [] to_remove = [] node.attributes.each do |at| next unless at.name.start_with?("cx-on:") name = at.name.split(":", 2).last next unless name handler = at.value.literal to_remove << at.name if handler.include? "," warn "[CompEx] Ignoring custom event definition containing commas: #{at.name}=#{at.value.inspect}" next end events << { name:, handler: } end node.attributes.reject! { to_remove.include? it.name } return if events.empty? @literal << "cx-custom-handlers=" @literal << events.map { "#{it[:name]}->#{it[:handler]}" }.join(",").inspect @literal << " " end |
#push_literal ⇒ Object
28 29 30 31 32 33 |
# File 'lib/compex/template/assembler.rb', line 28 def push_literal return if @literal.empty? @result << { kind: :literal, value: @literal } @literal = +"" end |