From 9a7f8bac536287d0d4e85be72a03265a6223aa59 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 25 Feb 2024 11:43:16 +0400 Subject: [PATCH] Add format "summary_plain" --- examples/config.yml | 3 +++ examples/full.summary.txt | 2 ++ examples/hello.summary.txt | 1 + exe/repubmark | 17 ++++++++++++++--- lib/repubmark.rb | 2 +- lib/repubmark/elems/abbrev.rb | 2 ++ lib/repubmark/elems/annotation.rb | 2 ++ lib/repubmark/elems/article.rb | 7 +++++++ lib/repubmark/elems/base.rb | 2 ++ lib/repubmark/elems/canvas.rb | 14 ++++++++++++-- lib/repubmark/elems/caption.rb | 10 +++++++++- lib/repubmark/elems/chapter.rb | 12 +++++++++++- lib/repubmark/elems/code_inline.rb | 4 +++- lib/repubmark/elems/figure.rb | 2 +- lib/repubmark/elems/fraction.rb | 4 +++- lib/repubmark/elems/joint.rb | 2 ++ lib/repubmark/elems/list.rb | 4 +++- lib/repubmark/elems/list_item.rb | 7 +++++++ lib/repubmark/elems/paragraph.rb | 2 ++ lib/repubmark/elems/quote.rb | 4 ++++ lib/repubmark/elems/section.rb | 6 ++++-- lib/repubmark/elems/special.rb | 6 ++++-- lib/repubmark/elems/text.rb | 2 ++ test.rb | 1 + 24 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 examples/full.summary.txt create mode 100644 examples/hello.summary.txt diff --git a/examples/config.yml b/examples/config.yml index 7af5ebf..6b0a593 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -24,6 +24,9 @@ css_classes: &css_classes word_count: format: word_count +summary_plain: + format: summary_plain + gemini: <<: *paths format: gemtext diff --git a/examples/full.summary.txt b/examples/full.summary.txt new file mode 100644 index 0000000..9374b10 --- /dev/null +++ b/examples/full.summary.txt @@ -0,0 +1,2 @@ +Annotation paragraph text Chapter 1 Chapter 1 paragraph text Chapter 2 +Chapter 2 paragraph text diff --git a/examples/hello.summary.txt b/examples/hello.summary.txt new file mode 100644 index 0000000..8ab686e --- /dev/null +++ b/examples/hello.summary.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/exe/repubmark b/exe/repubmark index 2b112da..137632e 100755 --- a/exe/repubmark +++ b/exe/repubmark @@ -19,11 +19,22 @@ context = BasicObject.new def context.article = $article context.instance_eval template, __FILE__, __LINE__ +def wrap(str) + words = str.strip.split + lines = [[]] + words.each do |word| + lines << [] if [*lines.last, word].join(' ').length > 70 + lines.last << word + end + lines.map { |line| "#{line.join(' ')}\n" }.join.freeze +end + output = case config.format - when :word_count then $article.word_count - when :html then $article.to_html - when :gemtext then $article.to_gemtext + when :word_count then $article.word_count + when :summary_plain then wrap $article.to_summary_plain + when :html then $article.to_html + when :gemtext then $article.to_gemtext end.to_s.strip puts output unless output.empty? diff --git a/lib/repubmark.rb b/lib/repubmark.rb index 83aa5ef..ff58ca7 100644 --- a/lib/repubmark.rb +++ b/lib/repubmark.rb @@ -57,5 +57,5 @@ require_relative 'repubmark/elems/text' require_relative 'repubmark/elems/link' module Repubmark - FORMATS = %i[gemtext html word_count].freeze + FORMATS = %i[gemtext html summary_plain word_count].freeze end diff --git a/lib/repubmark/elems/abbrev.rb b/lib/repubmark/elems/abbrev.rb index b329b34..7bac8f3 100644 --- a/lib/repubmark/elems/abbrev.rb +++ b/lib/repubmark/elems/abbrev.rb @@ -20,6 +20,8 @@ module Repubmark def word_count = 1 + def to_summary_plain = abbrev + def to_html %(#{escape_abbrev}).freeze end diff --git a/lib/repubmark/elems/annotation.rb b/lib/repubmark/elems/annotation.rb index 46b6db3..74f0b3f 100644 --- a/lib/repubmark/elems/annotation.rb +++ b/lib/repubmark/elems/annotation.rb @@ -16,6 +16,8 @@ module Repubmark def word_count = @canvas.word_count + def to_summary_plain = @canvas.to_summary_plain + def to_html [ %(\n), diff --git a/lib/repubmark/elems/article.rb b/lib/repubmark/elems/article.rb index 1c2c740..2922d34 100644 --- a/lib/repubmark/elems/article.rb +++ b/lib/repubmark/elems/article.rb @@ -19,6 +19,13 @@ module Repubmark (@chapter&.word_count || 0) end + def to_summary_plain + [ + @annotation&.to_summary_plain, + @chapter&.to_summary_plain, + ].compact.join(' ').freeze + end + def to_html [ @prologue&.to_html, diff --git a/lib/repubmark/elems/base.rb b/lib/repubmark/elems/base.rb index ac1a852..5d2009f 100644 --- a/lib/repubmark/elems/base.rb +++ b/lib/repubmark/elems/base.rb @@ -15,6 +15,8 @@ module Repubmark def word_count = 0 + def to_summary_plain = nil + def to_html raise NotImplementedError, "#{self.class}#to_html" end diff --git a/lib/repubmark/elems/canvas.rb b/lib/repubmark/elems/canvas.rb index 41e463a..43762e8 100644 --- a/lib/repubmark/elems/canvas.rb +++ b/lib/repubmark/elems/canvas.rb @@ -16,9 +16,19 @@ module Repubmark def word_count = @items.sum(&:word_count) - def to_html = @items.map(&:to_html).join.freeze + def to_summary_plain + return if @items.empty? - def to_gemtext = @items.map(&:to_gemtext).join("\n").freeze + @items.filter_map(&:to_summary_plain).join(' ').freeze + end + + def to_html + @items.map(&:to_html).join.freeze unless @items.empty? + end + + def to_gemtext + @items.map(&:to_gemtext).join("\n").freeze unless @items.empty? + end ################### # Builder methods # diff --git a/lib/repubmark/elems/caption.rb b/lib/repubmark/elems/caption.rb index cf724b4..9a23eca 100644 --- a/lib/repubmark/elems/caption.rb +++ b/lib/repubmark/elems/caption.rb @@ -18,7 +18,13 @@ module Repubmark def word_count = @items.sum(&:word_count) + def to_summary_plain + @items.map(&:to_summary_plain).join(' ').freeze unless @items.empty? + end + def to_html + return if @items.empty? + [ '', *@items.map(&:to_html), @@ -26,7 +32,9 @@ module Repubmark ].map { |s| "#{s}\n" }.join.freeze end - def to_gemtext = @items.map(&:to_gemtext).join(' ').strip.freeze + def to_gemtext + @items.map(&:to_gemtext).join(' ').strip.freeze unless @items.empty? + end ################## # Helper methods # diff --git a/lib/repubmark/elems/chapter.rb b/lib/repubmark/elems/chapter.rb index 3d48161..ac9e6a6 100644 --- a/lib/repubmark/elems/chapter.rb +++ b/lib/repubmark/elems/chapter.rb @@ -25,6 +25,16 @@ module Repubmark @chapters.sum(&:word_count) end + def to_summary_plain + str = [ + @title, + @canvas.to_summary_plain, + *@chapters.map(&:to_summary_plain), + ].compact.join(' ').strip.freeze + + str.empty? ? nil : str + end + def to_html [ build_title_html, @@ -78,7 +88,7 @@ module Repubmark def title=(title) return @title = nil if title.nil? - title = String(title).strip.freeze + title = String(title).split.join(' ').strip.freeze raise 'Empty title' if title.empty? @title = title diff --git a/lib/repubmark/elems/code_inline.rb b/lib/repubmark/elems/code_inline.rb index 5d6ba15..f9e213b 100644 --- a/lib/repubmark/elems/code_inline.rb +++ b/lib/repubmark/elems/code_inline.rb @@ -14,9 +14,11 @@ module Repubmark # Basic methods # ################# + def to_summary_plain = "«#@str»".freeze + def to_html = "#{CGI.escape_html(@str)}".freeze - def to_gemtext = "«#{@str}»".freeze + def to_gemtext = "«#@str»".freeze private diff --git a/lib/repubmark/elems/figure.rb b/lib/repubmark/elems/figure.rb index 55a8862..eea1845 100644 --- a/lib/repubmark/elems/figure.rb +++ b/lib/repubmark/elems/figure.rb @@ -34,7 +34,7 @@ module Repubmark end def to_gemtext - caption = @caption.to_gemtext + caption = @caption.to_gemtext.to_s.strip caption = @alt if caption.empty? "=> #{src} #{caption}\n".freeze end diff --git a/lib/repubmark/elems/fraction.rb b/lib/repubmark/elems/fraction.rb index 6ab8fa9..f78d1f1 100644 --- a/lib/repubmark/elems/fraction.rb +++ b/lib/repubmark/elems/fraction.rb @@ -21,11 +21,13 @@ module Repubmark def word_count = 1 + def to_summary_plain = "#@top/#@bottom".freeze + def to_html "#@top#@bottom".freeze end - def to_gemtext = "#@top/#@bottom" + def to_gemtext = "#@top/#@bottom".freeze end end end diff --git a/lib/repubmark/elems/joint.rb b/lib/repubmark/elems/joint.rb index 93bfe88..ea21bad 100644 --- a/lib/repubmark/elems/joint.rb +++ b/lib/repubmark/elems/joint.rb @@ -24,6 +24,8 @@ module Repubmark def word_count = components.sum(&:word_count) + def to_summary_plain = components.map(&:to_summary_plain).join.freeze + def to_html = components.map(&:to_html).join.freeze def to_gemtext = components.map(&:to_gemtext).join.freeze diff --git a/lib/repubmark/elems/list.rb b/lib/repubmark/elems/list.rb index 9b42a45..ee3a428 100644 --- a/lib/repubmark/elems/list.rb +++ b/lib/repubmark/elems/list.rb @@ -20,6 +20,8 @@ module Repubmark def word_count = @items.sum(&:word_count) + def to_summary_plain = @items.map(&:to_summary_plain).join(' ').freeze + def to_html [ "<#{tag_name}>\n", @@ -28,7 +30,7 @@ module Repubmark ].join.freeze end - def to_gemtext = @items.map(&:to_gemtext).join + def to_gemtext = @items.map(&:to_gemtext).join.freeze ################## # Helper methods # diff --git a/lib/repubmark/elems/list_item.rb b/lib/repubmark/elems/list_item.rb index 9b73dd3..3b325b2 100644 --- a/lib/repubmark/elems/list_item.rb +++ b/lib/repubmark/elems/list_item.rb @@ -25,6 +25,13 @@ module Repubmark def word_count = @caption.word_count + (@sublist&.word_count || 0) + def to_summary_plain + [ + @caption.to_summary_plain, + @sublist&.to_summary_plain, + ].compact.join("\n").freeze + end + def to_html [ "
  • \n", diff --git a/lib/repubmark/elems/paragraph.rb b/lib/repubmark/elems/paragraph.rb index 9b46cd7..979e8e4 100644 --- a/lib/repubmark/elems/paragraph.rb +++ b/lib/repubmark/elems/paragraph.rb @@ -16,6 +16,8 @@ module Repubmark def word_count = @caption.word_count + def to_summary_plain = @caption.to_summary_plain + def to_html = "

    \n#{@caption.to_html}

    \n".freeze def to_gemtext = "#{@caption.to_gemtext}\n".freeze diff --git a/lib/repubmark/elems/quote.rb b/lib/repubmark/elems/quote.rb index bb685d4..1379016 100644 --- a/lib/repubmark/elems/quote.rb +++ b/lib/repubmark/elems/quote.rb @@ -18,6 +18,10 @@ module Repubmark def word_count = @items.sum(&:word_count) + def to_summary_plain + "«#{@items.map(&:to_summary_plain).join(' ')}»".freeze + end + def to_html = "«#{@items.map(&:to_html).join("\n")}»".freeze def to_gemtext = "«#{@items.map(&:to_gemtext).join(' ')}»".freeze diff --git a/lib/repubmark/elems/section.rb b/lib/repubmark/elems/section.rb index 02ac611..6c587ed 100644 --- a/lib/repubmark/elems/section.rb +++ b/lib/repubmark/elems/section.rb @@ -6,7 +6,7 @@ module Repubmark parents :Joint SECT_HTML = '§' - SECT_GEMTEXT = '§' + SECT_UNICODE = '§' attr_reader :count, :text @@ -30,9 +30,11 @@ module Repubmark def word_count = count_words @text + def to_summary_plain = "#{SECT_UNICODE * count}#{text}".freeze + def to_html = "#{SECT_HTML * count}#{text}".freeze - def to_gemtext = "#{SECT_GEMTEXT * count}#{text}".freeze + def to_gemtext = "#{SECT_UNICODE * count}#{text}".freeze private diff --git a/lib/repubmark/elems/special.rb b/lib/repubmark/elems/special.rb index eb1c2e3..dcbe7b6 100644 --- a/lib/repubmark/elems/special.rb +++ b/lib/repubmark/elems/special.rb @@ -11,7 +11,7 @@ module Repubmark mdash: '—', }.freeze - GEMTEXT = { + UNICODE = { ellipsis: '…', mdash: '—', }.freeze @@ -27,9 +27,11 @@ module Repubmark # Basic methods # ################# + def to_summary_plain = UNICODE.fetch @name + def to_html = HTML.fetch @name - def to_gemtext = GEMTEXT.fetch @name + def to_gemtext = UNICODE.fetch @name end end end diff --git a/lib/repubmark/elems/text.rb b/lib/repubmark/elems/text.rb index 827971e..7dd0eee 100644 --- a/lib/repubmark/elems/text.rb +++ b/lib/repubmark/elems/text.rb @@ -20,6 +20,8 @@ module Repubmark def word_count = count_words @str + def to_summary_plain = @str + def to_html = str_to_html def to_gemtext = @str diff --git a/test.rb b/test.rb index 62a31a0..14eaf43 100755 --- a/test.rb +++ b/test.rb @@ -9,6 +9,7 @@ EXAMPLES_GLOB = File.expand_path('examples/*.repub', __dir__).freeze CONFIG = File.expand_path('examples/config.yml', __dir__).freeze PROFILES = [ + %w[summary_plain summary.txt], %w[http html], %w[gemini gmi], ].map(&:freeze).freeze