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 h2. Configuration File
All of the options listed above can be specified on a site-by-site basis in 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 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 well as all of the options discussed in the last section, there are a few
additional options: additional options:

View File

@ -9,10 +9,10 @@ Basic Command Line Usage:
jekyll # . -> ./_site jekyll # . -> ./_site
jekyll <path to write generated site> # . -> <path> jekyll <path to write generated site> # . -> <path>
jekyll <path to source> <path to write generated site> # <path> -> <path> jekyll <path to source> <path to write generated site> # <path> -> <path>
Configuration is read from '<source>/_config.yaml' but can be overriden Configuration is read from '<source>/_config.yaml' but can be overriden
using the following options: using the following options:
HELP HELP
require 'optparse' require 'optparse'
@ -30,28 +30,28 @@ opts = OptionParser.new do |opts|
opts.on("--no-auto", "No auto-regenerate") do opts.on("--no-auto", "No auto-regenerate") do
options['auto'] = false options['auto'] = false
end end
opts.on("--server [PORT]", "Start web server (default port 4000)") do |port| opts.on("--server [PORT]", "Start web server (default port 4000)") do |port|
options['server'] = true options['server'] = true
options['server_port'] = port unless port.nil? options['server_port'] = port unless port.nil?
end end
opts.on("--lsi", "Use LSI for better related posts") do opts.on("--lsi", "Use LSI for better related posts") do
options['lsi'] = true options['lsi'] = true
end end
opts.on("--pygments", "Use pygments to highlight code") do opts.on("--pygments", "Use pygments to highlight code") do
options['pygments'] = true options['pygments'] = true
end end
opts.on("--rdiscount", "Use rdiscount gem for Markdown") do opts.on("--rdiscount", "Use rdiscount gem for Markdown") do
options['markdown'] = 'rdiscount' options['markdown'] = 'rdiscount'
end end
opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style| opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style|
options['permalink'] = style unless style.nil? options['permalink'] = style unless style.nil?
end end
opts.on("--version", "Display current version") do opts.on("--version", "Display current version") do
puts "Jekyll " + Jekyll.version puts "Jekyll " + Jekyll.version
exit 0 exit 0
@ -61,36 +61,20 @@ end
# Read command line options into `options` hash # Read command line options into `options` hash
opts.parse! 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 # Get source and destintation from command line
case ARGV.size case ARGV.size
when 0 when 0
when 1 when 1
destination = options['destination'] = ARGV[0] options['destination'] = ARGV[0]
when 2 when 2
source = options['source'] = ARGV[0] options['source'] = ARGV[0]
destination = options['destination'] = ARGV[1] options['destination'] = ARGV[1]
else else
puts "Invalid options. Run `jekyll --help` for assistance." puts "Invalid options. Run `jekyll --help` for assistance."
exit(1) exit(1)
end end
# Get configuration from <source>/_config.yaml options = Jekyll.configuration(options)
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)
# Get source and destination directories (possibly set by config file) # Get source and destination directories (possibly set by config file)
source = options['source'] source = options['source']
@ -106,30 +90,33 @@ def globs(source)
end end
end end
# Create the Site
site = Jekyll::Site.new(options)
# Run the directory watcher for auto-generation, if required # Run the directory watcher for auto-generation, if required
if options['auto'] if options['auto']
require 'directory_watcher' require 'directory_watcher'
puts "Auto-regenerating enabled: #{source} -> #{destination}" puts "Auto-regenerating enabled: #{source} -> #{destination}"
dw = DirectoryWatcher.new(source) dw = DirectoryWatcher.new(source)
dw.interval = 1 dw.interval = 1
dw.glob = globs(source) dw.glob = globs(source)
dw.add_observer do |*args| dw.add_observer do |*args|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S") t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
puts "[#{t}] regeneration: #{args.size} files changed" puts "[#{t}] regeneration: #{args.size} files changed"
Jekyll.process(options) site.process
end end
dw.start dw.start
unless options['server'] unless options['server']
loop { sleep 1000 } loop { sleep 1000 }
end end
else else
puts "Building site: #{source} -> #{destination}" puts "Building site: #{source} -> #{destination}"
Jekyll.process(options) site.process
puts "Successfully generated site: #{source} -> #{destination}" puts "Successfully generated site: #{source} -> #{destination}"
end end
@ -137,7 +124,7 @@ end
if options['server'] if options['server']
require 'webrick' require 'webrick'
include WEBrick include WEBrick
FileUtils.mkdir_p(destination) FileUtils.mkdir_p(destination)
s = HTTPServer.new( s = HTTPServer.new(
@ -147,7 +134,7 @@ if options['server']
t = Thread.new { t = Thread.new {
s.start s.start
} }
trap("INT") { s.shutdown } trap("INT") { s.shutdown }
t.join() t.join()
end end

View File

@ -27,8 +27,6 @@ require 'jekyll/tags/include'
require 'jekyll/albino' require 'jekyll/albino'
module Jekyll module Jekyll
VERSION = '0.3.0'
# Default options. Overriden by values in _config.yaml or command-line opts. # Default options. Overriden by values in _config.yaml or command-line opts.
# (Strings rather symbols used for compatability with YAML) # (Strings rather symbols used for compatability with YAML)
DEFAULTS = { DEFAULTS = {
@ -52,83 +50,32 @@ module Jekyll
'png_url' => '/images/latex' 'png_url' => '/images/latex'
} }
} }
class << self
attr_accessor :source,:dest,:lsi,:pygments,:permalink_style
end
# Initializes some global Jekyll parameters # Generate a Jekyll configuration Hash by merging the default options
def self.configure(options) # with anything in _config.yml, and adding the given options on top
# Interpret the simple options and configure Jekyll appropriately # +override+ is a Hash of config directives
Jekyll.source = options['source'] #
Jekyll.dest = options['destination'] # Returns Hash
Jekyll.lsi = options['lsi'] def self.configuration(override)
Jekyll.pygments = options['pygments'] # _config.yml may override default source location, but until
Jekyll.permalink_style = options['permalink'].to_sym # then, we need to know where to look for _config.yml
source = override['source'] || Jekyll::DEFAULTS['source']
# Check to see if LSI is enabled. # Get configuration from <source>/_config.yaml
require 'classifier' if Jekyll.lsi config = {}
config_file = File.join(source, '_config.yml')
# Set the Markdown interpreter (and Maruku options, if necessary) begin
case options['markdown'] config = YAML.load_file(config_file)
puts "Configuration from #{config_file}"
when 'rdiscount' rescue => err
begin puts "WARNING: Could not read configuration. Using defaults (and options)."
require 'rdiscount' puts "\t" + err
def self.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 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
# Merge DEFAULTS < _config.yml < override
Jekyll::DEFAULTS.deep_merge(config).deep_merge(override)
end end
def self.textile(content)
RedCloth.new(content).to_html
end
def self.process(config)
Jekyll.configure(config)
Jekyll::Site.new(config).process
end
def self.version def self.version
yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml]))) yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml])))
"#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}" "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}"

View File

@ -38,7 +38,7 @@
# #
# To see all lexers and formatters available, run `pygmentize -L`. # To see all lexers and formatters available, run `pygmentize -L`.
# #
# Chris Wanstrath // chris@ozmm.org # Chris Wanstrath // chris@ozmm.org
# GitHub // http://github.com # GitHub // http://github.com
# #
require 'open4' require 'open4'

View File

@ -13,7 +13,7 @@ require File.join(File.dirname(__FILE__),"csv.rb")
# installed, running the following commands should work: # installed, running the following commands should work:
# $ sudo gem install sequel # $ sudo gem install sequel
# $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config # $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
module Jekyll module Jekyll
module Mephisto module Mephisto
#Accepts a hash with database config variables, exports mephisto posts into a csv #Accepts a hash with database config variables, exports mephisto posts into a csv
@ -38,24 +38,24 @@ module Jekyll
# through the created posts to make sure nothing is accidently published. # through the created posts to make sure nothing is accidently published.
QUERY = "SELECT id, permalink, body, published_at, title FROM contents WHERE user_id = 1 AND type = 'Article' AND published_at IS NOT NULL ORDER BY published_at" QUERY = "SELECT id, permalink, body, published_at, title FROM contents WHERE user_id = 1 AND type = 'Article' AND published_at IS NOT NULL ORDER BY published_at"
def self.process(dbname, user, pass, host = 'localhost') def self.process(dbname, user, pass, host = 'localhost')
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host) db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host)
FileUtils.mkdir_p "_posts" FileUtils.mkdir_p "_posts"
db[QUERY].each do |post| db[QUERY].each do |post|
title = post[:title] title = post[:title]
slug = post[:permalink] slug = post[:permalink]
date = post[:published_at] date = post[:published_at]
content = post[:body] content = post[:body]
# more_content = '' # more_content = ''
# Be sure to include the body and extended body. # Be sure to include the body and extended body.
# if more_content != nil # if more_content != nil
# content = content + " \n" + more_content # content = content + " \n" + more_content
# end # end
# Ideally, this script would determine the post format (markdown, html # Ideally, this script would determine the post format (markdown, html
# , etc) and create files with proper extensions. At this point it # , etc) and create files with proper extensions. At this point it
# just assumes that markdown will be acceptable. # just assumes that markdown will be acceptable.
@ -66,14 +66,14 @@ module Jekyll
'title' => title.to_s, 'title' => title.to_s,
'mt_id' => post[:entry_id], 'mt_id' => post[:entry_id],
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml }.delete_if { |k,v| v.nil? || v == ''}.to_yaml
File.open("_posts/#{name}", "w") do |f| File.open("_posts/#{name}", "w") do |f|
f.puts data f.puts data
f.puts "---" f.puts "---"
f.puts content f.puts content
end end
end end
end end
end end
end end

