Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented having comments in CSV #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
22 changes: 20 additions & 2 deletions lib/babelish/android2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,40 @@ def initialize(args = {:filenames => []})
super(args)
end

def parse_comment_line(line)
line.strip!
if line[0] != ?# && line[0] != ?=
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not use the character literal - use string literal instead.

m = line.match(/^\/\*(.*)\*\/\s*$/)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use %r around regular expression.

Copy link
Author

Choose a reason for hiding this comment

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

@netbe This function I copied from strings2csv.rb, so unsure if I should still change this according to houndci-bot.

Copy link
Owner

Choose a reason for hiding this comment

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

@Dchau95 yes please make any change @houndci-bot mention and add tests to check comments on android

return m[1].strip! unless m.nil?
end
end

def load_strings(strings_filename)
strings = {}
comments = {}
xml_file = File.open(strings_filename)

parser = Nokogiri::XML(xml_file) do |config|
config.strict.noent
end
parser.xpath("//string").each do |node|

previous_comment = nil
parser.xpath("//resources").children.each do |node|
if !node.nil? && !node["name"].nil?
strings.merge!(node["name"] => node.inner_html)
comments[node["name"]] = previous_comment if previous_comment
previous_comment = nil
end
if node.comment?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use next to skip iteration.

comment = node.content
parsed_comment = parse_comment_line(comment)
previous_comment = parsed_comment || comment
end
end

xml_file.close

[strings, {}]
[strings, comments]
end

end
Expand Down
19 changes: 19 additions & 0 deletions test/babelish/test_android2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ def test_initialize
assert_equal filenames, converter.filenames
end

def test_load_strings_with_comments
expected_output = [
{
"app_name" => "android2csv",
"action_greetings" => "Hello",
"ANOTHER_STRING" => "testEN",
"empty" => ""
},
{
"app_name" => "This is the app name",
"action_greetings" => "This is a greeting",
"ANOTHER_STRING" => "This is another string",
"empty" => "This is an empty string"
}
]
output = Babelish::Android2CSV.new.load_strings "test/data/android-comments.xml"
assert_equal expected_output, output
end

def test_initialize_with_default_values
converter = Babelish::Android2CSV.new
assert_not_nil converter.csv_filename
Expand Down
13 changes: 13 additions & 0 deletions test/data/android-comments.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- This is the app name -->
<string name="app_name">android2csv</string>
<!-- This is a greeting -->
<string name="action_greetings">Hello</string>
<!-- This is another string -->
<string name="ANOTHER_STRING">testEN</string>
<!-- This is an empty string -->
<string name="empty"></string>

</resources>