56 lines
1.1 KiB
Ruby
56 lines
1.1 KiB
Ruby
require "rdoc"
|
|
require_relative "../core_ext"
|
|
require_relative "../utils/string_short_name"
|
|
|
|
class Blog::Post
|
|
using StringShortname
|
|
|
|
def self.get_by(date: nil, topic: nil)
|
|
date ? get_by_date(date) : get_by_topic(topic)
|
|
end
|
|
|
|
def self.date_to_filepath(date)
|
|
"blog/posts/dates/#{date}".slug_to_file
|
|
end
|
|
|
|
def self.get_by_date(date)
|
|
path = date_to_filepath(date)
|
|
h1 = date.file_to_slug
|
|
body = File.read(path)
|
|
new(type: :date, h1:, body:, path:)
|
|
end
|
|
|
|
def self.topic_to_filepath(topic)
|
|
"blog/posts/topics/#{topic}".slug_to_file
|
|
end
|
|
|
|
def self.get_by_topic(topic)
|
|
path = topic_to_filepath(topic)
|
|
lines = File.readlines(path)
|
|
if !lines.empty?
|
|
h1 = lines[0][2..]
|
|
body = lines[2..].join("\n")
|
|
end
|
|
new(type: :topic, h1:, body:, path:)
|
|
end
|
|
|
|
attr_reader :h1, :body, :path
|
|
|
|
def initialize(type:, h1:, body:, path:)
|
|
@type = type
|
|
@h1 = h1
|
|
@body = body
|
|
@path = path
|
|
end
|
|
|
|
def body_to_html
|
|
data = body
|
|
formatter = RDoc::Markup::ToHtml.new(RDoc::Options.new, nil)
|
|
RDoc::Markdown.parse(data).accept(formatter)
|
|
end
|
|
|
|
def url
|
|
"/#{@type}/#{h1.slugify}"
|
|
end
|
|
end
|