Permalink
Browse files

second commit

  • Loading branch information...
1 parent ed004f6 commit 58261df7ef496b01befec5f1b0f3e74fc94c391b @jugyo committed Feb 23, 2011
Showing with 70 additions and 5 deletions.
  1. +2 −2 Rakefile
  2. +22 −0 bin/i18n-grep
  3. +46 −0 lib/i18n-grep.rb
  4. +0 −3 spec/i18n-grep_spec.rb
View
@@ -15,8 +15,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "i18n-grep"
gem.homepage = "http://github.com/jugyo/i18n-grep"
gem.license = "MIT"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{grep I18n yaml}
+ gem.description = %Q{The command 'i18n-grep' that does grep I18n yaml files.}
gem.email = "jugyo.org@gmail.com"
gem.authors = ["jugyo"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
View
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby
+$:.unshift File.dirname(__FILE__) + "/../lib"
+require 'i18n-grep'
+require 'optparse'
+
+verbose = false
+dirs = []
+opt = OptionParser.new
+opt.banner += " [file...]"
+opt.on('-v') { verbose = true}
+opt.on('-d=DIRECTORY', 'directory') { |dir| dirs << dir }
+opt.parse!(ARGV)
+
+files = ARGV[1..-1] + dirs.map { |dir| Dir[File.join(dir, '**', '*.yml')] }.flatten
+
+I18n.grep(ARGV[0], files).each do |path, line, key, word|
+ if verbose
+ puts "#{path}:#{line} - #{key}: #{word}"
+ else
+ puts "#{path}:#{line}"
+ end
+end
View
@@ -0,0 +1,46 @@
+module I18n
+ class << self
+ def grep(pattern, files)
+ results = []
+
+ files.each do |filepath|
+ File.open(filepath) do |file|
+ key_stack = []
+ prev_indent = 0
+ indent_spaces = nil
+ file.each_with_index do |line, index|
+ line = line.chomp
+ line_number = index + 1
+ next unless line =~ /\s*\w+:/
+
+ key = line[/\w+:/].sub(':', '')
+ word = line.gsub(/.*:\s*/, '')
+
+ indent = (line =~ /\w/)
+ if indent_spaces.nil? && indent > 0
+ indent_spaces = indent
+ end
+ indent /= indent_spaces if indent > 0
+
+ unless indent == prev_indent
+ if indent < prev_indent
+ (prev_indent - indent + 1).times { key_stack.pop }
+ end
+ else
+ key_stack.pop
+ end
+ key_stack.push(key)
+ prev_indent = indent
+
+ joined_key = key_stack.join('.')
+ if joined_key =~ /#{pattern}/
+ results << [filepath, line_number, joined_key, word]
+ end
+ end
+ end
+ end
+
+ results
+ end
+ end
+end
View
@@ -1,7 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "I18nGrep" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
end

0 comments on commit 58261df

Please sign in to comment.