mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-24 14:27:41 +00:00
159 lines
4.7 KiB
Ruby
159 lines
4.7 KiB
Ruby
require "pressa/utils/file_writer"
|
|
require "pressa/utils/gemtext_renderer"
|
|
|
|
module Pressa
|
|
module Posts
|
|
class GeminiWriter
|
|
RECENT_POSTS_LIMIT = 20
|
|
|
|
def initialize(site:, posts_by_year:)
|
|
@site = site
|
|
@posts_by_year = posts_by_year
|
|
end
|
|
|
|
def write_posts(target_path:)
|
|
@posts_by_year.all_posts.each do |post|
|
|
write_post(post:, target_path:)
|
|
end
|
|
end
|
|
|
|
def write_recent_posts(target_path:, limit: RECENT_POSTS_LIMIT)
|
|
rows = ["# #{@site.title}", ""]
|
|
home_links.each do |link|
|
|
rows << "=> #{link.href}"
|
|
end
|
|
rows << "" unless home_links.empty?
|
|
rows << "Recent posts"
|
|
rows << ""
|
|
|
|
@posts_by_year.recent_posts(limit).each do |post|
|
|
rows << post_link_line(post)
|
|
end
|
|
|
|
rows << ""
|
|
rows << "=> /posts/ Archive"
|
|
rows << "=> #{web_url_for("/")} Website"
|
|
rows << ""
|
|
|
|
file_path = File.join(target_path, "index.gmi")
|
|
Utils::FileWriter.write(path: file_path, content: rows.join("\n"))
|
|
end
|
|
|
|
def write_archive(target_path:)
|
|
rows = ["# Archive", ""]
|
|
|
|
@posts_by_year.sorted_years.each do |year|
|
|
rows << "=> /posts/#{year}/ #{year}"
|
|
end
|
|
|
|
rows << ""
|
|
rows << "=> / Home"
|
|
rows << "=> #{web_url_for("/posts/")} Read on the web"
|
|
rows << ""
|
|
|
|
file_path = File.join(target_path, "posts", "index.gmi")
|
|
Utils::FileWriter.write(path: file_path, content: rows.join("\n"))
|
|
end
|
|
|
|
def write_year_indexes(target_path:)
|
|
@posts_by_year.sorted_years.each do |year|
|
|
year_posts = @posts_by_year.by_year[year]
|
|
write_year_index(year:, year_posts:, target_path:)
|
|
end
|
|
end
|
|
|
|
def write_month_rollups(target_path:)
|
|
@posts_by_year.by_year.each do |year, year_posts|
|
|
year_posts.sorted_months.each do |month_posts|
|
|
write_month_rollup(year:, month_posts:, target_path:)
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def write_post(post:, target_path:)
|
|
rows = ["# #{post.title}", "", "#{post.formatted_date} by #{post.author}", ""]
|
|
|
|
if post.link_post?
|
|
rows << "=> #{post.link}"
|
|
rows << ""
|
|
end
|
|
|
|
gemtext_body = Utils::GemtextRenderer.render(post.markdown_body)
|
|
rows << gemtext_body unless gemtext_body.empty?
|
|
rows << "" unless rows.last.to_s.empty?
|
|
|
|
rows << "=> /posts/ Back to archive"
|
|
rows << "=> #{web_url_for("#{post.path}/")} Read on the web" if include_web_link?(post)
|
|
rows << ""
|
|
|
|
file_path = File.join(target_path, post.path.sub(%r{^/}, ""), "index.gmi")
|
|
Utils::FileWriter.write(path: file_path, content: rows.join("\n"))
|
|
end
|
|
|
|
def write_year_index(year:, year_posts:, target_path:)
|
|
rows = ["# #{year}", ""]
|
|
|
|
year_posts.sorted_months.each do |month_posts|
|
|
month = month_posts.month
|
|
rows << "## #{month.name}"
|
|
month_posts.sorted_posts.each do |post|
|
|
rows.concat(post_archive_lines(post))
|
|
end
|
|
rows << ""
|
|
end
|
|
|
|
rows << "=> /posts/ Back to archive"
|
|
rows << "=> #{web_url_for("/posts/#{year}/")} Read on the web"
|
|
rows << ""
|
|
|
|
file_path = File.join(target_path, "posts", year.to_s, "index.gmi")
|
|
Utils::FileWriter.write(path: file_path, content: rows.join("\n"))
|
|
end
|
|
|
|
def write_month_rollup(year:, month_posts:, target_path:)
|
|
month = month_posts.month
|
|
rows = ["# #{month.name} #{year}", ""]
|
|
|
|
month_posts.sorted_posts.each do |post|
|
|
rows.concat(post_archive_lines(post))
|
|
end
|
|
|
|
rows << ""
|
|
rows << "=> /posts/#{year}/ Back to year"
|
|
rows << "=> /posts/ Back to archive"
|
|
rows << "=> #{web_url_for("/posts/#{year}/#{month.padded}/")} Read on the web"
|
|
rows << ""
|
|
|
|
file_path = File.join(target_path, "posts", year.to_s, month.padded, "index.gmi")
|
|
Utils::FileWriter.write(path: file_path, content: rows.join("\n"))
|
|
end
|
|
|
|
def post_link_line(post)
|
|
"=> #{post.path}/ #{post.date.strftime("%Y-%m-%d")} - #{post.title}"
|
|
end
|
|
|
|
def post_archive_lines(post)
|
|
rows = [post_link_line(post)]
|
|
rows << "=> #{post.link}" if post.link_post?
|
|
rows
|
|
end
|
|
|
|
def include_web_link?(post)
|
|
markdown_without_fences = post.markdown_body.gsub(/```.*?```/m, "")
|
|
markdown_without_fences.match?(
|
|
%r{<\s*(?:a|p|div|span|ul|ol|li|audio|video|source|img|h[1-6]|blockquote|pre|code|table|tr|td|th|em|strong|br)\b}i
|
|
)
|
|
end
|
|
|
|
def web_url_for(path)
|
|
@site.url_for(path)
|
|
end
|
|
|
|
def home_links
|
|
@site.gemini_output_options&.home_links || []
|
|
end
|
|
end
|
|
end
|
|
end
|