View File

@ -11,31 +11,31 @@ require 'fileutils'
# installed, running the following commands should work: # installed, running the following commands should work:
# $ sudo gem install sequel # $ sudo gem install sequel
# $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config # $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
module Jekyll module Jekyll
module MT module MT
# This query will pull blog posts from all entries across all blogs. If # This query will pull blog posts from all entries across all blogs. If
# you've got unpublished, deleted or otherwise hidden posts please sift # you've got unpublished, deleted or otherwise hidden posts please sift
# through the created posts to make sure nothing is accidently published. # through the created posts to make sure nothing is accidently published.
QUERY = "SELECT entry_id, entry_basename, entry_text, entry_text_more, entry_created_on, entry_title FROM mt_entry" QUERY = "SELECT entry_id, entry_basename, entry_text, entry_text_more, entry_created_on, entry_title FROM mt_entry"
def self.process(dbname, user, pass, host = 'localhost') def self.process(dbname, user, pass, host = 'localhost')
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host) db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host)
FileUtils.mkdir_p "_posts" FileUtils.mkdir_p "_posts"
db[QUERY].each do |post| db[QUERY].each do |post|
title = post[:entry_title] title = post[:entry_title]
slug = post[:entry_basename] slug = post[:entry_basename]
date = post[:entry_created_on] date = post[:entry_created_on]
content = post[:entry_text] content = post[:entry_text]
more_content = post[:entry_text_more] more_content = post[:entry_text_more]
# Be sure to include the body and extended body. # Be sure to include the body and extended body.
if more_content != nil if more_content != nil
content = content + " \n" + more_content content = content + " \n" + more_content
end end
# Ideally, this script would determine the post format (markdown, html # Ideally, this script would determine the post format (markdown, html
# , etc) and create files with proper extensions. At this point it # , etc) and create files with proper extensions. At this point it
# just assumes that markdown will be acceptable. # just assumes that markdown will be acceptable.
@ -46,14 +46,14 @@ module Jekyll
'title' => title.to_s, 'title' => title.to_s,
'mt_id' => post[:entry_id], 'mt_id' => post[:entry_id],
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml }.delete_if { |k,v| v.nil? || v == ''}.to_yaml
File.open("_posts/#{name}", "w") do |f| File.open("_posts/#{name}", "w") do |f|
f.puts data f.puts data
f.puts "---" f.puts "---"
f.puts content f.puts content
end end
end end
end end
end end
end end

View File

@ -18,20 +18,20 @@ module Jekyll
def self.process(dbname, user, pass, host = 'localhost') def self.process(dbname, user, pass, host = 'localhost')
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host) db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host)
FileUtils.mkdir_p "_posts" FileUtils.mkdir_p "_posts"
db[QUERY].each do |post| db[QUERY].each do |post|
# Get required fields and construct Jekyll compatible name # Get required fields and construct Jekyll compatible name
title = post[:Title] title = post[:Title]
slug = post[:url_title] slug = post[:url_title]
date = post[:Posted] date = post[:Posted]
content = post[:Body] content = post[:Body]
name = [date.strftime("%Y-%m-%d"), slug].join('-') + ".textile" name = [date.strftime("%Y-%m-%d"), slug].join('-') + ".textile"
# Get the relevant fields as a hash, delete empty fields and convert # Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header # to YAML for the header
data = { data = {
'layout' => 'post', 'layout' => 'post',
'title' => title.to_s, 'title' => title.to_s,

View File

@ -2,22 +2,22 @@
require 'fileutils' require 'fileutils'
require 'rubygems' require 'rubygems'
require 'sequel' require 'sequel'
module Jekyll module Jekyll
module Typo module Typo
# this SQL *should* work for both MySQL and PostgreSQL, but I haven't # this SQL *should* work for both MySQL and PostgreSQL, but I haven't
# tested PostgreSQL yet (as of 2008-12-16) # tested PostgreSQL yet (as of 2008-12-16)
SQL = <<-EOS SQL = <<-EOS
SELECT c.id id, SELECT c.id id,
c.title title, c.title title,
c.permalink slug, c.permalink slug,
c.body body, c.body body,
c.published_at date, c.published_at date,
c.state state, c.state state,
COALESCE(tf.name, 'html') filter COALESCE(tf.name, 'html') filter
FROM contents c FROM contents c
LEFT OUTER JOIN text_filters tf LEFT OUTER JOIN text_filters tf
ON c.text_filter_id = tf.id ON c.text_filter_id = tf.id
EOS EOS
def self.process dbname, user, pass, host='localhost' def self.process dbname, user, pass, host='localhost'
@ -30,7 +30,7 @@ module Jekyll
sprintf("%.02d", post[:date].month), sprintf("%.02d", post[:date].month),
sprintf("%.02d", post[:date].day), sprintf("%.02d", post[:date].day),
post[:slug].strip ].join('-') post[:slug].strip ].join('-')
# Can have more than one text filter in this field, but we just want # Can have more than one text filter in this field, but we just want
# the first one for this # the first one for this
name += '.' + post[:filter].split(' ')[0] name += '.' + post[:filter].split(' ')[0]

