Huge refactor to move all config into Jekyll::Site

This commit makes Jekyll threadsafe (or at least makes it possible to be so).
It also makes it a ton easier to use Jekyll as a library for scripted site
transformation. I did, however, break all the tests. =(
This commit is contained in:
Tom Preston-Werner 2009-03-12 19:05:43 -07:00
parent cb13ea3000
commit 73d42b24ad
17 changed files with 263 additions and 252 deletions

View File

@ -177,7 +177,7 @@ leaf directory resulting in URLs like 2008/11/17/blogging-like-a-hacker/.
h2. Configuration File
All of the options listed above can be specified on a site-by-site basis in
a '_config.yaml' file at the root of the site's source. As the filename
a '_config.yml' file at the root of the site's source. As the filename
suggests, the configuration is given in "YAML":http://www.yaml.org/. As
well as all of the options discussed in the last section, there are a few
additional options:

View File

@ -61,36 +61,20 @@ end
# Read command line options into `options` hash
opts.parse!
# Temporarily set source and destination options to read in config file
source = Jekyll::DEFAULTS['source']
destination = Jekyll::DEFAULTS['destination']
# Get source and destintation from command line
case ARGV.size
when 0
when 1
destination = options['destination'] = ARGV[0]
options['destination'] = ARGV[0]
when 2
source = options['source'] = ARGV[0]
destination = options['destination'] = ARGV[1]
options['source'] = ARGV[0]
options['destination'] = ARGV[1]
else
puts "Invalid options. Run `jekyll --help` for assistance."
exit(1)
end
# Get configuration from <source>/_config.yaml
config = {}
config_file = File.join(source, '_config.yaml')
begin
config = YAML.load_file( config_file )
puts "Configuration from #{config_file}"
rescue => err
puts "WARNING: Could not read configuration. Using defaults (and options)."
puts "\t" + err
end
# Merge DEFAULTS < config file < command line options
options = Jekyll::DEFAULTS.deep_merge(config).deep_merge(options)
options = Jekyll.configuration(options)
# Get source and destination directories (possibly set by config file)
source = options['source']
@ -106,6 +90,9 @@ def globs(source)
end
end
# Create the Site
site = Jekyll::Site.new(options)
# Run the directory watcher for auto-generation, if required
if options['auto']
require 'directory_watcher'
@ -119,7 +106,7 @@ if options['auto']
dw.add_observer do |*args|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
puts "[#{t}] regeneration: #{args.size} files changed"
Jekyll.process(options)
site.process
end
dw.start
@ -129,7 +116,7 @@ if options['auto']
end
else
puts "Building site: #{source} -> #{destination}"
Jekyll.process(options)
site.process
puts "Successfully generated site: #{source} -> #{destination}"
end

View File

@ -27,8 +27,6 @@ require 'jekyll/tags/include'
require 'jekyll/albino'
module Jekyll
VERSION = '0.3.0'
# Default options. Overriden by values in _config.yaml or command-line opts.
# (Strings rather symbols used for compatability with YAML)
DEFAULTS = {
@ -53,80 +51,29 @@ module Jekyll
}
}
class << self
attr_accessor :source,:dest,:lsi,:pygments,:permalink_style
end
# Generate a Jekyll configuration Hash by merging the default options
# with anything in _config.yml, and adding the given options on top
# +override+ is a Hash of config directives
#
# Returns Hash
def self.configuration(override)
# _config.yml may override default source location, but until
# then, we need to know where to look for _config.yml
source = override['source'] || Jekyll::DEFAULTS['source']
# Initializes some global Jekyll parameters
def self.configure(options)
# Interpret the simple options and configure Jekyll appropriately
Jekyll.source = options['source']
Jekyll.dest = options['destination']
Jekyll.lsi = options['lsi']
Jekyll.pygments = options['pygments']
Jekyll.permalink_style = options['permalink'].to_sym
# Check to see if LSI is enabled.
require 'classifier' if Jekyll.lsi
# Set the Markdown interpreter (and Maruku options, if necessary)
case options['markdown']
when 'rdiscount'
# Get configuration from <source>/_config.yaml
config = {}
config_file = File.join(source, '_config.yml')
begin
require 'rdiscount'
def self.markdown(content)
RDiscount.new(content).to_html
config = YAML.load_file(config_file)
puts "Configuration from #{config_file}"
rescue => err
puts "WARNING: Could not read configuration. Using defaults (and options)."
puts "\t" + err
end
puts 'Using rdiscount for Markdown'
rescue LoadError
puts 'You must have the rdiscount gem installed first'
end
when 'maruku'
begin
require 'maruku'
def self.markdown(content)
Maruku.new(content).to_html
end
if options['maruku']['use_divs']
require 'maruku/ext/div'
puts 'Maruku: Using extended syntax for div elements.'
end
if options['maruku']['use_tex']
require 'maruku/ext/math'
puts "Maruku: Using LaTeX extension. Images in `#{options['maruku']['png_dir']}`."
# Switch off MathML output
MaRuKu::Globals[:html_math_output_mathml] = false
MaRuKu::Globals[:html_math_engine] = 'none'
# Turn on math to PNG support with blahtex
# Resulting PNGs stored in `images/latex`
MaRuKu::Globals[:html_math_output_png] = true
MaRuKu::Globals[:html_png_engine] = options['maruku']['png_engine']
MaRuKu::Globals[:html_png_dir] = options['maruku']['png_dir']
MaRuKu::Globals[:html_png_url] = options['maruku']['png_url']
end
rescue LoadError
puts "The maruku gem is required for markdown support!"
end
end
end
def self.textile(content)
RedCloth.new(content).to_html
end
def self.process(config)
Jekyll.configure(config)
Jekyll::Site.new(config).process
# Merge DEFAULTS < _config.yml < override
Jekyll::DEFAULTS.deep_merge(config).deep_merge(override)
end
def self.version

