class Jekyll::Site

Attributes

baseurl[RW]
config[R]
converters[RW]
data[RW]
dest[R]
drafts[RW]
exclude[RW]
file_read_opts[RW]
future[RW]
gems[RW]
generators[RW]
highlighter[RW]
include[RW]
includes_load_paths[R]
keep_files[RW]
layouts[RW]
limit_posts[RW]
liquid_renderer[R]
lsi[RW]
pages[RW]
plugin_manager[RW]
plugins[RW]
reader[RW]
regenerator[R]
safe[RW]
show_drafts[RW]
source[R]
static_files[RW]
theme[RW]
time[RW]
unpublished[RW]

Public Class Methods

new(config) click to toggle source

Public: Initialize a new Site.

config - A Hash containing site configuration details.

# File lib/jekyll/site.rb, line 18
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

  self.config = config

  @reader          = Reader.new(self)
  @regenerator     = Regenerator.new(self)
  @liquid_renderer = LiquidRenderer.new(self)

  Jekyll.sites << self

  reset
  setup

  Jekyll::Hooks.trigger :site, :after_init, self
end

Public Instance Methods

categories() click to toggle source
# File lib/jekyll/site.rb, line 247
def categories
  post_attr_hash("categories")
end
cleanup() click to toggle source

Remove orphaned files and empty directories in destination.

Returns nothing.

# File lib/jekyll/site.rb, line 200
def cleanup
  site_cleaner.cleanup!
end
collection_names() click to toggle source

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.
# File lib/jekyll/site.rb, line 147
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
collections() click to toggle source

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, a new hash is returned otherwise.

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

# File lib/jekyll/site.rb, line 137
def collections
  @collections ||= Hash[collection_names.map do |coll|
    [coll, Jekyll::Collection.new(self, coll)]
  end]
end
collections_path() click to toggle source

Public: The full path to the directory that houses all the collections registered with the current site.

Returns the source directory or the absolute path to the custom collections_dir

# File lib/jekyll/site.rb, line 401
def collections_path
  dir_str = config["collections_dir"]
  @collections_path ||= dir_str.empty? ? source : in_source_dir(dir_str)
end
config=(config) click to toggle source

Public: Set the site's configuration. This handles side-effects caused by changing values in the configuration.

config - a Jekyll::Configuration, containing the new configuration.

Returns the new configuration.

# File lib/jekyll/site.rb, line 43
def config=(config)
  @config = config.clone

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

  # keep using `gems` to avoid breaking change
  self.gems = config["plugins"]

  configure_plugins
  configure_theme
  configure_include_paths
  configure_file_read_opts

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

  @config
end
docs_to_write() click to toggle source

Get the to be written documents

Returns an Array of Documents which should be written

# File lib/jekyll/site.rb, line 316
def docs_to_write
  @docs_to_write ||= documents.select(&:write?)
end
documents() click to toggle source

Get all the documents

Returns an Array of all Documents

# File lib/jekyll/site.rb, line 323
def documents
  collections.reduce(Set.new) do |docs, (_, collection)|
    docs + collection.docs + collection.files
  end.to_a
end
each_site_file() { |item| ... } click to toggle source
# File lib/jekyll/site.rb, line 329
def each_site_file
  %w(pages static_files docs_to_write).each do |type|
    send(type).each do |item|
      yield item
    end
  end
end
ensure_not_in_dest() click to toggle source

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

# File lib/jekyll/site.rb, line 122
def ensure_not_in_dest
  dest_pathname = Pathname.new(dest)
  Pathname.new(source).ascend do |path|
    if path == dest_pathname
      raise Errors::FatalException,
        "Destination directory cannot be or contain the Source directory."
    end
  end
end
find_converter_instance(klass) click to toggle source

Get the implementation class for the given Converter. Returns the Converter instance implementing the given Converter. klass - The Class of the Converter to fetch.

# File lib/jekyll/site.rb, line 280
def find_converter_instance(klass)
  @find_converter_instance ||= {}
  @find_converter_instance[klass] ||= begin
    converters.find { |converter| converter.instance_of?(klass) } || \
      raise("No Converters found for #{klass}")
  end
end
frontmatter_defaults() click to toggle source

Returns the FrontmatterDefaults or creates a new FrontmatterDefaults if it doesn't already exist.

Returns The FrontmatterDefaults

# File lib/jekyll/site.rb, line 341
def frontmatter_defaults
  @frontmatter_defaults ||= FrontmatterDefaults.new(self)
end
generate() click to toggle source

Run each of the Generators.

Returns nothing.

# File lib/jekyll/site.rb, line 172
def generate
  generators.each do |generator|
    start = Time.now
    generator.generate(self)
    Jekyll.logger.debug "Generating:",
      "#{generator.class} finished in #{Time.now - start} seconds."
  end
end
in_dest_dir(*paths) click to toggle source

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.

# File lib/jekyll/site.rb, line 391
def in_dest_dir(*paths)
  paths.reduce(dest) do |base, path|
    Jekyll.sanitized_path(base, path)
  end
end
in_source_dir(*paths) click to toggle source

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.

# File lib/jekyll/site.rb, line 366
def in_source_dir(*paths)
  paths.reduce(source) do |base, path|
    Jekyll.sanitized_path(base, path)
  end
end
in_theme_dir(*paths) click to toggle source

Public: Prefix a given path with the theme directory.

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

theme directory

Returns a path which is prefixed with the theme root directory.

# File lib/jekyll/site.rb, line 378
def in_theme_dir(*paths)
  return nil unless theme
  paths.reduce(theme.root) do |base, path|
    Jekyll.sanitized_path(base, path)
  end
end
incremental?(override = {}) click to toggle source

Whether to perform a full rebuild without incremental regeneration

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

# File lib/jekyll/site.rb, line 348
def incremental?(override = {})
  override["incremental"] || config["incremental"]
end
instantiate_subclasses(klass) click to toggle source

klass - class or module containing the subclasses. Returns array of instances of subclasses of parameter. Create array of instances of the subclasses of the class or module passed in as argument.

# File lib/jekyll/site.rb, line 293
def instantiate_subclasses(klass)
  klass.descendants.select { |c| !safe || c.safe }.sort.map do |c|
    c.new(config)
  end
end
post_attr_hash(post_attr) click to toggle source

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.
# File lib/jekyll/site.rb, line 232
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 do |p|
    p.data[post_attr].each { |t| hash[t] << p } if p.data[post_attr]
  end
  hash.each_value { |posts| posts.sort!.reverse! }
  hash
end
posts() click to toggle source
# File lib/jekyll/site.rb, line 215
def posts
  collections["posts"] ||= Collection.new(self, "posts")
end
print_stats() click to toggle source
process() click to toggle source

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

Returns nothing.

# File lib/jekyll/site.rb, line 67
def process
  reset
  read
  generate
  render
  cleanup
  write
  print_stats if config["profile"]
end
publisher() click to toggle source

Returns the publisher or creates a new publisher if it doesn't already exist.

Returns The Publisher

# File lib/jekyll/site.rb, line 356
def publisher
  @publisher ||= Publisher.new(self)
end
read() click to toggle source

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

Returns nothing.

# File lib/jekyll/site.rb, line 163
def read
  reader.read
  limit_posts!
  Jekyll::Hooks.trigger :site, :post_read, self
end
render() click to toggle source

Render the site to the destination.

Returns nothing.

# File lib/jekyll/site.rb, line 184
def render
  relative_permalinks_are_deprecated

  payload = site_payload

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

  render_docs(payload)
  render_pages(payload)

  Jekyll::Hooks.trigger :site, :post_render, self, payload
end
reset() click to toggle source

Reset Site details.

Returns nothing

# File lib/jekyll/site.rb, line 84
def reset
  self.time = if config["time"]
                Utils.parse_date(config["time"].to_s, "Invalid time in _config.yml.")
              else
                Time.now
              end
  self.layouts = {}
  self.pages = []
  self.static_files = []
  self.data = {}
  @site_data = nil
  @collections = nil
  @docs_to_write = nil
  @regenerator.clear_cache
  @liquid_renderer.reset
  @site_cleaner = nil

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

  Jekyll::Hooks.trigger :site, :after_reset, self
end
setup() click to toggle source

Load necessary libraries, plugins, converters, and generators.

Returns nothing.

# File lib/jekyll/site.rb, line 111
def setup
  ensure_not_in_dest

  plugin_manager.conscientious_require

  self.converters = instantiate_subclasses(Jekyll::Converter)
  self.generators = instantiate_subclasses(Jekyll::Generator)
end
site_data() click to toggle source

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.

# File lib/jekyll/site.rb, line 255
def site_data
  @site_data ||= (config["data"] || data)
end
site_payload() click to toggle source

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.
# File lib/jekyll/site.rb, line 272
def site_payload
  Drops::UnifiedPayloadDrop.new self
end
Also aliased as: to_liquid
tags() click to toggle source
# File lib/jekyll/site.rb, line 243
def tags
  post_attr_hash("tags")
end
to_liquid()
Alias for: site_payload
write() click to toggle source

Write static files, pages, and posts.

Returns nothing.

# File lib/jekyll/site.rb, line 207
def write
  each_site_file do |item|
    item.write(dest) if regenerator.regenerate?(item)
  end
  regenerator.write_metadata
  Jekyll::Hooks.trigger :site, :post_write, self
end

Private Instance Methods

configure_file_read_opts() click to toggle source
# File lib/jekyll/site.rb, line 454
def configure_file_read_opts
  self.file_read_opts = {}
  self.file_read_opts[:encoding] = config["encoding"] if config["encoding"]
  self.file_read_opts = Jekyll::Utils.merged_file_read_opts(self, {})
end
configure_include_paths() click to toggle source
# File lib/jekyll/site.rb, line 448
def configure_include_paths
  @includes_load_paths = Array(in_source_dir(config["includes_dir"].to_s))
  @includes_load_paths << theme.includes_path if theme && theme.includes_path
end
configure_plugins() click to toggle source
# File lib/jekyll/site.rb, line 427
def configure_plugins
  self.plugin_manager = Jekyll::PluginManager.new(self)
  self.plugins        = plugin_manager.plugins_path
end
configure_theme() click to toggle source
# File lib/jekyll/site.rb, line 433
def configure_theme
  self.theme = nil
  return if config["theme"].nil?

  self.theme =
    if config["theme"].is_a?(String)
      Jekyll::Theme.new(config["theme"])
    else
      Jekyll.logger.warn "Theme:", "value of 'theme' in config should be " \
      "String to use gem-based themes, but got #{config["theme"].class}"
      nil
    end
end
limit_posts!() click to toggle source

Limits the current posts; removes the posts which exceed the #limit_posts

Returns nothing

# File lib/jekyll/site.rb, line 410
def limit_posts!
  if limit_posts > 0
    limit = posts.docs.length < limit_posts ? posts.docs.length : limit_posts
    self.posts.docs = posts.docs[-limit, limit]
  end
end
render_docs(payload) click to toggle source
# File lib/jekyll/site.rb, line 461
def render_docs(payload)
  collections.each_value do |collection|
    collection.docs.each do |document|
      render_regenerated(document, payload)
    end
  end
end
render_pages(payload) click to toggle source
# File lib/jekyll/site.rb, line 470
def render_pages(payload)
  pages.flatten.each do |page|
    render_regenerated(page, payload)
  end
end
render_regenerated(document, payload) click to toggle source
# File lib/jekyll/site.rb, line 477
def render_regenerated(document, payload)
  return unless regenerator.regenerate?(document)
  document.output = Jekyll::Renderer.new(self, document, payload).run
  document.trigger_hooks(:post_render)
end
site_cleaner() click to toggle source

Returns the Cleaner or creates a new Cleaner if it doesn't already exist.

Returns The Cleaner

# File lib/jekyll/site.rb, line 422
def site_cleaner
  @site_cleaner ||= Cleaner.new(self)
end