Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Jekyll file types and site #1300

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jekyll.rb
Expand Up @@ -38,6 +38,7 @@ def require_all(path)
require 'jekyll/convertible'
require 'jekyll/url'
require 'jekyll/layout'
require 'jekyll/item'
require 'jekyll/page'
require 'jekyll/post'
require 'jekyll/excerpt'
Expand Down
13 changes: 6 additions & 7 deletions lib/jekyll/convertible.rb
Expand Up @@ -34,23 +34,22 @@ def merged_file_read_opts(opts)

# Read the YAML frontmatter.
#
# base - The String path to the dir containing the file.
# name - The String filename of the file.
# path - The String path of the file.
# opts - optional parameter to File.read, default at site configs
#
# Returns nothing.
def read_yaml(base, name, opts = {})
def read_yaml(path, opts ={})
begin
self.content = File.read(File.join(base, name),
merged_file_read_opts(opts))
self.content = File.read(path, merged_file_read_opts(opts))

if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
self.data = SafeYAML.load($1)
end
rescue SyntaxError => e
puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"
puts "YAML Exception reading #{path}: #{e.message}"
rescue Exception => e
puts "Error reading file #{File.join(base, name)}: #{e.message}"
puts "Error reading file #{path}: #{e.message}"
end

self.data ||= {}
Expand Down
12 changes: 1 addition & 11 deletions lib/jekyll/draft.rb
Expand Up @@ -13,24 +13,14 @@ def self.valid?(name)
name =~ MATCHER
end

# Get the full path to the directory containing the draft files
def containing_dir(source, dir)
File.join(source, dir, '_drafts')
end

# The path to the draft source file, relative to the site source
def relative_path
File.join(@dir, '_drafts', @name)
end

# Extract information from the post filename.
#
# name - The String filename of the post file.
#
# Returns nothing.
def process(name)
m, slug, ext = *name.match(MATCHER)
self.date = File.mtime(File.join(@base, name))
self.date = File.mtime(self.file_path)
self.slug = slug
self.ext = ext
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll/generators/pagination.rb
Expand Up @@ -43,10 +43,10 @@ def paginate(site, page)
(1..pages).each do |num_page|
pager = Pager.new(site, num_page, all_posts, pages)
if num_page > 1
newpage = Page.new(site, site.source, page.dir, page.name)
newpage = Page.new(site, File.join(site.source, page.dir, page.name))
newpage.pager = pager
newpage.dir = Pager.paginate_path(site, num_page)
site.pages << newpage
site.files << newpage
else
page.pager = pager
end
Expand Down
45 changes: 45 additions & 0 deletions lib/jekyll/item.rb
@@ -0,0 +1,45 @@
module Jekyll
class Item
include Convertible

attr_accessor :site, :pager, :name, :ext, :basename, :data, :content, :output, :file_path

# Initialize this Post instance.
#
# site - The Site.
# path - The String path of the post file.
#
# Returns the new Post.
def initialize(site, path)
@file_path = path
@site = site
@name = File.basename(path)
process(@name)
read_yaml(path)
end

# Add any necessary layouts to this post.
#
# layouts - A Hash of {"name" => "layout"}.
# site_payload - The site payload hash.
#
# Returns nothing.
def render(template, layouts, site_payload)
payload = template.deep_merge(site_payload)
do_layout(payload, layouts)
end

# The full path and filename of the post. Defined in the YAML of the post
# body.
#
# Returns the String permalink or nil if none has been set.
def permalink
data && data['permalink']
end

# Returns the object as a debug String.
def inspect
"#<Jekyll:#{self.class.name} @name=#{self.name.inspect}>"
end
end
end
26 changes: 6 additions & 20 deletions lib/jekyll/layout.rb
Expand Up @@ -2,35 +2,21 @@ module Jekyll
class Layout
include Convertible

# Gets the Site object.
attr_reader :site

# Gets the name of this layout.
attr_reader :name

# Gets/Sets the extension of this layout.
attr_accessor :ext

# Gets/Sets the Hash that holds the metadata for this layout.
attr_accessor :data

# Gets/Sets the content of this layout.
attr_accessor :content
attr_reader :site, :name
attr_accessor :data, :content

# Initialize a new Layout.
#
# site - The Site.
# base - The String path to the source.
# name - The String filename of the post file.
def initialize(site, base, name)
# path - The String path of the post file.
def initialize(site, path)
@site = site
@base = base
@name = name
@name = File.basename(path)

self.data = {}

process(name)
read_yaml(base, name)
read_yaml(path)
end

# Extract information from the layout filename.
Expand Down
49 changes: 20 additions & 29 deletions lib/jekyll/page.rb
@@ -1,11 +1,8 @@
module Jekyll
class Page
include Convertible
class Page < Item

attr_writer :dir
attr_accessor :site, :pager
attr_accessor :name, :ext, :basename
attr_accessor :data, :content, :output
attr_accessor :pager, :basename

