wca_i18n 0.4.1 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4713f25dc69e1f630a44ecb3d2c2cb2bb4241953
4
- data.tar.gz: 30a164cc5597240d23a833c6424f5c72c05ba999
2
+ SHA256:
3
+ metadata.gz: a2d83fc07a32950b5c6dd5901b9069fea37cd51b4350de35c8ced8c39fb84fd3
4
+ data.tar.gz: 5b7a70f04b64b5d80adda2e977a882bdfc5b9aa6e7890ece5af1f6ac97061c15
5
5
  SHA512:
6
- metadata.gz: e6409df5ab8301050dd40c1b677a50ac94756f0823ebd87ab4e9a8d565c3298e453d66e20b85e2c5bda9b79d66f3e509643a375ddc968cc47c55343dea21ebef
7
- data.tar.gz: 614c59db2f914fbc5085c6bf431d4a91bfac69c2877cbbd176d21bd077b9eabe3c42f7e74c45e582d678665dc40779ab43f8717132f3f87a881ad8f7332eb8be
6
+ metadata.gz: 051dbff44fab404672157a6e0000216a216e505db49c85e66cf00cfe0e6a47737ee66e344360307538c77c05d36501636bab13a9f45cc690a1c687113db7ed2a
7
+ data.tar.gz: 17dab112e3fb171bbc7aaacfc2e99145c5b348d2a8e42cfca6243832506b6aca974b877b8d146564e81e8124c8f1dad9e662efafd0a54710359b05e7af96e21e
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /vendor/bundle/
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
data/.travis.yml CHANGED
@@ -1,5 +1,11 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.4.3
5
- before_install: gem install bundler -v 1.16.0
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
6
+ deploy:
7
+ provider: rubygems
8
+ api_key:
9
+ secure: LOAbx7/3XEoTqOS91rFP6U9CA8wAYThY8eAuZxQh0VVAi6EushRNQQJ21EFtatRB86/VNZ2fLQe3fDYQltKDrGjEcf/jT3j0PAMUbvhFIS8OlOFPUdK/TPfssPcrDzU8u7YJ3WZ4X+mAimCOokXoJ8ueqeNuhpCV3rXdxaVcxn0C0r4dzfzsPWihle3wopBak13hnZ2baiHGt8INWVO+kcukeW0ldscwt1A8XiCggBWpFHtsRHU+ki1UWNXIw5NKy4/hLVuDyy9FRi4ofOXvdW0R8Pybg5Aqvx9rs91Xkv6sQt1dPYy5QAblF2KidHfDXPk7uuJKYEO04MMLxKsqO39CpckxoYyS8qmDn9On6s/flVElye95+4ZgjYsiSrx4U1OnP0rgFMCbh9jh29w5e7T4MwaKkEqbK2KZGD0z9unbm5+Klk4+jI5BdgFNw8sw4W/1T1xaNO8FWWlq6OKk+U2MIsVXZqWy1mIEO14+eEODauFrFYh+BzigjG66oMnLt0G6D6xDJWSGKgPhbS8BvGWmTeNT5gDLizG+cHhV4RxsPCq3+NPXhNtTFXHXZHb7XWMTgIUd3r5aBSmkY6yfmWjUqgWtJYAdzjkL3hHMx14iatMz5tN3ApWu3It8o9wOFTfwU4lkqQO8h20wtPmZOmAPic4Id9z3CRPkU3GKsao=
10
+ on:
11
+ tags: true
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wca_i18n (0.4.1)
4
+ wca_i18n (0.4.4)
5
5
  colorize (~> 0.8)
6
6
 
7
7
  GEM
@@ -11,19 +11,19 @@ GEM
11
11
  colorize (0.8.1)
12
12
  diff-lcs (1.3)
13
13
  rake (10.5.0)
