Class: Bade::Generator
- Inherits:
-
Object
- Object
- Bade::Generator
- Defined in:
- lib/bade/generator.rb
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.
- #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
313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/bade/generator.rb', line 313 def block_definition(block_node) buff_code "__blocks['#{block_node.name}'] = __create_block('#{block_node.name}') do" code_indent do buff_code '__buffs_push()' visit_node_children(block_node) buff_code '__buffs_pop()' end buff_code 'end' end |
#block_name_declaration(block_name) ⇒ nil
Generates code for block variables declaration in mixin definition
333 334 335 |
# File 'lib/bade/generator.rb', line 333 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
341 342 343 344 345 346 347 |
# File 'lib/bade/generator.rb', line 341 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 |
# File 'lib/bade/generator.rb', line 77 def buff_code(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
258 259 260 261 262 |
# File 'lib/bade/generator.rb', line 258 def code_indent(plus = 1) @code_indent += plus yield @code_indent -= plus end |
#escape_double_quotes!(str) ⇒ Void
371 372 373 |
# File 'lib/bade/generator.rb', line 371 def escape_double_quotes!(str) str.gsub!(/"/, '\"') end |
#formatted_attributes(tag_node) ⇒ String
Returns formatted attributes.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/bade/generator.rb', line 238 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.
268 269 270 271 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 |
# File 'lib/bade/generator.rb', line 268 def formatted_mixin_params(mixin_node) params = mixin_node.params result = [] if mixin_node.type == :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) def_block_node.name = DEFAULT_BLOCK_NAME def_block_node.children = other_children blocks << def_block_node end if !blocks.empty? buff_code '__blocks = {}' blocks.each do |block| block_definition(block) end result << '__blocks.dup' else result << '{}' end elsif mixin_node.type == :mixin_decl result << '__blocks' end # normal params result += params.select { |n| n.type == :mixin_param }.map(&:value) 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 |
# 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}" 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 |
#visit_block_decl(current_node) ⇒ nil
353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/bade/generator.rb', line 353 def visit_block_decl(current_node) params = formatted_mixin_params(current_node) buff_code "#{MIXINS_NAME}['#{current_node.name}'] = __create_mixin('#{current_node.name}', &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 |
# File 'lib/bade/generator.rb', line 118 def visit_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 # buff_print_value(NEW_LINE_NAME) 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? 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
182 183 184 185 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 |
# File 'lib/bade/generator.rb', line 182 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 |