class Bridgetown::Page
TODO: to be retired once the Resource
engine is made official
Constants
- HTML_EXTENSIONS
A set of extensions that are considered HTML or HTML-like so we should not alter them, this includes .xhtml through XHTM5.
Attributes
Public Class Methods
Initialize a new Page
.
site - The Site
object. base - The String path to the source. dir - The String path between the source and the file. name - The String filename of the file. from_plugin - true if the Page
file is located in a Gem-based plugin folder rubocop:disable Metrics/ParameterLists
# File lib/bridgetown-core/page.rb, line 36 def initialize(site, base, dir, name, from_plugin: false) @site = site @base = base @dir = dir @name = name @path = if from_plugin File.join(base, dir, name) else site.in_source_dir(base, dir, name) end process(name) read_yaml(File.join(base, dir), name) data.default_proc = proc do |_, key| site.frontmatter_defaults.find(relative_path, type, key.to_s) end Bridgetown::Hooks.trigger :pages, :post_init, self end
Public Instance Methods
Obtain destination path.
dest - The String path to the destination dir.
Returns the destination file path String.
# File lib/bridgetown-core/page.rb, line 176 def destination(dest) path = site.in_dest_dir(dest, URL.unescape_path(url)) path = File.join(path, "index") if url.end_with?("/") path << output_ext unless path.end_with? output_ext path end
The generated directory into which the page will be placed upon generation. This is derived from the permalink or, if permalink is absent, will be '/'
Returns the String destination directory.
# File lib/bridgetown-core/page.rb, line 63 def dir if url.end_with?("/") url else url_dir = File.dirname(url) url_dir.end_with?("/") ? url_dir : "#{url_dir}/" end end
Returns the Boolean of whether this Page
is HTML or not.
# File lib/bridgetown-core/page.rb, line 202 def html? HTML_EXTENSIONS.include?(output_ext) end
Returns the Boolean of whether this Page
is an index file or not.
# File lib/bridgetown-core/page.rb, line 207 def index? basename == "index" end
Returns the object as a debug String.
# File lib/bridgetown-core/page.rb, line 197 def inspect "#<#{self.class} #{relative_path}>" end
# File lib/bridgetown-core/page.rb, line 72 def liquid_drop @liquid_drop ||= begin defaults = site.frontmatter_defaults.all(relative_path, type) unless defaults.empty? Utils.deep_merge_hashes!(data, Utils.deep_merge_hashes!(defaults, data)) end Drops::PageDrop.new(self) end end
FIXME: spinning up a new Renderer
object just to get an extension seems excessive
The output extension of the page.
Returns the output extension
# File lib/bridgetown-core/page.rb, line 167 def output_ext @output_ext ||= Bridgetown::Renderer.new(site, self).output_ext end
The path to the source file
Returns the path to the source file
# File lib/bridgetown-core/page.rb, line 152 def path data.fetch("path") { relative_path } end
The full path and filename of the post. Defined in the YAML of the post body.
Returns the String permalink or nil if none has been set.
# File lib/bridgetown-core/page.rb, line 93 def permalink data.nil? ? nil : data["permalink"] end
Extract information from the page filename.
name - The String filename of the page file.
NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`) Returns nothing.
# File lib/bridgetown-core/page.rb, line 144 def process(name) self.ext = File.extname(name) self.basename = name[0..-ext.length - 1].gsub(%r!\.*\z!, "") end
Strips _pages prefix off if needed for the url/destination generation @return [String]
# File lib/bridgetown-core/page.rb, line 134 def qualified_pages_path_for_url @dir.sub(%r!^/_pages!, "") end
The path to the page source file, relative to the site source
# File lib/bridgetown-core/page.rb, line 157 def relative_path @relative_path ||= File.join(*[@dir, @name].map(&:to_s).reject(&:empty?)).delete_prefix("/") end
The template of the permalink.
Returns the template String.
# File lib/bridgetown-core/page.rb, line 100 def template if !html? "/:path/:basename:output_ext" elsif index? "/:path/" else Utils.add_permalink_suffix("/:path/:basename", site.permalink_style) end end
Public
Liquid representation of current page
# File lib/bridgetown-core/page.rb, line 85 def to_liquid liquid_drop end
# File lib/bridgetown-core/page.rb, line 211 def trigger_hooks(hook_name, *args) Bridgetown::Hooks.trigger :pages, hook_name, self, *args end
# File lib/bridgetown-core/page.rb, line 215 def type :pages end
The generated relative url of this page. e.g. /about.html.
Returns the String url.
# File lib/bridgetown-core/page.rb, line 113 def url @url ||= URL.new( template: template, placeholders: url_placeholders, permalink: permalink ).to_s end
Returns a hash of URL
placeholder names (as symbols) mapping to the desired placeholder replacements. For details see “url.rb”
# File lib/bridgetown-core/page.rb, line 124 def url_placeholders { path: qualified_pages_path_for_url, basename: basename, output_ext: output_ext, } end
Write the generated page file to the destination directory.
dest - The String path to the destination dir.
Returns nothing.
# File lib/bridgetown-core/page.rb, line 188 def write(dest) path = destination(dest) FileUtils.mkdir_p(File.dirname(path)) Bridgetown.logger.debug "Writing:", path File.write(path, output, mode: "wb") Bridgetown::Hooks.trigger :pages, :post_write, self end
# File lib/bridgetown-core/page.rb, line 219 def write? true end