aboutsummaryrefslogtreecommitdiff
path: root/lib/base_exporter.rb
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-07-01 08:40:49 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-07-01 08:53:56 +0200
commit34e16b7d7a88013578014be0402ab686b03f235b (patch)
tree5ef848d3c44f72ff95a24b123e5da88bc5aef1a2 /lib/base_exporter.rb
parent24714068efc5866999311ad482d54f5c7a90c449 (diff)
downloadoperum-34e16b7d7a88013578014be0402ab686b03f235b.tar.gz
operum-34e16b7d7a88013578014be0402ab686b03f235b.zip
lib: Introduce the PlainExporter
This class exports things into plain text. It follows a close format to that of the one for UOC, but without any formatting. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'lib/base_exporter.rb')
-rw-r--r--lib/base_exporter.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/base_exporter.rb b/lib/base_exporter.rb
index b040592..f64301b 100644
--- a/lib/base_exporter.rb
+++ b/lib/base_exporter.rb
@@ -5,6 +5,8 @@ class BaseExporter
@things = things
end
+ protected
+
# Returns a string which forces a new TeX line.
def tex_new_line
'\\\\~\\\\'
@@ -26,4 +28,44 @@ class BaseExporter
[a[0..-2].join(' '), a.last]
end
+
+ # Parse the string in `thing.authors` and put each author followed by
+ # semicolon and the right order for first and last name. It will also cover
+ # whenever editors are involved.
+ def parse_authors(thing:)
+ str = thing.authors.split(',').map do |author|
+ first, last = parse_author(author: author.strip)
+ last.blank? ? upper_case(text: first).to_s : "#{upper_case(text: last)}, #{first}"
+ end.join('; ')
+
+ str += ' (eds.)' if thing.editors
+ str
+ end
+
+ # Returns an "Inside of" string where `sub` is the substring contained in it.
+ def inside_of(sub:)
+ str = if sub.downcase.start_with?(*%w[a e i o u])
+ I18n.t('general.insideof-vowel')
+ else
+ I18n.t('general.insideof')
+ end
+
+ str + " #{emph(text: sub)}"
+ end
+
+ # Returns the head of a formatted "thing", containing authors, year, title
+ # and, if available, the "inside of" information. This is shared across some
+ # exporters, thus placed in here.
+ #
+ # NOTE: the subclass *must* implement `parse_title`.
+ def head(thing:)
+ str = "#{parse_authors(thing:)} (#{thing.year}). #{parse_title(title: thing.title)}."
+
+ if thing.insideof.present?
+ str += " #{inside_of(sub: thing.insideof)}"
+ str += thing.pages.present? ? ", #{thing.pages}." : '.'
+ end
+
+ str
+ end
end