diff --git a/Gemfile b/Gemfile index 6b3d5bea..e70ac6fd 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,10 @@ source :rubygems gem 'fast_gettext' group :dev do + gem 'haml' + gem 'slim' + gem 'ruby_parser' + gem 'gettext' gem 'sqlite3', '~>1.3.4' gem 'rails', ENV['RAILS'] || '~>3' gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock index 087dff7f..b9df151b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,13 +36,17 @@ GEM diff-lcs (1.1.2) erubis (2.7.0) fast_gettext (0.5.13) + gettext (2.1.0) + locale (>= 2.0.5) git (1.2.5) + haml (3.1.4) hike (1.2.1) i18n (0.6.0) jeweler (1.6.4) bundler (~> 1.0) git (>= 1.2.5) rake + locale (2.0.5) mail (2.3.0) i18n (>= 0.4.0) mime-types (~> 1.16) @@ -84,11 +88,18 @@ GEM rspec-expectations (2.6.0) diff-lcs (~> 1.1.2) rspec-mocks (2.6.0) + ruby_parser (2.3.1) + sexp_processor (~> 3.0) + sexp_processor (3.0.10) + slim (1.1.0) + temple (~> 0.3.5) + tilt (~> 1.3.2) sprockets (2.0.0) hike (~> 1.2) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) sqlite3 (1.3.4) + temple (0.3.5) thor (0.14.6) tilt (1.3.3) treetop (1.4.10) @@ -101,8 +112,12 @@ PLATFORMS DEPENDENCIES fast_gettext + gettext + haml jeweler rails (~> 3) rake rspec (~> 2) + ruby_parser + slim sqlite3 (~> 1.3.4) diff --git a/lib/gettext_i18n_rails/haml_parser.rb b/lib/gettext_i18n_rails/haml_parser.rb index 04209b23..dcd7ad47 100644 --- a/lib/gettext_i18n_rails/haml_parser.rb +++ b/lib/gettext_i18n_rails/haml_parser.rb @@ -14,18 +14,18 @@ def target?(file) end def parse(file, msgids = []) - return msgids unless load_haml - require 'gettext_i18n_rails/ruby_gettext_extractor' - - text = IO.readlines(file).join + return msgids unless prepare_haml_parsing + code = haml_to_code(File.read(file)) + RubyGettextExtractor.parse_string(code, file, msgids) + end - haml = Haml::Engine.new(text) - code = haml.precompiled - return RubyGettextExtractor.parse_string(code, file, msgids) + def haml_to_code(haml) + Haml::Engine.new(haml).precompiled end - def load_haml + def prepare_haml_parsing return true if @haml_loaded + begin require "#{::Rails.root.to_s}/vendor/plugins/haml/lib/haml" rescue LoadError @@ -36,6 +36,8 @@ def load_haml return false end end + + require 'gettext_i18n_rails/ruby_gettext_extractor' @haml_loaded = true end end diff --git a/spec/gettext_i18n_rails/haml_parser_spec.rb b/spec/gettext_i18n_rails/haml_parser_spec.rb new file mode 100644 index 00000000..07316416 --- /dev/null +++ b/spec/gettext_i18n_rails/haml_parser_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' +require 'gettext_i18n_rails/haml_parser' + +describe GettextI18nRails::HamlParser do + let(:parser){ GettextI18nRails::HamlParser } + + describe "#target?" do + it "targets .haml" do + parser.target?('foo/bar/xxx.haml').should == true + end + + it "does not target anything else" do + parser.target?('foo/bar/xxx.erb').should == false + end + end + + describe "#parse" do + it "finds messages in haml" do + with_file '= _("xxxx")' do |path| + parser.parse(path, []).should == [ + ["xxxx", "#{path}:1"] + ] + end + end + + it "does not find messages in text" do + with_file '_("xxxx")' do |path| + parser.parse(path, []).should == [] + end + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7ec9749d..213956a0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,9 +2,31 @@ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__)) +require 'tempfile' require 'active_support' require 'active_record' require 'action_controller' require 'action_mailer' require 'fast_gettext' require 'gettext_i18n_rails' + +begin + Gem.all_load_paths +rescue + puts "Fixing Gem.all_load_paths" + module Gem;def self.all_load_paths;[];end;end +end + +module Rails + def self.root + File.dirname(__FILE__) + end +end + +def with_file(content) + Tempfile.open('gettext_i18n_rails_specs') do |f| + f.write(content) + f.close + yield f.path + end +end \ No newline at end of file