Skip to content

Instantly share code, notes, and snippets.

@anthonycrumley
Created January 20, 2014 20:28
Show Gist options
  • Save anthonycrumley/8528458 to your computer and use it in GitHub Desktop.
Save anthonycrumley/8528458 to your computer and use it in GitHub Desktop.
require 'haml'
def line_counter(node)
decendants = node.children.map{|n| line_counter(n)}
if node.type == :filter && %w{ javascript erb plain }.include?(node.value[:name])
me = node.value[:text].split("\n").length
else
me = 0
end
sum(decendants) + me
end
def sum(values)
values.reduce(0) { |total, value| total += value }
end
haml_files = File.join("../gradesfirst/app/views", "**", "*.haml")
puts Dir.glob(haml_files).inject(0){ |line_count, file_name|
begin
text = File.read(file_name)
parser = Haml::Parser.new(text, Haml::Options.new)
tree = parser.parse
file_line_count = line_counter(tree)
line_count += file_line_count
puts " #{file_line_count} #{file_name}"
rescue
puts ' ERROR: ' + file_name
end
line_count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment