apstrings 0.1.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
+ SHA1:
3
+ metadata.gz: b254fe96c0c7a6649d63b91f3149576422524c70
4
+ data.tar.gz: e80f20ebe76cffd2c692cf95aca6540bf4f1f156
5
+ SHA512:
6
+ metadata.gz: cd106d42a84875e270ca91763c46e7a34a5cb8210de8a7c0cfe46f4aafd93c4c8e887c143a1c48614136e69be2238895e31caf4acf551ac71203e6490382782c
7
+ data.tar.gz: 95acbc76b7b0bdd8a213d3aeba65d197265c78315786c809da0a67acb916232641a8c366a82c7527b38e2ea2e279594eb83af9fb6b4ef1c9676ec6ea0a4756a1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apstrings.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 卡迩
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,41 @@
1
+ # Apstrings
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/apstrings`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'apstrings'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install apstrings
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake true` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/apstrings. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/apstrings.gemspec ADDED
@@ -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 'apstrings/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apstrings"
8
+ spec.version = Apstrings::VERSION
9
+ spec.authors = ["JasonWorking"]
10
+ spec.email = ["331314708@qq.com"]
11
+
12
+ spec.summary = %q{Apple dot strings file parser.}
13
+ spec.description = %q{An easy to use Apple dot strings file parser with encoding handled.}
14
+ spec.homepage = "https://github.com/JasonWorking"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "true"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "apstrings"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,65 @@
1
+ module Apstrings
2
+ require 'json'
3
+ class DotStringFile
4
+ attr_accessor :kv_pairs
5
+
6
+ def initialize
7
+ @kv_pairs = []
8
+ end
9
+
10
+ def key_values
11
+ kv_pairs.map do |pair|
12
+ {pair.key => pair.value}
13
+ end
14
+ end
15
+
16
+ def keys
17
+ kv_pairs.map do |pair|
18
+ pair.key
19
+ end
20
+ end
21
+
22
+ def values
23
+ kv_pairs.map do |pair|
24
+ pair.value
25
+ end
26
+ end
27
+
28
+ def comments(args={})
29
+ with_keys = args[:with_keys].nil? ? true : args[:with_keys]
30
+ cleaned_pairs = kv_pairs.map do |pair|
31
+ pair
32
+ end
33
+ with_keys ? build_comment_hash(cleaned_pairs) : cleaned_pairs.map(&:comment)
34
+ end
35
+
36
+ def to_hash(args={})
37
+ with_comments = args[:with_comments].nil? ? true : args[:with_comments]
38
+
39
+ build_hash { |hash, pair|
40
+ hash_value = with_comments ? { pair.value => pair.comment } : pair.value
41
+ hash[pair.key] = hash_value
42
+ }
43
+ end
44
+
45
+ def to_json(args={})
46
+ self.to_hash(with_comments: args[:with_comments]).to_json
47
+ end
48
+
49
+ private
50
+
51
+ def build_comment_hash(kv_pairs)
52
+ build_hash { |hash, pair|
53
+ hash[pair.key] = pair.comment
54
+ }
55
+ end
56
+
57
+ def build_hash(&block)
58
+ hash = {}
59
+ kv_pairs.each do |pair|
60
+ yield hash, pair
61
+ end
62
+ hash
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,29 @@
1
+ module Apstrings
2
+ class KVPair
3
+ attr_reader :line, :raw_comment
4
+
5
+ def initialize(line, comment)
6
+ @line = line
7
+ @raw_comment = comment
8
+ end
9
+
10
+ def key
11
+ line.key.strip unless line.key.nil?
12
+ end
13
+
14
+ def value
15
+ line.value.strip unless line.key.nil?
16
+ end
17
+
18
+ def comment
19
+ if raw_comment.nil?
20
+ @raw_comment = ""
21
+ else
22
+ raw_comment.gsub!(/(\/\*)|(\*\/)/,"")
23
+ raw_comment.gsub!("\n", " ")
24
+ raw_comment.gsub!(/\s+/, " ")
25
+ raw_comment.strip
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,72 @@
1
+ module Apstrings
2
+ class Line
3
+ attr_reader :content
4
+ attr_accessor :in_comment
5
+
6
+ def initialize(line)
7
+ @content = line
8
+ @in_comment = false
9
+ raise "Line does not end in ;" unless valid?
10
+ end
11
+
12
+ def empty_line?
13
+ /^\s*$/.match(content) || false
14
+ end
15
+
16
+ def whole_comment
17
+ /((^\/\*(.+)\*\/)|(^\/\/(.+)))/.match(content).to_s
18
+ end
19
+
20
+ def whole_comment?
21
+ !(whole_comment.empty?)
22
+ end
23
+
24
+ def open_comment
25
+ /^\/\*(.+)$/.match(content).to_s
26
+ end
27
+
28
+ def open_comment?
29
+ !(open_comment.empty?)
30
+ end
31
+
32
+ def close_comment
33
+ /^(.+)\*\/\s*$/.match(content).to_s
34
+ end
35
+
36
+ def close_comment?
37
+ !(close_comment.empty?)
38
+ end
39
+
40
+ def key_value_pair?
41
+ !!(/^\s*"([^"]+)"\s*=/.match(content))
42
+ end
43
+
44
+ def cleaned_content
45
+ content.gsub(/;\s*$/, "")
46
+ end
47
+
48
+ def valid?
49
+ if key_value_pair?
50
+ !!(/;[\s]*$/.match(content))
51
+ else
52
+ true
53
+ end
54
+ end
55
+
56
+ def key
57
+ if key_value_pair?
58
+ cleaned_content.partition(/"\s*=\s*"/)[0].gsub!(/(^"|"$)/, "")
59
+ end
60
+ end
61
+
62
+ def value
63
+ if key_value_pair?
64
+ cleaned_content.partition(/"\s*=\s*"/)[2].gsub!(/(^"|"$)/, "")
65
+ end
66
+ end
67
+
68
+ def is_comment?
69
+ whole_comment? || open_comment? || close_comment? || in_comment
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,35 @@
1
+ module Apstrings
2
+ require 'FileUtils'
3
+ # Class for reading in files and returning an array of its content
4
+ class Reader
5
+ # Reads in a file and returns an array consisting of each line of input
6
+ # cleaned of new line characters
7
+ def self.read(file)
8
+ File.open(file, 'r') do |f|
9
+ str = f.read
10
+ if str.bytesize >= 3 and str.getbyte(0) == 0xFF and str.getbyte(1) == 0xFE
11
+ #UTF-16lE
12
+ encoder = Encoding::Converter.new("UTF-16lE", "UTF-8")
13
+ str = encoder.convert(str)
14
+ str = str.byteslice(3, str.bytesize - 3) # 去掉FFFE
15
+ elsif str.bytesize >= 3 and str.getbyte(0) == 0xFE and str.getbyte(1) == 0xFF
16
+ #UTF-16bE
17
+ encoder = Encoding::Converter.new("UTF-16bE", "UTF-8")
18
+ str = encoder.convert(str)
19
+ str = str.byteslice(3, str.bytesize - 3) # 去掉FEFF
20
+ elsif str.bytesize >= 4 and str.getbyte(0) == 0xEF and str.getbyte(1) == 0xBB and str.getbyte(2) == 0xBF
21
+ #UTF8
22
+ str = str.byteslice(4, str.bytesize - 4) # 去掉EFBBBF
23
+ else
24
+ #ASCII
25
+ str = str.force_encoding('UTF-8')
26
+ end
27
+ content = str
28
+ content.each_line.inject([]) do |content_array, line|
29
+ line.gsub!("\n","")
30
+ content_array.push(line)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,55 @@
1
+ module Apstrings
2
+ require 'apstrings/line'
3
+ require 'apstrings/kv_pair'
4
+ require 'apstrings/dot_string_file'
5
+ class StringsParser
6
+ KEY = "KEY"
7
+ COMMENT = "COMMENT"
8
+
9
+ def initialize(read_file, parsed_file = DotStringFile.new)
10
+ @read_file = read_file
11
+ @parsed_file = parsed_file
12
+ end
13
+
14
+ def parse_file
15
+ state = KEY
16
+ current_comment = nil
17
+ comments_for_keys = {}
18
+ @read_file.each do |content_line|
19
+ current_line = Line.new(content_line)
20
+ next if current_line.empty_line? && current_line.in_comment == false
21
+
22
+ #State machine
23
+ case state
24
+ when KEY
25
+ if current_line.whole_comment?
26
+ unless current_line.whole_comment.strip == 'No comment provided by engineer.'
27
+ current_comment = current_line.whole_comment
28
+ end
29
+ elsif current_line.key_value_pair? && current_comment
30
+ comments_for_keys[current_line.key] = current_comment
31
+ current_comment = nil
32
+ elsif current_line.open_comment?
33
+ current_comment = current_line.open_comment + "\n"
34
+ state = COMMENT
35
+ end
36
+ when COMMENT
37
+ if current_line.close_comment?
38
+ current_comment += current_line.close_comment
39
+ state = KEY
40
+ else
41
+ current_line.in_comment = true
42
+ current_comment = current_comment + current_line.content + "\n"
43
+ end
44
+ end
45
+
46
+ unless current_line.is_comment?
47
+ @parsed_file.kv_pairs << KVPair.new(current_line, comments_for_keys[current_line.key])
48
+ end
49
+ end
50
+
51
+ raise "Invalid .string file: Unterminated comment" unless state == KEY
52
+ @parsed_file
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Apstrings
2
+ VERSION = "0.1.0"
3
+ end
data/lib/apstrings.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "apstrings/version"
2
+
3
+ module Apstrings
4
+ require 'apstrings/reader'
5
+ require 'apstrings/strings_parser'
6
+ # Public
7
+ def self.parse(file)
8
+ file = read(file)
9
+ StringsParser.new(file).parse_file
10
+ end
11
+
12
+ def self.read(file)
13
+ Reader.read(file)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apstrings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - JasonWorking
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-26 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: 'true'
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: An easy to use Apple dot strings file parser with encoding handled.
56
+ email:
57
+ - 331314708@qq.com
58
+ executables:
59
+ - console
60
+ - setup
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - .travis.yml
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - apstrings.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/apstrings.rb
75
+ - lib/apstrings/dot_string_file.rb
76
+ - lib/apstrings/kv_pair.rb
77
+ - lib/apstrings/line.rb
78
+ - lib/apstrings/reader.rb
79
+ - lib/apstrings/strings_parser.rb
80
+ - lib/apstrings/version.rb
81
+ homepage: https://github.com/JasonWorking
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.14
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Apple dot strings file parser.
105
+ test_files: []