Remove unnecessary references to `self`

This commit is contained in:
Corey Ward 2014-02-17 15:15:15 -06:00
parent 276b400a86
commit f1c4e247a5
8 changed files with 156 additions and 162 deletions

View File

@ -6,8 +6,8 @@ module Jekyll
Jekyll.logger.log_level = Jekyll::Stevenson::ERROR if options['quiet']
self.build(site, options)
self.watch(site, options) if options['watch']
build(site, options)
watch(site, options) if options['watch']
end
# Private: Build the site from source into destination.
@ -22,7 +22,7 @@ module Jekyll
Jekyll.logger.info "Source:", source
Jekyll.logger.info "Destination:", destination
print Jekyll.logger.formatted_topic "Generating..."
self.process_site(site)
process_site(site)
puts "done."
end
@ -52,7 +52,7 @@ module Jekyll
t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
n = modified.length + added.length + removed.length
print Jekyll.logger.formatted_topic("Regenerating:") + "#{n} files at #{t} "
self.process_site(site)
process_site(site)
puts "...done."
end
listener.start

View File

@ -18,18 +18,18 @@ module Jekyll
module Convertible
# Returns the contents as a String.
def to_s
self.content || ''
content || ''
end
# Whether the file is published or not, as indicated in YAML front-matter
def published?
!(self.data.has_key?('published') && self.data['published'] == false)
!(data.has_key?('published') && data['published'] == false)
end
# Returns merged option hash for File.read of self.site (if exists)
# and a given param
def merged_file_read_opts(opts)
(self.site ? self.site.file_read_opts : {}).merge(opts)
(site ? site.file_read_opts : {}).merge(opts)
end
# Read the YAML frontmatter.
@ -43,7 +43,7 @@ module Jekyll
begin
self.content = File.read(File.join(base, name),
merged_file_read_opts(opts))
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
self.data = SafeYAML.load($1)
end
@ -60,10 +60,10 @@ module Jekyll
#
# Returns nothing.
def transform
self.content = converter.convert(self.content)
self.content = converter.convert(content)
rescue => e
Jekyll.logger.error "Conversion error:", "There was an error converting" +
" '#{self.path}'."
" '#{path}'."
raise e
end
@ -72,7 +72,7 @@ module Jekyll
# Returns the String extension for the output file.
# e.g. ".html" for an HTML output file.
def output_ext
converter.output_ext(self.ext)
converter.output_ext(ext)
end
# Determine which converter to use based on this convertible's
@ -80,7 +80,7 @@ module Jekyll
#
# Returns the Converter instance.
def converter
@converter ||= self.site.converters.find { |c| c.matches(self.ext) }
@converter ||= site.converters.find { |c| c.matches(ext) }
end
# Render Liquid in the content
@ -119,16 +119,16 @@ module Jekyll
# Returns nothing
def render_all_layouts(layouts, payload, info)
# recursively render layouts
layout = layouts[self.data["layout"]]
layout = layouts[data["layout"]]
used = Set.new([layout])
while layout
payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
payload = payload.deep_merge({"content" => output, "page" => layout.data})
self.output = self.render_liquid(layout.content,
self.output = render_liquid(layout.content,
payload,
info,
File.join(self.site.config['layouts'], layout.name))
File.join(site.config['layouts'], layout.name))
if layout = layouts[layout.data["layout"]]
if used.include?(layout)
@ -147,21 +147,19 @@ module Jekyll
#
# Returns nothing.
def do_layout(payload, layouts)
info = { :filters => [Jekyll::Filters], :registers => { :site => self.site, :page => payload['page'] } }
info = { :filters => [Jekyll::Filters], :registers => { :site => site, :page => payload['page'] } }
# render and transform content (this becomes the final content of the object)
payload["highlighter_prefix"] = converter.highlighter_prefix
payload["highlighter_suffix"] = converter.highlighter_suffix
self.content = self.render_liquid(self.content,
payload,
info)
self.transform
self.content = render_liquid(content, payload, info)
transform
# output keeps track of what will finally be written
self.output = self.content
self.output = content
self.render_all_layouts(layouts, payload, info)
render_all_layouts(layouts, payload, info)
end
# Write the generated page file to the destination directory.
@ -173,7 +171,7 @@ module Jekyll
path = destination(dest)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'wb') do |f|
f.write(self.output)
f.write(output)
end
end

