repubmark/lib/repubmark/elems/article.rb

102 lines
2.3 KiB
Ruby

# frozen_string_literal: true
module Repubmark
module Elems
class Article < Base
attr_reader :config
def initialize(config) # rubocop:disable Lint/MissingSuper
@parent = nil
self.config = config
end
#################
# Basic methods #
#################
def word_count
(@annotation&.word_count || 0) +
(@chapter&.word_count || 0)
end
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
###################
# 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 = CustomLogue.new self
yield @prologue
nil
end
def annotation
raise 'Annotation already exists' if @annotation
raise 'Annotation after chapters' if @chapter
raise 'Annotation after epilogue' if @epilogue
@annotation = Annotation.new self
yield @annotation
nil
end
def respond_to_missing?(method_name, _include_private)
chapter = @chapter || Chapter.new(self)
chapter.respond_to?(method_name) || super
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
@chapter.public_send(method_name, ...)
else
super
end
end
def epilogue
raise 'Epilogue already exists' if @epilogue
@epilogue = CustomLogue.new self
yield @epilogue
nil
end
private
def config=(config)
unless config.instance_of? Config
raise TypeError, "Expected #{Config}, got #{config.class}"
end
@config = config
end
end
end
end