# Attributes for Liquid templates
ATTRIBUTES_FOR_LIQUID = %w[
Expand All @@ -22,14 +19,9 @@ class Page
# base - The String path to the source.
# dir - The String path between the source and the file.
# name - The String filename of the file.
def initialize(site, base, dir, name)
@site = site
@base = base
@dir = dir
@name = name

process(name)
read_yaml(File.join(base, dir), name)
def initialize(site, path)
super(site, path)
@dir = File.dirname(path).sub(site.source, '')
end

# The generated directory into which the page will be placed
Expand All @@ -41,19 +33,6 @@ def dir
url[-1, 1] == '/' ? url : File.dirname(url)
end

# The full path and filename of the post. Defined in the YAML of the post
# body.
#
# Returns the String permalink or nil if none has been set.
def permalink
return nil if data.nil? || data['permalink'].nil?
if site.config['relative_permalinks']
File.join(@dir, data['permalink'])
else
data['permalink']
end
end

# The template of the permalink.
#
# Returns the template String.
Expand Down Expand Up @@ -109,12 +88,24 @@ def process(name)
#
# Returns nothing.
def render(layouts, site_payload)
payload = Utils.deep_merge_hashes({
relative_permalinks_deprecation_method if uses_relative_permalinks
template = {
"page" => to_liquid,
'paginator' => pager.to_liquid
}, site_payload)
}
super(template, layouts, site_payload)
end

do_layout(payload, layouts)
def relative_permalinks_deprecation_method
if config['relative_permalinks'] && has_relative_page?
$stderr.puts # Places newline after "Generating..."
Jekyll.logger.warn "Deprecation:", "Starting in 2.0, permalinks for pages" +
" in subfolders must be relative to the" +
" site source directory, not the parent" +
" directory. Check http://jekyllrb.com/docs/upgrading/"+
" for more info."
$stderr.print Jekyll.logger.formatted_topic("") + "..." # for "done."
end
end

# The path to the source file
Expand Down
74 changes: 30 additions & 44 deletions lib/jekyll/post.rb
@@ -1,7 +1,6 @@
module Jekyll
class Post
class Post < Item
include Comparable
include Convertible

# Valid post name regex.
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
Expand Down Expand Up @@ -33,35 +32,41 @@ def self.valid?(name)
name =~ MATCHER
end

attr_accessor :site
attr_accessor :data, :extracted_excerpt, :content, :output, :ext
attr_accessor :date, :slug, :tags, :categories

attr_reader :name
attr_accessor :extracted_excerpt, :date, :slug, :published, :tags, :categories

# Initialize this Post instance.
#
# site - The Site.
# base - The String path to the dir containing the post file.
# name - The String filename of the post file.
# path - The String path of the post file.
#
# Returns the new Post.
def initialize(site, source, dir, name)
@site = site
@dir = dir
@base = containing_dir(source, dir)
@name = name

self.categories = dir.downcase.split('/').reject { |x| x.empty? }
process(name)
read_yaml(@base, name)
def initialize(site, path)
super(site, path)
raise NameError, "Invalid filename" unless self.class.valid?(@name)
@dir = File.dirname(path).sub(site.source, '').sub(/(_posts|_drafts)/, '')
self.categories = @dir.downcase.split('/').reject { |x| x.empty? }
process(@name)
read_yaml(path)

if data.has_key?('date')
self.date = Time.parse(data["date"].to_s)
end

published = self.published?

populate_categories
populate_tags
raise RangeError, "Not published" unless published? && (site.future || self.date <= site.time)

site.aggregate_post_info(self)
end

def published?
if self.data.has_key?('published') && self.data['published'] == false
false
else
true
end
end

def populate_categories
Expand All @@ -75,19 +80,14 @@ def populate_tags
self.tags = Utils.pluralized_array_from_hash(data, "tag", "tags").flatten
end

# Get the full path to the directory containing the post files
def containing_dir(source, dir)
return File.join(source, dir, '_posts')
end

# Read the YAML frontmatter.
#
# base - The String path to the dir containing the file.
# name - The String filename of the file.
#
# Returns nothing.
def read_yaml(base, name)
super(base, name)
def read_yaml(path)
super(path)
self.extracted_excerpt = extract_excerpt
end

Expand Down Expand Up @@ -167,14 +167,6 @@ def dir
File.dirname(url)
end

# The full path and filename of the post. Defined in the YAML of the post
# body (optional).
#
# Returns the String permalink.
def permalink
data && data['permalink']
end

def template
case site.permalink_style
when :pretty
Expand Down Expand Up @@ -240,17 +232,11 @@ def related_posts(posts)
#
# Returns nothing.
def render(layouts, site_payload)
# construct payload
payload = Utils.deep_merge_hashes({
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
"page" => to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID)
}, site_payload)

if generate_excerpt?
extracted_excerpt.do_layout(payload, {})
end

do_layout(payload.merge({"page" => to_liquid}), layouts)
template = {
"page" => self.to_liquid,
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) }
}
super(template, layouts, site_payload)
end

# Obtain destination path.
Expand Down
3 changes: 0 additions & 3 deletions lib/jekyll/related_posts.rb
Expand Up @@ -28,15 +28,12 @@ def build
def build_index
self.class.lsi ||= begin
lsi = Classifier::LSI.new(:auto_rebuild => false)
display("Populating LSI...")

site.posts.each do |x|
lsi.add_item(x)
end

display("Rebuilding index...")
lsi.build_index
display("")
lsi
end
end
Expand Down