iphone_parser 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 720614ec125a2d4dcc200607a272dbc0c74c2239
4
+ data.tar.gz: 6d552ed1e57811e0d694fcb43ab7feb2303b1eb5
5
+ SHA512:
6
+ metadata.gz: 7b2b431a92b4ed8d5dd43cbe823b386f83f37c9c26f500cad9f9576b2c8968c27810b02f819218b72c6b10b9eeb09153a9e9d06af2bd18fa2561a06d7dd50638
7
+ data.tar.gz: a5e1fa2e1da38dd879aa8645d4efa0d7527f6fc14d42e846280c1b16cb52e34b271fcb1985f6ac60595dfd563e9b16524e945245d044d3f2ed1fef6aa6cc269a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in iphone_parser.gemspec
4
+ gemspec
5
+
6
+ gem 'treetop'
7
+ gem 'rspec'
8
+ gem 'byebug'
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # IphoneParser
2
+
3
+ Parser for iphone resource files - also can create those files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'iphone_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install iphone_parser
20
+
21
+ ## Usage
22
+
23
+ Check the spec folder for lots of examples. A real usage would look like this:
24
+
25
+ require 'iphone_parser'
26
+ strings = IphoneParser.parse(File.open("Localizable.strings").read)
27
+ strings['one label']
28
+ => { text: 'the text for this label', comment: all comments on top of this string }
29
+
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/fotanus/iphone-parser/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
Binary file
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iphone_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "iphone_parser"
8
+ spec.version = IphoneParser::VERSION
9
+ spec.authors = ["fotanus@gmail.com"]
10
+ spec.email = ["Felipe Tanus"]
11
+ spec.summary = %q{Parser for iphone resource files}
12
+ spec.description = %q{Parse iphone resource files to extract strings, and create a file with the given strings}
13
+ spec.homepage = "http://fotanus.com"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ spec.add_dependency "treetop", "~> 1.5"
24
+ end
@@ -0,0 +1,51 @@
1
+ require "treetop"
2
+ require "iphone_parser/version"
3
+ require 'iphone_parser/ast_nodes'
4
+ require 'iphone_parser/grammar'
5
+ require 'iphone_parser/exceptions'
6
+
7
+ module IphoneParser
8
+ def self.parse(file_content)
9
+ @@parser ||= IphoneResourceFileParser.new
10
+ ast = @@parser.parse(file_content)
11
+ if ast
12
+ generate_hash(ast)
13
+ else
14
+ @@parser.failure_reason =~ /^(Expected .+) after/m
15
+ error = "#{$1.gsub("\n", '$NEWLINE')}:" + "\n"
16
+ error += file_content.lines.to_a[@@parser.failure_line - 1]
17
+ error += "#{'~' * (@@parser.failure_column - 1)}^"
18
+ raise ParseError, error
19
+ end
20
+ end
21
+
22
+ def self.create_resource_file(entries)
23
+ raise InvalidEntry unless entries.kind_of? Hash
24
+
25
+ entries.reduce("") do |out, entry|
26
+ label = entry[0]
27
+ text_and_comment = entry[1]
28
+ raise InvalidEntry unless text_and_comment.kind_of?(Hash)
29
+ text = text_and_comment[:text]
30
+
31
+ raise InvalidEntry if !label.kind_of?(String) || !text.kind_of?(String)
32
+
33
+ out += "\"#{label}\"=\"#{text_and_comment[:text]}\";"
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def self.generate_hash(ast)
40
+ ast.elements.delete_if { |node| !node.kind_of? Entry }
41
+
42
+ hash_constructor = ast.elements.map do |entry|
43
+ label = entry.label.text_value[1..-2]
44
+ text = entry.text.text_value[1..-2]
45
+ comments = entry.comments.text_value
46
+ [ label, {text: text, comments: comments} ]
47
+ end
48
+
49
+ Hash[hash_constructor]
50
+ end
51
+ end
@@ -0,0 +1,6 @@
1
+ module IphoneParser
2
+ class Entry < Treetop::Runtime::SyntaxNode ; end
3
+ class Comment < Treetop::Runtime::SyntaxNode ; end
4
+ class Label < Treetop::Runtime::SyntaxNode ; end
5
+ class Text < Treetop::Runtime::SyntaxNode ; end
6
+ end
@@ -0,0 +1,8 @@
1
+ module IphoneParser
2
+ class ParseError < StandardError ; end
3
+ class InvalidEntry < StandardError
4
+ def to_s
5
+ "Should provide a hash like: {'label' => {text: 'text'}}"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,46 @@
1
+ #grammar.treetop
2
+ grammar IphoneResourceFile
3
+ rule entries
4
+ entry+
5
+ end
6
+
7
+ rule entry
8
+ blank comments blank label blank equal blank text blank semicolon blank <IphoneParser::Entry>
9
+ end
10
+
11
+ rule comments
12
+ ( blank comment blank )* <IphoneParser::Comment>
13
+ end
14
+
15
+ rule comment
16
+ single_line_comment / multi_line_comment
17
+ end
18
+
19
+ rule single_line_comment
20
+ '//' ( ![\n] . )* [\n]
21
+ end
22
+
23
+ rule multi_line_comment
24
+ '/*' ( !'*/' . )* '*/'
25
+ end
26
+
27
+ rule label
28
+ '"' ( '\"' / !'"' . )* '"' <IphoneParser::Label>
29
+ end
30
+
31
+ rule equal
32
+ '='
33
+ end
34
+
35
+ rule text
36
+ '"' ( '\"' / !'"' . )* '"' <IphoneParser::Text>
37
+ end
38
+
39
+ rule semicolon
40
+ ';' / ''
41
+ end
42
+
43
+ rule blank
44
+ [ \t\n]*
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module IphoneParser
2
+ def self.parse
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module IphoneParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe IphoneParser do
4
+ describe "IphoneParser.parse" do
5
+ it "parse a simple entry" do
6
+ resource_file = '"label"="text";'
7
+ strings = IphoneParser.parse(resource_file)
8
+ expect(strings.count).to eq(1)
9
+ expect(strings['label'][:text]).to eq('text')
10
+
11
+ end
12
+
13
+ it "parse single line comments" do
14
+ resource_file = <<-eof
15
+ // my comment
16
+ "label" = "text";
17
+ eof
18
+
19
+ strings = IphoneParser.parse(resource_file)
20
+ expect(strings.count).to eq(1)
21
+ expect(strings['label'][:text]).to eq('text')
22
+ expect(strings['label'][:comments]).to match(/\/\/ my comment\n/)
23
+ end
24
+
25
+ it "parse multi line comments" do
26
+ resource_file = <<-eof
27
+ /* my comment */
28
+ "label" = "text";
29
+ eof
30
+ strings = IphoneParser.parse(resource_file)
31
+ expect(strings.count).to eq(1)
32
+ expect(strings['label'][:comments]).to match(/\/\* my comment \*\//)
33
+ expect(strings['label'][:text]).to eq("text")
34
+ end
35
+
36
+ it "parse multiple strings" do
37
+ resource_file = <<-eof
38
+ // comment 1
39
+ "label 1" = "text 1";
40
+ /* comment 2 */
41
+ "label 2" = "text 2";
42
+ eof
43
+ strings = IphoneParser.parse(resource_file)
44
+ expect(strings.count).to eq(2)
45
+ expect(strings['label 1'][:comments]).to match(/\/\/ comment 1\n/)
46
+ expect(strings['label 1'][:text]).to eq("text 1")
47
+ expect(strings['label 2'][:comments]).to match(/\/\* comment 2 \*\//)
48
+ expect(strings['label 2'][:text]).to eq("text 2")
49
+ end
50
+
51
+ it "semicolon is optional" do
52
+ resource_file = <<-eof
53
+ // comment 1
54
+ "label 1" = "text 1"
55
+ /* comment 2 */
56
+ "label 2" = "text 2"
57
+ eof
58
+ strings = IphoneParser.parse(resource_file)
59
+ expect(strings.count).to eq(2)
60
+ expect(strings['label 1'][:comments]).to match(/\/\/ comment 1\n/)
61
+ expect(strings['label 1'][:text]).to eq("text 1")
62
+ expect(strings['label 2'][:comments]).to match(/\/\* comment 2 \*\//)
63
+ expect(strings['label 2'][:text]).to eq("text 2")
64
+ end
65
+
66
+ it "accept escaped quotes" do
67
+ resource_file = '"\"label\"1\"" = "\"text\"1\"";'
68
+ strings = IphoneParser.parse(resource_file)
69
+ expect(strings.count).to eq(1)
70
+ expect(strings['\"label\"1\"'][:text]).to eq('\"text\"1\"')
71
+ end
72
+
73
+ it "parses two comments for same string" do
74
+ resource_file = <<-eof
75
+ /* my comment */
76
+ // other comment
77
+ "label1" = "text1";
78
+ /* second comment */
79
+ "label2" = "text2";
80
+ eof
81
+ strings = IphoneParser.parse(resource_file)
82
+ expect(strings.count).to eq(2)
83
+ expect(strings['label1'][:comments]).to match(/\/\* my comment \*\/.*\/\/ other comment/m)
84
+ expect(strings['label1'][:text]).to eq('text1')
85
+ expect(strings['label2'][:comments]).to match(/\/\* second comment \*\//)
86
+ expect(strings['label2'][:text]).to eq('text2')
87
+ end
88
+
89
+ it "raises for invalid format" do
90
+ resource_file = '"invalid""="format";'
91
+ expect {
92
+ strings = IphoneParser.parse(resource_file)
93
+ }.to raise_error(IphoneParser::ParseError)
94
+ end
95
+ end
96
+
97
+ describe "IphoneParser.create_resource_file" do
98
+ it "creates simple file" do
99
+ strings = {'label' => { text: 'text'} }
100
+ expected_output = '"label"="text";'
101
+ expect(IphoneParser.create_resource_file(strings)).to eq(expected_output)
102
+ end
103
+
104
+ it "ignore comments" do
105
+ strings = { 'label' => {comments: 'something', text: 'text'} }
106
+ expected_output = '"label"="text";'
107
+ expect(IphoneParser.create_resource_file(strings)).to eq(expected_output)
108
+ end
109
+
110
+ it 'raises invalid entry if missing text' do
111
+ strings = { 'label' => {} }
112
+ expect {
113
+ IphoneParser.create_resource_file(strings)
114
+ }.to raise_error(IphoneParser::InvalidEntry)
115
+ end
116
+
117
+ it 'raises invalid entry for random objects as label' do
118
+ strings = {'l' => { text: 't'}, Object.new => { text: 't' } }
119
+ expect {
120
+ IphoneParser.create_resource_file(strings)
121
+ }.to raise_error(IphoneParser::InvalidEntry)
122
+ end
123
+
124
+ it 'raises invalid entry for random objects as text' do
125
+ strings = {'l' => { text: 't'}, 'l2' => { text: Object.new } }
126
+ expect {
127
+ IphoneParser.create_resource_file(strings)
128
+ }.to raise_error(IphoneParser::InvalidEntry)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,92 @@
1
+ require 'iphone_parser'
2
+ require 'iphone_parser/exceptions'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ =begin
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
55
+ # For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iphone_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fotanus@gmail.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: treetop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ description: Parse iphone resource files to extract strings, and create a file with
70
+ the given strings
71
+ email:
72
+ - Felipe Tanus
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-version"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - builds/iphone_parser-0.1.0.gem
84
+ - iphone_parser.gemspec
85
+ - lib/iphone_parser.rb
86
+ - lib/iphone_parser/ast_nodes.rb
87
+ - lib/iphone_parser/exceptions.rb
88
+ - lib/iphone_parser/grammar.treetop
89
+ - lib/iphone_parser/parse.rb
90
+ - lib/iphone_parser/version.rb
91
+ - spec/lib/iphone_parser_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: http://fotanus.com
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Parser for iphone resource files
116
+ test_files:
117
+ - spec/lib/iphone_parser_spec.rb
118
+ - spec/spec_helper.rb