From 48c123e34612019737ffe6f33aba47681227bfe9 Mon Sep 17 00:00:00 2001 From: Daniel Bogan Date: Sat, 27 Jan 2024 08:45:41 +0000 Subject: [PATCH] Refactor the page to be the base class --- lib/dimples/page.rb | 57 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/dimples/page.rb b/lib/dimples/page.rb index 0f7c792..f6dd75c 100644 --- a/lib/dimples/page.rb +++ b/lib/dimples/page.rb @@ -1,8 +1,59 @@ # frozen_string_literal: true -require_relative 'document' - module Dimples - class Page < Document + # A single page on a site. + class Page + attr_accessor :metadata, :contents, :path, :rendered_contents + + def initialize(path, config) + @path = File.expand_path(path) + @config = config + @metadata, @contents = Dimples::FrontMatter.parse(File.read(@path)) + end + + def filename + "#{basename}.#{extension}" + end + + def basename + @metadata.fetch(:filename, 'index') + end + + def extension + @metadata.fetch(:extension, 'html') + end + + def layout + @metadata.fetch(:layout, nil) + end + + def slug + @metadata.fetch(:slug, File.basename(@path, File.extname(@path))) + end + + def template + @template ||= template_class.new(nil, 1, template_options) { @contents } + end + + def output_directory + @output_directory ||= File.dirname(@path).gsub(@config[:sources][:pages], '') << '/' + end + + def url + @url ||= String.new.tap do |url| + url << output_directory unless output_directory == '/' + url << ('/' + filename) unless basename == 'index' + end + end + + private + + def template_class + Tilt::ErbTemplate + end + + def template_options + {} + end end end