repubmark/lib/repubmark/elems/blockquote.rb

81 lines
1.7 KiB
Ruby

# frozen_string_literal: true
module Repubmark
module Elems
class Blockquote < Base
parents :Canvas
def initialize(parent)
super parent
@canvas = Canvas.new self
@caption = nil
end
#################
# Basic methods #
#################
def word_count = @canvas.word_count + (@caption&.word_count || 0)
def to_html
[
"<figure#{html_class(:blockquote_figure)}>\n",
"<blockquote#{html_class(:blockquote_blockquote)}>\n",
@canvas.to_html,
"</blockquote>\n",
caption_html,
"</figure>\n",
].join.freeze
end
def to_gemtext
[
@canvas.to_gemtext.split("\n").map { |s| "> #{s}\n" }.join,
caption_gemtext,
].join.freeze
end
###################
# Builder methods #
###################
def caption
@caption = Caption.new self
yield @caption
nil
end
def respond_to_missing?(method_name, _include_private)
@canvas.respond_to?(method_name) || super
end
def method_missing(method_name, ...)
if @canvas.respond_to? method_name
raise 'Paragraph after caption' unless @caption.nil?
@canvas.public_send(method_name, ...)
else
super
end
end
private
def caption_html
return if @caption.nil?
[
"<figcaption#{html_class(:blockquote_figcaption)}>\n",
@caption.to_html,
"</figcaption>\n",
].join.freeze
end
def caption_gemtext
"#{@caption.to_gemtext}\n".freeze unless @caption.nil?
end
end
end
end