Add class Repubmark::Elems::CustomLogue

This commit is contained in:
Alex Kotov 2024-02-24 08:02:58 +04:00
parent c901f4070e
commit a786c6d123
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 70 additions and 1 deletions

View File

@ -17,6 +17,7 @@ require_relative 'repubmark/elems/article'
# Always inside Article
require_relative 'repubmark/elems/annotation'
require_relative 'repubmark/elems/custom_logue'
# Always inside Article, Chapter
require_relative 'repubmark/elems/chapter'
# Always inside Annotation, Blockquote, Chapter

View File

@ -21,15 +21,19 @@ module Repubmark
def to_html
[
@prologue&.to_html,
@annotation&.to_html,
@chapter&.to_html,
@epilogue&.to_html,
].compact.join.freeze
end
def to_gemtext
[
@prologue&.to_gemtext,
@annotation&.to_gemtext,
@chapter&.to_gemtext,
@epilogue&.to_gemtext,
].compact.join("\n\n\n").freeze
end
@ -37,9 +41,21 @@ module Repubmark
# Builder methods #
###################
def prologue
raise 'Prologue already exists' if @prologue
raise 'Prologue after annotation' if @annotation
raise 'Prologue after chapters' if @chapters
raise 'Prologue after epilogue' if @epilogue
@prologue = CustomBlock.new self
yield @prologue
nil
end
def annotation
raise 'Annotation already exists' if @annotation
raise 'Annotation after chapter' if @chapter
raise 'Annotation after chapters' if @chapter
raise 'Annotation after epilogue' if @epilogue
@annotation = Annotation.new self
yield @annotation
@ -52,6 +68,8 @@ module Repubmark
end
def method_missing(method_name, ...)
raise 'Chapters after chapters' if @epilogue
chapter = @chapter || Chapter.new(self)
if chapter.respond_to? method_name
@chapter = chapter
@ -61,6 +79,14 @@ module Repubmark
end
end
def epilogue
raise 'Epilogue already exists' if @epilogue
@epilogue = CustomBlock.new self
yield @epilogue
nil
end
private
def config=(config)

View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
module Repubmark
module Elems
class CustomLogue < 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