Refactored configuration and command-line option code

This commit is contained in:
Mark 2009-02-22 22:29:26 +11:00 committed by mreid
parent 6fec047631
commit 6edfae8c26
7 changed files with 140 additions and 107 deletions

View File

@ -18,26 +18,7 @@ HELP
require 'optparse'
require 'jekyll'
# 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'
}
}
exec = {}
options = {}
opts = OptionParser.new do |opts|
opts.banner = help
@ -45,6 +26,10 @@ opts = OptionParser.new do |opts|
opts.on("--auto", "Auto-regenerate") do
options['auto'] = true
end
opts.on("--no-auto", "No auto-regenerate") do
options['auto'] = false
end
opts.on("--server [PORT]", "Start web server (default port 4000)") do |port|
options['server'] = true
@ -76,19 +61,18 @@ end
# Read command line options into `options` hash
opts.parse!
# Temporarily set source and destination options to read in config file
options['source'] = Jekyll::DEFAULTS['source']
options['destination'] = Jekyll::DEFAULTS['destination']
# Get source and destintation from command line
source = nil
destination = nil
case ARGV.size
when 0
source = '.'
destination = File.join('.', '_site')
when 1
source = '.'
options['destination'] = destination = ARGV[0]
options['destination'] = ARGV[0]
when 2
options['source'] = source = ARGV[0]
options['destination'] = destination = ARGV[1]
options['source'] = ARGV[0]
options['destination'] = ARGV[1]
else
puts "Invalid options. Run `jekyll --help` for assistance."
exit(1)
@ -96,7 +80,7 @@ end
# Get configuration from <source>/_config.yaml
config = {}
config_file = File.join(source, '_config.yaml')
config_file = File.join(options['source'], '_config.yaml')
begin
config = YAML.load_file( config_file )
puts "Configuration from #{config_file}"
@ -105,13 +89,14 @@ rescue => err
puts "\t" + err
end
# Merge DEFAULTS < config < options
options = DEFAULTS.deep_merge(config).deep_merge(options)
# Merge DEFAULTS < config file < command line options
options = Jekyll::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
# Get source and destination directories (possibly set by config file)
source = options['source']
destination = options['destination']
# Files to watch
def globs(source)
Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) }
@ -121,53 +106,6 @@ def globs(source)
end
end
# Interpret the simple options and configure Jekyll appropriately
Jekyll.lsi = options['lsi']
Jekyll.pygments = options['pygments']
Jekyll.permalink_style = options['permalink'].to_sym
# Set the Markdown interpreter (and Maruku options, if necessary)
case options['markdown']
when 'rdiscount'
begin
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
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
# Run the directory watcher for auto-generation, if required
if options['auto']
require 'directory_watcher'
@ -181,7 +119,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(source, destination)
Jekyll.process(options)
end
dw.start
@ -190,7 +128,8 @@ if options['auto']
loop { sleep 1000 }
end
else
Jekyll.process(source, destination)
puts "Building site: #{source} -> #{destination}"
Jekyll.process(options)
puts "Successfully generated site: #{source} -> #{destination}"
end

View File

@ -27,17 +27,104 @@ 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 = {
'auto' => false,
'server' => false,
'server_port' => 4000,
'source' => '.',
'destination' => File.join('.', '_site'),
'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'
}
}
class << self
attr_accessor :source, :dest, :lsi, :pygments, :markdown_proc,:permalink_style
attr_accessor :source,:dest,:lsi,:pygments,:permalink_style
end
def self.process(source, dest)
# Initializes some global Jekyll parameters
def self.configure(options)
# Interpret the simple options and configure Jekyll appropriately
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
Jekyll.source = source
Jekyll.dest = dest
Jekyll::Site.new(source, dest).process
# Set the Markdown interpreter (and Maruku options, if necessary)
case options['markdown']
when 'rdiscount'
begin
require 'rdiscount'
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
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

View File

@ -27,10 +27,10 @@ module Jekyll
case Jekyll.content_type
when :textile
self.ext = ".html"
self.content = RedCloth.new(self.content).to_html
self.content = Jekyll.textile(self.content)
when :markdown
self.ext = ".html"
self.content = Jekyll.markdown_proc.call(self.content)
self.content = Jekyll.markdown(self.content)
end
end

View File

@ -1,24 +1,25 @@
module Jekyll
class Site
attr_accessor :source, :dest
attr_accessor :layouts, :posts, :categories
attr_accessor :config, :layouts, :posts, :categories
# Initialize the site
# +source+ is String path to the source directory containing
# the proto-site
# +dest+ is the String path to the directory where the generated
# site should be written
# +config+ is a Hash containing site configurations details
#
# Returns <Site>
def initialize(source, dest)
self.source = source
self.dest = dest
def initialize(config)
self.config = config.clone
self.layouts = {}
self.posts = []
self.categories = Hash.new { |hash, key| hash[key] = Array.new }
end
# The directory containing the proto-site.
def source; self.config['source']; end
# Where the completed site should be written.
def dest; self.config['destination']; end
# Do the actual work of processing the site and generating the
# real deal.
#

View File

@ -3,8 +3,11 @@ require File.dirname(__FILE__) + '/helper'
class TestGeneratedSite < Test::Unit::TestCase
def setup
clear_dest
@source = File.join(File.dirname(__FILE__), *%w[source])
@s = Site.new(@source, dest_dir)
config = Jekyll::DEFAULTS.clone
config['source'] = File.join(File.dirname(__FILE__), *%w[source])
config['destination'] = dest_dir
Jekyll.configure(config)
@s = Site.new(config)
@s.process
@index = File.read(File.join(dest_dir, 'index.html'))
end

View File

@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/helper'
class TestPost < Test::Unit::TestCase
def setup
Jekyll.configure(Jekyll::DEFAULTS)
end
def test_valid

View File

@ -2,8 +2,11 @@ require File.dirname(__FILE__) + '/helper'
class TestSite < Test::Unit::TestCase
def setup
@source = File.join(File.dirname(__FILE__), *%w[source])
@s = Site.new(@source, dest_dir)
config = {
'source' => File.join(File.dirname(__FILE__), *%w[source]),
'destination' => dest_dir
}
@s = Site.new(config)
end
def test_site_init