Class: Jekyll::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, label) ⇒ Collection

Create a new Collection.

site - the site to which this collection belongs. label - the name of the collection

Returns nothing.



11
12
13
14
15
# File 'lib/jekyll/collection.rb', line 11

def initialize(site, label)
  @site     = site
  @label    = sanitize_label(label)
  @metadata = 
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label



3
4
5
# File 'lib/jekyll/collection.rb', line 3

def label
  @label
end

#metadataObject (readonly)

Returns the value of attribute metadata



3
4
5
# File 'lib/jekyll/collection.rb', line 3

def 
  @metadata
end

#siteObject (readonly)

Returns the value of attribute site



3
4
5
# File 'lib/jekyll/collection.rb', line 3

def site
  @site
end

Instance Method Details

#collection_dir(*files) ⇒ Object

The full path to the directory containing the collection, with

optional subpaths.

*files - (optional) any other path pieces relative to the

directory to append to the path

Returns a String containing th directory name where the collection

is stored on the filesystem.


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

def collection_dir(*files)
  return directory if files.empty?
  site.in_source_dir(relative_directory, *files)
end

#directoryObject

The full path to the directory containing the collection.

Returns a String containing th directory name where the collection

is stored on the filesystem.


91
92
93
# File 'lib/jekyll/collection.rb', line 91

def directory
  @directory ||= site.in_source_dir(relative_directory)
end

#docsObject

Fetch the Documents in this collection. Defaults to an empty array if no documents have been read in.

Returns an array of Jekyll::Document objects.



21
22
23
# File 'lib/jekyll/collection.rb', line 21

def docs
  @docs ||= []
end

#entriesObject

All the entries in this collection.

Returns an Array of file paths to the documents in this collection

relative to the collection's directory


56
57
58
59
60
61
62
# File 'lib/jekyll/collection.rb', line 56

def entries
  return Array.new unless exists?
  @entries ||=
    Dir.glob(collection_dir("**", "*.*")).map do |entry|
      entry["#{collection_dir}/"] = ''; entry
    end
end

#entry_filterObject

The entry filter for this collection. Creates an instance of Jekyll::EntryFilter.

Returns the instance of Jekyll::EntryFilter for this collection.



122
123
124
# File 'lib/jekyll/collection.rb', line 122

def entry_filter
  @entry_filter ||= Jekyll::EntryFilter.new(site, relative_directory)
end

#exists?Boolean

Checks whether the directory “exists” for this collection. The directory must exist on the filesystem and must not be a symlink

if in safe mode.

Returns false if the directory doesn't exist or if it's a symlink

and we're in safe mode.


114
115
116
# File 'lib/jekyll/collection.rb', line 114

def exists?
  File.directory?(directory) && !(File.symlink?(directory) && site.safe)
end

#extract_metadataObject

Extract options for this collection from the site configuration.

Returns the metadata for this collection



179
180
181
182
183
184
185
# File 'lib/jekyll/collection.rb', line 179

def 
  if site.config['collections'].is_a?(Hash)
    site.config['collections'][label] || Hash.new
  else
    {}
  end
end

#filesObject

Fetch the static files in this collection. Defaults to an empty array if no static files have been read in.

Returns an array of Jekyll::StaticFile objects.



29
30
31
# File 'lib/jekyll/collection.rb', line 29

def files
  @files ||= []
end

#filtered_entriesObject

Filtered version of the entries in this collection. See `Jekyll::EntryFilter#filter` for more information.

Returns a list of filtered entry paths.



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

def filtered_entries
  return Array.new unless exists?
  @filtered_entries ||=
    Dir.chdir(directory) do
      entry_filter.filter(entries).reject do |f|
        path = collection_dir(f)
        File.directory?(path) || (File.symlink?(f) && site.safe)
      end
    end
end

#inspectObject

An inspect string.

Returns the inspect string



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

def inspect
  "#<Jekyll::Collection @label=#{label} docs=#{docs}>"
end

#readObject

Read the allowed documents into the collection's array of docs.

Returns the sorted array of docs.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jekyll/collection.rb', line 36

def read
  filtered_entries.each do |file_path|
    full_path = collection_dir(file_path)
    next if File.directory?(full_path)
    if Utils.has_yaml_header? full_path
      doc = Jekyll::Document.new(full_path, { site: site, collection: self })
      doc.read
      docs << doc
    else
      relative_dir = Jekyll.sanitized_path(relative_directory, File.dirname(file_path)).chomp("/.")
      files << StaticFile.new(site, site.source, relative_dir, File.basename(full_path), self)
    end
  end
  docs.sort!
end

#relative_directoryObject

The directory for this Collection, relative to the site source.

Returns a String containing the directory name where the collection

is stored on the filesystem.


83
84
85
# File 'lib/jekyll/collection.rb', line 83

def relative_directory
  @relative_directory ||= "_#{label}"
end

#sanitize_label(label) ⇒ Object

Produce a sanitized label name Label names may not contain anything but alphanumeric characters,

underscores, and hyphens.

label - the possibly-unsafe label

Returns a sanitized version of the label.



140
141
142
# File 'lib/jekyll/collection.rb', line 140

def sanitize_label(label)
  label.gsub(/[^a-z0-9_\-\.]/i, '')
end

#to_liquidObject

Produce a representation of this Collection for use in Liquid. Exposes two attributes:

- label
- docs

Returns a representation of this collection for use in Liquid.



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

def to_liquid
  .merge({
    "label"     => label,
    "docs"      => docs,
    "files"     => files,
    "directory" => directory,
    "output"    => write?,
    "relative_directory" => relative_directory
  })
end

#url_templateObject

The URL template to render collection's documents at.

Returns the URL template to render collection's documents at.



172
173
174
# File 'lib/jekyll/collection.rb', line 172

def url_template
  .fetch('permalink', "/:collection/:path:output_ext")
end

#write?Boolean

Whether the collection's documents ought to be written as individual

files in the output.

Returns true if the 'write' metadata is true, false otherwise.



165
166
167
# File 'lib/jekyll/collection.rb', line 165

def write?
  !!['output']
end