Add class Repubmark::Elems::CustomBlock
This commit is contained in:
parent
c901f4070e
commit
d40150fbc4
|
@ -25,6 +25,7 @@ require_relative 'repubmark/elems/canvas'
|
|||
# Always inside Canvas
|
||||
require_relative 'repubmark/elems/blockquote'
|
||||
require_relative 'repubmark/elems/code_block'
|
||||
require_relative 'repubmark/elems/custom_block'
|
||||
require_relative 'repubmark/elems/figures'
|
||||
require_relative 'repubmark/elems/iframe'
|
||||
require_relative 'repubmark/elems/paragraph'
|
||||
|
|
|
@ -37,6 +37,13 @@ module Repubmark
|
|||
nil
|
||||
end
|
||||
|
||||
def custom
|
||||
custom_block = CustomBlock.new self
|
||||
@items << custom_block
|
||||
yield custom_block
|
||||
nil
|
||||
end
|
||||
|
||||
def iframe(*args, **kwargs)
|
||||
iframe = Iframe.new self, *args, **kwargs
|
||||
@items << iframe
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
# 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
|
Loading…
Reference in New Issue