103 lines
2.2 KiB
Ruby
103 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Repubmark
|
|
module Elems
|
|
class Base
|
|
attr_reader :parent
|
|
|
|
def initialize(parent)
|
|
self.parent = parent
|
|
end
|
|
|
|
#################
|
|
# Basic methods #
|
|
#################
|
|
|
|
def word_count = 0
|
|
|
|
def to_html
|
|
raise NotImplementedError, "#{self.class}#to_html"
|
|
end
|
|
|
|
def to_gemtext
|
|
raise NotImplementedError, "#{self.class}#to_gemtext"
|
|
end
|
|
|
|
##################
|
|
# Helper methods #
|
|
##################
|
|
|
|
def config = parent.config
|
|
|
|
class << self
|
|
def parents(*args)
|
|
if @parents
|
|
raise ArgumentError, 'Invalid args' unless args.empty?
|
|
|
|
return @parents
|
|
end
|
|
|
|
@parents = args.map { |arg| "#{Elems}::#{arg}".freeze }.to_a.freeze
|
|
nil
|
|
end
|
|
|
|
def parent?(klass)
|
|
unless klass.instance_of? Class
|
|
raise TypeError, "Expected #{Class}, got #{klass.class}"
|
|
end
|
|
|
|
parents.include? klass.name
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def parent=(parent)
|
|
unless parent.is_a? Base
|
|
raise TypeError, "Expected #{Base}, got #{parent.class}"
|
|
end
|
|
|
|
unless self.class.parent? parent.class
|
|
raise TypeError,
|
|
"Expected #{self.class.parents.join(', ')}, got #{parent.class}"
|
|
end
|
|
|
|
@parent = parent
|
|
end
|
|
|
|
def own_url(path)
|
|
config[:relative_urls] ? relative_url(path) : absolute_url(path)
|
|
end
|
|
|
|
def absolute_url(path)
|
|
base_url = String(config[:base_url]).strip.freeze
|
|
raise 'Invalid base URL' if base_url.empty?
|
|
|
|
path = "/#{path}" unless path.start_with? '/'
|
|
"#{base_url}#{path}"
|
|
end
|
|
|
|
def relative_url(path)
|
|
current_path = String(config[:current_path]).strip.freeze
|
|
raise 'Invalid current path URL' if current_path.empty?
|
|
|
|
Pathname
|
|
.new(path)
|
|
.relative_path_from("/#{current_path}/..")
|
|
.to_s
|
|
.freeze
|
|
end
|
|
|
|
def html_class(key)
|
|
if (value = config[:"css_class_#{key}"])
|
|
%( class="#{value}").freeze
|
|
else
|
|
''
|
|
end
|
|
end
|
|
|
|
def count_words(str) = str.split.count
|
|
end
|
|
end
|
|
end
|