Skip to content

Instantly share code, notes, and snippets.

@zigzag
Created December 13, 2009 15:50
Show Gist options
  • Save zigzag/255460 to your computer and use it in GitHub Desktop.
Save zigzag/255460 to your computer and use it in GitHub Desktop.
a handy tool for extract text from erb files for i18n
class I18nExtractor
SKIP_INLINE_TAG = [/<%(.*?)%>/,/<(.*?)>/,/<%(.*)$/,/^(.*)%>/]
SEPERATOR = '_@@@_'
SKIP_TAGS = [[/<script/i,/<\/script>/i],[/<%/,/%>/],[/<style/i,/\/style>/i]]
def initialize(filename)
p '-----------------------------------------'
p filename
p '-----------------------------------------'
@filename = filename
@stack = []
end
def extract
File.open(@filename).each_with_index do |line,i|
next if in_script_block?(line)
text = SKIP_INLINE_TAG.inject(line){|memo,tag| memo.gsub(tag,SEPERATOR)}.strip
arr = text.split SEPERATOR
arr.each { |e| printf("%3s:%s \n",i+1,e.strip) if e.strip.size > 1 }
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
Dir.glob(File.join("#{ARGV[0]}",'**/*.html.erb')).each{|f| I18nExtractor.new(f).extract}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment