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.



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

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

Instance Attribute Details

#baseurlObject

Returns the value of attribute baseurl.



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

def baseurl
  @baseurl
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#convertersObject

Returns the value of attribute converters.



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

def converters
  @converters
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#destObject (readonly)

Returns the value of attribute dest.



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

def dest
  @dest
end

#draftsObject

Returns the value of attribute drafts.



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

def drafts
  @drafts
end

#excludeObject

Returns the value of attribute exclude.



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

def exclude
  @exclude
end

#file_read_optsObject

Returns the value of attribute file_read_opts.



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

def file_read_opts
  @file_read_opts
end

#futureObject

Returns the value of attribute future.



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

def future
  @future
end

#gemsObject

Returns the value of attribute gems.



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

def gems
  @gems
end

#generatorsObject

Returns the value of attribute generators.



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

def generators
  @generators
end

#highlighterObject

Returns the value of attribute highlighter.



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

def highlighter
  @highlighter
end

#includeObject

Returns the value of attribute include.



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

def include
  @include
end

#includes_load_pathsObject (readonly)

Returns the value of attribute includes_load_paths.



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

def includes_load_paths
  @includes_load_paths
end

#keep_filesObject

Returns the value of attribute keep_files.



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

def keep_files
  @keep_files
end

#layoutsObject

Returns the value of attribute layouts.



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

def layouts
  @layouts
end

#limit_postsObject

Returns the value of attribute limit_posts.



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

def limit_posts
  @limit_posts
end

#liquid_rendererObject (readonly)

Returns the value of attribute liquid_renderer.



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

def liquid_renderer
  @liquid_renderer
end

#lsiObject

Returns the value of attribute lsi.



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

def lsi
  @lsi
end

#pagesObject

Returns the value of attribute pages.



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

def pages
  @pages
end

Returns the value of attribute permalink_style.



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

def permalink_style
  @permalink_style
end

#plugin_managerObject

Returns the value of attribute plugin_manager.



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

def plugin_manager
  @plugin_manager
end

#pluginsObject

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

#readerObject

Returns the value of attribute reader.



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

def reader
  @reader
end

#regeneratorObject (readonly)

Returns the value of attribute regenerator.



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

def regenerator
  @regenerator
end

#safeObject

Returns the value of attribute safe.



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

def safe
  @safe
end

#show_draftsObject

Returns the value of attribute show_drafts.



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

def show_drafts
  @show_drafts
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

#static_filesObject

Returns the value of attribute static_files.



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

def static_files
  @static_files
end

#themeObject

Returns the value of attribute theme.



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

def theme
  @theme
end

#timeObject

Returns the value of attribute time.



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

def time
  @time
end

#unpublishedObject

Returns the value of attribute unpublished.



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

def unpublished
  @unpublished
end

Instance Method Details

#categoriesObject



248
249
250
# File 'lib/jekyll/site.rb', line 248

def categories
  post_attr_hash("categories")
end

#cleanupObject

Remove orphaned files and empty directories in destination.

Returns nothing.



201
202
203
# File 'lib/jekyll/site.rb', line 201

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.


148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jekyll/site.rb', line 148

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

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



138
139
140
141
142
# File 'lib/jekyll/site.rb', line 138

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

#docs_to_writeObject

Get the to be written documents

Returns an Array of Documents which should be written



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

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

#documentsObject

Get all the documents

Returns an Array of all Documents



322
323
324
325
326
# File 'lib/jekyll/site.rb', line 322

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

#each_site_fileObject



328
329
330
331
332
333
334
# File 'lib/jekyll/site.rb', line 328

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_destObject

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



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jekyll/site.rb', line 121

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) ⇒ Object

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



282
283
284
285
# File 'lib/jekyll/site.rb', line 282

def find_converter_instance(klass)
  converters.find { |klass_| klass_.instance_of?(klass) } || \
    raise("No Converters found for #{klass}")
end

#frontmatter_defaultsObject

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

Returns The FrontmatterDefaults



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

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

#generateObject

Run each of the Generators.

Returns nothing.



173
174
175
176
177
178
179
180
# File 'lib/jekyll/site.rb', line 173

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



390
391
392
393
394
# File 'lib/jekyll/site.rb', line 390

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.



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

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

#in_theme_dir(*paths) ⇒ Object

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.



377
378
379
380
381
382
# File 'lib/jekyll/site.rb', line 377

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 = {}) ⇒ Boolean

Whether to perform a full rebuild without incremental regeneration

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

Returns:

  • (Boolean)


347
348
349
# File 'lib/jekyll/site.rb', line 347

def incremental?(override = {})
  override["incremental"] || config["incremental"]
end

#instantiate_subclasses(klass) ⇒ Object

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.



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

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


233
234
235
236
237
238
239
240
241
242
# File 'lib/jekyll/site.rb', line 233

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

#postsObject



216
217
218
# File 'lib/jekyll/site.rb', line 216

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


79
80
81
# File 'lib/jekyll/site.rb', line 79

def print_stats
  puts @liquid_renderer.stats_table
end

#processObject

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

Returns nothing.



69
70
71
72
73
74
75
76
77
# File 'lib/jekyll/site.rb', line 69

def process
  reset
  read
  generate
  render
  cleanup
  write
  print_stats if config["profile"]
end

#publisherObject

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

Returns The Publisher



355
356
357
# File 'lib/jekyll/site.rb', line 355

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

#readObject

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

Returns nothing.



164
165
166
167
168
# File 'lib/jekyll/site.rb', line 164

def read
  reader.read
  limit_posts!
  Jekyll::Hooks.trigger :site, :post_read, self
end

Warns the user if permanent links are relative to the parent directory. As this is a deprecated function of Jekyll.

Returns



302
303
304
305
306
307
308
309
310
# File 'lib/jekyll/site.rb', line 302

def relative_permalinks_are_deprecated
  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" \
                            " directory. Check https://jekyllrb.com/docs/upgrading/"\
                            " for more info."
  end
end

#renderObject

Render the site to the destination.

Returns nothing.



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/jekyll/site.rb', line 185

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

#resetObject

Reset Site details.

Returns nothing



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jekyll/site.rb', line 86

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 = {}
  @collections = nil
  @regenerator.clear_cache
  @liquid_renderer.reset

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

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

#setupObject

Load necessary libraries, plugins, converters, and generators.

Returns nothing.



110
111
112
113
114
115
116
117
# File 'lib/jekyll/site.rb', line 110

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.



256
257
258
# File 'lib/jekyll/site.rb', line 256

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

#site_payloadObject Also known as: to_liquid

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.


273
274
275
# File 'lib/jekyll/site.rb', line 273

def site_payload
  Drops::UnifiedPayloadDrop.new self
end

#tagsObject



244
245
246
# File 'lib/jekyll/site.rb', line 244

def tags
  post_attr_hash("tags")
end

#writeObject

Write static files, pages, and posts.

Returns nothing.



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

def write
  each_site_file do |item|
    item.write(dest) if regenerator.regenerate?(item)
  end
  regenerator.
  Jekyll::Hooks.trigger :site, :post_write, self
end