From f716fe70d5eb04465768c97d45b179e12833f924 Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Fri, 18 Mar 2022 17:44:35 +0100 Subject: [PATCH 1/5] Allow multiline strings that have the first line non-empty. --- lib/simple_po_parser/parser.rb | 13 +++++-------- spec/simple_po_parser/fixtures/multiline.po | 6 ++++++ spec/simple_po_parser/parser_spec.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 spec/simple_po_parser/fixtures/multiline.po diff --git a/lib/simple_po_parser/parser.rb b/lib/simple_po_parser/parser.rb index fa46af5..58fa5f9 100644 --- a/lib/simple_po_parser/parser.rb +++ b/lib/simple_po_parser/parser.rb @@ -109,7 +109,7 @@ def msgctxt skip_whitespace text = message_line add_result(:msgctxt, text) - message_multiline(:msgctxt) if text.empty? + message_multiline(:msgctxt) if @scanner.peek(1) == '"' end msgid rescue PoSyntaxError => pe @@ -127,7 +127,7 @@ def msgid skip_whitespace text = message_line add_result(:msgid, text) - message_multiline(:msgid) if text.empty? + message_multiline(:msgid) if @scanner.peek(1) == '"' if msgid_plural msgstr_plural else @@ -155,7 +155,7 @@ def msgid_plural skip_whitespace text = message_line add_result(:msgid_plural, text) - message_multiline(:msgid_plural) if text.empty? + message_multiline(:msgid_plural) if @scanner.peek(1) == '"' true else false @@ -174,7 +174,7 @@ def msgstr skip_whitespace text = message_line add_result(:msgstr, text) - message_multiline(:msgstr) if text.empty? + message_multiline(:msgstr) if @scanner.peek(1) == '"' skip_whitespace raise PoSyntaxError, "Unexpected content after expected message end #{@scanner.peek(10).inspect}" unless @scanner.eos? else @@ -202,7 +202,7 @@ def msgstr_plural(num = 0) skip_whitespace text = message_line add_result(msgstr_key, text) - message_multiline(msgstr_key) if text.empty? + message_multiline(msgstr_key) if @scanner.peek(1) == '"' msgstr_plural(num+1) elsif num == 0 # and msgstr_key was false raise PoSyntaxError, "Plural message without msgstr[0] is not allowed. Line started unexpectedly with #{@scanner.peek(10).inspect}." @@ -264,9 +264,6 @@ def previous_multiline(key) end # parses a multiline message - # - # multiline messages are indicated by an empty content as first line and the next line - # starting with the double quote character def message_multiline(key) begin skip_whitespace diff --git a/spec/simple_po_parser/fixtures/multiline.po b/spec/simple_po_parser/fixtures/multiline.po new file mode 100644 index 0000000..dba4af3 --- /dev/null +++ b/spec/simple_po_parser/fixtures/multiline.po @@ -0,0 +1,6 @@ +msgid "" +"By default, the follow-up check is done via the subject of an email. This " +"setting lets you add more fields for which the follow-up check will be " +"executed." +msgstr "默认情况下,通过电子邮件的主题进行跟进检查。这个设置允许你增加更多的字段执行" +"跟进检查。" \ No newline at end of file diff --git a/spec/simple_po_parser/parser_spec.rb b/spec/simple_po_parser/parser_spec.rb index 3791741..728d75f 100644 --- a/spec/simple_po_parser/parser_spec.rb +++ b/spec/simple_po_parser/parser_spec.rb @@ -4,6 +4,7 @@ let (:po_header) { File.read(File.expand_path("fixtures/header.po", __dir__))} let(:po_complex_message) { File.read(File.expand_path("fixtures/complex_entry.po", __dir__))} let(:po_simple_message) { File.read(File.expand_path("fixtures/simple_entry.po", __dir__))} + let(:po_multiline_message) { File.read(File.expand_path("fixtures/multiline.po", __dir__))} it "parses the PO header" do expected_result = { @@ -27,6 +28,14 @@ expect(SimplePoParser::Parser.new.parse(po_simple_message)).to eq(expected_result) end + it "parses the multiline entry as expected" do + expected_result = { + :msgid => ["", "By default, the follow-up check is done via the subject of an email. This ", "setting lets you add more fields for which the follow-up check will be ", "executed."], + :msgstr => ["默认情况下,通过电子邮件的主题进行跟进检查。这个设置允许你增加更多的字段执行", "跟进检查。"] + } + expect(SimplePoParser::Parser.new.parse(po_multiline_message)).to eq(expected_result) + end + it "parses the complex entry as expected" do expected_result = { :translator_comment => ["translator-comment", ""], From 8a289ece3ea79273072806c9cea1f6cd197f1c26 Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Fri, 18 Mar 2022 19:49:43 +0100 Subject: [PATCH 2/5] Improved test case and method comment. --- lib/simple_po_parser/parser.rb | 5 +++++ spec/simple_po_parser/fixtures/multiline.po | 11 ++++++----- spec/simple_po_parser/parser_spec.rb | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/simple_po_parser/parser.rb b/lib/simple_po_parser/parser.rb index 58fa5f9..e4b5e8a 100644 --- a/lib/simple_po_parser/parser.rb +++ b/lib/simple_po_parser/parser.rb @@ -264,6 +264,11 @@ def previous_multiline(key) end # parses a multiline message + # + # Multiline messages are often indicated by an empty content as first line, + # followed by more lines starting with the double quote character. + # + # However, the first line can also contain content, according to the PO file standard. def message_multiline(key) begin skip_whitespace diff --git a/spec/simple_po_parser/fixtures/multiline.po b/spec/simple_po_parser/fixtures/multiline.po index dba4af3..8e8c323 100644 --- a/spec/simple_po_parser/fixtures/multiline.po +++ b/spec/simple_po_parser/fixtures/multiline.po @@ -1,6 +1,7 @@ msgid "" -"By default, the follow-up check is done via the subject of an email. This " -"setting lets you add more fields for which the follow-up check will be " -"executed." -msgstr "默认情况下,通过电子邮件的主题进行跟进检查。这个设置允许你增加更多的字段执行" -"跟进检查。" \ No newline at end of file +"multiline string " +"with empty first line " +"and trailing spaces" +msgstr "multiline string" +"with non-empty first line" +"and no trailing spaces" \ No newline at end of file diff --git a/spec/simple_po_parser/parser_spec.rb b/spec/simple_po_parser/parser_spec.rb index 728d75f..3ac4980 100644 --- a/spec/simple_po_parser/parser_spec.rb +++ b/spec/simple_po_parser/parser_spec.rb @@ -30,8 +30,8 @@ it "parses the multiline entry as expected" do expected_result = { - :msgid => ["", "By default, the follow-up check is done via the subject of an email. This ", "setting lets you add more fields for which the follow-up check will be ", "executed."], - :msgstr => ["默认情况下,通过电子邮件的主题进行跟进检查。这个设置允许你增加更多的字段执行", "跟进检查。"] + :msgid => ["", "multiline string ", "with empty first line ", "and trailing spaces"], + :msgstr => ["multiline string", "with non-empty first line", "and no trailing spaces"], } expect(SimplePoParser::Parser.new.parse(po_multiline_message)).to eq(expected_result) end From b5ccb45426df4fbac2ce7c8b0e0a70719891a668 Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Sat, 19 Mar 2022 08:55:48 +0100 Subject: [PATCH 3/5] Adapted multiline handling also for 'previous' entries. --- lib/simple_po_parser/parser.rb | 6 +++++- spec/simple_po_parser/fixtures/multiline.po | 3 +++ spec/simple_po_parser/parser_spec.rb | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/simple_po_parser/parser.rb b/lib/simple_po_parser/parser.rb index e4b5e8a..cfeaec6 100644 --- a/lib/simple_po_parser/parser.rb +++ b/lib/simple_po_parser/parser.rb @@ -238,7 +238,11 @@ def previous_comments skip_whitespace text = message_line add_result(key, text) - previous_multiline(key) if text.empty? + pos = @scanner.pos + if @scanner.scan(/#\|\p{Blank}*"/) + @scanner.pos = pos + previous_multiline(key) + end else raise PoSyntaxError, "Previous comments must start with '#| msg'. #{@scanner.peek(10).inspect} unknown." end diff --git a/spec/simple_po_parser/fixtures/multiline.po b/spec/simple_po_parser/fixtures/multiline.po index 8e8c323..bbe887c 100644 --- a/spec/simple_po_parser/fixtures/multiline.po +++ b/spec/simple_po_parser/fixtures/multiline.po @@ -1,3 +1,6 @@ +#| msgid "multiline\n" +#|"previous messageid" +#|"with non-empty first line" msgid "" "multiline string " "with empty first line " diff --git a/spec/simple_po_parser/parser_spec.rb b/spec/simple_po_parser/parser_spec.rb index 3ac4980..dbc91e9 100644 --- a/spec/simple_po_parser/parser_spec.rb +++ b/spec/simple_po_parser/parser_spec.rb @@ -32,6 +32,7 @@ expected_result = { :msgid => ["", "multiline string ", "with empty first line ", "and trailing spaces"], :msgstr => ["multiline string", "with non-empty first line", "and no trailing spaces"], + :previous_msgid => ["multiline\\n", "previous messageid", "with non-empty first line"], } expect(SimplePoParser::Parser.new.parse(po_multiline_message)).to eq(expected_result) end From 3b8d302c01f5b3796bee5151c94f9900052a2d94 Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Mon, 21 Mar 2022 06:51:42 +0100 Subject: [PATCH 4/5] Simplified previous multiline detection logic. --- lib/simple_po_parser/parser.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/simple_po_parser/parser.rb b/lib/simple_po_parser/parser.rb index cfeaec6..f926e2b 100644 --- a/lib/simple_po_parser/parser.rb +++ b/lib/simple_po_parser/parser.rb @@ -238,11 +238,7 @@ def previous_comments skip_whitespace text = message_line add_result(key, text) - pos = @scanner.pos - if @scanner.scan(/#\|\p{Blank}*"/) - @scanner.pos = pos - previous_multiline(key) - end + previous_multiline(key) if @scanner.match?(/#\|\p{Blank}*"/) else raise PoSyntaxError, "Previous comments must start with '#| msg'. #{@scanner.peek(10).inspect} unknown." end From aabd494d949393497ef29c024bececfca0754657 Mon Sep 17 00:00:00 2001 From: Dennis-Florian Herr Date: Tue, 22 Mar 2022 16:16:21 +0100 Subject: [PATCH 5/5] slight change of wording in comment --- lib/simple_po_parser/parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/simple_po_parser/parser.rb b/lib/simple_po_parser/parser.rb index f926e2b..a452be7 100644 --- a/lib/simple_po_parser/parser.rb +++ b/lib/simple_po_parser/parser.rb @@ -265,10 +265,10 @@ def previous_multiline(key) # parses a multiline message # - # Multiline messages are often indicated by an empty content as first line, + # Multiline messages are usually indicated by an empty string as the first line, # followed by more lines starting with the double quote character. # - # However, the first line can also contain content, according to the PO file standard. + # However, according to the PO file standard, the first line can also contain content. def message_multiline(key) begin skip_whitespace