haml-i18n-extractor 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzYwYzZlM2U0ZTJjZjRjMTI4NzUzOGQ5YTJhZTZjNmJkN2RjZWM5MA==
4
+ OTBmMTFhZDE4NTI3ZjQ1ZjlkNjJiMTgxOGZhZjc0ODI4MzM0M2YxOQ==
5
5
  data.tar.gz: !binary |-
6
- ZTI2N2EzYmQxN2YyMzZmNjU1OGY5N2Q1MGY4ZWYwNGNiODcwOWVlNg==
6
+ MjVmYTU2MTY3ZjcxMDAzYTM2MjAzZDEzMWM3MmRkNGM1YTVlNzYxYg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YmJjOTk2ZmU0YTI2MzZmZWZkZTI4YjQ5MzVjOTNhNmRiZTE5ZWQwYzkxY2Zi
10
- ZjE4NmNlZGI0NWFmYjQ4NjAwMWY1ZDE5YmIxMjg1NGMzN2UzNjMzNmExNWFh
11
- ZGIwZGI0ZmU5NzQ0YmFmNDEwOTg3YWFkMTE0N2Q3NDE0NTliZDU=
9
+ MWMyMzQyNmQ5YWNiZGUxOWVkYzA1NDdhZjJkYzY2ZWUyYjdjNzk3MjQwNzhk
10
+ MmU1MzBjZjkyNjMyMTlhMDcxZTIyNWZhNjNlZGVmM2E0NzNmYjk0NGU1YTUx
11
+ YTJlNTFkYTRjOTY1ZmNiZThhODlkMmUyZTQ1NDg1NzRiMjE4M2Y=
12
12
  data.tar.gz: !binary |-
13
- M2IzYjJjNTdjMzMwYTU5NmJhODgxMTA4YzNiNDUzOTA4Yjk1MDNjZDc1MGE1
14
- YTU2YWNkMmIxY2Q0MGEyMGM3OWMxODhiMjc4YmMyMDExM2U5NzZjYjQzYjI2
15
- OTk1ZWNmYzVkNGE1MmY1NTJkNGVhOTNkMTk1MDg5NWEyMGEwMDI=
13
+ ZTcwMWFiYTI5ZmM0ZmMwNzUyNGU4Y2Y2OTIzZTBjODE1MDUzYmU0MmJhYTYw
14
+ ODQ0MzE4YTM3ZjBhZDYxZjU1OTQ4YjRlOTBlYWNkZjdiZjlkYTc4MzJmYTBk
15
+ OGVkMTAzMTQ5ZGQ2ZmRkY2VkZmZjNWFmZGJiZjdlMmM2Yjk1NTg=
data/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  Extract strings likely to be translated from haml templates for I18n translation. Replace the text, create yaml files, do all things you thought macros would solve, but didn't end up really saving THAT much time. Automate that pain away.
4
4
 
5
- # Idempotent
5
+ # Use it over and over again
6
6
 
7
- It's idempotent, which means you can run this library/executable against the same haml file after you've already translated the keys, and it won't try to retranslate already translated text. Pretty cool.
7
+ It doesn't translate already translated keys, or things it identfies that are not text. What this means is that you can run this library/executable against the same haml file(s) after you've already translated stuff, and it will only look at things you really need and not any prior stuff. Pretty great. Try it, and see.
8
8
 
9
9
  ## Usage
10
10
 
@@ -33,7 +33,12 @@ Check out the quite brief movie/swf file demo of this lib's executable in `demo/
33
33
 
