repubmark/lib/repubmark/elems/section.rb

57 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Repubmark
module Elems
class Section < Base
parents :Joint
SECT_HTML = '&sect;'
SECT_GEMTEXT = '§'
attr_reader :count, :text
def initialize(parent, *args)
super parent
case args.length
when 1
self.count = 1
self.text = args[0]
when 2
self.count, self.text = args
else
raise ArgumentError, 'Expected 1 or 2 arguments'
end
end
#################
# Basic methods #
#################
def word_count = count_words @text
def to_html = "#{SECT_HTML * count}#{text}".freeze
def to_gemtext = "#{SECT_GEMTEXT * count}#{text}".freeze
private
def count=(count)
unless count.instance_of? Integer
raise TypeError, "Expected #{Integer}, got #{count.class}"
end
raise 'Expected positive count' unless count.positive?
@count = count
end
def text=(text)
text = String(text).strip.freeze
raise 'Expected non-empty text' if text.empty?
@text = text
end
end
end
end