Merge pull request #1341 from maul-esel/minor-refactors

Minor refactors
This commit is contained in:
Matt Rogers 2013-08-29 20:21:08 -07:00
commit a9e2a74ea6
6 changed files with 61 additions and 63 deletions

View File

@ -14,14 +14,10 @@ module Jekyll
def convert(content)
# Check for use of coderay
if @config['kramdown']['use_coderay']
@config['kramdown'].merge!({
:coderay_wrap => @config['kramdown']['coderay']['coderay_wrap'],
:coderay_line_numbers => @config['kramdown']['coderay']['coderay_line_numbers'],
:coderay_line_number_start => @config['kramdown']['coderay']['coderay_line_number_start'],
:coderay_tab_width => @config['kramdown']['coderay']['coderay_tab_width'],
:coderay_bold_every => @config['kramdown']['coderay']['coderay_bold_every'],
:coderay_css => @config['kramdown']['coderay']['coderay_css']
})
%w[wrap line_numbers line_numbers_start tab_width bold_every css default_lang].each do |opt|
key = "coderay_#{opt}"
@config['kramdown'][key.to_sym] = @config['kramdown']['coderay'][key] unless @config['kramdown'].has_key?(key)
end
end
Kramdown::Document.new(content, @config["kramdown"].symbolize_keys).to_html

View File

@ -84,6 +84,16 @@ module Jekyll
raise e
end
# Convert this Convertible's data to a Hash suitable for use by Liquid.
#
# Returns the Hash representation of this Convertible.
def to_liquid(attrs = nil)
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
[attribute, send(attribute)]
}]
data.deep_merge(further_data)
end
# Recursively render layouts
#
# layouts - a list of the layouts

View File

@ -7,6 +7,13 @@ module Jekyll
attr_accessor :name, :ext, :basename
attr_accessor :data, :content, :output
# Attributes for Liquid templates
ATTRIBUTES_FOR_LIQUID = %w[
url
content
path
]
# Initialize a new Page.
#
# site - The Site object.
@ -108,21 +115,16 @@ module Jekyll
do_layout(payload, layouts)
end
# Convert this Page's data to a Hash suitable for use by Liquid.
#
# Returns the Hash representation of this Page.
def to_liquid
self.data.deep_merge({
"url" => self.url,
"content" => self.content,
"path" => self.data['path'] || path })
end
# The path to the source file
#
# Returns the path to the source file
def path
File.join(@dir, @name).sub(/\A\//, '')
self.data.fetch('path', self.relative_path.sub(/\A\//, ''))
end
# The path to the page source file, relative to the site source
def relative_path
File.join(@dir, @name)
end
# Obtain destination path.

View File

@ -3,10 +3,6 @@ module Jekyll
include Comparable
include Convertible
class << self
attr_accessor :lsi
end
# Valid post name regex.
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
@ -109,18 +105,19 @@ module Jekyll
#
# Returns excerpt string.
def excerpt
if self.data.has_key? 'excerpt'
self.data['excerpt']
else
self.extracted_excerpt.to_s
end
self.data.fetch('excerpt', self.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["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' ')
self.data.fetch("title", self.titleized_slug)
end
# Turns the post slug into a suitable title
def titleized_slug
self.slug.split('-').select {|w| w.capitalize! || w }.join(' ')
end
# Public: the path to the post relative to the site source,
@ -130,7 +127,12 @@ module Jekyll
#
# Returns the path to the file relative to the site source
def path
self.data['path'] || File.join(@dir, '_posts', @name).sub(/\A\//, '')
self.data.fetch('path', self.relative_path.sub(/\A\//, ''))
end
# The path to the post source file, relative to the site source
def relative_path
File.join(@dir, '_posts', @name)
end
# Compares Post objects. First compares the Post date. If the dates are
@ -269,16 +271,6 @@ module Jekyll
path
end
# Convert this post into a Hash for use in Liquid templates.
#
# Returns the representative Hash.
def to_liquid(attrs = ATTRIBUTES_FOR_LIQUID)
further_data = Hash[attrs.map { |attribute|
[attribute, send(attribute)]
}]
data.deep_merge(further_data)
end
# Returns the shorthand String identifier of this Post.
def inspect
"<Post: #{self.id}>"

View File

@ -13,20 +13,14 @@ module Jekyll
def initialize(config)
self.config = config.clone
self.safe = config['safe']
%w[safe lsi pygments baseurl exclude include future show_drafts limit_posts keep_files].each do |opt|
self.send("#{opt}=", config[opt])
end
self.source = File.expand_path(config['source'])
self.dest = File.expand_path(config['destination'])
self.plugins = plugins_path
self.lsi = config['lsi']
self.pygments = config['pygments']
self.baseurl = config['baseurl']
self.permalink_style = config['permalink'].to_sym
self.exclude = config['exclude']
self.include = config['include']
self.future = config['future']
self.show_drafts = config['show_drafts']
self.limit_posts = config['limit_posts']
self.keep_files = config['keep_files']
self.reset
self.setup

View File

@ -51,6 +51,18 @@ eos
def render(context)
includes_dir = File.join(context.registers[:site].source, '_includes')
return error if error = validate_file(includes_dir)
source = File.read(File.join(includes_dir, @file))
partial = Liquid::Template.parse(source)
context.stack do
context['include'] = parse_params(context) if @params
partial.render(context)
end
end
def validate_file(includes_dir)
if File.symlink?(includes_dir)
return "Includes directory '#{includes_dir}' cannot be a symlink"
end
@ -59,19 +71,11 @@ eos
return "Include file '#{@file}' contains invalid characters or sequences"
end
Dir.chdir(includes_dir) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
if choices.include?(@file)
source = File.read(@file)
partial = Liquid::Template.parse(source)
context.stack do
context['include'] = parse_params(context) if @params
partial.render(context)
end
else
"Included file '#{@file}' not found in _includes directory"
end
file = File.join(includes_dir, @file)
if !File.exists?(file)
return "Included file #{@file} not found in _includes directory"
elsif File.symlink?(file)
return "The included file '_includes/#{@file}' should not be a symlink"
end
end
end