34
34
  [Demo](http://shairosenfeld.com/haml-i18n-extractor-demo.swf)
35
35
 
36
- One thing missing there just added is the "tag" functionality, which enables you to tag a line you want to review for later, if you are unsure you want to replace it. It will create a list of /file/path:42 tags for you to go and revisit later.
36
+ Things that have been added since recording demo:
37
+
38
+ - A "tag" functionality, which enables you to tag a line you want to review for later, if you are unsure you want to replace it. It will create a list of /file/path:42 tags for you to go and revisit later.
39
+ - You can use "Next" if you're in the middle of processing a file and go to the next file.
40
+
41
+ Have any other ideas? Let me know or better yet, submit a pull request.
37
42
 
38
43
  ## Example output
39
44
 
@@ -1,14 +1 @@
1
- # @date = Hash.recursive_init
2
- # @date[month][day][hours][min][sec] = 1
3
- # @date now equals {month=>{day=>{hours=>{min=>{sec=>1}}}}}
4
- class Hash
5
- def self.recursive_init
6
- new do |hash, key|
7
- unless key.nil?
8
- hash[key] = recursive_init
9
- end
10
- end
11
- end
12
- end
13
-
14
1
  require 'active_support/core_ext/hash/deep_merge'
@@ -1,7 +1,7 @@
1
1
  module Haml
2
2
  module I18n
3
3
  class Extractor
4
- VERSION = "0.0.19"
4
+ VERSION = "0.0.20"
5
5
  end
6
6
  end
7
7
  end
@@ -3,7 +3,10 @@ module Haml
3
3
  class Extractor
4
4
  class Workflow
5
5
 
6
+ attr_reader :extractors
7
+
6
8
  def initialize(project_path)
9
+ @extractors = []
7
10
  @project_path = project_path
8
11
  @prompter = Haml::I18n::Extractor::Prompter.new
9
12
  unless File.directory?(@project_path)
@@ -56,8 +59,9 @@ module Haml
56
59
  options = {:type => type} # overwrite or dump haml
57
60
  options.merge!({:prompt_per_line => true}) # per-line prompts
58
61
  begin
59
- @ex1 = Haml::I18n::Extractor.new(haml_path, options)
60
- @ex1.run
62
+ @extractor = Haml::I18n::Extractor.new(haml_path, options)
63
+ @extractors << @extractor
64
+ @extractor.run
61
65
  rescue Haml::I18n::Extractor::InvalidSyntax
62
66
  @prompter.syntax_error(haml_path)
63
67
  end
@@ -22,10 +22,12 @@ module Haml
22
22
 
23
23
  # {:en => {:view_name => {:key_name => :string_name } } }
24
24
  def yaml_hash
25
- yml = HashWithIndifferentAccess.recursive_init
25
+ yml = Hash.new
26
26
  @locale_hash.map do |line_no, info|
27
27
  unless info[:keyname].nil?
28
- yml[i18n_scope][standardized_viewname(info[:path])][standarized_keyname(info[:keyname])] = info[:replaced_text]
28
+ keyspace = [i18n_scope,standardized_viewnames(info[:path]), standarized_keyname(info[:keyname]),
29
+ info[:replaced_text]].flatten
30
+ yml.deep_merge!(nested_hash({},keyspace))
29
31
  end
30
32
  end
31
33
  yml = hashify(yml)
@@ -57,7 +59,7 @@ module Haml
57
59
 
58
60
  # {:foo => {:bar => {:baz => :mam}, :barrr => {:bazzz => :mammm} }}
59
61
  def hashify(my_hash)
60
- if my_hash.is_a?(HashWithIndifferentAccess)
62
+ if my_hash.is_a?(Hash)
61
63
  result = Hash.new
62
64
  my_hash.each do |k, v|
63
65
  result[k.to_s] = hashify(v)
@@ -68,6 +70,17 @@ module Haml
68
70
  end
69
71
  end
70
72
 
73
+ # [1,2,3] => {1 => {2 => 3}}
74
+ def nested_hash(hash,array)
75
+ elem = array.shift
76
+ if array.size == 1
77
+ hash[elem] = array.last
78
+ else
79
+ hash[elem] = {}
80
+ nested_hash(hash[elem],array)
81
+ end
82
+ hash
83
+ end
71
84
  def i18n_scope
72
85
  :en
73
86
  end
@@ -78,12 +91,21 @@ module Haml
78
91
  $1
79
92
  end
80
93
 
81
- # assuming rails format, app/views/users/index.html.haml return users
82
- def standardized_viewname(pth)
83
- Pathname.new(pth).dirname.to_s.split("/").last
94
+ # assuming rails format, app/views/users/index.html.haml return [users]
95
+ # app/views/admin/users/index.html.haml return [admin, users]
96
+ # app/views/admin/users/with_namespace/index.html.haml return [admin, users, with_namespace]
97
+ # otherwise, just grab the last one.
98
+ def standardized_viewnames(pth)
99
+ array_of_dirs = Pathname.new(pth).dirname.to_s.split("/")
100
+ index = array_of_dirs.index("views")
101
+ if index
102
+ array_of_dirs[index+1..-1]
103
+ else
104
+ [array_of_dirs.last]
105
+ end
84
106
  end
85
107
 
86
- end
108
+ end
87
109
  end
88
110
  end
89
111
  end
@@ -52,7 +52,7 @@ module Haml
52
52
  end
53
53
 
54
54
  test "with a prompt_per_line option takes user input into consideration for yaml" do
55
- hax_shit
55
+ TestHelper.hax_shit
56
56
  h = Haml::I18n::Extractor.new(file_path("ex1.haml"), :prompt_per_line => true)
57
57
  user_input = "D" # dump
58
58
  File.readlines(file_path("ex1.haml")).size.times do
@@ -66,7 +66,7 @@ module Haml
66
66
  end
67
67
 
68
68
  test "with a prompt_per_line option user can tag a line for later review" do
69
- hax_shit
69
+ TestHelper.hax_shit
70
70
  if File.exist?(Haml::I18n::Extractor::TaggingTool::DB)
71
71
  assert_equal File.readlines(Haml::I18n::Extractor::TaggingTool::DB), []
72
72
  end
@@ -103,16 +103,8 @@ module Haml
103
103
  assert_equal File.exists?(@ex1.haml_writer.path), true
104
104
  end
105
105
 
106
- def hax_shit
107
- Dir.glob("*yml").map {|p| FileUtils.rm(p) } # HAX, TODO: handle with yaml files correctly (config/en.yml)
108
- Dir.glob("config/locales/*yml").map {|p| FileUtils.rm(p) } # HAX, TODO: handle with yaml files correctly (config/en.yml)
109
- if File.exists?(Haml::I18n::Extractor::TaggingTool::DB)
110
- FileUtils.rm_rf(Haml::I18n::Extractor::TaggingTool::DB) # HAX, TODO: setup/teardown
111
- end
112
- end
113
-
114
106
  test "it writes the locale info to an out file when run" do
115
- hax_shit
107
+ TestHelper.hax_shit
116
108
  assert_equal File.exists?(@ex1.yaml_tool.locale_file), false
117
109
  @ex1.run
118
110
  assert_equal File.exists?(@ex1.yaml_tool.locale_file), true
@@ -0,0 +1,19 @@
1
+ module Haml
2
+ class IntegrationTest < MiniTest::Unit::TestCase
3
+
4
+ def setup
5
+ TestHelper.setup_project_directory!
6
+ end
7
+
8
+ def teardown
9
+ TestHelper.teardown_project_directory!
10
+ end
11
+
12
+ test "it can handle namespaced views" do
13
+ workflow = TestHelper.mock_full_project_user_interaction!
14
+ namespaced_extractor = workflow.extractors.select{|ex| ex.haml_writer.path.match /namespace/ }.last
15
+ assert namespaced_extractor.yaml_tool.yaml_hash["en"]["namespace"], "namespace key works"
16
+ end
17
+
18
+ end
19
+ end
@@ -11,6 +11,7 @@ module Haml
11
11
  end
12
12
 
13
13
  test "It uses a file which it uses to keep track of user tagged lines" do
14
+ TestHelper.hax_shit
14
15
  assert ! File.exists?(FILE), "no tagging file should exist"
15
16
  Haml::I18n::Extractor::TaggingTool.new
16
17
  assert File.exists?(FILE), "tagging file should be created on init"
data/test/test_helper.rb CHANGED
@@ -94,11 +94,37 @@ module TestHelper
94
94
  TMPDIR = File.join(File.dirname(__FILE__) + "/tmp/")
95
95
  PROJECT_DIR = File.join(TMPDIR, "workflow/")
96
96
 
97
+ def self.mock_full_project_user_interaction!
98
+ automate_user_interaction = ""
99
+ 6.times do # should be number of files we're testing on
100
+ automate_user_interaction << "O" # overwrite file
101
+ 50.times do # should be number of lines in file,
102
+ # this should do right now.
103
+ automate_user_interaction << "y" # replace line
104
+ end
105
+ end
106
+ @workflow = Haml::I18n::Extractor::Workflow.new(TestHelper::PROJECT_DIR)
107
+ with_highline(automate_user_interaction) do
108
+ @workflow.run
109
+ end
110
+ @workflow
111
+ end
112
+
113
+
114
+ def self.hax_shit
115
+ Dir.glob("*yml").map {|p| FileUtils.rm(p) } # HAX, TODO: handle with yaml files correctly (config/en.yml)
116
+ Dir.glob("config/locales/*yml").map {|p| FileUtils.rm(p) } # HAX, TODO: handle with yaml files correctly (config/en.yml)
117
+ if File.exists?(Haml::I18n::Extractor::TaggingTool::DB)
118
+ FileUtils.rm_rf(Haml::I18n::Extractor::TaggingTool::DB) # HAX, TODO: setup/teardown
119
+ end
120
+ end
121
+
97
122
  def self.setup_project_directory!
98
123
  # mimic a rails app.
99
124
  view1 = File.join(PROJECT_DIR, "app", "views", "view1")
100
125
  view2 = File.join(PROJECT_DIR, "app", "views", "view2")
101
- [ view1, view2 ].map do |dir|
126
+ view3 = File.join(PROJECT_DIR, "app", "views", "namespace", "view3")
127
+ [ view1, view2, view3 ].map do |dir|
102
128
  FileUtils.mkdir_p(dir)
103
129
  ["thing.haml", "thang.haml"].map do |fn|
104
130
  haml_text=<<EOH
@@ -121,11 +147,11 @@ module TestHelper
121
147
  %td.data= cn.notification.data
122
148
  %td.success= cn.success
123
149
  %td.reported_to= cn.reported_to
124
- .nav= will_paginate(@consumer_notifications)
150
+ .nav= will_paginate(@consumer_notifications)
125
151
  EOH
126
- File.open(File.join(dir, fn), "w") do |f|
127
- f.write haml_text
128
- end
152
+ File.open(File.join(dir, fn), "w") do |f|
153
+ f.write haml_text
154
+ end
129
155
  end
130
156
  end
131
157
  # config dir
@@ -3,57 +3,37 @@ require 'test_helper'
3
3
  module Haml
4
4
  class WorkflowTest < MiniTest::Unit::TestCase
5
5
 
6
- def setup
7
- TestHelper.setup_project_directory! # tests here rely on this setup...
8
- @workflow = Haml::I18n::Extractor::Workflow.new(TestHelper::PROJECT_DIR)
9
- end
10
-
11
- def teardown
12
- TestHelper.teardown_project_directory!
13
- end
6
+ def setup
7
+ TestHelper.setup_project_directory! # tests here rely on this setup...
8
+ @workflow = TestHelper.mock_full_project_user_interaction!
9
+ end
14
10
 
15
- def full_project_user_interaction
16
- automate_user_interaction = ""
17
- 6.times do # should be number of files we're testing on
18
- automate_user_interaction << "O" # overwrite file
19
- 50.times do # should be number of lines in file,
20
- # this should do right now.
21
- automate_user_interaction << "y" # replace line
22
- end
11
+ def teardown
12
+ TestHelper.teardown_project_directory!
23
13
  end
24
- automate_user_interaction
25
- end
26
14
 
27
- def test_it_should_work_on_a_directory_mkay
28
- filename = "#{TestHelper::PROJECT_DIR}app/views/bar/thang.haml"
29
- bad_worfklow = Haml::I18n::Extractor::Workflow.new(filename)
30
- assert false, "should raise"
15
+ def test_it_should_work_on_a_directory_mkay
16
+ filename = "#{TestHelper::PROJECT_DIR}app/views/bar/thang.haml"
17
+ bad_worfklow = Haml::I18n::Extractor::Workflow.new(filename)
18
+ assert false, "should raise"
31
19
  rescue Haml::I18n::Extractor::NotADirectory
32
20
  assert true, "workflow works on a directory bubba."
33
- end
34
-
35
- def test_it_finds_all_haml_files
36
- assert_equal @workflow.files.size, 4
37
- end
21
+ end
38
22
 
39
- def test_outputs_stats
40
- with_highline do
41
- @workflow.start_message
42
- assert @output.string.match(/Found 4 haml files/), "Outputs stats"
23
+ def test_it_finds_all_haml_files
24
+ assert_equal @workflow.files.size, 6
43
25
  end
44
- end
45
26
 
46
- def test_yaml_file_in_config
47
- with_highline(full_project_user_interaction) do
48
- @workflow.run
27
+ def test_outputs_stats
28
+ with_highline do
29
+ @workflow.start_message
30
+ assert @output.string.match(/Found 6 haml files/), "Outputs stats"
31
+ end
49
32
  end
50
- end
51
33
 
52
- def test_run_works
53
- with_highline(full_project_user_interaction) do
54
- @workflow.run
34
+ def test_yaml_file_in_config
35
+ TestHelper.mock_full_project_user_interaction!
55
36
  end
56
- end
57
37
 
58
38
  end
59
39
  end
@@ -5,6 +5,7 @@ module Haml
5
5
 
6
6
  def setup
7
7
  @temp_locale_dir = "./test/tmp/"
8
+ TestHelper.setup_project_directory!
8
9
  end
9
10
 
10
11
  def setup_locale_hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml-i18n-extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shai Rosenfeld
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-04 00:00:00.000000000 Z
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tilt
@@ -200,6 +200,7 @@ files:
200
200
  - test/extractor_test.rb
201
201
  - test/haml_reader_test.rb
202
202
  - test/haml_writer_test.rb
203
+ - test/integration_test.rb
203
204
  - test/prompter_test.rb
204
205
  - test/support/bad.haml
205
206
  - test/support/ex1.haml
@@ -239,6 +240,7 @@ test_files:
239
240
  - test/extractor_test.rb
240
241
  - test/haml_reader_test.rb
241
242
  - test/haml_writer_test.rb
243
+ - test/integration_test.rb
242
244
  - test/prompter_test.rb
243
245
  - test/support/bad.haml
244
246
  - test/support/ex1.haml