View File

@ -1,3 +1,8 @@
# Convertible provides methods for converting a pagelike item
# from a certain type of markup into actual content
#
# Requires
# self.site -> Jekyll::Site
module Jekyll
module Convertible
# Return the contents as a string
@ -27,10 +32,10 @@ module Jekyll
case self.content_type
when 'textile'
self.ext = ".html"
self.content = Jekyll.textile(self.content)
self.content = self.site.textile(self.content)
when 'markdown'
self.ext = ".html"
self.content = Jekyll.markdown(self.content)
self.content = self.site.markdown(self.content)
end
end
@ -54,9 +59,11 @@ module Jekyll
#
# Returns nothing
def do_layout(payload, layouts)
info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
# render and transform content (this becomes the final content of the object)
payload["content_type"] = self.content_type
self.content = Liquid::Template.parse(self.content).render(payload, [Jekyll::Filters])
self.content = Liquid::Template.parse(self.content).render(payload, info)
self.transform
# output keeps track of what will finally be written
@ -66,7 +73,7 @@ module Jekyll
layout = layouts[self.data["layout"]]
while layout
payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
self.output = Liquid::Template.parse(layout.content).render(payload, [Jekyll::Filters])
self.output = Liquid::Template.parse(layout.content).render(payload, info)
layout = layouts[layout.data["layout"]]
end

View File

@ -3,15 +3,18 @@ module Jekyll
class Layout
include Convertible
attr_accessor :site
attr_accessor :ext
attr_accessor :data, :content
# Initialize a new Layout.
# +site+ is the Site
# +base+ is the String path to the <source>
# +name+ is the String filename of the post file
#
# Returns <Page>
def initialize(base, name)
def initialize(site, base, name)
@site = site
@base = base
@name = name

View File

@ -3,16 +3,19 @@ module Jekyll
class Page
include Convertible
attr_accessor :site
attr_accessor :ext
attr_accessor :data, :content, :output
# Initialize a new Page.
# +site+ is the Site
# +base+ is the String path to the <source>
# +dir+ is the String path between <source> and the file
# +name+ is the String filename of the file
#
# Returns <Page>
def initialize(base, dir, name)
def initialize(site, base, dir, name)
@site = site
@base = base
@dir = dir
@name = name

View File

@ -18,16 +18,19 @@ module Jekyll
name =~ MATCHER
end
attr_accessor :site
attr_accessor :date, :slug, :ext, :categories, :topics, :published
attr_accessor :data, :content, :output
# Initialize this Post instance.
# +site+ is the Site
# +base+ is the String path to the dir containing the post file
# +name+ is the String filename of the post file
# +categories+ is an Array of Strings for the categories for this post
#
# Returns <Post>
def initialize(source, dir, name)
def initialize(site, source, dir, name)
@site = site
@base = File.join(source, dir, '_posts')
@name = name
@ -89,7 +92,7 @@ module Jekyll
permalink.to_s.split("/")[0..-2].join("/") + '/'
else
prefix = self.categories.empty? ? '' : '/' + self.categories.join('/')
if [:date, :pretty].include?(Jekyll.permalink_style)
if [:date, :pretty].include?(self.site.permalink_style)
prefix + date.strftime("/%Y/%m/%d/")
else
prefix + '/'
@ -111,7 +114,7 @@ module Jekyll
#
# Returns <String>
def url
ext = Jekyll.permalink_style == :pretty ? '' : '.html'
ext = self.site.permalink_style == :pretty ? '' : '.html'
permalink || self.id + ext
end
@ -129,7 +132,7 @@ module Jekyll
def related_posts(posts)
return [] unless posts.size > 1
if Jekyll.lsi
if self.site.lsi
self.class.lsi ||= begin
puts "Running the classifier... this could take a while."
lsi = Classifier::LSI.new
@ -171,7 +174,7 @@ module Jekyll
path = File.join(dest, self.url)
if Jekyll.permalink_style == :pretty
if self.site.permalink_style == :pretty
FileUtils.mkdir_p(path)
path = File.join(path, "index.html")
end

