Last active
April 11, 2017 05:50
-
-
Save holyshared/dd6001aad2f58d13a96350ffc23419dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'erb_parser' | |
class Collector | |
def initialize | |
@line = 1 | |
@crlf = nil | |
end | |
def collect(t) | |
result = ErbParser.parse(t) | |
result.treetop_ast.elements.map do |node| | |
ast(node) | |
end | |
end | |
def ast(node) | |
return next_line(node) if crlf?(node) | |
return nil if plain_text?(node) | |
return skip_erb_tag(node) if erb_tag?(node) | |
end | |
def crlf?(node) | |
return false unless plain_text?(node) | |
return false unless node.text_value == "\n" | |
true | |
end | |
def plain_text?(node) | |
node.is_a?(ErbParser::ErbGrammar::Text) | |
end | |
def erb_tag?(node) | |
node.is_a?(ErbParser::ErbGrammar::ErbTag) | |
end | |
def skip_erb_tag(node) | |
elements = node.elements.dup | |
elements = elements.drop_while { |n| n.text_value == "=" || n.text_value == "<%" || n.text_value == "" } | |
ruby_node = elements.first | |
diff_columns = ruby_node.interval.first - (@crlf_columns || 0) | |
puts @line | |
puts diff_columns | |
end | |
def next_line(node) | |
@line += 1 | |
@crlf_columns = node.interval.first + 1 | |
end | |
end | |
c = Collector.new | |
c.collect("Plain text <% 5.times {} %>\n<%= 2 + 3 %><%# Comment %>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment