Add format "summary_plain"

This commit is contained in:
Alex Kotov 2024-02-25 11:43:16 +04:00
parent 8ec0774613
commit 9a7f8bac53
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
24 changed files with 102 additions and 16 deletions

View file

@ -24,6 +24,9 @@ css_classes: &css_classes
word_count: word_count:
format: word_count format: word_count
summary_plain:
format: summary_plain
gemini: gemini:
<<: *paths <<: *paths
format: gemtext format: gemtext

View file

@ -0,0 +1,2 @@
Annotation paragraph text Chapter 1 Chapter 1 paragraph text Chapter 2
Chapter 2 paragraph text

View file

@ -0,0 +1 @@
Hello, World!

View file

@ -19,11 +19,22 @@ context = BasicObject.new
def context.article = $article def context.article = $article
context.instance_eval template, __FILE__, __LINE__ 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 = output =
case config.format case config.format
when :word_count then $article.word_count when :word_count then $article.word_count
when :html then $article.to_html when :summary_plain then wrap $article.to_summary_plain
when :gemtext then $article.to_gemtext when :html then $article.to_html
when :gemtext then $article.to_gemtext
end.to_s.strip end.to_s.strip
puts output unless output.empty? puts output unless output.empty?

View file

@ -57,5 +57,5 @@ require_relative 'repubmark/elems/text'
require_relative 'repubmark/elems/link' require_relative 'repubmark/elems/link'
module Repubmark module Repubmark
FORMATS = %i[gemtext html word_count].freeze FORMATS = %i[gemtext html summary_plain word_count].freeze
end end

View file

