Skip to content

Commit

Permalink
Merge pull request jekyll#4973 from ayastreb/site
Browse files Browse the repository at this point in the history
Merge pull request 4973
  • Loading branch information
jekyllbot committed Jun 6, 2016
2 parents 915b80b + 06cff8f commit 7197028
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 50 deletions.
3 changes: 1 addition & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ AllCops:
- lib/jekyll/filters.rb
- lib/jekyll/regenerator.rb
- lib/jekyll/renderer.rb
- lib/jekyll/site.rb
- lib/jekyll/static_file.rb
- lib/jekyll/utils.rb
- bin/**/*
Expand All @@ -28,7 +27,7 @@ Lint/UselessAccessModifier:
Metrics/AbcSize:
Max: 20
Metrics/ClassLength:
Max: 240
Max: 300
Exclude:
- !ruby/regexp /features\/.*.rb$/
- !ruby/regexp /test\/.*.rb$/
Expand Down
135 changes: 87 additions & 48 deletions lib/jekyll/site.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: UTF-8
require 'csv'
require "csv"

module Jekyll
class Site
Expand All @@ -18,8 +18,8 @@ class Site
# config - A Hash containing site configuration details.
def initialize(config)
# 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
@source = File.expand_path(config["source"]).freeze
@dest = File.expand_path(config["destination"]).freeze

self.config = config

Expand Down Expand Up @@ -49,19 +49,12 @@ def config=(config)
self.send("#{opt}=", config[opt])
end

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

self.theme = nil
self.theme = Jekyll::Theme.new(config["theme"]) if config["theme"]

@includes_load_paths = Array(in_source_dir(config["includes_dir"].to_s))
@includes_load_paths << theme.includes_path if self.theme

self.file_read_opts = {}
self.file_read_opts[:encoding] = config['encoding'] if config['encoding']
configure_plugins
configure_theme
configure_include_paths
configure_file_read_opts

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

@config
end
Expand All @@ -80,7 +73,7 @@ def process
end

def print_stats
if @config['profile']
if @config["profile"]
puts @liquid_renderer.stats_table
end
end
Expand All @@ -89,7 +82,11 @@ def print_stats
#
# Returns nothing
def reset
self.time = (config['time'] ? Utils.parse_date(config['time'].to_s, "Invalid time in _config.yml.") : Time.now)
if config["time"]
self.time = Utils.parse_date(config["time"].to_s, "Invalid time in _config.yml.")
else
self.time = Time.now
end
self.layouts = {}
self.pages = []
self.static_files = []
Expand Down Expand Up @@ -123,30 +120,35 @@ 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."
raise(
Errors::FatalException,
"Destination directory cannot be or contain the Source directory."
)
end
end
end

# The list of collections and their corresponding Jekyll::Collection instances.
# If config['collections'] is set, a new instance is created for each item in the collection.
# If config['collections'] is not set, a new hash is returned.
# If config['collections'] is set, a new instance is created
# for each item in the collection, a new hash is returned otherwise.
#
# Returns a Hash containing collection name-to-instance pairs.
def collections
@collections ||= Hash[collection_names.map { |coll| [coll, Jekyll::Collection.new(self, coll)] } ]
@collections ||= Hash[collection_names.map do |coll|
[coll, Jekyll::Collection.new(self, coll)]
end]
end

# 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.
def collection_names
case config['collections']
case config["collections"]
when Hash
config['collections'].keys
config["collections"].keys
when Array
config['collections']
config["collections"]
when nil
[]
else
Expand Down Expand Up @@ -182,26 +184,15 @@ def render

Jekyll::Hooks.trigger :site, :pre_render, self, payload

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

pages.flatten.each do |page|
if regenerator.regenerate?(page)
page.output = Jekyll::Renderer.new(self, page, payload).run
page.trigger_hooks(:post_render)
end
end
render_docs(payload)
render_pages(payload)

Jekyll::Hooks.trigger :site, :post_render, self, payload
# rubocop: disable HandleExceptions
rescue Errno::ENOENT
# ignore missing layout dir
end
# rubocop: enable HandleExceptions

# Remove orphaned files and empty directories in destination.
#
Expand All @@ -222,7 +213,7 @@ def write
end

def posts
collections['posts'] ||= Collection.new(self, 'posts')
collections["posts"] ||= Collection.new(self, "posts")
end

# Construct a Hash of Posts indexed by the specified Post attribute.
Expand All @@ -242,25 +233,27 @@ 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.docs.each { |p| p.data[post_attr].each { |t| hash[t] << p } if p.data[post_attr] }
posts.docs.each do |p|
p.data[post_attr].each { |t| hash[t] << p } if p.data[post_attr]
end
hash.values.each { |posts| posts.sort!.reverse! }
hash
end

def tags
post_attr_hash('tags')
post_attr_hash("tags")
end

def categories
post_attr_hash('categories')
post_attr_hash("categories")
end

# 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.
def site_data
config['data'] || data
config["data"] || data
end

# The Hash payload containing site-wide data.
Expand Down Expand Up @@ -306,7 +299,7 @@ def instantiate_subclasses(klass)
#
# Returns
def relative_permalinks_are_deprecated
if config['relative_permalinks']
if config["relative_permalinks"]
Jekyll.logger.abort_with "Since v3.0, permalinks for pages" \
" in subfolders must be relative to the" \
" site source directory, not the parent" \
Expand Down Expand Up @@ -351,7 +344,7 @@ def frontmatter_defaults
#
# Returns a Boolean: true for a full rebuild, false for normal build
def incremental?(override = {})
override['incremental'] || config['incremental']
override["incremental"] || config["incremental"]
end

# Returns the publisher or creates a new publisher if it doesn't
Expand Down Expand Up @@ -399,11 +392,10 @@ def in_dest_dir(*paths)
end
end

private

# Limits the current posts; removes the posts which exceed the limit_posts
#
# Returns nothing
private
def limit_posts!
if limit_posts > 0
limit = posts.docs.length < limit_posts ? posts.docs.length : limit_posts
Expand All @@ -415,8 +407,55 @@ def limit_posts!
# already exist.
#
# Returns The Cleaner
private
def site_cleaner
@site_cleaner ||= Cleaner.new(self)
end

private
def configure_plugins
self.plugin_manager = Jekyll::PluginManager.new(self)
self.plugins = plugin_manager.plugins_path
end

private
def configure_theme
self.theme = nil
self.theme = Jekyll::Theme.new(config["theme"]) if config["theme"]
end

private
def configure_include_paths
@includes_load_paths = Array(in_source_dir(config["includes_dir"].to_s))
@includes_load_paths << theme.includes_path if self.theme
end

private
def configure_file_read_opts
self.file_read_opts = {}
self.file_read_opts[:encoding] = config["encoding"] if config["encoding"]
end

private
def render_docs(payload)
collections.each do |_, collection|
collection.docs.each do |document|
if regenerator.regenerate?(document)
document.output = Jekyll::Renderer.new(self, document, payload).run
document.trigger_hooks(:post_render)
end
end
end
end

private
def render_pages(payload)
pages.flatten.each do |page|
if regenerator.regenerate?(page)
page.output = Jekyll::Renderer.new(self, page, payload).run
page.trigger_hooks(:post_render)
end
end
end
end
end

0 comments on commit 7197028

Please sign in to comment.