From ddf7f766bc226bd294a9f90bfda47f19ac14ba4d Mon Sep 17 00:00:00 2001 From: Daniel Bogan Date: Sun, 21 Jan 2024 04:05:12 +0000 Subject: [PATCH] Simplify the code --- lib/dimples/site.rb | 94 +++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 59 deletions(-) diff --git a/lib/dimples/site.rb b/lib/dimples/site.rb index e9bdce6..29102b0 100644 --- a/lib/dimples/site.rb +++ b/lib/dimples/site.rb @@ -1,36 +1,36 @@ # frozen_string_literal: true -require_relative 'pager' - require 'fileutils' -require 'tilt' require 'date' module Dimples - # A + # A class representing a single site class Site DEFAULT_CONFIG = { generation: { overwrite_directory: false }, paths: { posts: 'posts' } }.freeze + attr_accessor :paths, :posts, :pages, :templates, :config + def self.generate(source_path:, output_path:, config:) new(source_path: source_path, output_path: output_path, config: config).generate end - attr_accessor :posts, :pages, :categories, :config - def initialize(source_path:, output_path: nil, config: {}) set_paths(source_path: source_path, output_path: output_path || File.join(Dir.pwd, 'site')) - @config = config.merge(DEFAULT_CONFIG) + @config = DEFAULT_CONFIG.merge(config) - @posts = Scanner::Posts.scan(@paths[:posts]) - @pages = Scanner::Pages.scan(@paths[:pages]) - @templates = Scanner::Templates.scan(@paths[:templates]) + @posts = Scanner::Posts.scan(site: self) + @pages = Scanner::Pages.scan(site: self) + @templates = Scanner::Templates.scan(site: self) end def set_paths(source_path:, output_path:) + source_path = File.expand_path(source_path) + destination_path = File.expand_path(output_path) + raise Error, 'Source path must be a valid directory' unless source_path && File.exist?(source_path) - @paths = { source: File.expand_path(source_path), destination: File.expand_path(output_path) } + @paths = { source: source_path, destination: destination_path } source_paths = %w[pages posts static templates].to_h do |type| [type.to_sym, File.expand_path(File.join(@paths[:source], type))] @@ -49,6 +49,17 @@ def generate copy_assets end + def categories + {}.tap do |categories| + @posts.each do |post| + post.categories&.each do |category| + categories[category] ||= [] + categories[category] << post + end + end + end + end + private def prepare_output_directory @@ -63,78 +74,43 @@ def prepare_output_directory Dir.mkdir(@paths[:destination]) end - def write_file(path, content) - directory_path = File.dirname(path) - - FileUtils.mkdir_p(directory_path) - File.write(path, content) - end - - def generate_paginated_posts(posts, path, context = {}) - pager = Dimples::Pager.new("#{path.sub(@paths[:destination], '')}/", posts) - - pager.each do |index| - page = Dimples::Page.new(metadata: { layout: 'posts' }) - page_path = index == 1 ? path : File.join(path, "page_#{index}") - - write_file( - File.join(page_path, page.filename), - render(page, context.merge!(pagination: pager.to_context)) - ) - end - end - def generate_posts directory_path = File.join(@paths[:destination], @config.dig(:paths, :posts)) - Dir.mkdir(directory_path) @posts.each do |post| - path = File.join(directory_path, post.slug, post.filename) - write_file(path, render(post, post: post)) + post.write(path: File.join(directory_path, post.slug), metadata: { post: post }) end - generate_paginated_posts(@posts, directory_path) - generate_feed(@posts.slice(0, 10), @paths[:destination]) + Dimples::Pager.paginate(site: self, output_path: directory_path, posts: @posts) + Dimples::Feed.new(site: self).write(path: @paths[:destination], metadata: { posts: @posts.slice(0, 10) }) end def generate_pages @pages.each do |page| - path = - if page.path - File.dirname(page.path).sub(@paths[:pages], @paths[:destination]) - else - @paths[:destination] - end + path = if page.path + File.dirname(page.path).sub(@paths[:pages], @paths[:destination]) + else + @paths[:destination] + end - write_file(File.join(path, page.filename), render(page, page: page)) + page.write(path: path, metadata: { page: page }) end end def generate_categories - @categories.each do |category, posts| + categories.each do |category, posts| category_path = File.join(@paths[:destination], 'categories', category) + metadata = { category: category } - generate_paginated_posts(posts, category_path, category: category) - generate_feed(posts.slice(0, 10), category_path) + Dimples::Pager.paginate(site: self, output_path: category_path, posts: posts, metadata: metadata) + Dimples::Feed.new(site: self).write(path: category_path, metadata: { posts: posts.slice(0, 10) }) end end - def generate_feed(posts, path) - page = Dimples::Page.new(metadata: { layout: 'feed' }) - write_file(File.join(path, 'feed.atom'), render(page, posts: posts)) - end - def copy_assets return unless Dir.exist?(@paths[:static]) FileUtils.cp_r(File.join(@paths[:static], '.'), @paths[:destination]) end - - def render(object, context = {}, content = nil) - context[:site] ||= self - - output = object.render(context, content) - render(@templates[object.layout], context, output) if object.layout && @templates[object.layout] - end end end