docbrown/bin/untranslated_erb
Josh Soref 5324eb1477
Spelling fixes to live code (#15670)
Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2021-12-06 12:11:25 +02:00

56 lines
1.6 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'rails-html-sanitizer'
class I18nExtractor
ERB_TAG = /<%(.*?)%>/
HTML_TAG = /<[^>]*>|<\/[^>]*>|[^>]*\/>|\".+">|">|<\w+(.+|)|.+="(.+)|"/
SEPARATOR = '_@@@_'
SKIP_TAGS = [[/<script/i,/<\/script>/i],[/<%/,/%>/],[/<style/i,/\/style>/i]]
def initialize(filename)
@filename = filename
@stack = []
@files_with_results = []
end
def extract
sanitizer = Rails::Html::FullSanitizer.new
i = 0
File.foreach(@filename) do |line|
i += 1
next if in_script_block?(line)
striped_line = sanitizer.sanitize(line).chomp.strip.gsub(HTML_TAG, '')
next if striped_line.empty?
if striped_line.length > 1
unless @files_with_results.include?(@filename)
@files_with_results << @filename
printf "\n\n\e[4m#{@filename}\e[00m\n"
end
printf("%3s:%s\n",i+1,striped_line)
end
end
end
private
def in_script_block?(s)
return true if s.nil? || s.strip.size == 0
jump_in_tag = SKIP_TAGS.find{ |start_tag,end_tag| s =~ start_tag}
@stack.push jump_in_tag[1] if jump_in_tag
if @stack.last
end_tag_match = s.match(@stack.last)
if end_tag_match
@stack.pop
return in_script_block?(end_tag_match.post_match)
end
end
return !@stack.empty?
end
end
printf "\nSeeking out erb files that need translating. Just one moment...\n\n\e[1mThis script definitely includes false negatives and false positives, but hopefully it helps!!!\e[00m\n\n\n"
Dir.glob("**/*.html.erb").each do |filename|
next if filename.include?("/admin/") # We are not yet focused on this area
I18nExtractor.new(filename).extract
end