aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2026-06-26 19:28:57 +0200
committerMiquel Sabaté Solà <mssola@mssola.com>2026-06-26 19:28:57 +0200
commitbc62b98e2ae5b3d2d3efda24101590dd802d2a26 (patch)
tree5471ce6cd13729a5088ca5009744a5054847f2ba
parentd96b0bb6288ae17b7d7f21392180d1ac0c2270de (diff)
downloadoperum-bc62b98e2ae5b3d2d3efda24101590dd802d2a26.tar.gz
operum-bc62b98e2ae5b3d2d3efda24101590dd802d2a26.zip
exporter: handle names surrounded with underscores
Handle underscores which surround the full name, not just a part of it. In this case, then the whole thing should be considered as the name, and the last name should be left nil. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
-rw-r--r--lib/base_exporter.rb10
-rw-r--r--test/lib/base_exporter_test.rb27
2 files changed, 37 insertions, 0 deletions
diff --git a/lib/base_exporter.rb b/lib/base_exporter.rb
index a67784e..7879af1 100644
--- a/lib/base_exporter.rb
+++ b/lib/base_exporter.rb
@@ -19,10 +19,20 @@ class BaseExporter
# - 'Name Surname' -> ['Name', 'Surname']
# - 'Compound Name Surname' -> ['Compound Name', 'Surname']
# - 'Name _Surname1 Surname2_' -> ['Name', 'Surname1 Surname2']
+ # - '_Special name_' -> ['Special name', nil]
def parse_author(author:)
+ # Special case: the user might set the whole thing as the name on special
+ # scenarios. In this case everything will be surrounded by
+ # underscores. Hence, if this is the case, then just return the whole thing
+ # as a name with undescores stripped.
+ return [author.delete('_'), nil] if /^\s*_(.+)_\s*$/.match?(author)
+
+ # Other underscore scenarios are handled here.
matches = /(.+)?\s_(.+)_/.match(author)
return matches[1].strip, matches[2].strip if matches&.size == 3
+ # Otherwise we fallback to the usual route.
+
a = author.split
return [author, nil] if a.size == 1
diff --git a/test/lib/base_exporter_test.rb b/test/lib/base_exporter_test.rb
new file mode 100644
index 0000000..752445b
--- /dev/null
+++ b/test/lib/base_exporter_test.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+require 'application_system_test_case'
+
+class BaseExporterTest < ApplicationSystemTestCase
+ test 'parse authors works' do
+ instance = BaseExporter.new(things: [])
+
+ [
+ ['Name Surname', %w[Name Surname]],
+ ['Name', ['Name', nil]],
+ ['Compound Name Surname', ['Compound Name', 'Surname']],
+ ['Name _Underscored Surname_', ['Name', 'Underscored Surname']],
+ ['_Name in underscores_', ['Name in underscores', nil]]
+ ].each do |row|
+ got = instance.send(:parse_author, author: row[0])
+
+ assert_equal got[0], row[1][0]
+
+ if row[1][1]
+ assert_equal got[1], row[1][1]
+ else
+ assert_nil got[1]
+ end
+ end
+ end
+end