![](https://hybrd.co/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1f1e1fffe65fa2434c2f95db382a75f1e0de4579/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJYW5CbkJqb0dSVlE2RkhKbGMybDZaVjkwYjE5c2FXMXBkRnNIYVdscGFRPT0iLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--8001aed4a4a28c32bf4da791d57fa2bc8a236cea/me-2018.jpg)
Get a word count from your Rails app views
I needed a way to calculate the number of words in a Rails application’s views, to estimate the cost of translating the content to another language.
Pass this script the path to your views directory (or a subdirectory) to get a word count.
#!/usr/bin/env ruby
require "action_view"
require "fileutils"
class WordCounter
include ActionView::Helpers::SanitizeHelper
def initialize(initial_directory)
@initial_directory = initial_directory
puts count_directory(@initial_directory)
end
def count_directory(directory)
return 0 if !File.directory?(directory)
word_count_total = 0
FileUtils.cd(directory) do
files = Dir.glob("**/*")
files.each do |file_name|
full_path = "#{directory}/#{file_name}"
if File.directory?(full_path)
word_count_total += count_directory(full_path)
elsif File.exists?(full_path)
word_count_total += word_count(strip_html(strip_comments(strip_erb(File.read(full_path)))))
end
end
end
return word_count_total
end
private
def strip_erb(text)
text.gsub(/<%(?:(?!%>).)+%>/, "")
end
def strip_comments(text)
text.gsub(//, "")
end
def strip_html(text)
sanitize(text, :tags => [], :attributes => [])
end
def word_count(text)
text.split.length rescue 0
end
end
WordCounter.new(ARGV[0])
Here’s the gist on Github.
Need help building or maintaining a Rails app?
Jeremy is currently booked until mid-2023, but always happy to chat.