@ -20,6 +20,8 @@ module Repubmark
def word_count = 1 def word_count = 1
def to_summary_plain = abbrev
def to_html def to_html
%(<abbr title="#{escape_transcript}">#{escape_abbrev}</abbr>).freeze %(<abbr title="#{escape_transcript}">#{escape_abbrev}</abbr>).freeze
end end

View file

@ -16,6 +16,8 @@ module Repubmark
def word_count = @canvas.word_count def word_count = @canvas.word_count
def to_summary_plain = @canvas.to_summary_plain
def to_html def to_html
[ [
%(<div#{html_class(:annotation)}>\n), %(<div#{html_class(:annotation)}>\n),

View file

@ -19,6 +19,13 @@ module Repubmark
(@chapter&.word_count || 0) (@chapter&.word_count || 0)
end end
def to_summary_plain
[
@annotation&.to_summary_plain,
@chapter&.to_summary_plain,
].compact.join(' ').freeze
end
def to_html def to_html
[ [
@prologue&.to_html, @prologue&.to_html,

View file

@ -15,6 +15,8 @@ module Repubmark
def word_count = 0 def word_count = 0
def to_summary_plain = nil
def to_html def to_html
raise NotImplementedError, "#{self.class}#to_html" raise NotImplementedError, "#{self.class}#to_html"
end end

View file

@ -16,9 +16,19 @@ module Repubmark
def word_count = @items.sum(&:word_count) 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 # # Builder methods #

View file

@ -18,7 +18,13 @@ module Repubmark
def word_count = @items.sum(&:word_count) 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 def to_html
return if @items.empty?
[ [
'<span>', '<span>',
*@items.map(&:to_html), *@items.map(&:to_html),
@ -26,7 +32,9 @@ module Repubmark
].map { |s| "#{s}\n" }.join.freeze ].map { |s| "#{s}\n" }.join.freeze
end 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 # # Helper methods #

View file

@ -25,6 +25,16 @@ module Repubmark
@chapters.sum(&:word_count) @chapters.sum(&:word_count)
end 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 def to_html
[ [
build_title_html, build_title_html,
@ -78,7 +88,7 @@ module Repubmark
def title=(title) def title=(title)
return @title = nil if title.nil? return @title = nil if title.nil?
title = String(title).strip.freeze title = String(title).split.join(' ').strip.freeze
raise 'Empty title' if title.empty? raise 'Empty title' if title.empty?
@title = title @title = title

View file

@ -14,9 +14,11 @@ module Repubmark
# Basic methods # # Basic methods #
################# #################
def to_summary_plain = "«#@str»".freeze
def to_html = "<code>#{CGI.escape_html(@str)}</code>".freeze def to_html = "<code>#{CGI.escape_html(@str)}</code>".freeze
def to_gemtext = "«#{@str}»".freeze def to_gemtext = "«#@str»".freeze
private private

View file

@ -34,7 +34,7 @@ module Repubmark
end end
def to_gemtext def to_gemtext
caption = @caption.to_gemtext caption = @caption.to_gemtext.to_s.strip
caption = @alt if caption.empty? caption = @alt if caption.empty?
"=> #{src} #{caption}\n".freeze "=> #{src} #{caption}\n".freeze
end end

View file

@ -21,11 +21,13 @@ module Repubmark
def word_count = 1 def word_count = 1
def to_summary_plain = "#@top/#@bottom".freeze
def to_html def to_html
"<sup>#@top</sup>&frasl;<sub>#@bottom</sub>".freeze "<sup>#@top</sup>&frasl;<sub>#@bottom</sub>".freeze
end end
def to_gemtext = "#@top/#@bottom" def to_gemtext = "#@top/#@bottom".freeze
end end
end end
end end

View file

@ -24,6 +24,8 @@ module Repubmark
def word_count = components.sum(&:word_count) 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_html = components.map(&:to_html).join.freeze
def to_gemtext = components.map(&:to_gemtext).join.freeze def to_gemtext = components.map(&:to_gemtext).join.freeze

View file

@ -20,6 +20,8 @@ module Repubmark
def word_count = @items.sum(&:word_count) def word_count = @items.sum(&:word_count)
def to_summary_plain = @items.map(&:to_summary_plain).join(' ').freeze
def to_html def to_html
[ [
"<#{tag_name}>\n", "<#{tag_name}>\n",
@ -28,7 +30,7 @@ module Repubmark
].join.freeze ].join.freeze
end end
def to_gemtext = @items.map(&:to_gemtext).join def to_gemtext = @items.map(&:to_gemtext).join.freeze
################## ##################
# Helper methods # # Helper methods #

View file

@ -25,6 +25,13 @@ module Repubmark
def word_count = @caption.word_count + (@sublist&.word_count || 0) 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 def to_html
[ [
"<li>\n", "<li>\n",

View file

@ -16,6 +16,8 @@ module Repubmark
def word_count = @caption.word_count def word_count = @caption.word_count
def to_summary_plain = @caption.to_summary_plain
def to_html = "<p>\n#{@caption.to_html}</p>\n".freeze def to_html = "<p>\n#{@caption.to_html}</p>\n".freeze
def to_gemtext = "#{@caption.to_gemtext}\n".freeze def to_gemtext = "#{@caption.to_gemtext}\n".freeze

View file

@ -18,6 +18,10 @@ module Repubmark
def word_count = @items.sum(&:word_count) def word_count = @items.sum(&:word_count)
def to_summary_plain
"«#{@items.map(&:to_summary_plain).join(' ')}»".freeze
end
def to_html = "&laquo;#{@items.map(&:to_html).join("\n")}&raquo;".freeze def to_html = "&laquo;#{@items.map(&:to_html).join("\n")}&raquo;".freeze
def to_gemtext = "«#{@items.map(&:to_gemtext).join(' ')}»".freeze def to_gemtext = "«#{@items.map(&:to_gemtext).join(' ')}»".freeze

View file

@ -6,7 +6,7 @@ module Repubmark
parents :Joint parents :Joint
SECT_HTML = '&sect;' SECT_HTML = '&sect;'
SECT_GEMTEXT = '§' SECT_UNICODE = '§'
attr_reader :count, :text attr_reader :count, :text
@ -30,9 +30,11 @@ module Repubmark
def word_count = count_words @text 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_html = "#{SECT_HTML * count}#{text}".freeze
def to_gemtext = "#{SECT_GEMTEXT * count}#{text}".freeze def to_gemtext = "#{SECT_UNICODE * count}#{text}".freeze
private private

View file

@ -11,7 +11,7 @@ module Repubmark
mdash: '&mdash;', mdash: '&mdash;',
}.freeze }.freeze
GEMTEXT = { UNICODE = {
ellipsis: '…', ellipsis: '…',
mdash: '—', mdash: '—',
}.freeze }.freeze
@ -27,9 +27,11 @@ module Repubmark
# Basic methods # # Basic methods #
################# #################
def to_summary_plain = UNICODE.fetch @name
def to_html = HTML.fetch @name def to_html = HTML.fetch @name
def to_gemtext = GEMTEXT.fetch @name def to_gemtext = UNICODE.fetch @name
end end
end end
end end

View file

@ -20,6 +20,8 @@ module Repubmark
def word_count = count_words @str def word_count = count_words @str
def to_summary_plain = @str
def to_html = str_to_html def to_html = str_to_html
def to_gemtext = @str def to_gemtext = @str

View file

@ -9,6 +9,7 @@ EXAMPLES_GLOB = File.expand_path('examples/*.repub', __dir__).freeze
CONFIG = File.expand_path('examples/config.yml', __dir__).freeze CONFIG = File.expand_path('examples/config.yml', __dir__).freeze
PROFILES = [ PROFILES = [
%w[summary_plain summary.txt],
%w[http html], %w[http html],
%w[gemini gmi], %w[gemini gmi],
].map(&:freeze).freeze ].map(&:freeze).freeze