From 6edfae8c26671293466328bb301a44315f93f6ef Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 22 Feb 2009 22:29:26 +1100 Subject: [PATCH] Refactored configuration and command-line option code --- bin/jekyll | 105 ++++++++---------------------------- lib/jekyll.rb | 103 ++++++++++++++++++++++++++++++++--- lib/jekyll/convertible.rb | 4 +- lib/jekyll/site.rb | 19 +++---- test/test_generated_site.rb | 7 ++- test/test_post.rb | 2 +- test/test_site.rb | 7 ++- 7 files changed, 140 insertions(+), 107 deletions(-) diff --git a/bin/jekyll b/bin/jekyll index 9e7fd081..9a0646d7 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -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 /_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 diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 36b5494c..ce8a717a 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -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 diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 62463209..e7d17789 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -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 diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 781f7d5a..a52a5f45 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -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 - 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. # diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 6e3e5f77..567335fc 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -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 diff --git a/test/test_post.rb b/test/test_post.rb index 4627d43c..1af7df6d 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/helper' class TestPost < Test::Unit::TestCase def setup - + Jekyll.configure(Jekyll::DEFAULTS) end def test_valid diff --git a/test/test_site.rb b/test/test_site.rb index 8cf17ada..df4e0e2d 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -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