samhuri.net/pressa/lib/projects/plugin.rb

69 lines
1.8 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

require_relative '../plugin'
require_relative '../utils/file_writer'
require_relative '../views/layout'
require_relative '../views/projects_view'
require_relative '../views/project_view'
require_relative 'models'
module Pressa
module Projects
class Plugin < Pressa::Plugin
def initialize(projects: [])
@projects = projects
end
def setup(site:, source_path:)
end
def render(site:, target_path:)
write_projects_index(site:, target_path:)
@projects.each do |project|
write_project_page(project:, site:, target_path:)
end
end
private
def write_projects_index(site:, target_path:)
content_view = Views::ProjectsView.new(projects: @projects, site:)
html = render_layout(
site:,
page_title: "Projects #{site.title}",
canonical_url: site.url_for('/projects/'),
content: content_view
)
file_path = File.join(target_path, 'projects', 'index.html')
Utils::FileWriter.write(path: file_path, content: html)
end
def write_project_page(project:, site:, target_path:)
content_view = Views::ProjectView.new(project:, site:)
html = render_layout(
site:,
page_title: "#{project.title} #{site.title}",
canonical_url: site.url_for(project.path),
content: content_view
)
file_path = File.join(target_path, 'projects', project.name, 'index.html')
Utils::FileWriter.write(path: file_path, content: html)
end
def render_layout(site:, page_title:, canonical_url:, content:)
layout = Views::Layout.new(
site:,
page_title:,
canonical_url:
)
layout.call do
content.call
end
end
end
end
end