View File

@ -1,10 +1,15 @@
# 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 Jekyll
module Convertible module Convertible
# Return the contents as a string # Return the contents as a string
def to_s def to_s
self.content || '' self.content || ''
end end
# Read the YAML frontmatter # Read the YAML frontmatter
# +base+ is the String path to the dir containing the file # +base+ is the String path to the dir containing the file
# +name+ is the String filename of the file # +name+ is the String filename of the file
@ -12,14 +17,14 @@ module Jekyll
# Returns nothing # Returns nothing
def read_yaml(base, name) def read_yaml(base, name)
self.content = File.read(File.join(base, name)) self.content = File.read(File.join(base, name))
if self.content =~ /^(---\s*\n.*?)\n---\s*\n/m if self.content =~ /^(---\s*\n.*?)\n---\s*\n/m
self.content = self.content[($1.size + 5)..-1] self.content = self.content[($1.size + 5)..-1]
self.data = YAML.load($1) self.data = YAML.load($1)
end end
end end
# Transform the contents based on the file extension. # Transform the contents based on the file extension.
# #
# Returns nothing # Returns nothing
@ -27,13 +32,13 @@ module Jekyll
case self.content_type case self.content_type
when 'textile' when 'textile'
self.ext = ".html" self.ext = ".html"
self.content = Jekyll.textile(self.content) self.content = self.site.textile(self.content)
when 'markdown' when 'markdown'
self.ext = ".html" self.ext = ".html"
self.content = Jekyll.markdown(self.content) self.content = self.site.markdown(self.content)
end end
end end
# Determine which formatting engine to use based on this convertible's # Determine which formatting engine to use based on this convertible's
# extension # extension
# #
@ -44,30 +49,32 @@ module Jekyll
return 'textile' return 'textile'
when /markdown/i, /mkdn/i, /md/i when /markdown/i, /mkdn/i, /md/i
return 'markdown' return 'markdown'
end end
return 'unknown' return 'unknown'
end end
# Add any necessary layouts to this convertible document # Add any necessary layouts to this convertible document
# +layouts+ is a Hash of {"name" => "layout"} # +layouts+ is a Hash of {"name" => "layout"}
# +site_payload+ is the site payload hash # +site_payload+ is the site payload hash
# #
# Returns nothing # Returns nothing
def do_layout(payload, layouts) 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) # render and transform content (this becomes the final content of the object)
payload["content_type"] = self.content_type 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 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 = self.content
# recursively render layouts # recursively render layouts
layout = layouts[self.data["layout"]] layout = layouts[self.data["layout"]]
while layout while layout
payload = payload.deep_merge({"content" => self.output, "page" => layout.data}) 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"]] layout = layouts[layout.data["layout"]]
end end
end end

View File

@ -1,22 +1,22 @@
class Hash class Hash
# Merges self with another hash, recursively. # Merges self with another hash, recursively.
# #
# This code was lovingly stolen from some random gem: # This code was lovingly stolen from some random gem:
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
# #
# Thanks to whoever made it. # Thanks to whoever made it.
def deep_merge(hash) def deep_merge(hash)
target = dup target = dup
hash.keys.each do |key| hash.keys.each do |key|
if hash[key].is_a? Hash and self[key].is_a? Hash if hash[key].is_a? Hash and self[key].is_a? Hash
target[key] = target[key].deep_merge(hash[key]) target[key] = target[key].deep_merge(hash[key])
next next
end end
target[key] = hash[key] target[key] = hash[key]
end end
target target
end end
end end

View File

