Added ability to set Jekyll parameters via _config.yaml file

This commit is contained in:
Mark 2009-02-04 20:07:35 +11:00 committed by mreid
parent e72cde12fa
commit 6fec047631
3 changed files with 155 additions and 60 deletions

View File

@ -174,6 +174,43 @@ leaf directory resulting in URLs like 2008/11/17/blogging-like-a-hacker/.
$ jekyll --permalink [date|none|pretty] $ jekyll --permalink [date|none|pretty]
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
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:
destination: [PATH] # Specify where the site should be rendered
markdown: [maruku|rdiscount] # Which markdown renderer to use?
maruku: # This is a YAML hash for Maruku settings
use_divs: [BOOLEAN] # Use the div element Maruku extension
use_tex: [BOOLEAN] # Use the LaTeX extension to Maruku
png_dir: [PATH] # Where should the math PNGs be stored?
png_url: [URL] # A relative URL for the PNGs
The default configuration is shown below as in YAML format:
destination: ./_site
auto: false
lsi: false
server_port: 4000
pygments: false
markdown: maruku
permalink: date
maruku:
use_tex: false
use_divs: false
png_dir: images/latex
png_url: /images/latex
Parameters set in a configuration file override the default values. Parameters
set using command line options override both the default values and those set
in a configuration file.
h2. Data h2. Data
Jekyll traverses your site looking for files to process. Any files with YAML Jekyll traverses your site looking for files to process. Any files with YAML

View File

@ -10,46 +10,61 @@ Basic Command Line Usage:
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>
Options: Configuration is read from '<source>/_config.yaml' but can be overriden
using the following options:
HELP HELP
require 'optparse' require 'optparse'
require 'jekyll' require 'jekyll'
options = {} # Default options. Overriden by values in _config.yaml or command-line opts.
# (Strings rather symbols used for compatability with YAML)
DEFAULTS = {
'auto' => false,
'server' => false,
'server_port' => 4000,
'lsi' => false,
'pygments' => false,
'markdown' => 'maruku',
'permalink' => 'date',
'maruku' => {
'use_tex' => false,
'use_divs' => false,
'png_engine' => 'blahtex',
'png_dir' => 'images/latex',
'png_url' => '/images/latex'
}
}
options = {}
opts = OptionParser.new do |opts| opts = OptionParser.new do |opts|
opts.banner = help opts.banner = help
opts.on("--auto", "Auto-regenerate") do opts.on("--auto", "Auto-regenerate") do
options[:auto] = true options['auto'] = true
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 || 4000 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
Jekyll.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
Jekyll.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
begin options['markdown'] = 'rdiscount'
require 'rdiscount'
Jekyll.markdown_proc = Proc.new { |x| RDiscount.new(x).to_html }
puts 'Using rdiscount for Markdown'
rescue LoadError
puts 'You must have the rdiscount gem installed first'
end
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|
Jekyll.permalink_style = (style || 'date').to_sym options['permalink'] = style unless style.nil?
end end
opts.on("--version", "Display current version") do opts.on("--version", "Display current version") do
@ -58,13 +73,45 @@ opts = OptionParser.new do |opts|
end end
end end
# Read command line options into `options` hash
opts.parse! opts.parse!
def clean(dest) # Get source and destintation from command line
FileUtils.rm_rf(dest) source = nil
FileUtils.mkdir_p(dest) destination = nil
case ARGV.size
when 0
source = '.'
destination = File.join('.', '_site')
when 1
source = '.'
options['destination'] = destination = ARGV[0]
when 2
options['source'] = source = ARGV[0]
options['destination'] = destination = ARGV[1]
else
puts "Invalid options. Run `jekyll --help` for assistance."
exit(1)
end 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 < options
options = DEFAULTS.deep_merge(config).deep_merge(options)
# Override source and destination directories if set by option or config
source = options['source'] || source
destination = options['destination'] || destination
def globs(source) def globs(source)
Dir.chdir(source) do Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) } dirs = Dir['*'].select { |x| File.directory?(x) }
@ -74,25 +121,55 @@ def globs(source)
end end
end end
source = nil # Interpret the simple options and configure Jekyll appropriately
destination = nil Jekyll.lsi = options['lsi']
Jekyll.pygments = options['pygments']
Jekyll.permalink_style = options['permalink'].to_sym
case ARGV.size # Set the Markdown interpreter (and Maruku options, if necessary)
when 0 case options['markdown']
source = '.'
destination = File.join('.', '_site') when 'rdiscount'
when 1 begin
source = '.' require 'rdiscount'
destination = ARGV[0] Jekyll.markdown_proc = Proc.new { |x| RDiscount.new(x).to_html }
when 2 puts 'Using rdiscount for Markdown'
source = ARGV[0] rescue LoadError
destination = ARGV[1] puts 'You must have the rdiscount gem installed first'
else end
puts "Invalid options. Run `jekyll --help` for assistance."
exit(1) when 'maruku'
begin
require 'maruku'
Jekyll.markdown_proc = Proc.new { |x| Maruku.new(x).to_html }
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
if options[:auto] # Run the directory watcher for auto-generation, if required
if options['auto']
require 'directory_watcher' require 'directory_watcher'
puts "Auto-regenerating enabled: #{source} -> #{destination}" puts "Auto-regenerating enabled: #{source} -> #{destination}"
@ -109,22 +186,23 @@ if options[:auto]
dw.start dw.start
unless options[:server] unless options['server']
loop { sleep 1000 } loop { sleep 1000 }
end end
else else
Jekyll.process(source, destination) Jekyll.process(source, destination)
puts "Successfully generated site in #{destination}" puts "Successfully generated site: #{source} -> #{destination}"
end end
if options[:server] # Run the server on the specified port, if required
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(
:Port => options[:server_port], :Port => options['server_port'],
:DocumentRoot => destination :DocumentRoot => destination
) )
t = Thread.new { t = Thread.new {

View File

@ -13,22 +13,6 @@ require 'yaml'
# 3rd party # 3rd party
require 'liquid' require 'liquid'
require 'redcloth' require 'redcloth'
begin
require 'maruku'
require 'maruku/ext/math'
# 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] = 'blahtex'
MaRuKu::Globals[:html_png_dir] = 'images/latex'
MaRuKu::Globals[:html_png_url] = '/images/latex/'
rescue LoadError
puts "The maruku gem is required for markdown support!"
end
# internal requires # internal requires
require 'jekyll/core_ext' require 'jekyll/core_ext'
@ -47,10 +31,6 @@ module Jekyll
attr_accessor :source, :dest, :lsi, :pygments, :markdown_proc,:permalink_style attr_accessor :source, :dest, :lsi, :pygments, :markdown_proc,:permalink_style
end end
Jekyll.lsi = false
Jekyll.pygments = false
Jekyll.markdown_proc = Proc.new { |x| Maruku.new(x).to_html }
Jekyll.permalink_style = :date
def self.process(source, dest) def self.process(source, dest)
require 'classifier' if Jekyll.lsi require 'classifier' if Jekyll.lsi