Class: Bade::RubyGenerator
- Defined in:
- lib/bade/generator/ruby_generator.rb
Constant Summary collapse
- BUFF_NAME =
'__buff'- MIXINS_NAME =
'__mixins'- START_STRING =
" lambda { #{BUFF_NAME} = [] #{MIXINS_NAME} = Hash.new { |hash, key| raise \"Undefined mixin '\#{key}'\" } "- END_STRING =
" #{BUFF_NAME}.join }"
Class Method Summary collapse
- .document_to_lambda(document, new_line: "\n", indent: "\t", filename: '') ⇒ Proc
- .document_to_lambda_string(document, new_line: "\n", indent: "\t", filename: '') ⇒ String
Instance Method Summary collapse
- #block_name_declaration(block_name) ⇒ Object
- #blocks_name_declaration(mixin_node) ⇒ Object
- #buff_code(text) ⇒ Object
- #buff_print_text(text, indent: false, new_line: false) ⇒ Object
- #escape_double_quotes!(str) ⇒ Void
-
#formatted_attributes(tag_node) ⇒ String
Formatted attributes.
-
#formatted_mixin_params(mixin_node) ⇒ String
Formatted params.
- #generate_lambda(document, filename) ⇒ Object
-
#generate_lambda_string(document) ⇒ String
String to parse with Ruby.
- #indent(plus = 1) ⇒ Object
-
#initialize(new_line_string, indent_string) ⇒ RubyGenerator
constructor
A new instance of RubyGenerator.
- #visit_document(document) ⇒ Object
- #visit_node(current_node) ⇒ Object
- #visit_node_childrens(current_node) ⇒ Object
- #visit_nodes(nodes) ⇒ Object
- #visit_tag(current_node) ⇒ Object
Methods inherited from Generator
Constructor Details
#initialize(new_line_string, indent_string) ⇒ RubyGenerator
Returns a new instance of RubyGenerator.
43 44 45 46 |
# File 'lib/bade/generator/ruby_generator.rb', line 43 def initialize(new_line_string, indent_string) @new_line_string = new_line_string @indent_string = indent_string end |
Class Method Details
.document_to_lambda(document, new_line: "\n", indent: "\t", filename: '') ⇒ Proc
24 25 26 27 |
# File 'lib/bade/generator/ruby_generator.rb', line 24 def self.document_to_lambda(document, new_line: "\n", indent: "\t", filename: '') generator = self.new(new_line, indent) generator.generate_lambda(document, filename) end |
.document_to_lambda_string(document, new_line: "\n", indent: "\t", filename: '') ⇒ String
33 34 35 36 |
# File 'lib/bade/generator/ruby_generator.rb', line 33 def self.document_to_lambda_string(document, new_line: "\n", indent: "\t", filename: '') generator = self.new(new_line, indent) generator.generate_lambda_string(document) end |
Instance Method Details
#block_name_declaration(block_name) ⇒ Object
310 311 312 |
# File 'lib/bade/generator/ruby_generator.rb', line 310 def block_name_declaration(block_name) buff_code "#{block_name} = __blocks.delete('#{block_name}') { __create_block('#{block_name}') }" end |
#blocks_name_declaration(mixin_node) ⇒ Object
316 317 318 319 320 321 322 323 324 |
# File 'lib/bade/generator/ruby_generator.rb', line 316 def blocks_name_declaration(mixin_node) mixin_node.params.select { |param| param.type == :mixin_block_param }.each { |param| block_name_declaration(param.data) } block_name_declaration('default_block') end |
#buff_code(text) ⇒ Object
89 90 91 |
# File 'lib/bade/generator/ruby_generator.rb', line 89 def buff_code(text) @buff << "\t" * @code_indent + text end |
#buff_print_text(text, indent: false, new_line: false) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bade/generator/ruby_generator.rb', line 75 def buff_print_text(text, indent: false, new_line: false) indent_text = if indent @indent_string * @indent else '' end prepended_text = indent_text + text if prepended_text.length > 0 buff_code %Q{#{BUFF_NAME} << %Q{#{prepended_text}}} end end |
#escape_double_quotes!(str) ⇒ Void
332 333 334 |
# File 'lib/bade/generator/ruby_generator.rb', line 332 def escape_double_quotes!(str) str.gsub!(/"/, '\"') end |
#formatted_attributes(tag_node) ⇒ String
Returns formatted attributes.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/bade/generator/ruby_generator.rb', line 235 def formatted_attributes(tag_node) all_attributes = Hash.new { |hash, key| hash[key] = [] } xml_attributes = [] tag_node.attributes.each do |attr| unless all_attributes.include?(attr.name) xml_attributes << attr.name end all_attributes[attr.name] << attr.value end xml_attributes.map do |attr_name| joined = all_attributes[attr_name].join('), (') %Q{\#{tag_render_attribute('#{attr_name}', (#{joined}))}} end.join end |
#formatted_mixin_params(mixin_node) ⇒ String
Returns formatted params.
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 297 298 299 300 301 302 303 304 305 |
# File 'lib/bade/generator/ruby_generator.rb', line 263 def formatted_mixin_params(mixin_node) params = mixin_node.params result = [] if mixin_node.type == :mixin_call if mixin_node.blocks.length > 0 buff_code '__blocks = {}' mixin_node.blocks.each { |block| block_name = block.data ? block.data : 'default_block' buff_code "__blocks['#{block_name}'] = __create_block('#{block_name}') do" indent { visit_node_childrens(block) } buff_code 'end' } result << '__blocks.dup' else result << '{}' end elsif mixin_node.type == :mixin_declaration result << '__blocks' end # normal params result += params.select { |param| param.type == :mixin_param }.map { |param| param.data } result += params.select { |param| param.type == :mixin_key_param }.map { |param| "#{param.name}: #{param.value}" } result.join(', ') end |
#generate_lambda(document, filename) ⇒ Object
51 52 53 |
# File 'lib/bade/generator/ruby_generator.rb', line 51 def generate_lambda(document, filename) eval(generate_lambda_string(document), nil, filename) end |
#generate_lambda_string(document) ⇒ String
Returns string to parse with Ruby.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bade/generator/ruby_generator.rb', line 59 def generate_lambda_string(document) @buff = [] @indent = 0 @code_indent = 1 @buff << START_STRING visit_document(document) @buff << END_STRING @buff.join("\n") end |
#indent(plus = 1) ⇒ Object
253 254 255 256 257 |
# File 'lib/bade/generator/ruby_generator.rb', line 253 def indent(plus = 1) @code_indent += plus yield @code_indent -= plus end |
#visit_document(document) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/bade/generator/ruby_generator.rb', line 96 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
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 181 182 183 184 185 186 |
# File 'lib/bade/generator/ruby_generator.rb', line 122 def visit_node(current_node) case current_node.type when :root visit_node_childrens(current_node) when :text buff_print_text current_node.data when :tag visit_tag(current_node) when :ruby_code buff_code current_node.data when :html_comment buff_print_text '<!-- ' visit_node_childrens(current_node) buff_print_text ' -->' when :comment comment_text = current_node.childrens.select { |node| !node.data.nil? }.map { |node| node.data }.join(@new_line_string + '#') buff_code '#' + comment_text when :doctype buff_print_text current_node.xml_output when :mixin_declaration params = formatted_mixin_params(current_node) buff_code "#{MIXINS_NAME}['#{current_node.data}'] = lambda { |#{params}|" indent { blocks_name_declaration(current_node) visit_node_childrens(current_node) } buff_code '}' when :mixin_call params = formatted_mixin_params(current_node) buff_code "#{MIXINS_NAME}['#{current_node.data}'].call(#{params})" when :output data = current_node.data output_code = if current_node.escaped "\#{html_escaped(#{data})}" else "\#{#{data}}" end buff_print_text output_code when :newline buff_print_text @new_line_string if @new_line_string.length > 0 when :import # nothing else raise "Unknown type #{current_node.type}" end end |
#visit_node_childrens(current_node) ⇒ Object
108 109 110 |
# File 'lib/bade/generator/ruby_generator.rb', line 108 def visit_node_childrens(current_node) visit_nodes(current_node.childrens) end |
#visit_nodes(nodes) ⇒ Object
114 115 116 117 118 |
# File 'lib/bade/generator/ruby_generator.rb', line 114 def visit_nodes(nodes) nodes.each { |node| visit_node(node) } end |
#visit_tag(current_node) ⇒ Object
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 |
# File 'lib/bade/generator/ruby_generator.rb', line 190 def visit_tag(current_node) attributes = formatted_attributes current_node text = "<#{current_node.name}" if attributes.length > 0 text += "#{attributes}" end other_than_new_lines = current_node.childrens.any? { |node| node.type != :newline } if other_than_new_lines text += '>' else text += '/>' end buff_print_text text, new_line: true, indent: true if other_than_new_lines last_node = current_node.childrens.last is_last_newline = !last_node.nil? && last_node.type == :newline nodes = if is_last_newline current_node.childrens[0...-1] else current_node.childrens end 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 end |