From ac41312c5d928d041be818f83802cd7450ba8642 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 28 Dec 2014 14:37:02 -0500 Subject: [PATCH 1/3] Separate jekyll-docs out into a separate gem & bless it --- bin/jekyll | 9 ++---- lib/jekyll/commands/docs.rb | 30 ------------------- lib/jekyll/external.rb | 59 +++++++++++++++++++++++++++++++++++++ site/_docs/usage.md | 5 +++- 4 files changed, 66 insertions(+), 37 deletions(-) delete mode 100644 lib/jekyll/commands/docs.rb create mode 100644 lib/jekyll/external.rb diff --git a/bin/jekyll b/bin/jekyll index 060c9125..3f2988bc 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -6,12 +6,9 @@ $:.unshift File.join(File.dirname(__FILE__), *%w{ .. lib }) require 'jekyll' require 'mercenary' -%w[jekyll-import].each do |blessed_gem| - begin - require blessed_gem - rescue LoadError - end -end +Jekyll::External.require_if_present( + Jekyll::External.blessed_gems +) Jekyll::PluginManager.require_from_bundler diff --git a/lib/jekyll/commands/docs.rb b/lib/jekyll/commands/docs.rb deleted file mode 100644 index 71c10160..00000000 --- a/lib/jekyll/commands/docs.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Jekyll - module Commands - class Docs < Command - - class << self - - def init_with_program(prog) - prog.command(:docs) do |c| - c.syntax 'docs' - c.description "Launch local server with docs for Jekyll v#{Jekyll::VERSION}" - - c.option 'port', '-P', '--port [PORT]', 'Port to listen on' - c.option 'host', '-H', '--host [HOST]', 'Host to bind to' - - c.action do |args, options| - options.merge!({ - 'source' => File.expand_path("../../../site", File.dirname(__FILE__)), - 'destination' => File.expand_path("../../../site/_site", File.dirname(__FILE__)) - }) - Jekyll::Commands::Build.process(options) - Jekyll::Commands::Serve.process(options) - end - end - end - - end - - end - end -end diff --git a/lib/jekyll/external.rb b/lib/jekyll/external.rb new file mode 100644 index 00000000..e41bce27 --- /dev/null +++ b/lib/jekyll/external.rb @@ -0,0 +1,59 @@ +module Jekyll + module External + class << self + + # + # Gems that, if installed, should be loaded. + # Usually contain subcommands. + # + def blessed_gems + %w{ + jekyll-docs + jekyll-import + } + end + + # + # Require a gem or file if it's present, otherwise silently fail. + # + # names - a string gem name or array of gem names + # + def require_if_present(names) + Array(names).each do |name| + begin + require name + rescue LoadError + Jekyll.logger.debug "Couldn't load #{name}. Skipping." + false + end + end + end + + # + # Require a gem or gems. If it's not present, show a very nice error + # message that explains everything and is much more helpful than the + # normal LoadError. + # + # names - a string gem name or array of gem names + # + def require_with_graceful_fail(names) + Array(names).each do |name| + begin + require name + rescue LoadError => e + Jekyll.logger.error "Dependency Error:", <<-MSG +Yikes! It looks like you don't have #{name} or one of its dependencies installed. +In order to use Jekyll as currently configured, you'll need to install this gem. + +The full error message from Ruby is: '#{e.message}' + +If you run into trouble, you can find helpful resources at http://jekyllrb.com/help/! + MSG + raise Jekyll::Errors::MissingDependencyException.new(name) + end + end + end + + end + end +end diff --git a/site/_docs/usage.md b/site/_docs/usage.md index 4959ad45..7d2d68e9 100644 --- a/site/_docs/usage.md +++ b/site/_docs/usage.md @@ -81,4 +81,7 @@ $ jekyll build --source _source --destination _deploy {% endhighlight %} For more about the possible configuration options, see the -[configuration](../configuration/) page. \ No newline at end of file +[configuration](../configuration/) page. + +If you're interested in browsing these docs on-the-go, install the +`jekyll-docs` gem and run `jekyll docs` in your terminal. From 8c19a6f43082deb1dabbf636e178f841c0ce1940 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 28 Dec 2014 14:37:14 -0500 Subject: [PATCH 2/3] Print an error message in bin/jekyll with no arguments --- bin/jekyll | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/jekyll b/bin/jekyll index 3f2988bc..6c968cec 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -29,6 +29,7 @@ Mercenary.program(:jekyll) do |p| p.action do |args, options| if args.empty? + Jekyll.logger.error "A subcommand is required." puts p else unless p.has_command?(args.first) From c576d239089ed301d0c003b8afd394271cca912c Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 28 Dec 2014 14:37:49 -0500 Subject: [PATCH 3/3] Use External instead of Deprecator for requiring gracefully --- lib/jekyll.rb | 12 +++++------- lib/jekyll/commands/build.rb | 2 +- .../converters/markdown/rdiscount_parser.rb | 2 +- .../converters/markdown/redcarpet_parser.rb | 6 +++--- lib/jekyll/deprecator.rb | 17 ----------------- 5 files changed, 10 insertions(+), 29 deletions(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index bde11581..3f01e4d0 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -29,12 +29,6 @@ require 'liquid' require 'kramdown' require 'colorator' -# Conditional optimizations -begin - require 'liquid-c' -rescue LoadError -end - SafeYAML::OPTIONS[:suppress_warnings] = true Liquid::Template.error_mode = :strict @@ -51,6 +45,7 @@ module Jekyll autoload :EntryFilter, 'jekyll/entry_filter' autoload :Errors, 'jekyll/errors' autoload :Excerpt, 'jekyll/excerpt' + autoload :External, 'jekyll/external' autoload :Filters, 'jekyll/filters' autoload :FrontmatterDefaults, 'jekyll/frontmatter_defaults' autoload :Layout, 'jekyll/layout' @@ -162,6 +157,9 @@ module Jekyll end end + # Conditional optimizations + Jekyll::External.require_if_present('liquid-c') + end end @@ -172,7 +170,7 @@ require_all 'jekyll/generators' require_all 'jekyll/tags' # Eventually remove these for 3.0 as non-core -Jekyll::Deprecator.gracefully_require(%w[ +Jekyll::External.require_with_graceful_fail(%w[ toml jekyll-paginate jekyll-gist diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index 11f1f75e..536324fc 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -66,7 +66,7 @@ module Jekyll # # Returns nothing. def watch(site, options) - Deprecator.gracefully_require 'jekyll-watch' + External.require_with_graceful_fail 'jekyll-watch' Jekyll::Watcher.watch(options) end diff --git a/lib/jekyll/converters/markdown/rdiscount_parser.rb b/lib/jekyll/converters/markdown/rdiscount_parser.rb index 0c8634e4..fb5172e7 100644 --- a/lib/jekyll/converters/markdown/rdiscount_parser.rb +++ b/lib/jekyll/converters/markdown/rdiscount_parser.rb @@ -3,7 +3,7 @@ module Jekyll class Markdown class RDiscountParser def initialize(config) - Jekyll::Deprecator.gracefully_require "rdiscount" + Jekyll::External.require_with_graceful_fail "rdiscount" @config = config @rdiscount_extensions = @config['rdiscount']['extensions'].map { |e| e.to_sym } end diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index 468069e9..3be60739 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -14,7 +14,7 @@ module Jekyll module WithPygments include CommonMethods def block_code(code, lang) - Jekyll::Deprecator.gracefully_require("pygments") + Jekyll::External.require_with_graceful_fail("pygments") lang = lang && lang.split.first || "text" add_code_tags( Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), @@ -55,7 +55,7 @@ module Jekyll def initialize(config) - Deprecator.gracefully_require("redcarpet") + External.require_with_graceful_fail("redcarpet") @config = config @redcarpet_extensions = {} @config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true } @@ -71,7 +71,7 @@ module Jekyll end when "rouge" Class.new(Redcarpet::Render::HTML) do - Jekyll::Deprecator.gracefully_require(%w[ + Jekyll::External.require_with_graceful_fail(%w[ rouge rouge/plugins/redcarpet ]) diff --git a/lib/jekyll/deprecator.rb b/lib/jekyll/deprecator.rb index 8e4daf9c..9ec5857e 100644 --- a/lib/jekyll/deprecator.rb +++ b/lib/jekyll/deprecator.rb @@ -40,22 +40,5 @@ module Jekyll Jekyll.logger.warn "Defaults:", "Please update your front-matter defaults to use 'type: #{current}'." end - def gracefully_require(gem_name) - Array(gem_name).each do |name| - begin - require name - rescue LoadError => e - Jekyll.logger.error "Dependency Error:", <<-MSG - Yikes! It looks like you don't have #{name} or one of its dependencies installed. - In order to use Jekyll as currently configured, you'll need to install this gem. - - The full error message from Ruby is: '#{e.message}' - - If you run into trouble, you can find helpful resources at http://jekyllrb.com/help/! -MSG - raise Errors::MissingDependencyException.new(name) - end - end - end end end