From 0ce47d94dabadf7fc23ea9a7d6bb8a4a14578499 Mon Sep 17 00:00:00 2001 From: David Chau Date: Wed, 9 Aug 2017 12:08:32 -0700 Subject: [PATCH 1/3] Implemented having comments in CSV --- lib/babelish/android2csv.rb | 22 ++++++++++++++++++++-- test/babelish/test_android2csv.rb | 8 ++++++++ test/data/android-comments.xml | 13 +++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/data/android-comments.xml diff --git a/lib/babelish/android2csv.rb b/lib/babelish/android2csv.rb index b6e6ffd..c5cf8aa 100644 --- a/lib/babelish/android2csv.rb +++ b/lib/babelish/android2csv.rb @@ -6,22 +6,40 @@ def initialize(args = {:filenames => []}) super(args) end + def parse_comment_line(line) + line.strip! + if line[0] != ?# && line[0] != ?= + m = line.match(/^\/\*(.*)\*\/\s*$/) + 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? + comment = node.content + parsed_comment = parse_comment_line(comment) + previous_comment = parsed_comment || comment end end xml_file.close - [strings, {}] + [strings, comments] end end diff --git a/test/babelish/test_android2csv.rb b/test/babelish/test_android2csv.rb index d38a43e..48c8eaf 100644 --- a/test/babelish/test_android2csv.rb +++ b/test/babelish/test_android2csv.rb @@ -27,6 +27,14 @@ 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 diff --git a/test/data/android-comments.xml b/test/data/android-comments.xml new file mode 100644 index 0000000..c54c592 --- /dev/null +++ b/test/data/android-comments.xml @@ -0,0 +1,13 @@ + + + + + android2csv + + Hello + + testEN + + + + From 86e3ee4b129a014924644607ef2218601062d757 Mon Sep 17 00:00:00 2001 From: David Chau Date: Fri, 11 Aug 2017 13:49:17 -0700 Subject: [PATCH 2/3] Fixed a few of houndci comments --- test/babelish/test_android2csv.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/babelish/test_android2csv.rb b/test/babelish/test_android2csv.rb index 48c8eaf..e6622d5 100644 --- a/test/babelish/test_android2csv.rb +++ b/test/babelish/test_android2csv.rb @@ -28,9 +28,20 @@ def test_initialize 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"}] + 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 From 106a6c2c250ba6dd416de40673211194a3f547c5 Mon Sep 17 00:00:00 2001 From: David Chau Date: Fri, 11 Aug 2017 14:01:17 -0700 Subject: [PATCH 3/3] Fixed more houndci code style issues --- test/babelish/test_android2csv.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/babelish/test_android2csv.rb b/test/babelish/test_android2csv.rb index e6622d5..8ea1140 100644 --- a/test/babelish/test_android2csv.rb +++ b/test/babelish/test_android2csv.rb @@ -29,19 +29,19 @@ def test_initialize def test_load_strings_with_comments expected_output = [ - { - "app_name" => "android2csv", - "action_greetings" => "Hello", - "ANOTHER_STRING" => "testEN", + { + "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" - } - ] + }, + { + "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