Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
vendor
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp

# JetBrains
.idea

# mac
.DS_Store
__MACOSX

# emacs turds
(.*/)?\#[^/]*\#$

34 changes: 0 additions & 34 deletions Gemfile.lock

This file was deleted.

4 changes: 3 additions & 1 deletion lib/apfel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module Apfel
# strings object
def self.parse(file)
file = read(file)
# confirmed that read does remove the first comment in utf
DotStringsParser.new(file).parse_file
end

def self.read(file)
Reader.read(file)
# confirmed that read does remove the first comment in utf
Reader.read(file)
end
end
37 changes: 36 additions & 1 deletion lib/apfel/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,47 @@ def key

def value
if key_value_pair?
cleaned_content.partition(/"\s*=\s*"/)[2].gsub!(/(^"|"$)/, "")
unescape_value cleaned_content.partition(/"\s*=\s*"/)[2].gsub!(/(^"|"$)/, "")
end
end

def is_comment?
whole_comment? || open_comment? || close_comment? || in_comment
end

private

# http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LoadingResources/Strings/Strings.html
def unescape_value(string)
state = :normal
out = ''
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the name "out". How about "output"?

string.each_char do |c|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think that "char" is better than "c"

case state
when :normal
if c == '\\'
state = :escape
else
out += c
end
when :escape
state = :normal
case c
when '\\'
out += '\\'
when '"'
out += '"'
when 'r'
out += "\r"
when 'n'
out += "\n"
when 't'
out += "\t"
else
out += '\\' + c # Do nothing, however in the future handling unicode escapes could be good
end
end
end
out
end
end
end
9 changes: 6 additions & 3 deletions lib/apfel/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ class Reader
# Reads in a file and returns an array consisting of each line of input
# cleaned of new line characters
def self.read(file)
File.open(file, "r") do |f|
content_array=[]
content = f.read
File.open(file, 'r') do |f|
content_array=[]
# http://stackoverflow.com/questions/5011504/is-there-a-way-to-remove-the-bom-from-a-utf-8-encoded-file
# problem is the BOM that can be found at char 0 in strings files
content = f.read.force_encoding('UTF-8')
content.sub!("\xEF\xBB\xBF".force_encoding("UTF-8"), '')
content.each_line do |line|
line.gsub!("\n","")
content_array.push(line)
Expand Down
32 changes: 32 additions & 0 deletions spec/apfel_escapes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require 'apfel'

describe Apfel do
describe '::parse_file' do
context 'when given DotStrings file with escapes'do
let(:parsed_file_hash) do
Apfel.parse('./spec/files/escapes.strings').to_hash(:with_comments => false)
end

it 'should parse nl' do
parsed_file_hash['multiline'].should eq "line 1\nline 2"
end

it 'should parse cr' do
parsed_file_hash['mac'].should eq "before cr\rafter cr"
end

it 'should parse tabs' do
parsed_file_hash['tabs'].should eq "two spaces \t equals tab"
end

it 'should parse double quotes' do
parsed_file_hash['dq'].should eq "\"someone said this\""
end

it 'should parse backslashes' do
parsed_file_hash['backslash'].should eq "\\not a forward slash"
end
end
end
end
50 changes: 50 additions & 0 deletions spec/apfel_parse_ascii_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'
require 'apfel'
require 'apfel/parsed_dot_strings'

describe Apfel do
describe '::parse_file' do
context 'when given a ASCII DotStrings file'do

it 'the file should be ascii' do
res = `file -I ./spec/files/ascii.strings`
encoding = res.split(/=/).last.gsub!("\n",'')
encoding.should == 'us-ascii'
end

let(:parsed_file) do
Apfel.parse('./spec/files/ascii.strings')
end

it 'returns a ParsedDotStrings object' do
parsed_file.should be_a(Apfel::ParsedDotStrings)
end

#it 'should have the correct keys' do
# parsed_file.keys.should include 'key_number_one'
# parsed_file.keys.should include 'key_number_two'
# parsed_file.keys.should include 'key_number_three'
#end
#
#it 'should have the correct values' do
# parsed_file.values.should include 'value number one'
# parsed_file.values.should include 'value number two'
# parsed_file.values.should include 'value number three'
#end
#
describe 'should have the correct comments' do
it 'should have the correct comment for avoided social event' do
parsed_file.comments['avoided social event'].should == 'No comment provided by engineer.'
end

it 'should have the correct comment for binged' do
parsed_file.comments['binged'].should == 'No comment provided by engineer.'
end

