Class: Jekyll::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Public: Initialize a new Site.

config - A Hash containing site configuration details.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jekyll/site.rb', line 19

def initialize(config)
  @config = config.clone

  %w[safe lsi highlighter baseurl exclude include future unpublished
    show_drafts limit_posts keep_files gems].each do |opt|
    self.send("#{opt}=", config[opt])
  end

  # Source and destination may not be changed after the site has been created.
  @source              = File.expand_path(config['source']).freeze
  @dest                = File.expand_path(config['destination']).freeze

  # Initialize incremental regenerator
  @regenerator = Regenerator.new(self)

  self.plugin_manager = Jekyll::PluginManager.new(self)
  self.plugins        = plugin_manager.plugins_path

  self.file_read_opts = {}
  self.file_read_opts[:encoding] = config['encoding'] if config['encoding']

  self.permalink_style = config['permalink'].to_sym

  Jekyll.sites << self

  reset
  setup
end

Instance Attribute Details

#baseurlObject

Returns the value of attribute baseurl



7
8
9
# File 'lib/jekyll/site.rb', line 7

def baseurl
  @baseurl
end

#configObject (readonly)

Returns the value of attribute config



6
7
8
# File 'lib/jekyll/site.rb', line 6

def config
  @config
end

#convertersObject

Returns the value of attribute converters



13
14
15
# File 'lib/jekyll/site.rb', line 13

def converters
  @converters
end

#dataObject

Returns the value of attribute data



7
8
9
# File 'lib/jekyll/site.rb', line 7

def data
  @data
end

#destObject (readonly)

Returns the value of attribute dest



6
7
8
# File 'lib/jekyll/site.rb', line 6

def dest
  @dest
end

#excludeObject

Returns the value of attribute exclude



7
8
9
# File 'lib/jekyll/site.rb', line 7

def exclude
  @exclude
end

#file_read_optsObject

Returns the value of attribute file_read_opts



7
8
9
# File 'lib/jekyll/site.rb', line 7

def file_read_opts
  @file_read_opts
end

#futureObject

Returns the value of attribute future



7
8
9
# File 'lib/jekyll/site.rb', line 7

def future
  @future
end

#gemsObject

Returns the value of attribute gems



7
8
9
# File 'lib/jekyll/site.rb', line 7

def gems
  @gems
end

#generatorsObject

Returns the value of attribute generators



13
14
15
# File 'lib/jekyll/site.rb', line 13

def generators
  @generators
end

#highlighterObject

Returns the value of attribute highlighter



7
8
9
# File 'lib/jekyll/site.rb', line 7

def highlighter
  @highlighter
end

#includeObject

Returns the value of attribute include



7
8
9
# File 'lib/jekyll/site.rb', line 7

def include
  @include
end

#keep_filesObject

Returns the value of attribute keep_files



7
8
9
# File 'lib/jekyll/site.rb', line 7

def keep_files
  @keep_files
end

#layoutsObject

Returns the value of attribute layouts



7
8
9
# File 'lib/jekyll/site.rb', line 7

def layouts
  @layouts
end

#limit_postsObject

Returns the value of attribute limit_posts



7
8
9
# File 'lib/jekyll/site.rb', line 7

def limit_posts
  @limit_posts
end

#lsiObject

Returns the value of attribute lsi



7
8
9
# File 'lib/jekyll/site.rb', line 7

def lsi
  @lsi
end

#pagesObject

Returns the value of attribute pages



7
8
9
# File 'lib/jekyll/site.rb', line 7

def pages
  @pages
end

Returns the value of attribute permalink_style



7
8
9
# File 'lib/jekyll/site.rb', line 7

def permalink_style
  @permalink_style
end

#plugin_managerObject

Returns the value of attribute plugin_manager



7
8
9
# File 'lib/jekyll/site.rb', line 7

def plugin_manager
  @plugin_manager
end

#pluginsObject

Returns the value of attribute plugins



7
8
9
# File 'lib/jekyll/site.rb', line 7

def plugins
  @plugins
end

#postsObject

Returns the value of attribute posts



7
8
9
# File 'lib/jekyll/site.rb', line 7

def posts
  @posts
end

#regeneratorObject (readonly)