View File

@ -49,7 +49,7 @@ module Jekyll
#
# Returns true if the string passed in
def include?(something)
(self.output && self.output.include?(something)) || self.content.include?(something)
(output && output.include?(something)) || content.include?(something)
end
# The UID for this post (useful in feeds).
@ -61,7 +61,7 @@ module Jekyll
end
def to_s
self.output || self.content
output || content
end
# Returns the shorthand String identifier of this Post.

View File

@ -29,8 +29,8 @@ module Jekyll
self.data = {}
self.process(name)
self.read_yaml(base, name)
process(name)
read_yaml(base, name)
end
# Extract information from the layout filename.

View File

@ -28,8 +28,8 @@ module Jekyll
@dir = dir
@name = name
self.process(name)
self.read_yaml(File.join(base, dir), name)
process(name)
read_yaml(File.join(base, dir), name)
end
# The generated directory into which the page will be placed
@ -46,11 +46,11 @@ module Jekyll
#
# Returns the String permalink or nil if none has been set.
def permalink
return nil if self.data.nil? || self.data['permalink'].nil?
return nil if data.nil? || data['permalink'].nil?
if site.config['relative_permalinks']
File.join(@dir, self.data['permalink'])
File.join(@dir, data['permalink'])
else
self.data['permalink']
data['permalink']
end
end
@ -58,7 +58,7 @@ module Jekyll
#
# Returns the template String.
def template
if self.site.permalink_style == :pretty
if site.permalink_style == :pretty
if index? && html?
"/:path/"
elsif html?
@ -87,8 +87,8 @@ module Jekyll
def url_placeholders
{
:path => @dir,
:basename => self.basename,
:output_ext => self.output_ext
:basename => basename,
:output_ext => output_ext
}
end
@ -99,7 +99,7 @@ module Jekyll
# Returns nothing.
def process(name)
self.ext = File.extname(name)
self.basename = name[0 .. -self.ext.length-1]
self.basename = name[0 .. -ext.length - 1]
end
# Add any necessary layouts to this post
@ -110,7 +110,7 @@ module Jekyll
# Returns nothing.
def render(layouts, site_payload)
payload = {
"page" => self.to_liquid,
"page" => to_liquid,
'paginator' => pager.to_liquid
}.deep_merge(site_payload)
@ -121,7 +121,7 @@ module Jekyll
#
# Returns the path to the source file
def path
self.data.fetch('path', self.relative_path.sub(/\A\//, ''))
data.fetch('path', relative_path.sub(/\A\//, ''))
end
# The path to the page source file, relative to the site source
@ -135,14 +135,14 @@ module Jekyll
#
# Returns the destination file path String.
def destination(dest)
path = File.join(dest, File.expand_path(self.url, "/"))
path = File.join(path, "index.html") if self.url =~ /\/$/
path = File.join(dest, File.expand_path(url, "/"))
path = File.join(path, "index.html") if url =~ /\/$/
path
end
# Returns the object as a debug String.
def inspect
"#<Jekyll:Page @name=#{self.name.inspect}>"
"#<Jekyll:Page @name=#{name.inspect}>"
end
# Returns the Boolean of whether this Page is HTML or not.

View File

@ -49,30 +49,30 @@ module Jekyll
def initialize(site, source, dir, name)
@site = site
@dir = dir
@base = self.containing_dir(source, dir)
@base = containing_dir(source, dir)
@name = name
self.categories = dir.downcase.split('/').reject { |x| x.empty? }
self.process(name)
self.read_yaml(@base, name)
process(name)
read_yaml(@base, name)
if self.data.has_key?('date')
self.date = Time.parse(self.data["date"].to_s)
if data.has_key?('date')
self.date = Time.parse(data["date"].to_s)
end
self.populate_categories
self.populate_tags
populate_categories
populate_tags
end
def populate_categories
if self.categories.empty?
self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.to_s.downcase}
if categories.empty?
self.categories = data.pluralized_array('category', 'categories').map {|c| c.to_s.downcase}
end
self.categories.flatten!
categories.flatten!
end
def populate_tags
self.tags = self.data.pluralized_array("tag", "tags").flatten
self.tags = data.pluralized_array("tag", "tags").flatten
end
# Get the full path to the directory containing the post files
@ -88,7 +88,7 @@ module Jekyll
# Returns nothing.
def read_yaml(base, name)
super(base, name)
self.extracted_excerpt = self.extract_excerpt
self.extracted_excerpt = extract_excerpt
end
# The post excerpt. This is either a custom excerpt
@ -96,19 +96,19 @@ module Jekyll
#
# Returns excerpt string.
def excerpt
self.data.fetch('excerpt', self.extracted_excerpt.to_s)
data.fetch('excerpt', extracted_excerpt.to_s)
end
# Public: the Post title, from the YAML Front-Matter or from the slug
#
# Returns the post title
def title
self.data.fetch("title", self.titleized_slug)
data.fetch("title", titleized_slug)
end
# Turns the post slug into a suitable title
def titleized_slug
self.slug.split('-').select {|w| w.capitalize! || w }.join(' ')
slug.split('-').select {|w| w.capitalize! || w }.join(' ')
end
# Public: the path to the post relative to the site source,
@ -118,7 +118,7 @@ module Jekyll
#
# Returns the path to the file relative to the site source
def path
self.data.fetch('path', self.relative_path.sub(/\A\//, ''))
data.fetch('path', relative_path.sub(/\A\//, ''))
end
# The path to the post source file, relative to the site source
@ -172,11 +172,11 @@ module Jekyll
#
# Returns the String permalink.
def permalink
self.data && self.data['permalink']
data && data['permalink']
end
def template
case self.site.permalink_style
case site.permalink_style
when :pretty
"/:categories/:year/:month/:day/:title/"
when :none
@ -186,7 +186,7 @@ module Jekyll
when :ordinal
"/:categories/:year/:y_day/:title.html"
else
self.site.permalink_style.to_s
site.permalink_style.to_s
end
end
@ -214,7 +214,7 @@ module Jekyll
:categories => (categories || []).map { |c| URI.escape(c.to_s) }.join('/'),
:short_month => date.strftime("%b"),
:y_day => date.strftime("%j"),
:output_ext => self.output_ext
:output_ext => output_ext
}
end
@ -223,7 +223,7 @@ module Jekyll
#
# Returns the String UID.
def id
File.join(self.dir, self.slug)
File.join(dir, slug)
end
# Calculate related posts.
@ -243,14 +243,14 @@ module Jekyll
# construct payload
payload = {
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
"page" => self.to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID)
"page" => to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID)
}.deep_merge(site_payload)
if generate_excerpt?
self.extracted_excerpt.do_layout(payload, {})
extracted_excerpt.do_layout(payload, {})
end
do_layout(payload.merge({"page" => self.to_liquid}), layouts)
do_layout(payload.merge({"page" => to_liquid}), layouts)
end
# Obtain destination path.
@ -260,29 +260,29 @@ module Jekyll
# Returns destination file path String.
def destination(dest)
# The url needs to be unescaped in order to preserve the correct filename
path = File.join(dest, File.expand_path(CGI.unescape(self.url), "/"))
path = File.join(dest, File.expand_path(CGI.unescape(url), "/"))
path = File.join(path, "index.html") if path[/\.html$/].nil?
path
end
# Returns the shorthand String identifier of this Post.
def inspect
"<Post: #{self.id}>"
"<Post: #{id}>"
end
def next
pos = self.site.posts.index {|post| post.equal?(self) }
if pos && pos < self.site.posts.length-1
self.site.posts[pos+1]
pos = site.posts.index {|post| post.equal?(self) }
if pos && pos < site.posts.length - 1
site.posts[pos + 1]
else
nil
end
end
def previous
pos = self.site.posts.index {|post| post.equal?(self) }
pos = site.posts.index {|post| post.equal?(self) }
if pos && pos > 0
self.site.posts[pos-1]
site.posts[pos - 1]
else
nil
end

View File

@ -14,9 +14,9 @@ module Jekyll
end
def build
return [] unless self.site.posts.size > 1
return [] unless site.posts.size > 1
if self.site.lsi
if site.lsi
build_index
lsi_related_posts
else
@ -30,7 +30,7 @@ module Jekyll
lsi = Classifier::LSI.new(:auto_rebuild => false)
display("Populating LSI...")
self.site.posts.each do |x|
site.posts.each do |x|
lsi.add_item(x)
end
@ -42,11 +42,11 @@ module Jekyll
end
def lsi_related_posts
self.class.lsi.find_related(post.content, 11) - [self.post]
self.class.lsi.find_related(post.content, 11) - [post]
end
def most_recent_posts
recent_posts = self.site.posts.reverse - [self.post]
recent_posts = site.posts.reverse - [post]
recent_posts.first(10)
end

View File

@ -25,31 +25,27 @@ module Jekyll
self.file_read_opts = {}
self.file_read_opts[:encoding] = config['encoding'] if config['encoding']
self.reset
self.setup
reset
setup
end
# Public: Read, process, and write this Site to output.
#
# Returns nothing.
def process
self.reset
self.read
self.generate
self.render
self.cleanup
self.write
reset
read
generate
render
cleanup
write
end
# Reset Site details.
#
# Returns nothing
def reset
self.time = if self.config['time']
Time.parse(self.config['time'].to_s)
else
Time.now
end
self.time = (config['time'] ? Time.parse(config['time'].to_s) : Time.now)
self.layouts = {}
self.posts = []
self.pages = []
@ -58,7 +54,7 @@ module Jekyll
self.tags = Hash.new { |hash, key| hash[key] = [] }
self.data = {}
if self.limit_posts < 0
if limit_posts < 0
raise ArgumentError, "limit_posts must be a non-negative number"
end
end
@ -71,8 +67,8 @@ module Jekyll
# If safe mode is off, load in any Ruby files under the plugins
# directory.
unless self.safe
self.plugins.each do |plugins|
unless safe
plugins.each do |plugins|
Dir[File.join(plugins, "**/*.rb")].sort.each do |f|
require f
end
@ -88,16 +84,16 @@ module Jekyll
# Check that the destination dir isn't the source dir or a directory
# parent to the source dir.
def ensure_not_in_dest
dest = Pathname.new(self.dest)
Pathname.new(self.source).ascend do |path|
if path == dest
dest_pathname = Pathname.new(dest)
Pathname.new(source).ascend do |path|
if path == dest_pathname
raise FatalException.new "Destination directory cannot be or contain the Source directory."
end
end
end
def require_gems
self.gems.each do |gem|
gems.each do |gem|
if plugin_allowed?(gem)
require gem
end
@ -105,11 +101,11 @@ module Jekyll
end
def plugin_allowed?(gem_name)
whitelist.include?(gem_name) || !self.safe
whitelist.include?(gem_name) || !safe
end
def whitelist
@whitelist ||= Array[self.config['whitelist']].flatten || []
@whitelist ||= Array[config['whitelist']].flatten
end
# Internal: Setup the plugin search path
@ -117,7 +113,7 @@ module Jekyll
# Returns an Array of plugin search paths
def plugins_path
if (config['plugins'] == Jekyll::Configuration::DEFAULTS['plugins'])
[File.join(self.source, config['plugins'])]
[File.join(source, config['plugins'])]
else
Array(config['plugins']).map { |d| File.expand_path(d) }
end
@ -128,8 +124,8 @@ module Jekyll
# Returns nothing.
def read
self.layouts = LayoutReader.new(self).read
self.read_directories
self.read_data(config['data_source'])
read_directories
read_data(config['data_source'])
end
# Recursively traverse directories to find posts, pages and static files
@ -140,24 +136,24 @@ module Jekyll
#
# Returns nothing.
def read_directories(dir = '')
base = File.join(self.source, dir)
base = File.join(source, dir)
entries = Dir.chdir(base) { filter_entries(Dir.entries('.'), base) }
self.read_posts(dir)
self.read_drafts(dir) if self.show_drafts
self.posts.sort!
read_posts(dir)
read_drafts(dir) if show_drafts
posts.sort!
limit_posts! if limit_posts > 0 # limit the posts if :limit_posts option is set
entries.each do |f|
f_abs = File.join(base, f)
if File.directory?(f_abs)
f_rel = File.join(dir, f)
read_directories(f_rel) unless self.dest.sub(/\/$/, '') == f_abs
read_directories(f_rel) unless dest.sub(/\/$/, '') == f_abs
elsif has_yaml_header?(f_abs)
page = Page.new(self, self.source, dir, f)
page = Page.new(self, source, dir, f)
pages << page if page.published?
else
static_files << StaticFile.new(self, self.source, dir, f)
static_files << StaticFile.new(self, source, dir, f)
end
end
@ -174,7 +170,7 @@ module Jekyll
posts = read_content(dir, '_posts', Post)
posts.each do |post|
if post.published? && (self.future || post.date <= self.time)
if post.published? && (future || post.date <= time)
aggregate_post_info(post)
end
end
@ -196,7 +192,7 @@ module Jekyll
def read_content(dir, magic_dir, klass)
get_entries(dir, magic_dir).map do |entry|
klass.new(self, self.source, dir, entry) if klass.valid?(entry)
klass.new(self, source, dir, entry) if klass.valid?(entry)
end.reject do |entry|
entry.nil?
end
@ -206,15 +202,15 @@ module Jekyll
#
# Returns nothing
def read_data(dir)
base = File.join(self.source, dir)
return unless File.directory?(base) && (!self.safe || !File.symlink?(base))
base = File.join(source, dir)
return unless File.directory?(base) && (!safe || !File.symlink?(base))
entries = Dir.chdir(base) { Dir['*.{yaml,yml}'] }
entries.delete_if { |e| File.directory?(File.join(base, e)) }
entries.each do |entry|
path = File.join(self.source, dir, entry)
next if File.symlink?(path) && self.safe
path = File.join(source, dir, entry)
next if File.symlink?(path) && safe
key = sanitize_filename(File.basename(entry, '.*'))
self.data[key] = SafeYAML.load_file(path)
@ -225,7 +221,7 @@ module Jekyll
#
# Returns nothing.
def generate
self.generators.each do |generator|
generators.each do |generator|
generator.generate(self)
end
end
@ -237,12 +233,12 @@ module Jekyll
relative_permalinks_deprecation_method
payload = site_payload
[self.posts, self.pages].flatten.each do |page_or_post|
page_or_post.render(self.layouts, payload)
[posts, pages].flatten.each do |page_or_post|
page_or_post.render(layouts, payload)
end
self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a } }
self.tags.values.map { |ps| ps.sort! { |a, b| b <=> a } }
categories.values.map { |ps| ps.sort! { |a, b| b <=> a } }
tags.values.map { |ps| ps.sort! { |a, b| b <=> a } }
rescue Errno::ENOENT => e
# ignore missing layout dir
end
@ -258,7 +254,7 @@ module Jekyll
#
# Returns nothing.
def write
each_site_file { |item| item.write(self.dest) }
each_site_file { |item| item.write(dest) }
end
# Construct a Hash of Posts indexed by the specified Post attribute.
@ -277,8 +273,8 @@ module Jekyll
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 { |hsh, key| hsh[key] = Array.new }
self.posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
hash = Hash.new { |hash, key| hash[key] = [] }
posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
hash.values.map { |sortme| sortme.sort! { |a, b| b <=> a } }
hash
end
@ -288,7 +284,7 @@ module Jekyll
#
# Returns the Hash to be hooked to site.data.
def site_data
self.config['data'] || self.data
config['data'] || data
end
# The Hash payload containing site-wide data.
@ -306,12 +302,12 @@ module Jekyll
# See Site#post_attr_hash for type info.
def site_payload
{"jekyll" => { "version" => Jekyll::VERSION },
"site" => self.config.merge({
"time" => self.time,
"posts" => self.posts.sort { |a, b| b <=> a },
"pages" => self.pages,
"static_files" => self.static_files.sort { |a, b| a.relative_path <=> b.relative_path },
"html_pages" => self.pages.reject { |page| !page.html? },
"site" => config.merge({
"time" => time,
"posts" => posts.sort { |a, b| b <=> a },
"pages" => pages,
"static_files" => static_files.sort { |a, b| a.relative_path <=> b.relative_path },
"html_pages" => pages.reject { |page| !page.html? },
"categories" => post_attr_hash('categories'),
"tags" => post_attr_hash('tags'),
"data" => site_data})}
@ -335,7 +331,7 @@ module Jekyll
#
# Returns the Converter instance implementing the given Converter.
def getConverterImpl(klass)
matches = self.converters.select { |c| c.class == klass }
matches = converters.select { |c| c.class == klass }
if impl = matches.first
impl
else
@ -352,9 +348,9 @@ module Jekyll
# Returns array of instances of subclasses of parameter
def instantiate_subclasses(klass)
klass.subclasses.select do |c|
!self.safe || c.safe
!safe || c.safe
end.sort.map do |c|
c.new(self.config)
c.new(config)
end
end
@ -365,7 +361,7 @@ module Jekyll
#
# Returns the list of entries to process
def get_entries(dir, subfolder)
base = File.join(self.source, dir, subfolder)
base = File.join(source, dir, subfolder)
return [] unless File.exists?(base)
entries = Dir.chdir(base) { filter_entries(Dir['**/*'], base) }
entries.delete_if { |e| File.directory?(File.join(base, e)) }
@ -377,9 +373,9 @@ module Jekyll
#
# Returns nothing
def aggregate_post_info(post)
self.posts << post
post.categories.each { |c| self.categories[c] << post }
post.tags.each { |c| self.tags[c] << post }
posts << post
post.categories.each { |c| categories[c] << post }
post.tags.each { |c| tags[c] << post }
end
def relative_permalinks_deprecation_method
@ -396,7 +392,7 @@ module Jekyll
def each_site_file
%w(posts pages static_files).each do |type|
self.send(type).each do |item|
send(type).each do |item|
yield item
end
end
@ -405,7 +401,7 @@ module Jekyll
private
def has_relative_page?
self.pages.any? { |page| page.uses_relative_permalinks }
pages.any? { |page| page.uses_relative_permalinks }
end
def has_yaml_header?(file)
@ -413,8 +409,8 @@ module Jekyll
end
def limit_posts!
limit = self.posts.length < limit_posts ? self.posts.length : limit_posts
self.posts = self.posts[-limit, limit]
limit = posts.length < limit_posts ? posts.length : limit_posts
self.posts = posts[-limit, limit]
end
def site_cleaner
@ -422,9 +418,9 @@ module Jekyll
end
def sanitize_filename(name)
name = name.gsub(/[^\w\s_-]+/, '')
name = name.gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
name = name.gsub(/\s+/, '_')
name.gsub!(/[^\w\s_-]+/, '')
name.gsub!(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
name.gsub(/\s+/, '_')
end
end
end