it 'should have the correct comment for called a friend' do
parsed_file.comments['called a friend'].should == 'No comment provided by engineer.'
end
end
end
end
end
53 changes: 53 additions & 0 deletions spec/apfel_parse_utf8_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'spec_helper'
require 'apfel'
require 'apfel/parsed_dot_strings'

describe Apfel do
describe '::parse_file' do
context 'when given a UTF8 DotStrings file'do

it 'the file should be utf-8' do
File.open('./spec/files/utf8.strings', 'r') do |f|
f.external_encoding.name.should == 'UTF-8'
content = f.read
content.encoding.name.should == 'UTF-8'
end
end

let(:parsed_file) do
Apfel.parse('./spec/files/utf8.strings')
end

it 'returns a ParsedDotStrings object' do
parsed_file.should be_a(Apfel::ParsedDotStrings)
end

#it 'should have the correct keys' do
# parsed_file.keys.should include 'key_number_one'
# parsed_file.keys.should include 'key_number_two'
# parsed_file.keys.should include 'key_number_three'
#end
#
#it 'should have the correct values' do
# parsed_file.values.should include 'value number one'
# parsed_file.values.should include 'value number two'
# parsed_file.values.should include 'value number three'
#end
#
describe 'should have the correct comments' do
it 'should have the correct comment for anger' do
parsed_file.comments['anger'].should == 'No comment provided by engineer.'
end

it 'should have the correct comment for anxiety' do
parsed_file.comments['anxiety'].should == 'No comment provided by engineer.'
end

it 'should have the correct comment for boredom' do
parsed_file.comments['boredom'].should == 'No comment provided by engineer.'
end
end

end
end
end
35 changes: 31 additions & 4 deletions spec/apfel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe Apfel do
describe '::parse_file' do
context 'when given a valid DotStrings file'do
context 'when given an ASCII DotStrings file'do

let(:parsed_file) do
Apfel.parse(valid_file)
Expand All @@ -13,16 +13,43 @@
it 'returns a ParsedDotStrings object' do
parsed_file.should be_a(Apfel::ParsedDotStrings)
end

it 'should have the correct keys' do
parsed_file.keys.should include 'key_number_one'
parsed_file.keys.should include 'key_number_two'
parsed_file.keys.should include 'key_number_three'
end

it 'should have the correct values' do
parsed_file.values.should include 'value number one'
parsed_file.values.should include 'value number two'
parsed_file.values.should include 'value number three'
end

describe 'should have the correct comments' do
it 'should have the correct comment for first' do
parsed_file.comments(with_keys: false).should include 'This is the first comment'
end

it 'should have the correct comment for second' do
parsed_file.comments['key_number_two'].should == 'This is a multiline comment'
end


it 'should have the correct comment for third' do
parsed_file.comments(with_keys: false).should include 'This is comment number 3'
end
end
end

context 'when given an invalid strings file' do
context 'missing a semicolon' do

let(:invalid_file_semicolon) do
create_temp_file( <<-EOS
create_temp_file('ascii', <<-EOS
/* This is the first comment */
"key_number_one" = "value number one"
EOS
EOS
)
end

Expand All @@ -35,7 +62,7 @@

context 'not closed comment' do
let(:invalid_file_comment) do
create_temp_file(<<-EOS
create_temp_file('ascii', <<-EOS
/* This is the first comment
"key_number_one" = "value number one";

Expand Down
9 changes: 9 additions & 0 deletions spec/files/ascii.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* No comment provided by engineer. */
"avoided social event" = "avoided social event";

/* No comment provided by engineer. */
"binged" = "binged";

/* No comment provided by engineer. */
"called a friend" = "called a friend";

5 changes: 5 additions & 0 deletions spec/files/escapes.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"multiline" = "line 1\nline 2";
"mac" = "before cr\rafter cr";
"tabs" = "two spaces \t equals tab";
"dq" = "\"someone said this\"";
"backslash" = "\\not a forward slash";
8 changes: 8 additions & 0 deletions spec/files/utf8.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* No comment provided by engineer. */
"anger" = "anger";

/* No comment provided by engineer. */
"anxiety" = "anxiety";

/* No comment provided by engineer. */
"boredom" = "boredom";
2 changes: 1 addition & 1 deletion spec/reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Apfel
describe Reader do
describe '#read' do
let(:temp_file) do
create_temp_file(<<-EOS
create_temp_file('ascii', <<-EOS
This is a file with some lines.
Roses are red, violets are blue.
This text is really boring,
Expand Down
Loading