repubmark/lib/repubmark/elems/figure.rb

74 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Repubmark
module Elems
class Figure < Base
parents :Canvas, :Figures
def initialize(parent, name, alt)
super parent
alt = String(alt).strip.split.join(' ').freeze
raise 'Empty alt text' if alt.empty?
@name = String(name).strip.freeze
@alt = alt
@caption = Caption.new self
end
#################
# Basic methods #
#################
def to_html
[
"<div#{html_class(:figure_wrap)}>\n",
"<figure#{html_class(:figure_self)}>\n",
"<img src=\"#{src}\" alt=\"#{CGI.escape_html(@alt)}\"/>\n",
"<figcaption>\n",
caption_html,
"</figcaption>\n",
"</figure>\n",
"</div>\n",
].join.freeze
end
def to_gemtext
caption = @caption.to_gemtext
caption = @alt if caption.empty?
"=> #{src} #{caption}\n".freeze
end
###################
# Builder methods #
###################
def respond_to_missing?(method_name, _include_private)
@caption.respond_to?(method_name) || super
end
def method_missing(method_name, ...)
if @caption.respond_to? method_name
@caption.public_send(method_name, ...)
else
super
end
end
private
def src
@src ||= own_url "#{config[:images_prefix]}/#@name"
end
def caption_html
if @caption.empty?
"#{CGI.escape_html(@alt)}\n"
else
@caption.to_html
end
end
end
end
end