#!/usr/bin/env ruby $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) help = < ./_site jekyll # . -> jekyll # -> Options: HELP require 'optparse' require 'jekyll' options = {} opts = OptionParser.new do |opts| opts.banner = help opts.on("--auto", "Auto-regenerate") do options[:auto] = true end opts.on("--server [PORT]", "Start web server (default port 4000)") do |port| options[:server] = true options[:server_port] = port || 4000 end opts.on("--lsi", "Use LSI for better related posts") do Jekyll.lsi = true end opts.on("--pygments", "Use pygments to highlight code") do Jekyll.pygments = true end opts.on("--rdiscount", "Use rdiscount gem for Markdown") do 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 end opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style| Jekyll.permalink_style = (style || 'date').to_sym end end opts.parse! def clean(dest) FileUtils.rm_rf(dest) FileUtils.mkdir_p(dest) end def globs(source) Dir.chdir(source) do dirs = Dir['*'].select { |x| File.directory?(x) } dirs -= ['_site'] dirs = dirs.map { |x| "#{x}/**/*" } dirs += ['*'] end end source = nil destination = nil case ARGV.size when 0 source = '.' destination = File.join('.', '_site') when 1 source = '.' destination = ARGV[0] when 2 source = ARGV[0] destination = ARGV[1] else puts "Invalid options. Run `jekyll --help` for assistance." exit(1) end if options[:auto] require 'directory_watcher' puts "Auto-regenerating enabled: #{source} -> #{destination}" dw = DirectoryWatcher.new(source) dw.interval = 1 dw.glob = globs(source) 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) end dw.start unless options[:server] loop { sleep 1000 } end else Jekyll.process(source, destination) puts "Successfully generated site in #{destination}" end if options[:server] require 'webrick' include WEBrick FileUtils.mkdir_p(destination) s = HTTPServer.new( :Port => options[:server_port], :DocumentRoot => destination ) t = Thread.new { s.start } trap("INT") { s.shutdown } t.join() end