Class: Plate::Site

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/plate/site.rb

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

Instance Method Summary collapse

Methods included from Callbacks

included

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, options = {})
  # 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.options = Hash === options ? options.clone : {}
  self.options.symbolize_keys!

  clear
end

Instance Attribute Details

#assetsObject

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_destinationObject

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_locationObject

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

#destinationObject

String

The destination directory file path for this site



19
20
21
# File 'lib/plate/site.rb', line 19

def destination
  @destination
end

#draftsObject

Array

Any posts marked as drafts for this site



22
23
24
# File 'lib/plate/site.rb', line 22

def drafts
  @drafts
end

#layoutsObject

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

#loggerObject

Object

The logger instance for this site.



28
29
30
# File 'lib/plate/site.rb', line 28

def logger
  @logger
end

#metadataObject

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

#optionsObject

Hash

A hash of configuration options for this site



34
35
36
# File 'lib/plate/site.rb', line 34

def options
  @options
end

#pagesObject

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

#partialsObject

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

#postsObject

PostCollection

All blog posts for this site



43
44
45
# File 'lib/plate/site.rb', line 43

def posts
  @posts
end

#sourceObject

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_filesObject



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_extensionsObject

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

#categoriesObject

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

#clearObject

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_categoryObject

The default blog post category



97
98
99
# File 'lib/plate/site.rb', line 97

def default_category
  options[:default_category] || 'Posts'
end

#default_layoutObject

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

#inspectObject



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.

Returns:

  • (Boolean)


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(message, style = :indent)
  logger.send(:log, message, style) if logger and logger.respond_to?(:log)
end

#metaObject

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:

Examples:

# Set the meta data hash
@site.meta = { :title => 'My Site Title' }

# Does the title key exist?
@site.meta.title? # => true

# Return the title value
@site.meta.title # => 'My Site 'Title'


191
192
193
# File 'lib/plate/site.rb', line 191

def meta
  @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 meta=(hash)
  # Reset the meta hash proxy
  @meta = nil

  self. = hash
end

#page_engine_extensionsObject



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_enginesObject

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_enginesObject

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

#tagsObject

All tags used on this site



227
228
229
# File 'lib/plate/site.rb', line 227

def tags
  @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

#urlObject

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.options[:base_url]
  @url ||= self.options[:base_url].to_s.gsub(/(.*?)\/?$/, '\1')
end