View File

@ -2,6 +2,7 @@ module Jekyll
class Site
attr_accessor :config, :layouts, :posts, :categories
attr_accessor :source, :dest, :lsi, :pygments, :permalink_style
# Initialize the site
# +config+ is a Hash containing site configurations details
@ -9,16 +10,75 @@ module Jekyll
# Returns <Site>
def initialize(config)
self.config = config.clone
self.source = config['source']
self.dest = config['destination']
self.lsi = config['lsi']
self.pygments = config['pygments']
self.permalink_style = config['permalink'].to_sym
self.layouts = {}
self.posts = []
self.categories = Hash.new { |hash, key| hash[key] = Array.new }
self.setup
end
# The directory containing the proto-site.
def source; self.config['source']; end
def setup
# Check to see if LSI is enabled.
require 'classifier' if self.lsi
# Where the completed site should be written.
def dest; self.config['destination']; end
# Set the Markdown interpreter (and Maruku self.config, if necessary)
case self.config['markdown']
when 'rdiscount'
begin
require 'rdiscount'
def markdown(content)
RDiscount.new(content).to_html
end
puts 'Using rdiscount for Markdown'
rescue LoadError
puts 'You must have the rdiscount gem installed first'
end
when 'maruku'
begin
require 'maruku'
def markdown(content)
Maruku.new(content).to_html
end
if self.config['maruku']['use_divs']
require 'maruku/ext/div'
puts 'Maruku: Using extended syntax for div elements.'
end
if self.config['maruku']['use_tex']
require 'maruku/ext/math'
puts "Maruku: Using LaTeX extension. Images in `#{self.config['maruku']['png_dir']}`."
# Switch off MathML output
MaRuKu::Globals[:html_math_output_mathml] = false
MaRuKu::Globals[:html_math_engine] = 'none'
# Turn on math to PNG support with blahtex
# Resulting PNGs stored in `images/latex`
MaRuKu::Globals[:html_math_output_png] = true
MaRuKu::Globals[:html_png_engine] = self.config['maruku']['png_engine']
MaRuKu::Globals[:html_png_dir] = self.config['maruku']['png_dir']
MaRuKu::Globals[:html_png_url] = self.config['maruku']['png_url']
end
rescue LoadError
puts "The maruku gem is required for markdown support!"
end
end
end
def textile(content)
RedCloth.new(content).to_html
end
# Do the actual work of processing the site and generating the
# real deal.
@ -40,7 +100,7 @@ module Jekyll
entries.each do |f|
name = f.split(".")[0..-2].join(".")
self.layouts[name] = Layout.new(base, f)
self.layouts[name] = Layout.new(self, base, f)
end
rescue Errno::ENOENT => e
# ignore missing layout dir
@ -57,7 +117,7 @@ module Jekyll
# first pass processes, but does not yet render post content
entries.each do |f|
if Post.valid?(f)
post = Post.new(self.source, dir, f)
post = Post.new(self, self.source, dir, f)
if post.published
self.posts << post
@ -117,7 +177,7 @@ module Jekyll
if first3 == "---"
# file appears to have a YAML header so process it as a page
page = Page.new(self.source, dir, f)
page = Page.new(self, self.source, dir, f)
page.render(self.layouts, site_payload)
page.write(self.dest)
else

View File

@ -2,6 +2,7 @@ module Jekyll
class HighlightBlock < Liquid::Block
include Liquid::StandardFilters
# we need a language, but the linenos argument is optional.
SYNTAX = /(\w+)\s?(:?linenos)?\s?/
@ -21,7 +22,7 @@ module Jekyll
end
def render(context)
if Jekyll.pygments
if context.registers[:site].pygments
render_pygments(context, super.to_s)
else
render_codehighlighter(context, super.to_s)
@ -31,7 +32,7 @@ module Jekyll
def render_pygments(context, code)
if context["content_type"] == :markdown
return "\n" + Albino.new(code, @lang).to_s(@options) + "\n"
elsif content["content_type"] == :textile
elsif context["content_type"] == :textile
return "<notextile>" + Albino.new(code, @lang).to_s(@options) + "</notextile>"
else
return Albino.new(code, @lang).to_s(@options)

View File

@ -11,7 +11,7 @@ module Jekyll
return "Include file '#{@file}' contains invalid characters or sequences"
end
Dir.chdir(File.join(Jekyll.source, '_includes')) do
Dir.chdir(File.join('.', '_includes')) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
if choices.include?(@file)
source = File.read(@file)