@ -1,10 +1,10 @@
module Jekyll module Jekyll
module Filters module Filters
def textilize(input) def textilize(input)
RedCloth.new(input).to_html RedCloth.new(input).to_html
end end
def date_to_string(date) def date_to_string(date)
date.strftime("%d %b %Y") date.strftime("%d %b %Y")
end end
@ -12,19 +12,19 @@ module Jekyll
def date_to_long_string(date) def date_to_long_string(date)
date.strftime("%d %B %Y") date.strftime("%d %B %Y")
end end
def date_to_xmlschema(date) def date_to_xmlschema(date)
date.xmlschema date.xmlschema
end end
def xml_escape(input) def xml_escape(input)
input.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;") input.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
end end
def number_of_words(input) def number_of_words(input)
input.split.length input.split.length
end end
def array_to_sentence_string(array) def array_to_sentence_string(array)
connector = "and" connector = "and"
case array.length case array.length
@ -39,5 +39,5 @@ module Jekyll
end end
end end
end end
end end

View File

@ -2,25 +2,28 @@ module Jekyll
class Layout class Layout
include Convertible include Convertible
attr_accessor :site
attr_accessor :ext attr_accessor :ext
attr_accessor :data, :content attr_accessor :data, :content
# Initialize a new Layout. # Initialize a new Layout.
# +site+ is the Site
# +base+ is the String path to the <source> # +base+ is the String path to the <source>
# +name+ is the String filename of the post file # +name+ is the String filename of the post file
# #
# Returns <Page> # Returns <Page>
def initialize(base, name) def initialize(site, base, name)
@site = site
@base = base @base = base
@name = name @name = name
self.data = {} self.data = {}
self.process(name) self.process(name)
self.read_yaml(base, name) self.read_yaml(base, name)
end end
# Extract information from the layout filename # Extract information from the layout filename
# +name+ is the String filename of the layout file # +name+ is the String filename of the layout file
# #

View File

@ -2,28 +2,31 @@ module Jekyll
class Page class Page
include Convertible include Convertible
attr_accessor :site
attr_accessor :ext attr_accessor :ext
attr_accessor :data, :content, :output attr_accessor :data, :content, :output
# Initialize a new Page. # Initialize a new Page.
# +site+ is the Site
# +base+ is the String path to the <source> # +base+ is the String path to the <source>
# +dir+ is the String path between <source> and the file # +dir+ is the String path between <source> and the file
# +name+ is the String filename of the file # +name+ is the String filename of the file
# #
# Returns <Page> # Returns <Page>
def initialize(base, dir, name) def initialize(site, base, dir, name)
@site = site
@base = base @base = base
@dir = dir @dir = dir
@name = name @name = name
self.data = {} self.data = {}
self.process(name) self.process(name)
self.read_yaml(File.join(base, dir), name) self.read_yaml(File.join(base, dir), name)
#self.transform #self.transform
end end
# Extract information from the page filename # Extract information from the page filename
# +name+ is the String filename of the page file # +name+ is the String filename of the page file
# #
@ -31,7 +34,7 @@ module Jekyll
def process(name) def process(name)
self.ext = File.extname(name) self.ext = File.extname(name)
end end
# Add any necessary layouts to this post # Add any necessary layouts to this post
# +layouts+ is a Hash of {"name" => "layout"} # +layouts+ is a Hash of {"name" => "layout"}
# +site_payload+ is the site payload hash # +site_payload+ is the site payload hash
@ -41,19 +44,19 @@ module Jekyll
payload = {"page" => self.data}.deep_merge(site_payload) payload = {"page" => self.data}.deep_merge(site_payload)
do_layout(payload, layouts) do_layout(payload, layouts)
end end
# Write the generated page file to the destination directory. # Write the generated page file to the destination directory.
# +dest+ is the String path to the destination dir # +dest+ is the String path to the destination dir
# #
# Returns nothing # Returns nothing
def write(dest) def write(dest)
FileUtils.mkdir_p(File.join(dest, @dir)) FileUtils.mkdir_p(File.join(dest, @dir))
name = @name name = @name
if self.ext != "" if self.ext != ""
name = @name.split(".")[0..-2].join('.') + self.ext name = @name.split(".")[0..-2].join('.') + self.ext
end end
path = File.join(dest, @dir, name) path = File.join(dest, @dir, name)
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write(self.output) f.write(self.output)

View File

