repubmark/lib/repubmark/elems/custom_block.rb

43 lines
855 B
Ruby

# frozen_string_literal: true
module Repubmark
module Elems
class CustomBlock < Base
FORMATS = %i[html gemtext].freeze
parents :Canvas
def initialize(parent)
super parent
@for = {}
end
#################
# Basic methods #
#################
def to_html
"<div>\n#{@for[:html].strip}\n</div>\n".freeze if @for[:html]
end
def to_gemtext
"#{@for[:gemtext].strip}\n".freeze if @for[:gemtext]
end
###################
# Builder methods #
###################
def for(format, str)
raise 'Invalid format' unless FORMATS.include? format
raise 'Format already configured' if @for.key? format
str = String(str).strip.freeze
return if str.empty?
@for[format] = str
end
end
end
end