Class: ErectorToFortitude::Rewriter
- Inherits:
-
Parser::Rewriter
- Object
- Parser::Rewriter
- ErectorToFortitude::Rewriter
- Defined in:
- lib/erector_to_fortitude/rewriter.rb
Instance Method Summary collapse
- #get_classes_and_ids_sent(node) ⇒ Object
- #get_first_node(node) ⇒ Object
- #get_node_after_classes_and_ids(node) ⇒ Object
- #get_sends(node) ⇒ Object
- #on_send(node) ⇒ Object
Instance Method Details
#get_classes_and_ids_sent(node) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/erector_to_fortitude/rewriter.rb', line 83 def get_classes_and_ids_sent(node) arr = [] while node.children[0] && node.children[0].type == :send arr << node.children[1] node = node.children[0] end arr.reverse end |
#get_first_node(node) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/erector_to_fortitude/rewriter.rb', line 64 def get_first_node(node) while node.children[0].is_a?(Parser::AST::Node) && node.children[0].type == :send node = node.children[0] end node end |
#get_node_after_classes_and_ids(node) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/erector_to_fortitude/rewriter.rb', line 94 def get_node_after_classes_and_ids(node) found_classes_and_ids = false node.children.each do |child| if child.is_a?(Symbol) found_classes_and_ids = true end if found_classes_and_ids && !child.is_a?(Symbol) return child end end end |
#get_sends(node) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/erector_to_fortitude/rewriter.rb', line 72 def get_sends(node) arr = [] while node.children[0] && node.children[0].type == :send arr << node node = node.children[0] end arr end |
#on_send(node) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 60 61 62 |
# File 'lib/erector_to_fortitude/rewriter.rb', line 5 def on_send(node) # Why is this necessary? return unless node.children.length > 1 # How do I explain this? lol return unless node.children[0] tag_node = get_first_node(node.children[0]) # The tag method is an instance method return unless tag_node.children[0] == nil # Validate tag name return unless ErectorToFortitude::HTML_TAGS.include?(tag_node.children[1].to_s) ids = [] classes = [] get_classes_and_ids_sent(node).each do |class_or_id| if class_or_id[-1] == '!' ids << class_or_id[0..-2] else classes << class_or_id end end new_opts = [] if classes.length > 0 new_opts << %{class: '#{classes.join(' ')}'} end if ids.length > 0 new_opts << %{id: '#{ids.join(' ')}'} end new_opt_str = new_opts.join(', ') # Replace the existing dots get_sends(node).each do |child| replace child.loc.dot, '' replace child.loc.selector, '' end if (text_node = get_node_after_classes_and_ids(node)) && text_node.is_a?(Parser::AST::Node) && text_node.type != :hash insert_after text_node.loc.expression, ', ' + new_opt_str # Add class to existing hash elsif node.children[2] && node.children[2].type == :hash insert_before node.children[2].children[0].loc.expression, new_opt_str + ', ' # Add a new options hash else # Remove the dot insert_after node.loc.selector, "(#{new_opt_str})" end end |