@ -3,13 +3,13 @@ module Jekyll
class Post class Post
include Comparable include Comparable
include Convertible include Convertible
class << self class << self
attr_accessor :lsi attr_accessor :lsi
end end
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/ MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
# Post name validator. Post filenames must be like: # Post name validator. Post filenames must be like:
# 2008-11-05-my-awesome-post.textile # 2008-11-05-my-awesome-post.textile
# #
@ -17,34 +17,37 @@ module Jekyll
def self.valid?(name) def self.valid?(name)
name =~ MATCHER name =~ MATCHER
end end
attr_accessor :site
attr_accessor :date, :slug, :ext, :categories, :topics, :published attr_accessor :date, :slug, :ext, :categories, :topics, :published
attr_accessor :data, :content, :output attr_accessor :data, :content, :output
# Initialize this Post instance. # Initialize this Post instance.
# +site+ is the Site
# +base+ is the String path to the dir containing the post file # +base+ is the String path to the dir containing the post file
# +name+ is the String filename of the post file # +name+ is the String filename of the post file
# +categories+ is an Array of Strings for the categories for this post # +categories+ is an Array of Strings for the categories for this post
# #
# Returns <Post> # Returns <Post>
def initialize(source, dir, name) def initialize(site, source, dir, name)
@site = site
@base = File.join(source, dir, '_posts') @base = File.join(source, dir, '_posts')
@name = name @name = name
self.categories = dir.split('/').reject { |x| x.empty? } self.categories = dir.split('/').reject { |x| x.empty? }
parts = name.split('/') parts = name.split('/')
self.topics = parts.size > 1 ? parts[0..-2] : [] self.topics = parts.size > 1 ? parts[0..-2] : []
self.process(name) self.process(name)
self.read_yaml(@base, name) self.read_yaml(@base, name)
if self.data.has_key?('published') && self.data['published'] == false if self.data.has_key?('published') && self.data['published'] == false
self.published = false self.published = false
else else
self.published = true self.published = true
end end
if self.categories.empty? if self.categories.empty?
if self.data.has_key?('category') if self.data.has_key?('category')
self.categories << self.data['category'] self.categories << self.data['category']
@ -59,14 +62,14 @@ module Jekyll
end end
end end
end end
# Spaceship is based on Post#date # Spaceship is based on Post#date
# #
# Returns -1, 0, 1 # Returns -1, 0, 1
def <=>(other) def <=>(other)
self.date <=> other.date self.date <=> other.date
end end
# Extract information from the post filename # Extract information from the post filename
# +name+ is the String filename of the post file # +name+ is the String filename of the post file
# #
@ -77,7 +80,7 @@ module Jekyll
self.slug = slug self.slug = slug
self.ext = ext self.ext = ext
end end
# The generated directory into which the post will be placed # The generated directory into which the post will be placed
# upon generation. This is derived from the permalink or, if # upon generation. This is derived from the permalink or, if
# permalink is absent, set to the default date # permalink is absent, set to the default date
@ -89,14 +92,14 @@ module Jekyll
permalink.to_s.split("/")[0..-2].join("/") + '/' permalink.to_s.split("/")[0..-2].join("/") + '/'
else else
prefix = self.categories.empty? ? '' : '/' + self.categories.join('/') 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/") prefix + date.strftime("/%Y/%m/%d/")
else else
prefix + '/' prefix + '/'
end end
end end
end end
# The full path and filename of the post. # The full path and filename of the post.
# Defined in the YAML of the post body # Defined in the YAML of the post body
# (Optional) # (Optional)
@ -105,16 +108,16 @@ module Jekyll
def permalink def permalink
self.data && self.data['permalink'] self.data && self.data['permalink']
end end
# The generated relative url of this post # The generated relative url of this post
# e.g. /2008/11/05/my-awesome-post.html # e.g. /2008/11/05/my-awesome-post.html
# #
# Returns <String> # Returns <String>
def url def url
ext = Jekyll.permalink_style == :pretty ? '' : '.html' ext = self.site.permalink_style == :pretty ? '' : '.html'
permalink || self.id + ext permalink || self.id + ext
end end
# The UID for this post (useful in feeds) # The UID for this post (useful in feeds)
# e.g. /2008/11/05/my-awesome-post # e.g. /2008/11/05/my-awesome-post
# #
@ -122,14 +125,14 @@ module Jekyll
def id def id
self.dir + self.slug self.dir + self.slug
end end
# Calculate related posts. # Calculate related posts.
# #
# Returns [<Post>] # Returns [<Post>]
def related_posts(posts) def related_posts(posts)
return [] unless posts.size > 1 return [] unless posts.size > 1
if Jekyll.lsi if self.site.lsi
self.class.lsi ||= begin self.class.lsi ||= begin
puts "Running the classifier... this could take a while." puts "Running the classifier... this could take a while."
lsi = Classifier::LSI.new lsi = Classifier::LSI.new
@ -144,7 +147,7 @@ module Jekyll
(posts - [self])[0..9] (posts - [self])[0..9]
end end
end end
# Add any necessary layouts to this post # Add any necessary layouts to this post
# +layouts+ is a Hash of {"name" => "layout"} # +layouts+ is a Hash of {"name" => "layout"}
# +site_payload+ is the site payload hash # +site_payload+ is the site payload hash
@ -158,20 +161,20 @@ module Jekyll
"page" => self.to_liquid "page" => self.to_liquid
} }
payload = payload.deep_merge(site_payload) payload = payload.deep_merge(site_payload)
do_layout(payload, layouts) do_layout(payload, layouts)
end end
# Write the generated post file to the destination directory. # Write the generated post file to the destination directory.
# +dest+ is the String path to the destination dir # +dest+ is the String path to the destination dir
# #
# Returns nothing # Returns nothing
def write(dest) def write(dest)
FileUtils.mkdir_p(File.join(dest, dir)) FileUtils.mkdir_p(File.join(dest, dir))
path = File.join(dest, self.url) path = File.join(dest, self.url)
if Jekyll.permalink_style == :pretty if self.site.permalink_style == :pretty
FileUtils.mkdir_p(path) FileUtils.mkdir_p(path)
path = File.join(path, "index.html") path = File.join(path, "index.html")
end end
@ -180,7 +183,7 @@ module Jekyll
f.write(self.output) f.write(self.output)
end end
end end
# Convert this post into a Hash for use in Liquid templates. # Convert this post into a Hash for use in Liquid templates.
# #
# Returns <Hash> # Returns <Hash>
@ -192,7 +195,7 @@ module Jekyll
"topics" => self.topics, "topics" => self.topics,
"content" => self.content }.deep_merge(self.data) "content" => self.content }.deep_merge(self.data)
end end
def inspect def inspect
"<Post: #{self.id}>" "<Post: #{self.id}>"
end end