Returns the value of attribute regenerator



14
15
16
# File 'lib/jekyll/site.rb', line 14

def regenerator
  @regenerator
end

#safeObject

Returns the value of attribute safe



7
8
9
# File 'lib/jekyll/site.rb', line 7

def safe
  @safe
end

#show_draftsObject

Returns the value of attribute show_drafts



7
8
9
# File 'lib/jekyll/site.rb', line 7

def show_drafts
  @show_drafts
end

#sourceObject (readonly)

Returns the value of attribute source



6
7
8
# File 'lib/jekyll/site.rb', line 6

def source
  @source
end

#static_filesObject

Returns the value of attribute static_files



7
8
9
# File 'lib/jekyll/site.rb', line 7

def static_files
  @static_files
end

#timeObject

Returns the value of attribute time



7
8
9
# File 'lib/jekyll/site.rb', line 7

def time
  @time
end

#unpublishedObject

Returns the value of attribute unpublished



7
8
9
# File 'lib/jekyll/site.rb', line 7

def unpublished
  @unpublished
end

Instance Method Details

#aggregate_post_info(post) ⇒ Object

Aggregate post information

post - The Post object to aggregate information for

Returns nothing



468
469
470
# File 'lib/jekyll/site.rb', line 468

def (post)
  posts << post
end

#categoriesObject



367
368
369
# File 'lib/jekyll/site.rb', line 367

def categories
  post_attr_hash('categories')
end

#cleanupObject

Remove orphaned files and empty directories in destination.

Returns nothing.



327
328
329
# File 'lib/jekyll/site.rb', line 327

def cleanup
  site_cleaner.cleanup!
end

#collection_namesObject

The list of collection names.

Returns an array of collection names from the configuration,

or an empty array if the `collections` key is not set.


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jekyll/site.rb', line 137

def collection_names
  case config['collections']
  when Hash
    config['collections'].keys
  when Array
    config['collections']
  when nil
    []
  else
    raise ArgumentError, "Your `collections` key must be a hash or an array."
  end
end

#collectionsObject

The list of collections and their corresponding Jekyll::Collection instances. If config is set, a new instance is created for each item in the collection. If config is not set, a new hash is returned.

Returns a Hash containing collection name-to-instance pairs.



129
130
131
# File 'lib/jekyll/site.rb', line 129

def collections
  @collections ||= Hash[collection_names.map { |coll| [coll, Jekyll::Collection.new(self, coll)] } ]
end

#docs_to_writeObject



482
483
484
# File 'lib/jekyll/site.rb', line 482

def docs_to_write
  documents.select(&:write?)
end

#documentsObject



486
487
488
489
490
# File 'lib/jekyll/site.rb', line 486

def documents
  collections.reduce(Set.new) do |docs, (_, collection)|
    docs + collection.docs + collection.files
  end.to_a
end

#each_site_fileObject



492
493
494
495
496
497
498
# File 'lib/jekyll/site.rb', line 492

def each_site_file
  %w(posts pages static_files docs_to_write).each do |type|
    send(type).each do |item|
      yield item
    end
  end
end

#ensure_not_in_destObject

Check that the destination dir isn't the source dir or a directory parent to the source dir.



91
92
93
94
95
96
97
98
# File 'lib/jekyll/site.rb', line 91

def ensure_not_in_dest
  dest_pathname = Pathname.new(dest)
  Pathname.new(source).ascend do |path|
    if path == dest_pathname
      raise Errors::FatalException.new "Destination directory cannot be or contain the Source directory."
    end
  end
end

#filter_entries(entries, base_directory = nil) ⇒ Object

