Merge pull request #2090 from coreyward/selfish

This commit is contained in:
Parker Moore 2014-02-21 18:19:32 -05:00
commit 788a3f7c8d
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'] Jekyll.logger.log_level = Jekyll::Stevenson::ERROR if options['quiet']
self.build(site, options) build(site, options)
self.watch(site, options) if options['watch'] watch(site, options) if options['watch']
end end
# Private: Build the site from source into destination. # Private: Build the site from source into destination.
@ -22,7 +22,7 @@ module Jekyll
Jekyll.logger.info "Source:", source Jekyll.logger.info "Source:", source
Jekyll.logger.info "Destination:", destination Jekyll.logger.info "Destination:", destination
print Jekyll.logger.formatted_topic "Generating..." print Jekyll.logger.formatted_topic "Generating..."
self.process_site(site) process_site(site)
puts "done." puts "done."
end end
@ -52,7 +52,7 @@ module Jekyll
t = Time.now.strftime("%Y-%m-%d %H:%M:%S") t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
n = modified.length + added.length + removed.length n = modified.length + added.length + removed.length
print Jekyll.logger.formatted_topic("Regenerating:") + "#{n} files at #{t} " print Jekyll.logger.formatted_topic("Regenerating:") + "#{n} files at #{t} "
self.process_site(site) process_site(site)
puts "...done." puts "...done."
end end
listener.start listener.start

View File

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

View File

@ -8,7 +8,7 @@ module Jekyll
attr_accessor :post attr_accessor :post
attr_accessor :content, :output, :ext attr_accessor :content, :output, :ext
def_delegator :@post, :site, :site def_delegator :@post, :site, :site
def_delegator :@post, :name, :name def_delegator :@post, :name, :name
def_delegator :@post, :ext, :ext def_delegator :@post, :ext, :ext
@ -39,7 +39,7 @@ module Jekyll
end end
# 'Path' of the excerpt. # 'Path' of the excerpt.
# #
# Returns the path for the post this excerpt belongs to with #excerpt appended # Returns the path for the post this excerpt belongs to with #excerpt appended
def path def path
File.join(post.path, "#excerpt") File.join(post.path, "#excerpt")
@ -47,9 +47,9 @@ module Jekyll
# Check if excerpt includes a string # Check if excerpt includes a string
# #
# Returns true if the string passed in # Returns true if the string passed in
def include?(something) def include?(something)
(self.output && self.output.include?(something)) || self.content.include?(something) (output && output.include?(something)) || content.include?(something)
end end
# The UID for this post (useful in feeds). # The UID for this post (useful in feeds).
@ -61,7 +61,7 @@ module Jekyll
end end
def to_s def to_s
self.output || self.content output || content
end end
# Returns the shorthand String identifier of this Post. # Returns the shorthand String identifier of this Post.

View File

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

View File

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

View File

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

View File

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