View File

@ -1,25 +1,85 @@
module Jekyll module Jekyll
class Site class Site
attr_accessor :config, :layouts, :posts, :categories attr_accessor :config, :layouts, :posts, :categories
attr_accessor :source, :dest, :lsi, :pygments, :permalink_style
# Initialize the site # Initialize the site
# +config+ is a Hash containing site configurations details # +config+ is a Hash containing site configurations details
# #
# Returns <Site> # Returns <Site>
def initialize(config) def initialize(config)
self.config = config.clone self.config = config.clone
self.layouts = {}
self.posts = [] self.source = config['source']
self.categories = Hash.new { |hash, key| hash[key] = Array.new } 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 end
# The directory containing the proto-site. def setup
def source; self.config['source']; end # 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 # Do the actual work of processing the site and generating the
# real deal. # real deal.
# #
@ -37,15 +97,15 @@ module Jekyll
base = File.join(self.source, "_layouts") base = File.join(self.source, "_layouts")
entries = [] entries = []
Dir.chdir(base) { entries = filter_entries(Dir['*.*']) } Dir.chdir(base) { entries = filter_entries(Dir['*.*']) }
entries.each do |f| entries.each do |f|
name = f.split(".")[0..-2].join(".") name = f.split(".")[0..-2].join(".")
self.layouts[name] = Layout.new(base, f) self.layouts[name] = Layout.new(self, base, f)
end end
rescue Errno::ENOENT => e rescue Errno::ENOENT => e
# ignore missing layout dir # ignore missing layout dir
end end
# Read all the files in <base>/_posts and create a new Post object with each one. # Read all the files in <base>/_posts and create a new Post object with each one.
# #
# Returns nothing # Returns nothing
@ -57,7 +117,7 @@ module Jekyll
# first pass processes, but does not yet render post content # first pass processes, but does not yet render post content
entries.each do |f| entries.each do |f|
if Post.valid?(f) if Post.valid?(f)
post = Post.new(self.source, dir, f) post = Post.new(self, self.source, dir, f)
if post.published if post.published
self.posts << post self.posts << post
@ -65,18 +125,18 @@ module Jekyll
end end
end end
end end
# second pass renders each post now that full site payload is available # second pass renders each post now that full site payload is available
self.posts.each do |post| self.posts.each do |post|
post.render(self.layouts, site_payload) post.render(self.layouts, site_payload)
end end
self.posts.sort! self.posts.sort!
self.categories.values.map { |cats| cats.sort! { |a, b| b <=> a} } self.categories.values.map { |cats| cats.sort! { |a, b| b <=> a} }
rescue Errno::ENOENT => e rescue Errno::ENOENT => e
# ignore missing layout dir # ignore missing layout dir
end end
# Write each post to <dest>/<year>/<month>/<day>/<slug> # Write each post to <dest>/<year>/<month>/<day>/<slug>
# #
# Returns nothing # Returns nothing
@ -85,7 +145,7 @@ module Jekyll
post.write(self.dest) post.write(self.dest)
end end
end end
# Copy all regular files from <source> to <dest>/ ignoring # Copy all regular files from <source> to <dest>/ ignoring
# any files/directories that are hidden or backup files (start # any files/directories that are hidden or backup files (start
# with "." or "#" or end with "~") or contain site content (start with "_") # with "." or "#" or end with "~") or contain site content (start with "_")
@ -101,7 +161,7 @@ module Jekyll
directories = entries.select { |e| File.directory?(File.join(base, e)) } directories = entries.select { |e| File.directory?(File.join(base, e)) }
files = entries.reject { |e| File.directory?(File.join(base, e)) } files = entries.reject { |e| File.directory?(File.join(base, e)) }
# we need to make sure to process _posts *first* otherwise they # we need to make sure to process _posts *first* otherwise they
# might not be available yet to other templates as {{ site.posts }} # might not be available yet to other templates as {{ site.posts }}
if directories.include?('_posts') if directories.include?('_posts')
directories.delete('_posts') directories.delete('_posts')
@ -114,10 +174,10 @@ module Jekyll
transform_pages(File.join(dir, f)) transform_pages(File.join(dir, f))
else else
first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) } first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }
if first3 == "---" if first3 == "---"
# file appears to have a YAML header so process it as a page # 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.render(self.layouts, site_payload)
page.write(self.dest) page.write(self.dest)
else else
@ -150,7 +210,7 @@ module Jekyll
# "topics" => [<Post>] }} # "topics" => [<Post>] }}
def site_payload def site_payload
{"site" => { {"site" => {
"time" => Time.now, "time" => Time.now,
"posts" => self.posts.sort { |a,b| b <=> a }, "posts" => self.posts.sort { |a,b| b <=> a },
"categories" => post_attr_hash('categories'), "categories" => post_attr_hash('categories'),
"topics" => post_attr_hash('topics') "topics" => post_attr_hash('topics')

View File

@ -1,10 +1,11 @@
module Jekyll module Jekyll
class HighlightBlock < Liquid::Block class HighlightBlock < Liquid::Block
include Liquid::StandardFilters include Liquid::StandardFilters
# we need a language, but the linenos argument is optional. # we need a language, but the linenos argument is optional.
SYNTAX = /(\w+)\s?(:?linenos)?\s?/ SYNTAX = /(\w+)\s?(:?linenos)?\s?/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
super super
if markup =~ SYNTAX if markup =~ SYNTAX
@ -19,25 +20,25 @@ module Jekyll
raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]") raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight <lang> [linenos]")
end end
end end
def render(context) def render(context)
if Jekyll.pygments if context.registers[:site].pygments
render_pygments(context, super.to_s) render_pygments(context, super.to_s)
else else
render_codehighlighter(context, super.to_s) render_codehighlighter(context, super.to_s)
end end
end end
def render_pygments(context, code) def render_pygments(context, code)
if context["content_type"] == :markdown if context["content_type"] == :markdown
return "\n" + Albino.new(code, @lang).to_s(@options) + "\n" 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>" return "<notextile>" + Albino.new(code, @lang).to_s(@options) + "</notextile>"
else else
return Albino.new(code, @lang).to_s(@options) return Albino.new(code, @lang).to_s(@options)
end end
end end
def render_codehighlighter(context, code) def render_codehighlighter(context, code)
#The div is required because RDiscount blows ass #The div is required because RDiscount blows ass
<<-HTML <<-HTML
@ -49,7 +50,7 @@ module Jekyll
HTML HTML
end end
end end
end end
Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock) Liquid::Template.register_tag('highlight', Jekyll::HighlightBlock)

View File

@ -1,17 +1,17 @@
module Jekyll module Jekyll
class IncludeTag < Liquid::Tag class IncludeTag < Liquid::Tag
def initialize(tag_name, file, tokens) def initialize(tag_name, file, tokens)
super super
@file = file.strip @file = file.strip
end end
def render(context) def render(context)
if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./ if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
return "Include file '#{@file}' contains invalid characters or sequences" return "Include file '#{@file}' contains invalid characters or sequences"
end end
Dir.chdir(File.join(Jekyll.source, '_includes')) do Dir.chdir(File.join('.', '_includes')) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) } choices = Dir['**/*'].reject { |x| File.symlink?(x) }
if choices.include?(@file) if choices.include?(@file)
source = File.read(@file) source = File.read(@file)
@ -25,7 +25,7 @@ module Jekyll
end end
end end
end end
end end
Liquid::Template.register_tag('include', Jekyll::IncludeTag) Liquid::Template.register_tag('include', Jekyll::IncludeTag)