Class: Jekyll::Document

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/jekyll/document.rb

Constant Summary collapse

YAML_FRONT_MATTER_REGEXP =
/\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, relations) ⇒ Document

Create a new Document.

site - the Jekyll::Site instance to which this Document belongs path - the path to the file

Returns nothing.


18
19
20
21
22
23
24
25
# File 'lib/jekyll/document.rb', line 18

def initialize(path, relations)
  @site = relations[:site]
  @path = path
  @extname = File.extname(path)
  @output_ext = Jekyll::Renderer.new(site, self).output_ext
  @collection = relations[:collection]
  @has_yaml_header = nil
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection


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

def collection
  @collection
end

#contentObject

Returns the value of attribute content


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

def content
  @content
end

#extnameObject (readonly)

Returns the value of attribute extname


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

def extname
  @extname
end

#outputObject

Returns the value of attribute output


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

def output
  @output
end

#output_extObject (readonly)

Returns the value of attribute output_ext


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

def output_ext
  @output_ext
end

#pathObject (readonly)

Returns the value of attribute path


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

def path
  @path
end

#siteObject (readonly)

Returns the value of attribute site


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

def site
  @site
end

Instance Method Details

#<=>(anotherDocument) ⇒ Object

Compare this document against another document. Comparison is a comparison between the 2 paths of the documents.

Returns -1, 0, +1 or nil depending on whether this doc's path is less than,

equal or greater than the other doc's path. See String#<=> for more details.

270
271
272
# File 'lib/jekyll/document.rb', line 270

def <=>(anotherDocument)
  path <=> anotherDocument.path
end

#asset_file?Boolean

Determine whether the document is an asset file. Asset files include CoffeeScript files and Sass/SCSS files.

Returns true if the extname belongs to the set of extensions

that asset files use.

Returns:

  • (Boolean)

85
86
87
# File 'lib/jekyll/document.rb', line 85

def asset_file?
  sass_file? || coffeescript_file?
end

#basenameObject

The base filename of the document.

Returns the base filename of the document.


53
54
55
# File 'lib/jekyll/document.rb', line 53

def basename
  @basename ||= File.basename(path)
end

#basename_without_extObject

The base filename of the document, without the file extname.

Returns the basename without the file extname.


46
47
48
# File 'lib/jekyll/document.rb', line 46

def basename_without_ext
  @basename_without_ext ||= File.basename(path, '.*')
end

#cleaned_relative_pathObject

Produces a “cleaned” relative path. The “cleaned” relative path is the relative path without the extname

and with the collection's directory removed as well.

This method is useful when building the URL of the document.

Examples:

When relative_path is "_methods/site/generate.md":
  cleaned_relative_path
  # => "/site/generate"

Returns the cleaned relative path of the document.


68
69
70
71
# File 'lib/jekyll/document.rb', line 68

def cleaned_relative_path
  @cleaned_relative_path ||=
    relative_path[0 .. -extname.length - 1].sub(collection.relative_directory, "")
end

#coffeescript_file?Boolean

Determine whether the document is a CoffeeScript file.

Returns true if extname == .coffee, false otherwise.

Returns:

  • (Boolean)

99
100
101
# File 'lib/jekyll/document.rb', line 99

def coffeescript_file?
  '.coffee'.eql?(extname)
end

#dataObject

Fetch the Document's data.

Returns a Hash containing the data. An empty hash is returned if

no data was read.

31
32
33
# File 'lib/jekyll/document.rb', line 31

def data
  @data ||= Hash.new
end

#destination(base_directory) ⇒ Object

The full path to the output file.

base_directory - the base path of the output directory

Returns the full path to the output file of this document.


164
165
166
167
168
169
170
# File 'lib/jekyll/document.rb', line 164

def destination(base_directory)
  dest = site.in_dest_dir(base_directory)
  path = site.in_dest_dir(dest, URL.unescape_path(url))
  path = File.join(path, "index.html") if url.end_with?("/")
  path << output_ext unless path.end_with?(output_ext)
  path
end

#inspectObject

The inspect string for this document. Includes the relative path and the collection label.

Returns the inspect string for this document.


254
255
256
# File 'lib/jekyll/document.rb', line 254

def inspect
  "#<Jekyll::Document #{relative_path} collection=#{collection.label}>"
end

#merged_file_read_opts(opts) ⇒ Object

Returns merged option hash for File.read of self.site (if exists) and a given param

opts - override options

Return the file read options hash.


191
192
193
# File 'lib/jekyll/document.rb', line 191

def merged_file_read_opts(opts)
  site ? site.file_read_opts.merge(opts) : opts
end

The permalink for this Document. Permalink is set via the data Hash.

Returns the permalink or nil if no permalink was set in the data.


144
145
146
# File 'lib/jekyll/document.rb', line 144

def permalink
  data && data.is_a?(Hash) && data['permalink']
end

#place_in_layout?Boolean

Determine whether the file should be placed into layouts.

Returns false if the document is either an asset file or a yaml file,

true otherwise.

Returns:

  • (Boolean)

115
116
117
# File 'lib/jekyll/document.rb', line 115

def place_in_layout?
  !(asset_file? || yaml_file?)
end

#published?Boolean

Whether the file is published or not, as indicated in YAML front-matter

Returns true if the 'published' key is specified in the YAML front-matter and not `false`.

Returns:

  • (Boolean)

198
199
200
# File 'lib/jekyll/document.rb', line 198

def published?
  !(data.key?('published') && data['published'] == false)
end

#read(opts = {}) ⇒ Object

Read in the file and assign the content and data based on the file contents. Merge the frontmatter of the file with the frontmatter default values

Returns nothing.


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/jekyll/document.rb', line 207

def read(opts = {})
  if yaml_file?
    @data = SafeYAML.load_file(path)
  else
    begin
      defaults = @site.frontmatter_defaults.all(url, collection.label.to_sym)
      unless defaults.empty?
        @data = defaults
      end
      @content = File.read(path, merged_file_read_opts(opts))
      if content =~ YAML_FRONT_MATTER_REGEXP
        @content = $POSTMATCH
        data_file = SafeYAML.load($1)
        unless data_file.nil?
          @data = Utils.deep_merge_hashes(defaults, data_file)
        end
      end
    rescue SyntaxError => e
      puts "YAML Exception reading #{path}: #{e.message}"
    rescue Exception => e
      puts "Error reading file #{path}: #{e.message}"
    end
  end
end

#relative_pathObject

The path to the document, relative to the site source.

Returns a String path which represents the relative path

from the site source to this document

39
40
41
# File 'lib/jekyll/document.rb', line 39

def relative_path
  @relative_path ||= Pathname.new(path).relative_path_from(Pathname.new(site.source)).to_s
end

#render_with_liquid?Boolean

Determine whether the file should be rendered with Liquid.

Returns false if the document is either an asset file or a yaml file,

true otherwise.

Returns:

  • (Boolean)

107
108
109
# File 'lib/jekyll/document.rb', line 107

def render_with_liquid?
  !(coffeescript_file? || yaml_file?)
end

#sass_file?Boolean

Determine whether the document is a Sass file.

Returns true if extname == .sass or .scss, false otherwise.

Returns:

  • (Boolean)

92
93
94
# File 'lib/jekyll/document.rb', line 92

def sass_file?
  %w[.sass .scss].include?(extname)
end

#to_liquidObject

Create a Liquid-understandable version of this Document.

Returns a Hash representing this Document's data.


235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/jekyll/document.rb', line 235

def to_liquid
  if data.is_a?(Hash)
    Utils.deep_merge_hashes data, {
      "output"        => output,
      "content"       => content,
      "relative_path" => relative_path,
      "path"          => relative_path,
      "url"           => url,
      "collection"    => collection.label
    }
  else
    data
  end
end

#to_sObject

The string representation for this document.

Returns the content of the document


261
262
263
# File 'lib/jekyll/document.rb', line 261

def to_s
  content || ''
end

#urlObject

The computed URL for the document. See `Jekyll::URL#to_s` for more details.

Returns the computed URL for the document.


151
152
153
154
155
156
157
# File 'lib/jekyll/document.rb', line 151

def url
  @url = URL.new({
    template:     url_template,
    placeholders: url_placeholders,
    permalink:    permalink
  }).to_s
end

#url_placeholdersObject

Construct a Hash of key-value pairs which contain a mapping between

a key in the URL template and the corresponding value for this document.

Returns the Hash of key-value pairs for replacement in the URL.


130
131
132
133
134
135
136
137
138
# File 'lib/jekyll/document.rb', line 130

def url_placeholders
  {
    collection: collection.label,
    path:       cleaned_relative_path,
    output_ext: output_ext,
    name:       Utils.slugify(basename_without_ext),
    title:      Utils.slugify(data['slug']) || Utils.slugify(basename_without_ext)
  }
end

#url_templateObject

The URL template where the document would be accessible.

Returns the URL template for the document.


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

def url_template
  collection.url_template
end

#write(dest) ⇒ Object

Write the generated Document file to the destination directory.

dest - The String path to the destination dir.

Returns nothing.


177
178
179
180
181
182
183
# File 'lib/jekyll/document.rb', line 177

def write(dest)
  path = destination(dest)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'wb') do |f|
    f.write(output)
  end
end

#write?Boolean

Determine whether this document should be written. Based on the Collection to which it belongs.

True if the document has a collection and if that collection's #write?

method returns true, otherwise false.

Returns:

  • (Boolean)

279
280
281
# File 'lib/jekyll/document.rb', line 279

def write?
  collection && collection.write?
end

#yaml_file?Boolean

Determine whether the document is a YAML file.

Returns true if the extname is either .yml or .yaml, false otherwise.

Returns:

  • (Boolean)

76
77
78
# File 'lib/jekyll/document.rb', line 76

def yaml_file?
  %w[.yaml .yml].include?(extname)
end