docbrown/bin/untranslated_erb
William Kennedy 6a8fcf6f33
HTML tag and attribute matcher (#15091)
* HTML tag and attribute matcher

This regex was designed for the following matches. It should ignore
plain text

<tag>
</tag>
<hr/>
<div class="signout_confirm-wrapper">
" target="_blank" rel="noopener me" class="profile-header__meta__item p-1">
class="crayons-layout crayons-layout--limited-l crayons-layout--2-cols crayons-layout--2-cols--1-2 pt-4 m:pt-0"
href="javascript:void(0);"
id="asdf"
class="sas"
data-profile-user-id="asdf">sads</s>
data-tag=""

* added rails sanitizer

* removed swap file

* Update bin/untranslated_erb

Co-authored-by: Michael Kohl <me@citizen428.net>

* revert gitignore

Co-authored-by: Michael Kohl <me@citizen428.net>
2021-11-03 11:27:12 +07:00

56 lines
1.6 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'rails-html-sanitizer'
class I18nExtractor
ERB_TAG = /<%(.*?)%>/
HTML_TAG = /<[^>]*>|<\/[^>]*>|[^>]*\/>|\".+">|">|<\w+(.+|)|.+="(.+)|"/
SEPERATOR = '_@@@_'
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