Skip to content

Commit

Permalink
Update to use the scanners and new method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
waferbaby committed Jan 19, 2024
1 parent 6fe4911 commit cc76daa
Showing 1 changed file with 21 additions and 47 deletions.
68 changes: 21 additions & 47 deletions lib/dimples/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@ module Dimples
class Site
DEFAULT_CONFIG = { generation: { overwrite_directory: false }, paths: { posts: 'posts' } }.freeze

def self.generate(source_path, output_path, config)
new(source_path, output_path, config).generate
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, config)
@paths = {
source: File.expand_path(source_path || Dir.pwd),
destination: File.expand_path(output_path || File.join(Dir.pwd, 'site'))
}
def initialize(source_path:, output_path: nil, config: {})
set_paths(source_path: source_path, output_path: output_path || File.join(Dir.pwd, 'site'))

@config = DEFAULT_CONFIG
@config = @config.merge(config) if config.is_a?(Hash)
@config = config.merge(DEFAULT_CONFIG)

%w[pages posts static templates].each { |type| @paths[type.to_sym] = File.join(@paths[:source], type) }
@posts = Scanner::Posts.scan(@paths[:posts])
@pages = Scanner::Pages.scan(@paths[:pages])
@templates = Scanner::Templates.scan(@paths[:templates])
end

def set_paths(source_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) }

source_paths = %w[pages posts static templates].to_h do |type|
[type.to_sym, File.expand_path(File.join(@paths[:source], type))]
end

scan_posts
scan_pages
scan_templates
@paths.merge!(source_paths)
end

def generate
Expand All @@ -57,38 +63,6 @@ def prepare_output_directory
Dir.mkdir(@paths[:destination])
end

def read_files(path)
Dir[File.join(path, '**', '*.*')]
end

def scan_posts
@posts = read_files(@paths[:posts]).map { |path| Dimples::Post.new(path) }
@posts.sort_by!(&:date).reverse!

@categories = {}

@posts.each do |post|
post.categories&.each do |category|
@categories[category] ||= []
@categories[category] << post
end
end
end

def scan_pages
@pages = read_files(@paths[:pages]).map { |path| Dimples::Page.new(path) }
end

def scan_templates
@templates =
{}.tap do |templates|
read_files(@paths[:templates]).each do |path|
key = File.basename(path, '.erb')
templates[key] = Dimples::Template.new(path)
end
end
end

def write_file(path, content)
directory_path = File.dirname(path)

Expand All @@ -100,7 +74,7 @@ def generate_paginated_posts(posts, path, context = {})
pager = Dimples::Pager.new("#{path.sub(@paths[:destination], '')}/", posts)

pager.each do |index|
page = Dimples::Page.new(nil, layout: 'posts')
page = Dimples::Page.new(metadata: { layout: 'posts' })
page_path = index == 1 ? path : File.join(path, "page_#{index}")

write_file(
Expand Down Expand Up @@ -146,7 +120,7 @@ def generate_categories
end

def generate_feed(posts, path)
page = Dimples::Page.new(nil, layout: 'feed')
page = Dimples::Page.new(metadata: { layout: 'feed' })
write_file(File.join(path, 'feed.atom'), render(page, posts: posts))
end

Expand Down

0 comments on commit cc76daa

Please sign in to comment.