i18n_extract 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f33f1b8308984c3005aa857ac7e1c02644027b1c6e8ddacc9cc5c4c4f02594e3
4
+ data.tar.gz: c112e2b679fa2cca77ea56bec0522cadbd41e4485fe78798ce036b068ab21e26
5
+ SHA512:
6
+ metadata.gz: c2c11d3be73db5d635b9a59ec2740d8fbdacbe3219da2c3a933fce155f0767ea8f609f1e4276faed832815a35c4cb0eff46188bc64a538b7c5a9ac0d0f25e956
7
+ data.tar.gz: 3c09998892ae1837462701b77398d5ffa4ad10899caaf43e4dbaa35287de158abf105d5683f65feff0135fe23da135dc92f5e3e7d55d16cfe521f2536b7ebd2f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in i18n_extract.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n_extract (1.0.0)
5
+ better_html (~> 1.0)
6
+ colorize (~> 0.8)
7
+ parser (~> 2.5)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionview (5.2.1)
13
+ activesupport (= 5.2.1)
14
+ builder (~> 3.1)
15
+ erubi (~> 1.4)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
18
+ activesupport (5.2.1)
19
+ concurrent-ruby (~> 1.0, >= 1.0.2)
20
+ i18n (>= 0.7, < 2)
21
+ minitest (~> 5.1)
22
+ tzinfo (~> 1.1)
23
+ ast (2.4.0)
24
+ better_html (1.0.11)
25
+ actionview (>= 4.0)
26
+ activesupport (>= 4.0)
27
+ ast (~> 2.0)
28
+ erubi (~> 1.4)
29
+ html_tokenizer (~> 0.0.6)
30
+ parser (>= 2.4)
31
+ smart_properties
32
+ builder (3.2.3)
33
+ colorize (0.8.1)
34
+ concurrent-ruby (1.0.5)
35
+ crass (1.0.4)
36
+ erubi (1.7.1)
37
+ html_tokenizer (0.0.7)
38
+ i18n (1.1.0)
39
+ concurrent-ruby (~> 1.0)
40
+ loofah (2.2.2)
41
+ crass (~> 1.0.2)
42
+ nokogiri (>= 1.5.9)
43
+ mini_portile2 (2.3.0)
44
+ minitest (5.11.3)
45
+ nokogiri (1.8.4)
46
+ mini_portile2 (~> 2.3.0)
47
+ parser (2.5.1.2)
48
+ ast (~> 2.4.0)
49
+ rails-dom-testing (2.0.3)
50
+ activesupport (>= 4.2.0)
51
+ nokogiri (>= 1.6)
52
+ rails-html-sanitizer (1.0.4)
53
+ loofah (~> 2.2, >= 2.2.2)
54
+ rake (10.5.0)
55
+ smart_properties (1.13.1)
56
+ thread_safe (0.3.6)
57
+ tzinfo (1.2.5)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ bundler (~> 1.16)
65
+ i18n_extract!
66
+ minitest (~> 5)
67
+ rake (~> 10.0)
68
+
69
+ BUNDLED WITH
70
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Marcel Müller
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # I18nExtract
2
+
3
+ This gem adds a new rake task 'i18n-extract:check' to your rails app.
4
+ It then highlights and raises an error when it finds hard-coded strings in your
5
+ views. Whitespace is ignored.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'i18n_extract'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install i18n_extract
22
+
23
+ ## Usage
24
+
25
+ Simply run:
26
+
27
+ ```sh
28
+ bundle exec rake i18n-extract:check
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec ruby test/erb_files_test.rb` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ 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).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TheNeikos/i18n_extract.
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "i18n_extract"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "i18n_extract/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i18n_extract"
8
+ spec.version = I18nExtract::VERSION
9
+ spec.authors = ["Marcel Müller"]
10
+ spec.email = ["neikos@neikos.email"]
11
+
12
+ spec.summary = "Extract hardcoded strings from your ERB views."
13
+ spec.homepage = "https://github.com/TheNeikos/i18n_extract"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "better_html", "~> 1.0"
33
+ spec.add_dependency "parser", "~> 2.5"
34
+ spec.add_dependency "colorize", "~> 0.8"
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5"
38
+ end
@@ -0,0 +1,46 @@
1
+ require "ast"
2
+ require "better_html"
3
+ require "better_html/parser"
4
+ require "parser"
5
+ require "i18n_extract/html_iterator"
6
+
7
+ module I18nExtract
8
+ class ExtractError < StandardError
9
+ attr_reader :node
10
+ def initialize(node, *args)
11
+ super *args
12
+ @node = node
13
+ end
14
+
15
+ def message
16
+ msg = super
17
+ "#{msg}: #{node.location.to_s}"
18
+ end
19
+ end
20
+
21
+ class Extractor
22
+ attr_reader :path
23
+
24
+ def initialize path
25
+ @path = path
26
+ @buffer = Parser::Source::Buffer.new(@path)
27
+ @buffer.source = File.read(@path)
28
+ @parser = BetterHtml::Parser.new(@buffer)
29
+ end
30
+
31
+ def extract
32
+ children = @parser.ast
33
+
34
+ HTMLIterator.descendants(children).reject { |node|
35
+ node.children.reject{|child| !child.is_a?(String) }.map(&:strip).join('').empty?
36
+ }.to_a
37
+ end
38
+
39
+ def validate!
40
+ res = extract
41
+ res.each do |node|
42
+ raise ExtractError.new(node, "Found hard coded string") unless node.children.join('').blank?
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,53 @@
1
+ require 'ast'
2
+
3
+ module I18nExtract
4
+ class HTMLIterator
5
+ def initialize(&block)
6
+ @block = block
7
+ end
8
+
9
+ def traverse(node)
10
+ return unless node.is_a?(::AST::Node)
11
+ case node.type
12
+ when :tag
13
+ tag_name, children = *node.to_a
14
+ return if %w(style script).include? tag_name.to_s
15
+ when :text
16
+ @block.call(node)
17
+ end
18
+ traverse_all(node)
19
+ end
20
+
21
+ def traverse_all(nodes)
22
+ clean(nodes).each do |node|
23
+ traverse(node) if node.is_a?(::AST::Node)
24
+ end
25
+ end
26
+
27
+ def clean(nodes)
28
+ skip = 0
29
+ nodes.to_a.reject do |node|
30
+ next true unless node.is_a?(::AST::Node)
31
+ if node&.type == :tag
32
+ solidus, tag_name = *node
33
+ if %w(style script).include? tag_name.to_a.first
34
+ if solidus.nil?
35
+ skip = skip + 1
36
+ else
37
+ skip = skip - 1
38
+ end
39
+ next true
40
+ end
41
+ end
42
+ skip != 0
43
+ end
44
+ end
45
+
46
+ def self.descendants(root_node)
47
+ Enumerator.new do |yielder|
48
+ t = new() { |node| yielder << node }
49
+ t.traverse(root_node)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ require 'i18n_extract'
2
+ require 'rails'
3
+
4
+ module I18nExtract
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :i18n_extract
7
+
8
+ rake_tasks do
9
+ path = File.expand_path(__dir__)
10
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ require 'colorize'
2
+
3
+ namespace :'i18n-extract' do
4
+ desc "Check views for hard-coded strings"
5
+ task :check => :environment do
6
+ root = Rails.root.join('app','views')
7
+ Dir.glob("#{root}/**/*.html.erb").each do |file|
8
+ extractor = I18nExtract::Extractor.new(file)
9
+ extractor.extract.each do |r|
10
+ puts r.location.to_s.colorize(:red)
11
+ loc = r.location
12
+ if loc.stop_line - loc.start_line > 0
13
+ lines = loc.source_buffer.source_lines
14
+ start_line = loc.start_line
15
+ stop_line = loc.stop_line
16
+ print lines[start_line - 1][0..(loc.start_column - 1)].colorize(:gray)
17
+ puts lines[start_line - 1][loc.start_column..-1].colorize(:red)
18
+ lines[start_line...(stop_line - 1)].each do |line|
19
+ puts line.colorize(:red)
20
+ end
21
+ print lines[stop_line - 1][0..(loc.stop_column - 1)].colorize(:red)
22
+ puts lines[stop_line - 1][(loc.stop_column)..-1].colorize(:gray)
23
+ else
24
+ line = loc.source_line
25
+ print line[0..(loc.start_column - 1)].colorize(:gray)
26
+ print line[loc.start_column..(loc.stop_column - 1)].colorize(:red)
27
+ print line[(loc.stop_column)..-1].colorize(:gray)
28
+ puts
29
+ puts "#{" " * loc.start_column}#{"^" * (loc.stop_column - loc.start_column)}"
30
+ end
31
+ puts
32
+ end
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ module I18nExtract
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "i18n_extract/version"
2
+ require "i18n_extract/extractor"
3
+
4
+ module I18nExtract
5
+ require 'i18n_extract/railtie' if defined?(Rails)
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_extract
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcel Müller
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: better_html
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5'
97
+ description:
98
+ email:
99
+ - neikos@neikos.email
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - i18n_extract.gemspec
115
+ - lib/i18n_extract.rb
116
+ - lib/i18n_extract/extractor.rb
117
+ - lib/i18n_extract/html_iterator.rb
118
+ - lib/i18n_extract/railtie.rb
119
+ - lib/i18n_extract/tasks/check.rake
120
+ - lib/i18n_extract/version.rb
121
+ homepage: https://github.com/TheNeikos/i18n_extract
122
+ licenses:
123
+ - MIT
124
+ metadata:
125
+ allowed_push_host: https://rubygems.org
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.7.7
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Extract hardcoded strings from your ERB views.
146
+ test_files: []