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) ⇒ 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) ⇒ String
21 22 23 24 |
# File 'lib/bade/generator.rb', line 21 def self.document_to_lambda_string(document) generator = new generator.generate_lambda_string(document) end |
Instance Method Details
#block_definition(block_node) ⇒ nil
Generates code for definition of block
304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/bade/generator.rb', line 304 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
324 325 326 |
# File 'lib/bade/generator.rb', line 324 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
332 333 334 335 336 337 338 |
# File 'lib/bade/generator.rb', line 332 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
76 77 78 |
# File 'lib/bade/generator.rb', line 76 def buff_code(text) @buff << ' ' * @code_indent + text end |
#buff_print_static_text(text) ⇒ Object
67 68 69 |
# File 'lib/bade/generator.rb', line 67 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
61 62 63 |
# File 'lib/bade/generator.rb', line 61 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
71 72 73 74 |
# File 'lib/bade/generator.rb', line 71 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
249 250 251 252 253 |
# File 'lib/bade/generator.rb', line 249 def code_indent(plus = 1) @code_indent += plus yield @code_indent -= plus end |
#escape_double_quotes!(str) ⇒ Void
362 363 364 |
# File 'lib/bade/generator.rb', line 362 def escape_double_quotes!(str) str.gsub!(/"/, '\"') end |
#formatted_attributes(tag_node) ⇒ String
Returns formatted attributes.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/bade/generator.rb', line 229 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.
259 260 261 262 263 264 265 266 267 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 |
# File 'lib/bade/generator.rb', line 259 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) ⇒ 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 |
# File 'lib/bade/generator.rb', line 30 def generate_lambda_string(document) @document = document @buff = [] @indent = 0 @code_indent = 0 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
344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/bade/generator.rb', line 344 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
83 84 85 86 87 88 89 90 91 |
# File 'lib/bade/generator.rb', line 83 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? visit_node(document.root) buff_code("# ----- end file #{document.file_path}") unless document.file_path.nil? end |
#visit_node(current_node) ⇒ Object
109 110 111 112 113 114 115 116 117 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 |
# File 'lib/bade/generator.rb', line 109 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
95 96 97 |
# File 'lib/bade/generator.rb', line 95 def visit_node_children(current_node) visit_nodes(current_node.children) end |
#visit_nodes(nodes) ⇒ Object
101 102 103 104 105 |
# File 'lib/bade/generator.rb', line 101 def visit_nodes(nodes) nodes.each do |node| visit_node(node) end end |
#visit_tag(current_node) ⇒ nil
173 174 175 176 177 178 179 180 181 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 |
# File 'lib/bade/generator.rb', line 173 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 |