Class: Jekyll::Site

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

Defined Under Namespace

Classes: Cleaner

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.



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

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

  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

#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



450
451
452
# File 'lib/jekyll/site.rb', line 450

def (post)
  posts << post
end

#categoriesObject



344
345
346
# File 'lib/jekyll/site.rb', line 344

def categories
  post_attr_hash('categories')
end

#cleanupObject

Remove orphaned files and empty directories in destination.

Returns nothing.



307
308
309
# File 'lib/jekyll/site.rb', line 307

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.


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/jekyll/site.rb', line 133

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.



125
126
127
# File 'lib/jekyll/site.rb', line 125

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

#docs_to_writeObject



464
465
466
# File 'lib/jekyll/site.rb', line 464

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

#documentsObject



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

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

#each_site_fileObject



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

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.



87
88
89
90
91
92
93
94
# File 'lib/jekyll/site.rb', line 87

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.



399
400
401
# File 'lib/jekyll/site.rb', line 399

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

#frontmatter_defaultsObject



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

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

#generateObject

Run each of the Generators.

Returns nothing.



278
279
280
281
282
# File 'lib/jekyll/site.rb', line 278

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



438
439
440
441
442
443
# File 'lib/jekyll/site.rb', line 438

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

#getConverterImpl(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.



408
409
410
411
412
413
414
415
# File 'lib/jekyll/site.rb', line 408

def getConverterImpl(klass)
  matches = converters.select { |c| c.class == klass }
  if impl = matches.first
    impl
  else
    raise "Converter implementation not found for #{klass}"
  end
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.



114
115
116
117
118
# File 'lib/jekyll/site.rb', line 114

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.



102
103
104
105
106
# File 'lib/jekyll/site.rb', line 102

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



424
425
426
427
428
429
430
# File 'lib/jekyll/site.rb', line 424

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.


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

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.



47
48
49
50
51
52
53
54
# File 'lib/jekyll/site.rb', line 47

def process
  reset
  read
  generate
  render
  cleanup
  write
end

#readObject

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

Returns nothing.



149
150
151
152
153
154
# File 'lib/jekyll/site.rb', line 149

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.



269
270
271
272
273
# File 'lib/jekyll/site.rb', line 269

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

#read_content(dir, magic_dir, klass) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/jekyll/site.rb', line 218

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



229
230
231
232
# File 'lib/jekyll/site.rb', line 229

def read_data(dir)
  base = in_source_dir(dir)
  read_data_to(base, self.data)
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



241
242
243
244
245
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 241

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
      case File.extname(path).downcase
      when '.csv'
        data[key] = CSV.read(path, :headers => true).map(&:to_hash)
      else
        data[key] = SafeYAML.load_file(path)
      end
    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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/jekyll/site.rb', line 163

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)
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.



208
209
210
211
212
213
214
215
216
# File 'lib/jekyll/site.rb', line 208

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.



194
195
196
197
198
199
200
# File 'lib/jekyll/site.rb', line 194

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

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


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

def relative_permalinks_deprecation_method
  if config['relative_permalinks'] && has_relative_page?
    Jekyll.logger.warn "Deprecation:", "Starting in 2.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.



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/jekyll/site.rb', line 287

def render
  relative_permalinks_deprecation_method

  collections.each do |label, collection|
    collection.docs.each do |document|
      document.output = Jekyll::Renderer.new(self, document).run
    end
  end

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

#resetObject

Reset Site details.

Returns nothing



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll/site.rb', line 59

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.



76
77
78
79
80
81
82
83
# File 'lib/jekyll/site.rb', line 76

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.



352
353
354
# File 'lib/jekyll/site.rb', line 352

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.


369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/jekyll/site.rb', line 369

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.sort { |a, b| a.relative_path <=> b.relative_path },
        "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



340
341
342
# File 'lib/jekyll/site.rb', line 340

def tags
  post_attr_hash('tags')
end

#writeObject

Write static files, pages, and posts.

Returns nothing.



314
315
316
# File 'lib/jekyll/site.rb', line 314

def write
  each_site_file { |item| item.write(dest) }
end