Skip to content
Browse files

Reorganizing some code, adding more classes, specs.

  • Loading branch information...
1 parent 97f387c commit ae2fa66514c77b1cd2256df2e225d74a7993848b @shaiguitar committed
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm use 1.9.3
View
14 lib/haml-i18n-extractor.rb
@@ -1,11 +1,5 @@
require "haml-i18n-extractor/version"
-require "haml-i18n-extractor/find_text"
-
-module Haml
- module I18n
- module Extractor
-
-
- end
- end
-end
+require "haml-i18n-extractor/text_finder"
+require "haml-i18n-extractor/text_replacer"
+require "haml-i18n-extractor/haml_reader"
+require "haml-i18n-extractor/extractor"
View
33 lib/haml-i18n-extractor/extractor.rb
@@ -0,0 +1,33 @@
+module Haml
+ module I18n
+ class Extractor
+
+ class InvalidSyntax < StandardError ; end
+
+ attr_reader :haml_reader
+
+ def initialize(haml_path)
+ @haml_reader = Haml::I18n::Extractor::HamlReader.new(haml_path)
+ validate_haml(@haml_reader.body)
+ end
+
+ def run!
+ @haml_reader.lines.each { |haml_line|
+ line_match = Haml::I18n::Extractor::TextFinder.new(haml_line)
+ line_replaced = Haml::I18n::Extractor::TextReplacer.new(haml_line, line_match)
+ }
+ end
+
+ private
+
+ def validate_haml(haml)
+ options ||= Haml::Options.new
+ parser = Haml::Parser.new(haml, options)
+ parser.parse
+ rescue Haml::SyntaxError
+ raise InvalidSyntax, "invalid syntax for haml #{@haml_reader.path}"
+ end
+
+ end
+ end
+end
View
20 lib/haml-i18n-extractor/haml_reader.rb
@@ -0,0 +1,20 @@
+module Haml
+ module I18n
+ class Extractor
+ class HamlReader
+
+ attr_reader :body, :path, :lines
+
+ def initialize(path)
+ file = File.open(path, "r")
+ @path = path
+ @body = file.read
+ file.rewind
+ @lines = file.readlines
+ file.rewind
+ end
+
+ end
+ end
+ end
+end
View
6 lib/haml-i18n-extractor/find_text.rb → lib/haml-i18n-extractor/text_finder.rb
@@ -2,8 +2,10 @@
require 'haml/parser'
module Haml
module I18n
- module Extractor
- class FindText
+ class Extractor
+ class TextFinder
+
+ # FIXME? right now this class should be used on a line per line basis
def initialize(haml)
@haml = haml
View
36 lib/haml-i18n-extractor/text_replacer.rb
@@ -0,0 +1,36 @@
+module Haml
+ module I18n
+ class Extractor
+ class TextReplacer
+
+ # limit the number of chars
+ LIMIT_KEY_NAME = 30
+
+ # do not pollute the key space it will make it invalid yaml
+ NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) )
+
+ def initialize(full_line, text_to_replace)
+ @full_line = full_line
+ @text_to_replace = text_to_replace
+ end
+
+ def replace!
+ t_text = "t(.#{to_keyname(@full_line)})"
+ replace_with = @full_line.gsub(@full_line, t_text)
+ {:replace_with => replace_with, :replace_this => @text_to_replace }
+ end
+
+ private
+
+ def to_keyname(str)
+ NOT_ALLOWED_IN_KEYNAME.each{|rm_me|
+ str.gsub!(rm_me, "")
+ }
+ str = str.gsub(/\s+/, " ").strip
+ str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
+ end
+
+ end
+ end
+ end
+end
View
2 lib/haml-i18n-extractor/version.rb
@@ -1,6 +1,6 @@
module Haml
module I18n
- module Extractor
+ class Extractor
VERSION = "0.0.1"
end
end
View
48 test/extractor_test.rb
@@ -0,0 +1,48 @@
+require 'test_helper'
+
+module Haml
+ class ExtractorTest < MiniTest::Unit::TestCase
+
+ # TODO: read in the file, parse it line by line, use text_finder and extract away
+ # TODO: ensure if there are duplicate strings on one line, to handle that correctly? not sure if that's even a use-case?
+ # for eg: find_text("%a{'href' => 'http://whatever'} whatever"), ["whatever"]
+ # TODO: Do extraction, verify the haml is parsable at the end.
+ # TODO: handles duplicate texts and doesn't silently let that pass somehow
+ # TODO: does not fail on handling text with weird characters so t() fails weirdly ("hell's a ~@~" => t(".hell's_a_~@~"))...
+
+ def file_path(name)
+ File.dirname(__FILE__) + "/support/#{name}"
+ end
+
+ def setup
+ @ex1 = Haml::I18n::Extractor.new(file_path("ex1.haml"))
+ end
+
+ test "can not initialize if the haml is not valid syntax" do
+ begin
+ Haml::I18n::Extractor.new(file_path("bad.haml"))
+ assert false, "should not get here"
+ rescue Haml::I18n::Extractor::InvalidSyntax
+ assert true, "it should fail with invalid syntax"
+ end
+ end
+
+ test "can initialize if the haml is valid syntax" do
+ assert true, "extractor can initialize"
+ end
+
+ test "it can process the haml and replace it with other text" do
+ @ex1.run!
+ end
+
+ test "it validates when it before it writes to an out file" do
+ assert false, "TODO"
+ end
+
+ test "it fails before it writes to an out file if it is not valid" do
+ # @haml_file_out = File.open(haml_path.gsub(/.haml$/, "i18n-extractor.haml"), "w+")
+ assert false, "TODO"
+ end
+
+ end
+end
View
29 test/haml_reader_test.rb
@@ -0,0 +1,29 @@
+require 'test_helper'
+require 'fileutils'
+
+module Haml
+ class HamlReaderTest < MiniTest::Unit::TestCase
+
+ TEMP_FILE_PATH = "/tmp/foo_haml_extractor_test"
+
+ def setup
+ FileUtils.rm_rf(TEMP_FILE_PATH)
+ 10.times do |index|
+ File.open(TEMP_FILE_PATH, "a+") do |f|
+ f.puts "line #{index}"
+ end
+ end
+ @reader = Haml::I18n::Extractor::HamlReader.new(TEMP_FILE_PATH)
+ end
+
+ test "has a body and a path" do
+ assert_equal @reader.path, TEMP_FILE_PATH
+ assert_equal @reader.body, File.read(TEMP_FILE_PATH)
+ end
+
+ test "has an array of lines in that file" do
+ assert_equal @reader.lines.size, 10
+ end
+
+ end
+end
View
12 test/support/bad.haml
@@ -0,0 +1,12 @@
+#clz
+ - else
+ 'foo'
+-this is
+ -not correct_haml
+
+ = in
+ kljlk
+ lkjl
+ ~invliz
+ ~invliz
+
View
0 test/support/badi18n-extractor.haml
No changes.
View
25 test/support/ex1.haml
@@ -0,0 +1,25 @@
+#header.page
+ %p#brand= link_to "Some place", '/'
+ #menu
+ %ul.header-links
+ - if true
+ - if false
+ %li
+ %a#cheese-rice{:href => url_for([:admin, :dashboard])}
+ %span.thing-ok Admin
+ #bla
+ %ul#admin-dropdown-box
+ %li
+ %a{:href => url_for([:admin, :dashboard])}
+ %span Admin Dashboard
+ %li
+ %a{:href => url_for([:admin, :stacks])}
+ %span Stacks
+ %li
+ %a{:href => url_for([:admin, :environments])}
+ %span Alerts
+ %li
+ %a{:href => url_for([:admin, :accounts])}
+ %span Accounts
+ - if true
+ %li{:class => some_ruby.any?{|a| a.ok? } ? 'ok' : 'notok'}
View
25 test/support/ex1.i18n-extractor.haml
@@ -0,0 +1,25 @@
+#header.page
+ %p#brand= link_to t(.some_place), '/'
+ #menu
+ %ul.header-links
+ - if true
+ - if false
+ %li
+ %a#cheese-rice{:href => url_for([:admin, :dashboard])}
+ %span.thing-ok t(".admin")
+ #bla
+ %ul#admin-dropdown-box
+ %li
+ %a{:href => url_for([:admin, :dashboard])}
+ %span t(".admin_dashboar")
+ %li
+ %a{:href => url_for([:admin, :stacks])}
+ %span t(".stacks")
+ %li
+ %a{:href => url_for([:admin, :environments])}
+ %span t(".alerts")
+ %li
+ %a{:href => url_for([:admin, :accounts])}
+ %span t(".accounts")
+ - if true
+ %li{:class => some_ruby.any?{|a| a.ok? } ? 'ok' : 'notok'}
View
0 test/support/ex1i18n-extractor.haml
No changes.
View
2 test/find_text_test.rb → test/text_finder_test.rb
@@ -89,7 +89,7 @@ class StringFinderTest < MiniTest::Unit::TestCase
def find_text(haml)
output_haml_debug(haml) if ENV['DEBUG']
- find_text = Haml::I18n::Extractor::FindText.new(haml) # this is on a line per-line basis.
+ find_text = Haml::I18n::Extractor::TextFinder.new(haml) # this is on a line per-line basis.
find_text.run
end
View
26 test/text_replacer_test.rb
@@ -0,0 +1,26 @@
+require 'test_helper'
+
+module Haml
+ class TextReplacerTest < MiniTest::Unit::TestCase
+
+ test "it initializes with the line it is going to replace and the match to replace" do
+ Haml::I18n::Extractor::TextReplacer.new("this is whatever", "this is whatever")
+ end
+
+ test "it can replace the body of haml with t() characters" do
+ replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever", "this is whatever")
+ assert_equal replacer.replace!, { :replace_with => "t(.this_is_whatever)", :replace_this => "this is whatever" }
+ end
+
+ test "it limits the characters of the t namespace it provides to LIMIT_KEY_NAME" do
+ replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever" * 80, "this is whatever" * 80)
+ assert_equal replacer.replace![:replace_with].size, Haml::I18n::Extractor::TextReplacer::LIMIT_KEY_NAME + 4 # t(.)
+ end
+
+ test "it does not allow weird characters in the keyname" do
+ replacer = Haml::I18n::Extractor::TextReplacer.new("this (is `ch@racter ~ madness!", "this (is `ch@racter ~ madness!")
+ assert_equal replacer.replace!, { :replace_with => "t(.this_is_chracter_madness)", :replace_this => "this (is `ch@racter ~ madness!" }
+ end
+
+ end
+end

0 comments on commit ae2fa66

Please sign in to comment.
Something went wrong with that request. Please try again.