14
- rspec (3.7.0)
15
- rspec-core (~> 3.7.0)
16
- rspec-expectations (~> 3.7.0)
17
- rspec-mocks (~> 3.7.0)
18
- rspec-core (3.7.1)
19
- rspec-support (~> 3.7.0)
20
- rspec-expectations (3.7.0)
14
+ rspec (3.8.0)
15
+ rspec-core (~> 3.8.0)
16
+ rspec-expectations (~> 3.8.0)
17
+ rspec-mocks (~> 3.8.0)
18
+ rspec-core (3.8.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-expectations (3.8.1)
21
21
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.7.0)
23
- rspec-mocks (3.7.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-mocks (3.8.0)
24
24
  diff-lcs (>= 1.2.0, < 2.0)
25
- rspec-support (~> 3.7.0)
26
- rspec-support (3.7.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-support (3.8.0)
27
27
 
28
28
  PLATFORMS
29
29
  ruby
@@ -36,4 +36,4 @@ DEPENDENCIES
36
36
  wca_i18n!
37
37
 
38
38
  BUNDLED WITH
39
- 1.16.0
39
+ 1.16.1
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # WcaI18n [![Build Status](https://travis-ci.org/thewca/wca-i18n.svg?branch=master)](https://travis-ci.org/thewca/wca-i18n)
1
+ # WcaI18n [![Build Status](https://travis-ci.org/thewca/wca_i18n.svg?branch=master)](https://travis-ci.org/thewca/wca_i18n)
2
2
 
3
3
  Use this Gem to diff Rails translations.
4
4
 
@@ -34,18 +34,28 @@ $ wca_i18n en.yml *.yml
34
34
  There are two parts to this library: `WcaI18n::YAMLWithComments` (a YAML parser
35
35
  that preserves comments) and `WcaI18n::Translation` (used to load and diff
36
36
  translation YAML files). Until we have better documentation, it's best to look
37
- at our [specs](https://github.com/thewca/wca-i18n/tree/master/spec) for how to
37
+ at our [specs](https://github.com/thewca/wca_i18n/tree/master/spec) for how to
38
38
  them.
39
39
 
40
40
  ## Development
41
41
 
42
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
43
+ `bin/rspec` to run the tests. You can also run `bin/console` for an interactive
44
+ prompt that will allow you to experiment.
43
45
 
44
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+ To install this gem onto your local machine, run `bundle exec rake install`.
47
+
48
+ ## Releasing
49
+
50
+ To release a new version, update the version number in `version.rb`, run
51
+ `bundle`, commit your changes, and then run `gem_push=no rake release`, which
52
+ will create a git tag for the version and push git commits and tags. After
53
+ this, Travis should run against the newly created tag, and push the `.gem` file
54
+ to [rubygems.org](https://rubygems.org/gems/wca_i18n).
45
55
 
46
56
  ## Contributing
47
57
 
48
- Bug reports and pull requests are welcome on GitHub at https://github.com/thewca/wca-i18n.
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thewca/wca_i18n.
49
59
 
50
60
  ## License
51
61
 
@@ -1,18 +1,15 @@
1
1
  require "json"
2
2
  require "digest"
3
- require "wca_i18n/yaml_with_comments"
3
+ require "wca_i18n/yaml_to_enriched_ruby_hash"
4
4
 
5
5
  module WcaI18n
6
- TranslatedLeaf = Struct.new(:translated, :original_hash)
7
-
8
6
  class Translation
9
- PLURALIZATION_KEYS = %w(zero one two few many other).freeze
10
7
 
11
8
  attr_accessor :locale, :data
12
9
 
13
10
  def initialize(locale, file_content)
14
11
  self.locale = locale.to_s
15
- self.data = commented_yaml_to_translated_yaml(YAMLWithComments.parse(file_content))
12
+ self.data = YAMLToEnrichedRubyHash.parse(file_content)
16
13
  end
17
14
 
18
15
  def compare_to(base)
@@ -26,17 +23,6 @@ module WcaI18n
26
23
  original_str = Digest::SHA1.hexdigest(to_digest)[0..6]
27
24
  end
28
25
 
29
- private def commented_yaml_to_translated_yaml(commented_value)
30
- if leaf?(commented_value.value)
31
- original_hash = extract_original_hash_from_comment(commented_value.comment)
32
- return TranslatedLeaf.new(commented_value.strip_comments, original_hash)
33
- end
34
-
35
- commented_value.value.map do |key, value|
36
- [key, commented_yaml_to_translated_yaml(value)]
37
- end.to_h
38
- end
39
-
40
26
  private def diff_recursive(base, translation, context)
41
27
  diff = { missing: [], unused: [], outdated: [] }
42
28
  base_leaf = base.is_a?(TranslatedLeaf)
@@ -91,11 +77,6 @@ module WcaI18n
91
77
  end
92
78
  end
93
79
 
94
- private def leaf?(node)
95
- # If the node is a pluralization it's also a leaf!
96
- node.nil? || node.is_a?(String) || self.class.pluralization?(node)
97
- end
98
-
99
80
  def self.pluralization?(node)
100
81
  node.is_a?(Hash) && (node.keys & PLURALIZATION_KEYS).any?
101
82
  end
@@ -1,3 +1,3 @@
1
1
  module WcaI18n
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.4"
3
3
  end
@@ -0,0 +1,97 @@
1
+ require "psych"
2
+
3
+ module WcaI18n
4
+ PLURALIZATION_KEYS = %w(zero one two few many other).freeze
5
+ ORIGINAL_HASH_TAG = "#original_hash: ".freeze
6
+ SHOVEL = "<<".freeze
7
+ TranslatedLeaf = Struct.new(:translated, :original_hash)
8
+
9
+ # Re-implement some parts of the ToRuby emitter to inject our TranslatedLeaf where needs be
10
+ class YAMLToEnrichedRubyHash < Psych::Visitors::ToRuby
11
+ def self.create(original_hashes_map)
12
+ class_loader = Psych::ClassLoader.new
13
+ scanner = Psych::ScalarScanner.new class_loader
14
+ new(scanner, class_loader, original_hashes_map)
15
+ end
16
+
17
+ def self.parse(text)
18
+ tree = Psych.parser.parse(text)
19
+ emitter = self.create(text)
20
+ # Not sure why, but "accept" returns an array with one element.
21
+ # Probably because the root of the YAML is a sequence by default?
22
+ emitter.accept(tree.handler.root).first
23
+ end
24
+
25
+ def initialize(*args, text)
26
+ super(*args)
27
+ _build_original_hashes_by_line_from_text(text)
28
+ end
29
+
30
+ def pluralization_map?(v)
31
+ return false unless v.is_a?(Psych::Nodes::Mapping)
32
+ v.children.each_slice(2) do |k,v|
33
+ return true if WcaI18n::PLURALIZATION_KEYS.include?(accept(k))
34
+ end
35
+ return false
36
+ end
37
+
38
+ # Copy from the revive_hash method in https://github.com/ruby/psych/blob/e9e4567adefc52e6511df7060851bce9fe408082/lib/psych/visitors/to_ruby.rb
39
+ # Except we override the generic case with our code.
40
+ def revive_hash hash, o
41
+ o.children.each_slice(2) { |k,v|
42
+ key = accept(k)
43
+ val = accept(v)
44
+
45
+ if key == SHOVEL && k.tag != "tag:yaml.org,2002:str"
46
+ case v
47
+ when Nodes::Alias, Nodes::Mapping
48
+ begin
49
+ hash.merge! val
50
+ rescue TypeError
51
+ hash[key] = val
52
+ end
53
+ when Nodes::Sequence
54
+ begin
55
+ h = {}
56
+ val.reverse_each do |value|
57
+ h.merge! value
58
+ end
59
+ hash.merge! h
60
+ rescue TypeError
61
+ hash[key] = val
62
+ end
63
+ else
64
+ hash[key] = val
65
+ end
66
+ else
67
+ # This is where we handle the translated key
68
+ if v.is_a?(Psych::Nodes::Scalar) && !WcaI18n::PLURALIZATION_KEYS.include?(key)
69
+ # For scalar value, the start line registered is the correct line
70
+ # We assume that the '#original_hash: ' comment comes on the line before.
71
+ original_hash = @original_hashes_by_line.delete(v.start_line - 1)
72
+ val = WcaI18n::TranslatedLeaf.new(val, original_hash)
73
+ end
74
+ if pluralization_map?(v)
75
+ # For mappings, the start line registered is the line of the first key/value!
76
+ original_hash = @original_hashes_by_line.delete(v.start_line - 2)
77
+ val = WcaI18n::TranslatedLeaf.new(val, original_hash)
78
+ end
79
+ hash[key] = val
80
+ end
81
+ }
82
+ hash
83
+ end
84
+
85
+ def _build_original_hashes_by_line_from_text(text)
86
+ @original_hashes_by_line = {}.tap do |hash|
87
+ # Build a hash mapping a line number to its comment
88
+ text.each_line.with_index do |line, index|
89
+ stripped_line = line.strip
90
+ if stripped_line.start_with?(ORIGINAL_HASH_TAG)
91
+ hash[index] = stripped_line[ORIGINAL_HASH_TAG.length..-1]
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
data/wca_i18n.gemspec CHANGED
@@ -6,7 +6,7 @@ require "wca_i18n/version"
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "wca_i18n"
8
8
  spec.version = WcaI18n::VERSION
9
- spec.authors = ["Jeremy Fleischman"]
9
+ spec.authors = ["WCA Software Team"]
10
10
  spec.email = ["software@worldcubeassociation.org"]
11
11
 
12
12
  spec.summary = %q{Track how up to date Rails translations are.}
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
  spec.add_development_dependency "byebug", "~> 9.0"
27
27
  spec.add_runtime_dependency "colorize", "~> 0.8"
28
+
29
+ spec.required_ruby_version = '>= 2.5'
28
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wca_i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
- - Jeremy Fleischman
8
- autorequire:
7
+ - WCA Software Team
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-07 00:00:00.000000000 Z
11
+ date: 2022-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.8'
83
- description:
83
+ description:
84
84
  email:
85
85
  - software@worldcubeassociation.org
86
86
  executables:
@@ -104,13 +104,13 @@ files:
104
104
  - lib/wca_i18n.rb
105
105
  - lib/wca_i18n/translation.rb
106
106
  - lib/wca_i18n/version.rb
107
- - lib/wca_i18n/yaml_with_comments.rb
107
+ - lib/wca_i18n/yaml_to_enriched_ruby_hash.rb
108
108
  - wca_i18n.gemspec
109
109
  homepage: https://github.com/thewca/wca_i18n
110
110
  licenses:
111
111
  - MIT
112
112
  metadata: {}
113
- post_install_message:
113
+ post_install_message:
114
114
  rdoc_options: []
115
115
  require_paths:
116
116
  - lib
@@ -118,16 +118,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
118
  requirements:
119
119
  - - ">="
120
120
  - !ruby/object:Gem::Version
121
- version: '0'
121
+ version: '2.5'
122
122
  required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  requirements:
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
127
  requirements: []
128
- rubyforge_project:
129
- rubygems_version: 2.6.14
130
- signing_key:
128
+ rubygems_version: 3.3.7
129
+ signing_key:
131
130
  specification_version: 4
132
131
  summary: Track how up to date Rails translations are.
133
132
  test_files: []
@@ -1,68 +0,0 @@
1
- require "yaml"
2
-
3
- module WcaI18n
4
- class YAMLWithComments < Struct.new(:comment, :value)
5
- def self.parse(text)
6
- _decorate_with_comments(YAML.safe_load(text), [], text)[1]
7
- end
8
-
9
- def strip_comments
10
- stripped_value = self.value
11
-
12
- if stripped_value.kind_of?(Hash)
13
- stripped_value = stripped_value.map do |key, value|
14
- [key, value.strip_comments]
15
- end.to_h
16
- end
17
-
18
- stripped_value
19
- end
20
-
21
- def self._decorate_with_comments(node, context, text)
22
- if node.kind_of?(Hash)
23
- node = node.map do |key, value|
24
- text, decorated = _decorate_with_comments(value, [*context, key], text)
25
- [key, decorated]
26
- end.to_h
27
- end
28
-
29
- trimmed_text, comment = _extract_comment(text, context)
30
- [trimmed_text, YAMLWithComments.new(comment, node)]
31
- end
32
-
33
- def self._extract_comment(text, context)
34
- return ["", ""] if context.empty?
35
-
36
- # Match any character, including newline.
37
- some_chars = /[\s\S]*?/
38
-
39
- comment_lines_group = /((?:^\s*#.*\n)*)/
40
-
41
- match_parent = ""
42
- context[0...-1].each do |key|
43
- match_parent = "#{match_parent}#{some_chars}#{build_key_matcher(key)}"
44
- end
45
- regexp = /(#{match_parent}#{some_chars})#{comment_lines_group}\s*#{build_key_matcher(context[-1])}/
46
-
47
- comment = nil
48
- text = text.sub(regexp) do
49
- # Group 1 is everything before the comment and the key.
50
- before = $1
51
- # Group 2 contains the comments matched before the key.
52
- comments = $2
53
- comment = comments.split('#').map(&:strip).reject(&:empty?).join("\n")
54
-
55
- # We return the beginning without the key, so that the current hash + key
56
- # are removed from the text, but the parents and the value stay in.
57
- before
58
- end
59
- throw "Could not find key: #{context} in given yaml text" unless comment
60
-
61
- [text, comment]
62
- end
63
-
64
- def self.build_key_matcher(key)
65
- /['\"]?#{key}['\"]?:/
66
- end
67
- end
68
- end