diff --git a/.rubocop.yml b/.rubocop.yml index 12e6553f..99e1e2b3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,14 +6,6 @@ AllCops: Exclude: - lib/jekyll/collection.rb - lib/jekyll/command.rb - - lib/jekyll/commands/build.rb - - lib/jekyll/commands/clean.rb - - lib/jekyll/commands/doctor.rb - - lib/jekyll/commands/help.rb - - lib/jekyll/commands/new.rb - - lib/jekyll/commands/serve - - lib/jekyll/commands/serve/servlet.rb - - lib/jekyll/commands/serve.rb - lib/jekyll/configuration.rb - lib/jekyll/converter.rb - lib/jekyll/converters/identity.rb diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index baa4e88a..cef13c91 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -5,8 +5,8 @@ module Jekyll # Create the Mercenary command for the Jekyll CLI for this Command def init_with_program(prog) prog.command(:build) do |c| - c.syntax 'build [options]' - c.description 'Build your site' + c.syntax "build [options]" + c.description "Build your site" c.alias :b add_build_options(c) @@ -27,15 +27,17 @@ module Jekyll options = configuration_from_options(options) site = Jekyll::Site.new(options) - if options.fetch('skip_initial_build', false) - Jekyll.logger.warn "Build Warning:", "Skipping the initial build. This may result in an out-of-date site." + if options.fetch("skip_initial_build", false) + Jekyll.logger.warn "Build Warning:", "Skipping the initial build." \ + " This may result in an out-of-date site." else build(site, options) end - if options.fetch('detach', false) - Jekyll.logger.info "Auto-regeneration:", "disabled when running server detached." - elsif options.fetch('watch', false) + if options.fetch("detach", false) + Jekyll.logger.info "Auto-regeneration:", + "disabled when running server detached." + elsif options.fetch("watch", false) watch(site, options) else Jekyll.logger.info "Auto-regeneration:", "disabled. Use --watch to enable." @@ -50,12 +52,13 @@ module Jekyll # Returns nothing. def build(site, options) t = Time.now - source = options['source'] - destination = options['destination'] - incremental = options['incremental'] + source = options["source"] + destination = options["destination"] + incremental = options["incremental"] Jekyll.logger.info "Source:", source Jekyll.logger.info "Destination:", destination - Jekyll.logger.info "Incremental build:", (incremental ? "enabled" : "disabled. Enable with --incremental") + Jekyll.logger.info "Incremental build:", + (incremental ? "enabled" : "disabled. Enable with --incremental") Jekyll.logger.info "Generating..." process_site(site) Jekyll.logger.info "", "done in #{(Time.now - t).round(3)} seconds." @@ -68,7 +71,7 @@ module Jekyll # # Returns nothing. def watch(_site, options) - External.require_with_graceful_fail 'jekyll-watch' + External.require_with_graceful_fail "jekyll-watch" Jekyll::Watcher.watch(options) end end # end of class << self diff --git a/lib/jekyll/commands/clean.rb b/lib/jekyll/commands/clean.rb index ca19d9b5..3a44d596 100644 --- a/lib/jekyll/commands/clean.rb +++ b/lib/jekyll/commands/clean.rb @@ -4,8 +4,9 @@ module Jekyll class << self def init_with_program(prog) prog.command(:clean) do |c| - c.syntax 'clean [subcommand]' - c.description 'Clean the site (removes site output and metadata file) without building.' + c.syntax "clean [subcommand]" + c.description "Clean the site " \ + "(removes site output and metadata file) without building." add_build_options(c) @@ -17,13 +18,13 @@ module Jekyll def process(options) options = configuration_from_options(options) - destination = options['destination'] - metadata_file = File.join(options['source'], '.jekyll-metadata') - sass_cache = File.join(options['source'], '.sass-cache') + destination = options["destination"] + metadata_file = File.join(options["source"], ".jekyll-metadata") + sass_cache = File.join(options["source"], ".sass-cache") - remove(destination, checker_func: :directory?) - remove(metadata_file, checker_func: :file?) - remove(sass_cache, checker_func: :directory?) + remove(destination, :checker_func => :directory?) + remove(metadata_file, :checker_func => :file?) + remove(sass_cache, :checker_func => :directory?) end def remove(filename, checker_func: :file?) diff --git a/lib/jekyll/commands/doctor.rb b/lib/jekyll/commands/doctor.rb index 5c48d5be..0c4f52f8 100644 --- a/lib/jekyll/commands/doctor.rb +++ b/lib/jekyll/commands/doctor.rb @@ -4,11 +4,12 @@ module Jekyll class << self def init_with_program(prog) prog.command(:doctor) do |c| - c.syntax 'doctor' - c.description 'Search site and print specific deprecation warnings' + c.syntax "doctor" + c.description "Search site and print specific deprecation warnings" c.alias(:hyde) - c.option 'config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file' + c.option "config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, + "Custom configuration file" c.action do |_, options| Jekyll::Commands::Doctor.process(options) @@ -37,7 +38,7 @@ module Jekyll end def deprecated_relative_permalinks(site) - if site.config['relative_permalinks'] + if site.config["relative_permalinks"] Jekyll::Deprecator.deprecation_message "Your site still uses relative" \ " permalinks, which was removed in" \ " Jekyll v3.0.0." @@ -78,7 +79,7 @@ module Jekyll def urls_only_differ_by_case(site) urls_only_differ_by_case = false urls = case_insensitive_urls(site.pages + site.docs_to_write, site.dest) - urls.each do |case_insensitive_url, real_urls| + urls.each do |_case_insensitive_url, real_urls| next unless real_urls.uniq.size > 1 urls_only_differ_by_case = true Jekyll.logger.warn "Warning:", "The following URLs only differ" \ @@ -102,10 +103,9 @@ module Jekyll end def case_insensitive_urls(things, destination) - things.inject({}) do |memo, thing| + things.each_with_object({}) do |thing, memo| dest = thing.destination(destination) (memo[dest.downcase] ||= []) << dest - memo end end end diff --git a/lib/jekyll/commands/help.rb b/lib/jekyll/commands/help.rb index 01bf3280..b4f136bf 100644 --- a/lib/jekyll/commands/help.rb +++ b/lib/jekyll/commands/help.rb @@ -4,8 +4,8 @@ module Jekyll class << self def init_with_program(prog) prog.command(:help) do |c| - c.syntax 'help [subcommand]' - c.description 'Show the help message, optionally for a given subcommand.' + c.syntax "help [subcommand]" + c.description "Show the help message, optionally for a given subcommand." c.action do |args, _| cmd = (args.first || "").to_sym @@ -22,7 +22,8 @@ module Jekyll end def invalid_command(prog, cmd) - Jekyll.logger.error "Error:", "Hmm... we don't know what the '#{cmd}' command is." + Jekyll.logger.error "Error:", + "Hmm... we don't know what the '#{cmd}' command is." Jekyll.logger.info "Valid commands:", prog.commands.keys.join(", ") end end diff --git a/lib/jekyll/commands/new.rb b/lib/jekyll/commands/new.rb index 50838f41..2658383c 100644 --- a/lib/jekyll/commands/new.rb +++ b/lib/jekyll/commands/new.rb @@ -1,4 +1,4 @@ -require 'erb' +require "erb" module Jekyll module Commands @@ -6,11 +6,11 @@ module Jekyll class << self def init_with_program(prog) prog.command(:new) do |c| - c.syntax 'new PATH' - c.description 'Creates a new Jekyll site scaffold in PATH' + c.syntax "new PATH" + c.description "Creates a new Jekyll site scaffold in PATH" - c.option 'force', '--force', 'Force creation even if PATH already exists' - c.option 'blank', '--blank', 'Creates scaffolding but with empty files' + c.option "force", "--force", "Force creation even if PATH already exists" + c.option "blank", "--blank", "Creates scaffolding but with empty files" c.action do |args, options| Jekyll::Commands::New.process(args, options) @@ -19,26 +19,19 @@ module Jekyll end def process(args, options = {}) - raise ArgumentError.new('You must specify a path.') if args.empty? + raise ArgumentError, "You must specify a path." if args.empty? new_blog_path = File.expand_path(args.join(" "), Dir.pwd) FileUtils.mkdir_p new_blog_path if preserve_source_location?(new_blog_path, options) - Jekyll.logger.abort_with "Conflict:", "#{new_blog_path} exists and is not empty." + Jekyll.logger.abort_with "Conflict:", + "#{new_blog_path} exists and is not empty." end if options["blank"] create_blank_site new_blog_path else - create_sample_files new_blog_path - - File.open(File.expand_path(initialized_post_name, new_blog_path), "w") do |f| - f.write(scaffold_post_content) - end - - File.open(File.expand_path("Gemfile", new_blog_path), "w") do |f| - f.write(gemfile_contents) - end + create_site new_blog_path end Jekyll.logger.info "New jekyll site installed in #{new_blog_path}." @@ -59,7 +52,7 @@ module Jekyll # # Returns the filename of the sample post, as a String def initialized_post_name - "_posts/#{Time.now.strftime('%Y-%m-%d')}-welcome-to-jekyll.markdown" + "_posts/#{Time.now.strftime("%Y-%m-%d")}-welcome-to-jekyll.markdown" end private @@ -90,12 +83,24 @@ gem "jekyll", "#{Jekyll::VERSION}" RUBY end + def create_site(new_blog_path) + create_sample_files new_blog_path + + File.open(File.expand_path(initialized_post_name, new_blog_path), "w") do |f| + f.write(scaffold_post_content) + end + + File.open(File.expand_path("Gemfile", new_blog_path), "w") do |f| + f.write(gemfile_contents) + end + end + def preserve_source_location?(path, options) !options["force"] && !Dir["#{path}/**/*"].empty? end def create_sample_files(path) - FileUtils.cp_r site_template + '/.', path + FileUtils.cp_r site_template + "/.", path FileUtils.rm File.expand_path(scaffold_path, path) end diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index 1482dbed..8c633397 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -14,7 +14,7 @@ module Jekyll "Show a directory listing instead of loading your index file."], "skip_initial_build" => ["skip_initial_build", "--skip-initial-build", "Skips the initial site build which occurs before the server is started."] - } + }.freeze # @@ -93,7 +93,7 @@ module Jekyll ) } - opts[:DirectoryIndex] = [] if opts[:JekyllOptions]['show_dir_listing'] + opts[:DirectoryIndex] = [] if opts[:JekyllOptions]["show_dir_listing"] enable_ssl(opts) enable_logging(opts) @@ -107,7 +107,7 @@ module Jekyll WEBrick::Config::FileHandler.merge({ :FancyIndexing => true, :NondisclosureName => [ - '.ht*', '~*' + ".ht*", "~*" ] }) end @@ -116,12 +116,12 @@ module Jekyll private def server_address(server, opts) - "%{prefix}://%{address}:%{port}%{baseurl}" % { + format("%{prefix}://%{address}:%{port}%{baseurl}", { :prefix => server.config[:SSLEnable] ? "https" : "http", :baseurl => opts["baseurl"] ? "#{opts["baseurl"]}/" : "", :address => server.config[:BindAddress], :port => server.config[:Port] - } + }) end # @@ -171,22 +171,27 @@ module Jekyll # forget to add one of the certificates. private + # rubocop:disable Metrics/AbcSize def enable_ssl(opts) return if !opts[:JekyllOptions]["ssl_cert"] && !opts[:JekyllOptions]["ssl_key"] if !opts[:JekyllOptions]["ssl_cert"] || !opts[:JekyllOptions]["ssl_key"] + # rubocop:disable Style/RedundantException raise RuntimeError, "--ssl-cert or --ssl-key missing." end - require "openssl" require "webrick/https" - source_key = Jekyll.sanitized_path(opts[:JekyllOptions]["source"], opts[:JekyllOptions]["ssl_key" ]) - source_certificate = Jekyll.sanitized_path(opts[:JekyllOptions]["source"], opts[:JekyllOptions]["ssl_cert"]) - opts[:SSLCertificate] = OpenSSL::X509::Certificate.new(File.read(source_certificate)) + source_key = Jekyll.sanitized_path(opts[:JekyllOptions]["source"], \ + opts[:JekyllOptions]["ssl_key" ]) + source_certificate = Jekyll.sanitized_path(opts[:JekyllOptions]["source"], \ + opts[:JekyllOptions]["ssl_cert"]) + opts[:SSLCertificate] = + OpenSSL::X509::Certificate.new(File.read(source_certificate)) opts[:SSLPrivateKey ] = OpenSSL::PKey::RSA.new(File.read(source_key)) opts[:SSLEnable] = true end private + def start_callback(detached) unless detached proc do @@ -197,7 +202,7 @@ module Jekyll private def mime_types - file = File.expand_path('../mime.types', File.dirname(__FILE__)) + file = File.expand_path("../mime.types", File.dirname(__FILE__)) WEBrick::HTTPUtils.load_mime_types(file) end end diff --git a/lib/jekyll/commands/serve/servlet.rb b/lib/jekyll/commands/serve/servlet.rb index bb4afe5a..5f258e6e 100644 --- a/lib/jekyll/commands/serve/servlet.rb +++ b/lib/jekyll/commands/serve/servlet.rb @@ -7,7 +7,7 @@ module Jekyll DEFAULTS = { "Cache-Control" => "private, max-age=0, proxy-revalidate, " \ "no-store, no-cache, must-revalidate" - } + }.freeze def initialize(server, root, callbacks) # So we can access them easily. @@ -25,8 +25,7 @@ module Jekyll super || super(req, res, "#{basename}.html") end - # - + # rubocop:disable Style/MethodName def do_GET(req, res) rtn = super validate_and_ensure_charset(req, res)