Class: Plate::Site
Overview
This class contains everything you’ll want to know about a site. It contains all data about the site, including blog posts, content pages, static files, assets and anything else.
Instance Attribute Summary collapse
-
#assets ⇒ Object
- Array
-
An array of all dynamic assets for this site.
-
#build_destination ⇒ Object
- String
-
The directory where the built site will be created for this site.
-
#cache_location ⇒ Object
- String
-
The file path of the cache directory for this site.
-
#destination ⇒ Object
- String
-
The destination directory file path for this site.
-
#drafts ⇒ Object
- Array
-
Any posts marked as drafts for this site.
-
#layouts ⇒ Object
- Array
-
An array of all layout classes used for this site.
-
#logger ⇒ Object
- Object
-
The logger instance for this site.
-
#metadata ⇒ Object
- Hash
-
A hash of all default meta data options for this site.
-
#options ⇒ Object
- Hash
-
A hash of configuration options for this site.
-
#pages ⇒ Object
- Array
-
An array of all non-blog post pages for this site.
-
#partials ⇒ Object
- Array
-
An array of all view partials available in this site.
-
#posts ⇒ Object
- PostCollection
-
All blog posts for this site.
-
#source ⇒ Object
- String
-
The source directory file path for building this site.
Instance Method Summary collapse
- #all_files ⇒ Object
-
#asset_engine_extensions ⇒ Object
All extensions that are registered, as strings.
-
#categories ⇒ Object
Alphabetical list of all blog post categories used.
-
#clear ⇒ Object
Clear out all data related to this site.
-
#default_category ⇒ Object
The default blog post category.
-
#default_layout ⇒ Object
The default layout for all pages that do not specifically name their own.
-
#find(search_path) ⇒ Object
Find a page, asset or layout by source relative file path.
-
#find_by_extension(extension) ⇒ Object
Find all registered files by the given file extension.
-
#find_by_layout(layout_name) ⇒ Object
Find all pages and posts with this layout.
-
#find_layout(layout_name) ⇒ Object
Find a specific layout by its file name.
-
#initialize(source, destination, options = {}) ⇒ Site
constructor
A new instance of Site.
- #inspect ⇒ Object
-
#load! ⇒ Object
Load all data from the various source directories.
-
#loaded? ⇒ Boolean
Returns true if the site has been loaded from the source directories.
-
#log(message, style = :indent) ⇒ Object
Write to the log if enable_logging is enabled.
-
#meta ⇒ Object
Access to read all meta data for this site.
-
#meta=(hash) ⇒ Object
Set the meta data hash object for this site.
- #page_engine_extensions ⇒ Object
-
#registered_asset_engines ⇒ Object
Returns the asset engines that are available for use.
-
#registered_page_engines ⇒ Object
Returns the engines available for use in page and layout formatting.
- #relative_path(file_or_directory) ⇒ Object
- #reload! ⇒ Object
-
#tags ⇒ Object
All tags used on this site.
- #to_url(str) ⇒ Object (also: #sanitize_slug)
-
#url ⇒ Object
The base URL for this site.
Methods included from Callbacks
Constructor Details
#initialize(source, destination, options = {}) ⇒ Site
Returns a new instance of Site.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/plate/site.rb', line 48 def initialize(source, destination, = {}) # Setup source and destination for the site files self.source = source self.destination = destination # By default, the build goes into the destination folder. # Override this to output to a different folder by default self.build_destination = destination # Sanitize options self. = Hash === ? .clone : {} self..symbolize_keys! clear end |
Instance Attribute Details
#assets ⇒ Object
- Array
-
An array of all dynamic assets for this site
10 11 12 |
# File 'lib/plate/site.rb', line 10 def assets @assets end |
#build_destination ⇒ Object
- String
-
The directory where the built site will be created for this site.
13 14 15 |
# File 'lib/plate/site.rb', line 13 def build_destination @build_destination end |
#cache_location ⇒ Object
- String
-
The file path of the cache directory for this site.
16 17 18 |
# File 'lib/plate/site.rb', line 16 def cache_location @cache_location end |
#destination ⇒ Object
- String
-
The destination directory file path for this site
19 20 21 |
# File 'lib/plate/site.rb', line 19 def destination @destination end |
#drafts ⇒ Object
- Array
-
Any posts marked as drafts for this site
22 23 24 |
# File 'lib/plate/site.rb', line 22 def drafts @drafts end |
#layouts ⇒ Object
- Array
-
An array of all layout classes used for this site
25 26 27 |
# File 'lib/plate/site.rb', line 25 def layouts @layouts end |
#logger ⇒ Object
- Object
-
The logger instance for this site.
28 29 30 |
# File 'lib/plate/site.rb', line 28 def logger @logger end |
#metadata ⇒ Object
- Hash
-
A hash of all default meta data options for this site.
31 32 33 |
# File 'lib/plate/site.rb', line 31 def @metadata end |
#options ⇒ Object
- Hash
-
A hash of configuration options for this site
34 35 36 |
# File 'lib/plate/site.rb', line 34 def @options end |
#pages ⇒ Object
- Array
-
An array of all non-blog post pages for this site
37 38 39 |
# File 'lib/plate/site.rb', line 37 def pages @pages end |
#partials ⇒ Object
- Array
-
An array of all view partials available in this site.
40 41 42 |
# File 'lib/plate/site.rb', line 40 def partials @partials end |
#posts ⇒ Object
- PostCollection
-
All blog posts for this site
43 44 45 |
# File 'lib/plate/site.rb', line 43 def posts @posts end |
#source ⇒ Object
- String
-
The source directory file path for building this site
46 47 48 |
# File 'lib/plate/site.rb', line 46 def source @source end |
Instance Method Details
#all_files ⇒ Object
68 69 70 |
# File 'lib/plate/site.rb', line 68 def all_files @all_files ||= self.assets + self.layouts + self.pages + self.posts.to_a + self.drafts end |
#asset_engine_extensions ⇒ Object
All extensions that are registered, as strings.
73 74 75 |
# File 'lib/plate/site.rb', line 73 def asset_engine_extensions @asset_engine_extensions ||= self.registered_asset_engines.keys.collect { |e| ".#{e}" } end |
#categories ⇒ Object
Alphabetical list of all blog post categories used.
78 79 80 |
# File 'lib/plate/site.rb', line 78 def categories @categories ||= self.posts.collect(&:category).uniq.sort end |
#clear ⇒ Object
Clear out all data related to this site. Prepare for a reload, or first time load.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/plate/site.rb', line 83 def clear @loaded = false @tags_counts = nil @default_layout = nil self.assets = [] self.layouts = [] self.pages = [] self.posts = PostCollection.new self.partials = [] self.drafts = [] end |
#default_category ⇒ Object
The default blog post category
97 98 99 |
# File 'lib/plate/site.rb', line 97 def default_category [:default_category] || 'Posts' end |
#default_layout ⇒ Object
The default layout for all pages that do not specifically name their own
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/plate/site.rb', line 102 def default_layout return nil if self.layouts.size == 0 return @default_layout if @default_layout layout ||= self.layouts.reject { |l| !l.default? } layout = self.layouts if layout.size == 0 if Array === layout and layout.size > 0 layout = layout[0] end @default_layout = layout end |
#find(search_path) ⇒ Object
Find a page, asset or layout by source relative file path
117 118 119 |
# File 'lib/plate/site.rb', line 117 def find(search_path) self.all_files.find { |file| file == search_path } end |
#find_by_extension(extension) ⇒ Object
Find all registered files by the given file extension
122 123 124 125 |
# File 'lib/plate/site.rb', line 122 def find_by_extension(extension) extension = extension.to_s.downcase.gsub(/^\./, '') self.all_files.select { |file| file.extension.to_s.downcase.gsub(/^\./, '') == extension } end |
#find_by_layout(layout_name) ⇒ Object
Find all pages and posts with this layout
128 129 130 131 132 133 134 135 |
# File 'lib/plate/site.rb', line 128 def find_by_layout(layout_name) result = [] result += self.pages.find_all { |page| page.layout == layout_name } result += self.posts.find_all { |post| post.layout == layout_name } result end |
#find_layout(layout_name) ⇒ Object
Find a specific layout by its file name. Any extensions are removed.
138 139 140 141 142 |
# File 'lib/plate/site.rb', line 138 def find_layout(layout_name) search_name = layout_name.to_s.downcase.strip.split('.')[0] matches = self.layouts.reject { |l| l.name != search_name } matches.empty? ? self.default_layout : matches[0] end |
#inspect ⇒ Object
144 145 146 |
# File 'lib/plate/site.rb', line 144 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} source=#{source.to_s.inspect}>" end |
#load! ⇒ Object
Load all data from the various source directories.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/plate/site.rb', line 149 def load! return if @loaded log("Loading site from source [#{source}]") run_callback :before_load self.load_pages! self.load_layouts! self.load_posts! run_callback :after_load @loaded = true end |
#loaded? ⇒ Boolean
Returns true if the site has been loaded from the source directories.
166 167 168 |
# File 'lib/plate/site.rb', line 166 def loaded? !!@loaded end |
#log(message, style = :indent) ⇒ Object
Write to the log if enable_logging is enabled
171 172 173 |
# File 'lib/plate/site.rb', line 171 def log(, style = :indent) logger.send(:log, , style) if logger and logger.respond_to?(:log) end |
#meta ⇒ Object
Access to read all meta data for this site. Meta data can be set on the site instance by passing in the ‘metadata` hash, or it can be pulled from the config file under the heading of `meta`.
All keys within the hash can be accessed directly by calling the key as a method name:
191 192 193 |
# File 'lib/plate/site.rb', line 191 def @meta ||= HashProxy.new(self.) end |
#meta=(hash) ⇒ Object
Set the meta data hash object for this site.
196 197 198 199 200 201 |
# File 'lib/plate/site.rb', line 196 def (hash) # Reset the meta hash proxy @meta = nil self. = hash end |
#page_engine_extensions ⇒ Object
203 204 205 |
# File 'lib/plate/site.rb', line 203 def page_engine_extensions @page_engine_extensions ||= self.registered_page_engines.keys.collect { |e| ".#{e}" } end |
#registered_asset_engines ⇒ Object
Returns the asset engines that are available for use.
217 218 219 |
# File 'lib/plate/site.rb', line 217 def registered_asset_engines Plate.asset_engines end |
#registered_page_engines ⇒ Object
Returns the engines available for use in page and layout formatting.
222 223 224 |
# File 'lib/plate/site.rb', line 222 def registered_page_engines Plate.template_engines end |
#relative_path(file_or_directory) ⇒ Object
207 208 209 |
# File 'lib/plate/site.rb', line 207 def relative_path(file_or_directory) file_or_directory.to_s.gsub(/^#{Regexp.quote(source)}(.*)$/, '\1') end |
#reload! ⇒ Object
211 212 213 214 |
# File 'lib/plate/site.rb', line 211 def reload! clear load! end |
#tags ⇒ Object
All tags used on this site
227 228 229 |
# File 'lib/plate/site.rb', line 227 def @tags ||= self.posts.tag_list end |
#to_url(str) ⇒ Object Also known as: sanitize_slug
231 232 233 234 235 236 237 |
# File 'lib/plate/site.rb', line 231 def to_url(str) result = str.to_s.strip.downcase result = result.gsub(/[^-a-z0-9~\s\.:;+=_]/, '') result = result.gsub(/[\.:;=+-]+/, '') result = result.gsub(/[\s]/, '-') result end |
#url ⇒ Object
The base URL for this site. The url can be set using the config option named ‘:base_url`.
The base URL will not have any trailing slashes.
243 244 245 246 |
# File 'lib/plate/site.rb', line 243 def url return '' unless self.[:base_url] @url ||= self.[:base_url].to_s.gsub(/(.*?)\/?$/, '\1') end |