From 34e16b7d7a88013578014be0402ab686b03f235b Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 1 Jul 2025 08:40:49 +0200 Subject: lib: Introduce the PlainExporter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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à --- lib/plain_exporter.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/plain_exporter.rb (limited to 'lib/plain_exporter.rb') diff --git a/lib/plain_exporter.rb b/lib/plain_exporter.rb new file mode 100644 index 0000000..514d243 --- /dev/null +++ b/lib/plain_exporter.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +# Plain text exporter. +class PlainExporter < BaseExporter + def export + @things.map { |thing| "#{head(thing:)} #{body(thing:)}".strip }.join("\n") + end + + protected + + # Returns the same text as the plain exporter does not have formatting. + def emph(text:) + text + end + + # Returns the same text as the plain exporter does not have formatting. + def upper_case(text:) + text + end + + # Returns the same text with underscores stripped, as the plain exporter does + # not have formatting. + def parse_title(title:) + title.delete('_') + end + + # Returns text with information on the publisher, notes, year, address, etc; + # if available. + def body(thing:) + str = '' + + if thing.publisher.present? + str += thing.note.present? ? "#{thing.note}. #{thing.publisher}" : thing.publisher + end + + if thing.address.present? + str + ", #{thing.address}: #{thing.year}." + else + "#{str}." + end + end +end -- cgit v1.2.3