Filter out any files/directories that are hidden or backup files (start with “.” or “#” or end with “~”), or contain site content (start with “_”), or are excluded in the site configuration, unless they are web server files such as '.htaccess'.

entries - The Array of String file/directory entries to filter.

Returns the Array of filtered entries.



422
423
424
# File 'lib/jekyll/site.rb', line 422

def filter_entries(entries, base_directory = nil)
  EntryFilter.new(self, base_directory).filter(entries)
end

#find_converter_instance(klass) ⇒ Object

Get the implementation class for the given Converter.

klass - The Class of the Converter to fetch.

Returns the Converter instance implementing the given Converter.



431
432
433
# File 'lib/jekyll/site.rb', line 431

def find_converter_instance(klass)
  converters.find { |c| c.class == klass } || proc { raise "No converter for #{klass}" }.call
end

#frontmatter_defaultsObject



500
501
502
# File 'lib/jekyll/site.rb', line 500

def frontmatter_defaults
  @frontmatter_defaults ||= FrontmatterDefaults.new(self)
end

#full_rebuild?(override = {}) ⇒ Boolean

Whether to perform a full rebuild without incremental regeneration

Returns a Boolean: true for a full rebuild, false for normal build

Returns:

  • (Boolean)


507
508
509
# File 'lib/jekyll/site.rb', line 507

def full_rebuild?(override = {})
  override['full_rebuild'] || config['full_rebuild']
end

#generateObject

Run each of the Generators.

Returns nothing.



293
294
295
296
297
# File 'lib/jekyll/site.rb', line 293

def generate
  generators.each do |generator|
    generator.generate(self)
  end
end

#get_entries(dir, subfolder) ⇒ Object

Read the entries from a particular directory for processing

dir - The String relative path of the directory to read subfolder - The String directory to read

Returns the list of entries to process



456
457
458
459
460
461
# File 'lib/jekyll/site.rb', line 456

def get_entries(dir, subfolder)
  base = in_source_dir(dir, subfolder)
  return [] unless File.exist?(base)
  entries = Dir.chdir(base) { filter_entries(Dir['**/*'], base) }
  entries.delete_if { |e| File.directory?(in_source_dir(base, e)) }
end

#in_dest_dir(*paths) ⇒ Object

Public: Prefix a given path with the destination directory.

paths - (optional) path elements to a file or directory within the

destination directory

Returns a path which is prefixed with the destination directory.



118
119
120
121
122
# File 'lib/jekyll/site.rb', line 118

def in_dest_dir(*paths)
  paths.reduce(dest) do |base, path|
    Jekyll.sanitized_path(base, path)
  end
end

#in_source_dir(*paths) ⇒ Object

Public: Prefix a given path with the source directory.

paths - (optional) path elements to a file or directory within the

source directory

Returns a path which is prefixed with the source directory.



106
107
108
109
110
# File 'lib/jekyll/site.rb', line 106

def in_source_dir(*paths)
  paths.reduce(source) do |base, path|
    Jekyll.sanitized_path(base, path)
  end
end

#instantiate_subclasses(klass) ⇒ Object

Create array of instances of the subclasses of the class or module

passed in as argument.

klass - class or module containing the subclasses which should be

instantiated

Returns array of instances of subclasses of parameter



442
443
444
445
446
447
448
# File 'lib/jekyll/site.rb', line 442

def instantiate_subclasses(klass)
  klass.descendants.select do |c|
    !safe || c.safe
  end.sort.map do |c|
    c.new(config)
  end
end

#post_attr_hash(post_attr) ⇒ Object

Construct a Hash of Posts indexed by the specified Post attribute.

post_attr - The String name of the Post attribute.

Examples

post_attr_hash('categories')
# => { 'tech' => [<Post A>, <Post B>],
#      'ruby' => [<Post B>] }

Returns the Hash: { attr => posts } where

attr  - One of the values for the requested attribute.
posts - The Array of Posts with the given attr value.


354
355
356
357
358
359
360
361
# File 'lib/jekyll/site.rb', line 354

def post_attr_hash(post_attr)
  # Build a hash map based on the specified post attribute ( post attr =>
  # array of posts ) then sort each array in reverse order.
  hash = Hash.new { |h, key| h[key] = [] }
  posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
  hash.values.each { |posts| posts.sort!.reverse! }
  hash
end

#processObject

Public: Read, process, and write this Site to output.

Returns nothing.



51
52
53
54
55
56
57
58
# File 'lib/jekyll/site.rb', line 51

def process
  reset
  read
  generate
  render
  cleanup
  write
end

#publisherObject



511
512
513
# File 'lib/jekyll/site.rb', line 511

def publisher
  @publisher ||= Publisher.new(self)
end

#readObject

Read Site data from disk and load it into internal data structures.

Returns nothing.



153
154
155
156
157
158
# File 'lib/jekyll/site.rb', line 153

def read
  self.layouts = LayoutReader.new(self).read
  read_directories
  read_data(config['data_source'])
  read_collections
end

#read_collectionsObject

Read in all collections specified in the configuration

Returns nothing.



284
285
286
287
288
# File 'lib/jekyll/site.rb', line 284

def read_collections
  collections.each do |_, collection|
    collection.read unless collection.label.eql?("data")
  end
end

#read_content(dir, magic_dir, klass) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/jekyll/site.rb', line 223

def read_content(dir, magic_dir, klass)
  get_entries(dir, magic_dir).map do |entry|
    klass.new(self, source, dir, entry) if klass.valid?(entry)
  end.reject do |entry|
    entry.nil?
  end
end

#read_data(dir) ⇒ Object

Read and parse all yaml files under <source>/<dir>

Returns nothing



234
235
236
237
# File 'lib/jekyll/site.rb', line 234

def read_data(dir)
  base = in_source_dir(dir)
  read_data_to(base, self.data)
end

#read_data_file(path) ⇒ Object

Determines how to read a data file.

Returns the contents of the data file.



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/jekyll/site.rb', line 269

def read_data_file(path)
  case File.extname(path).downcase
  when '.csv'
    CSV.read(path, {
      :headers => true,
      :encoding => config['encoding']
    }).map(&:to_hash)
  else
    SafeYAML.load_file(path)
  end
end

#read_data_to(dir, data) ⇒ Object

Read and parse all yaml files under <dir> and add them to the <data> variable.

dir - The string absolute path of the directory to read. data - The variable to which data will be added.

Returns nothing



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/jekyll/site.rb', line 246

def read_data_to(dir, data)
  return unless File.directory?(dir) && (!safe || !File.symlink?(dir))

  entries = Dir.chdir(dir) do
    Dir['*.{yaml,yml,json,csv}'] + Dir['*'].select { |fn| File.directory?(fn) }
  end

  entries.each do |entry|
    path = in_source_dir(dir, entry)
    next if File.symlink?(path) && safe

    key = sanitize_filename(File.basename(entry, '.*'))
    if File.directory?(path)
      read_data_to(path, data[key] = {})
    else
      data[key] = read_data_file(path)
    end
  end
end

#read_directories(dir = '') ⇒ Object

Recursively traverse directories to find posts, pages and static files that will become part of the site according to the rules in filter_entries.

dir - The String relative path of the directory to read. Default: ''.

Returns nothing.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/jekyll/site.rb', line 167

def read_directories(dir = '')
  base = in_source_dir(dir)
  entries = Dir.chdir(base) { filter_entries(Dir.entries('.'), base) }

  read_posts(dir)
  read_drafts(dir) if show_drafts
  posts.sort!
  limit_posts! if limit_posts > 0 # limit the posts if :limit_posts option is set

  entries.each do |f|
    f_abs = in_source_dir(base, f)
    if File.directory?(f_abs)
      f_rel = File.join(dir, f)
      read_directories(f_rel) unless dest.sub(/\/$/, '') == f_abs
    elsif Utils.has_yaml_header?(f_abs)
      page = Page.new(self, source, dir, f)
      pages << page if publisher.publish?(page)
    else
      static_files << StaticFile.new(self, source, dir, f)
    end
  end

  pages.sort_by!(&:name)
  static_files.sort_by!(&:relative_path)
end

#read_drafts(dir) ⇒ Object

Read all the files in <source>/<dir>/_drafts and create a new Post object with each one.

dir - The String relative path of the directory to read.

Returns nothing.



213
214
215
216
217
218
219
220
221
# File 'lib/jekyll/site.rb', line 213

def read_drafts(dir)
  drafts = read_content(dir, '_drafts', Draft)

  drafts.each do |draft|
    if draft.published?
      (draft)
    end
  end
end

#read_posts(dir) ⇒ Object

Read all the files in <source>/<dir>/_posts and create a new Post object with each one.

dir - The String relative path of the directory to read.

Returns nothing.



199
200
201
202
203
204
205
# File 'lib/jekyll/site.rb', line 199

def read_posts(dir)
  posts = read_content(dir, '_posts', Post)

  posts.each do |post|
    (post) if publisher.publish?(post)
  end
end


472
473
474
475
476
477
478
479
480
# File 'lib/jekyll/site.rb', line 472

def relative_permalinks_deprecation_method
  if config['relative_permalinks'] && has_relative_page?
    Jekyll::Deprecator.deprecation_message "Since v2.0, permalinks for pages" +
                                        " in subfolders must be relative to the" +
                                        " site source directory, not the parent" +
                                        " directory. Check http://jekyllrb.com/docs/upgrading/"+
                                        " for more info."
  end
end

#renderObject

Render the site to the destination.

Returns nothing.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/jekyll/site.rb', line 302

def render
  relative_permalinks_deprecation_method

  payload = site_payload
  collections.each do |label, collection|
    collection.docs.each do |document|
      if regenerator.regenerate?(document)
        document.output = Jekyll::Renderer.new(self, document, payload).run
      end
    end
  end

  payload = site_payload
  [posts, pages].flatten.each do |page_or_post|
    if regenerator.regenerate?(page_or_post)
      page_or_post.render(layouts, payload)
    end
  end
rescue Errno::ENOENT
  # ignore missing layout dir
end

#resetObject

Reset Site details.

Returns nothing



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll/site.rb', line 63

def reset
  self.time = (config['time'] ? Utils.parse_date(config['time'].to_s, "Invalid time in _config.yml.") : Time.now)
  self.layouts = {}
  self.posts = []
  self.pages = []
  self.static_files = []
  self.data = {}
  @collections = nil

  if limit_posts < 0
    raise ArgumentError, "limit_posts must be a non-negative number"
  end
end

#setupObject

Load necessary libraries, plugins, converters, and generators.

Returns nothing.



80
81
82
83
84
85
86
87
# File 'lib/jekyll/site.rb', line 80

def setup
  ensure_not_in_dest

  plugin_manager.conscientious_require

  self.converters = instantiate_subclasses(Jekyll::Converter)
  self.generators = instantiate_subclasses(Jekyll::Generator)
end

#site_dataObject

Prepare site data for site payload. The method maintains backward compatibility if the key 'data' is already used in _config.yml.

Returns the Hash to be hooked to site.data.



375
376
377
# File 'lib/jekyll/site.rb', line 375

def site_data
  config['data'] || data
end

#site_payloadObject

The Hash payload containing site-wide data.

Returns the Hash: { “site” => data } where data is a Hash with keys:

"time"       - The Time as specified in the configuration or the
               current time if none was specified.
"posts"      - The Array of Posts, sorted chronologically by post date
               and then title.
"pages"      - The Array of all Pages.
"html_pages" - The Array of HTML Pages.
"categories" - The Hash of category values and Posts.
               See Site#post_attr_hash for type info.
"tags"       - The Hash of tag values and Posts.
               See Site#post_attr_hash for type info.


392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/jekyll/site.rb', line 392

def site_payload
  {
    "jekyll" => {
      "version" => Jekyll::VERSION,
      "environment" => Jekyll.env
    },
    "site"   => Utils.deep_merge_hashes(config,
      Utils.deep_merge_hashes(Hash[collections.map{|label, coll| [label, coll.docs]}], {
        "time"         => time,
        "posts"        => posts.sort { |a, b| b <=> a },
        "pages"        => pages,
        "static_files" => static_files,
        "html_pages"   => pages.select { |page| page.html? || page.url.end_with?("/") },
        "categories"   => post_attr_hash('categories'),
        "tags"         => post_attr_hash('tags'),
        "collections"  => collections,
        "documents"    => documents,
        "data"         => site_data
    }))
  }
end

#tagsObject



363
364
365
# File 'lib/jekyll/site.rb', line 363

def tags
  post_attr_hash('tags')
end

#writeObject

Write static files, pages, and posts.

Returns nothing.



334
335
336
337
338
339
# File 'lib/jekyll/site.rb', line 334

def write
  each_site_file { |item|
    item.write(dest) if regenerator.regenerate?(item)
  }
  regenerator.
end