Class: Bade::Generator
Constant Summary collapse
- BUFF_NAME =
:__buff- MIXINS_NAME =
:__mixins- NEW_LINE_NAME =
:__new_line- CURRENT_INDENT_NAME =
:__indent- BASE_INDENT_NAME =
:__base_indent- DEFAULT_BLOCK_NAME =
'default_block'.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#block_definition(block_node) ⇒ nil
Generates code for definition of block.
-
#block_name_declaration(block_name) ⇒ nil
Generates code for block variables declaration in mixin definition.
- #blocks_name_declaration(mixin_node) ⇒ nil
- #buff_code(text) ⇒ Object
- #buff_print_static_text(text) ⇒ Object
- #buff_print_text(text, indent: false, new_line: false) ⇒ Object
- #buff_print_value(value) ⇒ Object
-
#code_indent(plus = 1) ⇒ nil
Method for indenting generated code, indent is raised only in passed block.
- #escape_double_quotes!(str) ⇒ Void
-
#formatted_attributes(tag_node) ⇒ String
Formatted attributes.
-
#formatted_mixin_params(mixin_node) ⇒ String
Formatted params.
-
#generate_lambda_string(document, optimize: false) ⇒ String
String to parse with Ruby.
- #location(filename:, lineno:, label:) ⇒ String
- #location_node(node) ⇒ String
- #update_location_node(node) ⇒ Void
- #visit_block_decl(current_node) ⇒ nil
- #visit_document(document) ⇒ Object
- #visit_node(current_node) ⇒ Object
- #visit_node_children(current_node) ⇒ Object
- #visit_nodes(nodes) ⇒ Object
- #visit_tag(current_node) ⇒ nil
Class Method Details
.document_to_lambda_string(document, optimize: false) ⇒ String
21 22 23 24 |
# File 'lib/bade/generator.rb', line 21 def self.document_to_lambda_string(document, optimize: false) generator = new generator.generate_lambda_string(document, optimize: optimize) end |
Instance Method Details
#block_definition(block_node) ⇒ nil
Generates code for definition of block
321 322 323 324 325 326 327 328 329 |
# File 'lib/bade/generator.rb', line 321 def block_definition(block_node) buff_code "__blocks['#{block_node.name}'] = __create_block('#{block_node.name}', #{location_node(block_node)}) do" code_indent do visit_node_children(block_node) end buff_code 'end' end |
#block_name_declaration(block_name) ⇒ nil
Generates code for block variables declaration in mixin definition
337 338 339 |
# File 'lib/bade/generator.rb', line 337 def block_name_declaration(block_name) buff_code "#{block_name} = __blocks.delete('#{block_name}') { __create_block('#{block_name}') }" end |
#blocks_name_declaration(mixin_node) ⇒ nil
345 346 347 348 349 350 351 |
# File 'lib/bade/generator.rb', line 345 def blocks_name_declaration(mixin_node) block_name_declaration(DEFAULT_BLOCK_NAME) mixin_node.params.select { |n| n.type == :mixin_block_param }.each do |param| block_name_declaration(param.value) end end |
#buff_code(text) ⇒ Object
78 79 80 |
# File 'lib/bade/generator.rb', line 78 def buff_code(text) @buff << "#{' ' * @code_indent}#{text}" end |
#buff_print_static_text(text) ⇒ Object
69 70 71 |
# File 'lib/bade/generator.rb', line 69 def buff_print_static_text(text) buff_print_value("'#{text.gsub("'", "\\\\'")}'") unless text.empty? end |
#buff_print_text(text, indent: false, new_line: false) ⇒ Object
63 64 65 |
# File 'lib/bade/generator.rb', line 63 def buff_print_text(text, indent: false, new_line: false) # rubocop:disable Lint/UnusedMethodArgument buff_print_value("%Q{#{text}}") unless text.empty? end |
#buff_print_value(value) ⇒ Object
73 74 75 76 |
# File 'lib/bade/generator.rb', line 73 def buff_print_value(value) # buff_code %Q{#{BUFF_NAME} << #{CURRENT_INDENT_NAME}} if indent buff_code("#{BUFF_NAME} << #{value}") end |
#code_indent(plus = 1) ⇒ nil
Method for indenting generated code, indent is raised only in passed block
262 263 264 265 266 |
# File 'lib/bade/generator.rb', line 262 def code_indent(plus = 1) @code_indent += plus yield @code_indent -= plus end |
#escape_double_quotes!(str) ⇒ Void
373 374 375 |
# File 'lib/bade/generator.rb', line 373 def escape_double_quotes!(str) str.gsub!(/"/, '\"') end |
#formatted_attributes(tag_node) ⇒ String
Returns formatted attributes.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/bade/generator.rb', line 242 def formatted_attributes(tag_node) all_attributes = Hash.new { |hash, key| hash[key] = [] } xml_attributes = [] tag_node.attributes.each do |attr| xml_attributes << attr.name unless all_attributes.include?(attr.name) all_attributes[attr.name] << attr.value end xml_attributes.map do |attr_name| joined = all_attributes[attr_name].join('), (') "\#{__tag_render_attribute('#{attr_name}', (#{joined}))}" end.join end |
#formatted_mixin_params(mixin_node) ⇒ String
Returns formatted params.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/bade/generator.rb', line 272 def formatted_mixin_params(mixin_node) params = mixin_node.params result = [] case mixin_node.type when :mixin_call blocks = mixin_node.blocks other_children = (mixin_node.children - mixin_node.blocks - mixin_node.params) if other_children.count { |n| n.type != :newline } > 0 def_block_node = AST::NodeRegistrator.create(:mixin_block, mixin_node, lineno: mixin_node.lineno) def_block_node.name = DEFAULT_BLOCK_NAME def_block_node.children = other_children blocks << def_block_node end if blocks.empty? result << '{}' else buff_code '__blocks = {}' blocks.each do |block| block_definition(block) end result << '__blocks.dup' end when :mixin_decl result << '__blocks' end # positional params result += params.select { |n| n.type == :mixin_param } .map { |param| param.default_value ? "#{param.value} = #{param.default_value}" : param.value } # key-value params result += params.select { |n| n.type == :mixin_key_param } .map { |param| "#{param.name}: #{param.value}" } result.join(', ') end |
#generate_lambda_string(document, optimize: false) ⇒ String
Returns string to parse with Ruby.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bade/generator.rb', line 30 def generate_lambda_string(document, optimize: false) @document = document @buff = [] @indent = 0 @code_indent = 0 @optimize = optimize buff_code '# frozen_string_literal: true' # so it can be faster on Ruby 2.3+ buff_code '' buff_code "lambda do |#{NEW_LINE_NAME}: \"\\n\", #{BASE_INDENT_NAME}: ' '|" code_indent do buff_code "self.#{NEW_LINE_NAME} = #{NEW_LINE_NAME}" buff_code "self.#{BASE_INDENT_NAME} = #{BASE_INDENT_NAME}" buff_code "__buffs_push(#{location(filename: document.file_path, lineno: 0, label: '<top>')})" visit_document(document) buff_code "output = #{BUFF_NAME}.join" buff_code 'self.__reset' buff_code 'output' end buff_code 'end' @document = nil @buff.join("\n") end |
#location(filename:, lineno:, label:) ⇒ String
401 402 403 404 405 406 407 408 409 |
# File 'lib/bade/generator.rb', line 401 def location(filename:, lineno:, label:) args = [ filename ? "path: '#{filename}'" : nil, "lineno: #{lineno}", "label: '#{label}'", ].compact "Location.new(#{args.join(',')})" end |
#location_node(node) ⇒ String
413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/bade/generator.rb', line 413 def location_node(node) label = case node.type when :mixin_decl "+#{node.name}" when :mixin_block "#{node.name} in +#{node.parent.name}" else node.name end location(filename: node.filename, lineno: node.lineno, label: label) end |
#update_location_node(node) ⇒ Void
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/bade/generator.rb', line 379 def update_location_node(node) should_skip = case node.type when :code value = node.value.strip %w[end else }].include?(value) || value.match(/^(when|elsif) /) || value.match(/^\./) when :newline true else false end return if should_skip return if node.lineno.nil? buff_code "__update_lineno(#{node.lineno})" end |
#visit_block_decl(current_node) ⇒ nil
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/bade/generator.rb', line 357 def visit_block_decl(current_node) params = formatted_mixin_params(current_node) buff_code "#{MIXINS_NAME}['#{current_node.name}'] = __create_mixin('#{current_node.name}', #{location_node(current_node)}, &lambda { |#{params}|" code_indent do blocks_name_declaration(current_node) visit_nodes(current_node.children - current_node.params) end buff_code '})' end |
#visit_document(document) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bade/generator.rb', line 84 def visit_document(document) document.sub_documents.each do |sub_document| visit_document(sub_document) end buff_code("# ----- start file #{document.file_path}") unless document.file_path.nil? new_root = if @optimize Optimizer.new(document.root).optimize else document.root end visit_node(new_root) buff_code("# ----- end file #{document.file_path}") unless document.file_path.nil? end |
#visit_node(current_node) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 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 174 175 176 177 178 179 180 |
# File 'lib/bade/generator.rb', line 118 def visit_node(current_node) update_location_node(current_node) case current_node.type when :root visit_node_children(current_node) when :static_text buff_print_static_text(current_node.value) when :tag visit_tag(current_node) when :code buff_code(current_node.value) when :html_comment buff_print_text '<!-- ' visit_node_children(current_node) buff_print_text ' -->' when :comment comment_text = "##{current_node.children.map(&:value).join("\n#")}" buff_code(comment_text) when :doctype buff_print_text current_node.xml_output when :mixin_decl visit_block_decl(current_node) when :mixin_call params = formatted_mixin_params(current_node) buff_code "#{MIXINS_NAME}['#{current_node.name}'].call!(#{params})" when :output data = current_node.value output_code = if current_node.escaped "\#{__html_escaped(#{data})}" else "\#{#{data}}" end buff_print_text output_code when :newline # no-op when :import base_path = File.(current_node.value, File.dirname(@document.file_path)) load_path = if base_path.end_with?('.rb') && File.exist?(base_path) base_path elsif File.exist?("#{base_path}.rb") "#{base_path}.rb" end buff_code "__load('#{load_path}')" unless load_path.nil? when :yield block_name = DEFAULT_BLOCK_NAME method = current_node.conditional ? 'call' : 'call!' buff_code "#{block_name}.#{method}" else raise "Unknown type #{current_node.type}" end end |
#visit_node_children(current_node) ⇒ Object
104 105 106 |
# File 'lib/bade/generator.rb', line 104 def visit_node_children(current_node) visit_nodes(current_node.children) end |
#visit_nodes(nodes) ⇒ Object
110 111 112 113 114 |
# File 'lib/bade/generator.rb', line 110 def visit_nodes(nodes) nodes.each do |node| visit_node(node) end end |
#visit_tag(current_node) ⇒ nil
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/bade/generator.rb', line 186 def visit_tag(current_node) attributes = formatted_attributes(current_node) children_wo_attributes = (current_node.children - current_node.attributes) text = "<#{current_node.name}" text += attributes.to_s unless attributes.empty? other_than_new_lines = children_wo_attributes.any? { |n| n.type != :newline } text += if other_than_new_lines '>' else '/>' end conditional_nodes = current_node.children.select { |n| n.type == :output && n.conditional } unless conditional_nodes.empty? buff_code "if (#{conditional_nodes.map(&:value).join(') && (')})" @code_indent += 1 end buff_print_text(text, new_line: true, indent: true) if other_than_new_lines last_node = children_wo_attributes.last is_last_newline = !last_node.nil? && last_node.type == :newline nodes = if is_last_newline children_wo_attributes[0...-1] else children_wo_attributes end code_indent do visit_nodes(nodes) end buff_print_text("</#{current_node.name}>", new_line: true, indent: true) # print new line after the tag visit_node(last_node) if is_last_newline end unless conditional_nodes.empty? # rubocop:disable Style/GuardClause @code_indent -= 1 buff_code 'end' end end |