Merge pull request #1341 from maul-esel/minor-refactors
Minor refactors
This commit is contained in:
commit
a9e2a74ea6
|
@ -14,14 +14,10 @@ module Jekyll
|
||||||
def convert(content)
|
def convert(content)
|
||||||
# Check for use of coderay
|
# Check for use of coderay
|
||||||
if @config['kramdown']['use_coderay']
|
if @config['kramdown']['use_coderay']
|
||||||
@config['kramdown'].merge!({
|
%w[wrap line_numbers line_numbers_start tab_width bold_every css default_lang].each do |opt|
|
||||||
:coderay_wrap => @config['kramdown']['coderay']['coderay_wrap'],
|
key = "coderay_#{opt}"
|
||||||
:coderay_line_numbers => @config['kramdown']['coderay']['coderay_line_numbers'],
|
@config['kramdown'][key.to_sym] = @config['kramdown']['coderay'][key] unless @config['kramdown'].has_key?(key)
|
||||||
:coderay_line_number_start => @config['kramdown']['coderay']['coderay_line_number_start'],
|
end
|
||||||
: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']
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Kramdown::Document.new(content, @config["kramdown"].symbolize_keys).to_html
|
Kramdown::Document.new(content, @config["kramdown"].symbolize_keys).to_html
|
||||||
|
|
|
@ -84,6 +84,16 @@ module Jekyll
|
||||||
raise e
|
raise e
|
||||||
end
|
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
|
# Recursively render layouts
|
||||||
#
|
#
|
||||||
# layouts - a list of the layouts
|
# layouts - a list of the layouts
|
||||||
|
|
|
@ -7,6 +7,13 @@ module Jekyll
|
||||||
attr_accessor :name, :ext, :basename
|
attr_accessor :name, :ext, :basename
|
||||||
attr_accessor :data, :content, :output
|
attr_accessor :data, :content, :output
|
||||||
|
|
||||||
|
# Attributes for Liquid templates
|
||||||
|
ATTRIBUTES_FOR_LIQUID = %w[
|
||||||
|
url
|
||||||
|
content
|
||||||
|
path
|
||||||
|
]
|
||||||
|
|
||||||
# Initialize a new Page.
|
# Initialize a new Page.
|
||||||
#
|
#
|
||||||
# site - The Site object.
|
# site - The Site object.
|
||||||
|
@ -108,21 +115,16 @@ module Jekyll
|
||||||
do_layout(payload, layouts)
|
do_layout(payload, layouts)
|
||||||
end
|
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
|
# The path to the source file
|
||||||
#
|
#
|
||||||
# Returns the path to the source file
|
# Returns the path to the source file
|
||||||
def path
|
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
|
end
|
||||||
|
|
||||||
# Obtain destination path.
|
# Obtain destination path.
|
||||||
|
|
|
@ -3,10 +3,6 @@ module Jekyll
|
||||||
include Comparable
|
include Comparable
|
||||||
include Convertible
|
include Convertible
|
||||||
|
|
||||||
class << self
|
|
||||||
attr_accessor :lsi
|
|
||||||
end
|
|
||||||
|
|
||||||
# Valid post name regex.
|
# Valid post name regex.
|
||||||
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
|
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
|
||||||
|
|
||||||
|
@ -109,18 +105,19 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns excerpt string.
|
# Returns excerpt string.
|
||||||
def excerpt
|
def excerpt
|
||||||
if self.data.has_key? 'excerpt'
|
self.data.fetch('excerpt', self.extracted_excerpt.to_s)
|
||||||
self.data['excerpt']
|
|
||||||
else
|
|
||||||
self.extracted_excerpt.to_s
|
|
||||||
end
|
|
||||||
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["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
|
end
|
||||||
|
|
||||||
# Public: the path to the post relative to the site source,
|
# 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
|
# Returns the path to the file relative to the site source
|
||||||
def path
|
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
|
end
|
||||||
|
|
||||||
# Compares Post objects. First compares the Post date. If the dates are
|
# Compares Post objects. First compares the Post date. If the dates are
|
||||||
|
@ -269,16 +271,6 @@ module Jekyll
|
||||||
path
|
path
|
||||||
end
|
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.
|
# Returns the shorthand String identifier of this Post.
|
||||||
def inspect
|
def inspect
|
||||||
"<Post: #{self.id}>"
|
"<Post: #{self.id}>"
|
||||||
|
|
|
@ -13,20 +13,14 @@ module Jekyll
|
||||||
def initialize(config)
|
def initialize(config)
|
||||||
self.config = config.clone
|
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.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.lsi = config['lsi']
|
|
||||||
self.pygments = config['pygments']
|
|
||||||
self.baseurl = config['baseurl']
|
|
||||||
self.permalink_style = config['permalink'].to_sym
|
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.reset
|
||||||
self.setup
|
self.setup
|
||||||
|
|
|
@ -51,6 +51,18 @@ eos
|
||||||
def render(context)
|
def render(context)
|
||||||
includes_dir = File.join(context.registers[:site].source, '_includes')
|
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)
|
if File.symlink?(includes_dir)
|
||||||
return "Includes directory '#{includes_dir}' cannot be a symlink"
|
return "Includes directory '#{includes_dir}' cannot be a symlink"
|
||||||
end
|
end
|
||||||
|
@ -59,19 +71,11 @@ eos
|
||||||
return "Include file '#{@file}' contains invalid characters or sequences"
|
return "Include file '#{@file}' contains invalid characters or sequences"
|
||||||
end
|
end
|
||||||
|
|
||||||
Dir.chdir(includes_dir) do
|
file = File.join(includes_dir, @file)
|
||||||
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
|
if !File.exists?(file)
|
||||||
if choices.include?(@file)
|
return "Included file #{@file} not found in _includes directory"
|
||||||
source = File.read(@file)
|
elsif File.symlink?(file)
|
||||||
partial = Liquid::Template.parse(source)
|
return "The included file '_includes/#{@file}' should not be a symlink"
|
||||||
|
|
||||||
context.stack do
|
|
||||||
context['include'] = parse_params(context) if @params
|
|
||||||
partial.render(context)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
"Included file '#{@file}' not found in _includes directory"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue