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
- REQUIRE_RELATIVE_REGEX =
/require_relative\s+(['"])(.+)['"]/.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#_fix_required_relative(text) ⇒ String
Fix require_relative paths to be relative to the main Bade file (instead of the current file).
-
#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
23 24 25 26 |
# File 'lib/bade/generator.rb', line 23 def self.document_to_lambda_string(document, optimize: false) generator = new generator.generate_lambda_string(document, optimize: optimize) end |
Instance Method Details
#_fix_required_relative(text) ⇒ String
Fix require_relative paths to be relative to the main Bade file (instead of the current file)
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/bade/generator.rb', line 438 def _fix_required_relative(text) text.gsub(REQUIRE_RELATIVE_REGEX) do quote = Regexp.last_match[1] relative_path = Regexp.last_match[2] should_not_process = quote == '"' && relative_path.include?('#{') new_relative_path = relative_path unless should_not_process abs_path = File.(relative_path, File.dirname(@documents.last.file_path)) document_abs_path = Pathname.new(File.(File.dirname(@documents.first.file_path))) new_relative_path = Pathname.new(abs_path).relative_path_from(document_abs_path).to_s end "require_relative #{quote}#{new_relative_path}#{quote}" end end |
#block_definition(block_node) ⇒ nil
Generates code for definition of block
326 327 328 329 330 331 332 333 334 |
# File 'lib/bade/generator.rb', line 326 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
342 343 344 |
# File 'lib/bade/generator.rb', line 342 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
350 351 352 353 354 355 356 |
# File 'lib/bade/generator.rb', line 350 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
77 78 79 80 81 |
# File 'lib/bade/generator.rb', line 77 def buff_code(text) text = _fix_required_relative(text) @buff << "#{' ' * @code_indent}#{text}" end |
#buff_print_static_text(text) ⇒ Object
68 69 70 |
# File 'lib/bade/generator.rb', line 68 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
62 63 64 |
# File 'lib/bade/generator.rb', line 62 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
72 73 74 75 |
# File 'lib/bade/generator.rb', line 72 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
267 268 269 270 271 |
# File 'lib/bade/generator.rb', line 267 def code_indent(plus = 1) @code_indent += plus yield @code_indent -= plus end |
#escape_double_quotes!(str) ⇒ Void
380 381 382 |
# File 'lib/bade/generator.rb', line 380 def escape_double_quotes!(str) str.gsub!(/"/, '\"') end |
#formatted_attributes(tag_node) ⇒ String
Returns formatted attributes.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/bade/generator.rb', line 247 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.
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 314 315 316 317 318 |
# File 'lib/bade/generator.rb', line 277 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.
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 |
# File 'lib/bade/generator.rb', line 32 def generate_lambda_string(document, optimize: false) @documents = [] @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' @buff.join("\n") end |
#location(filename:, lineno:, label:) ⇒ String
408 409 410 411 412 413 414 415 416 |
# File 'lib/bade/generator.rb', line 408 def location(filename:, lineno:, label:) args = [ filename ? "path: '#{filename}'" : nil, "lineno: #{lineno}", "label: '#{label}'", ].compact "Location.new(#{args.join(',')})" end |
#location_node(node) ⇒ String
420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/bade/generator.rb', line 420 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
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/bade/generator.rb', line 386 def update_location_node(node) should_skip = case node.type when :code value = node.value.strip value.match(/^(end|else|\}|class)\b/) || 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
362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/bade/generator.rb', line 362 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bade/generator.rb', line 85 def visit_document(document) @documents.append(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? @documents.pop end |
#visit_node(current_node) ⇒ Object
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 181 182 183 184 185 |
# File 'lib/bade/generator.rb', line 123 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(@documents.last.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
109 110 111 |
# File 'lib/bade/generator.rb', line 109 def visit_node_children(current_node) visit_nodes(current_node.children) end |
#visit_nodes(nodes) ⇒ Object
115 116 117 118 119 |
# File 'lib/bade/generator.rb', line 115 def visit_nodes(nodes) nodes.each do |node| visit_node(node) end end |
#visit_tag(current_node) ⇒ nil
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 237 238 239 240 241 |
# File 'lib/bade/generator.rb', line 191 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 |