lib/jekyll/commands/serve.rb in jekyll-3.1.6 vs lib/jekyll/commands/serve.rb in jekyll-3.2.0.pre.beta1
- old
+ new
@@ -1,20 +1,22 @@
module Jekyll
module Commands
class Serve < Command
class << self
COMMAND_OPTIONS = {
- "ssl_cert" => ["--ssl-cert [CERT]", "X.509 (SSL) certificate."],
- "host" => ["host", "-H", "--host [HOST]", "Host to bind to"],
- "open_url" => ["-o", "--open-url", "Launch your browser with your site."],
- "detach" => ["-B", "--detach", "Run the server in the background (detach)"],
- "ssl_key" => ["--ssl-key [KEY]", "X.509 (SSL) Private Key."],
- "port" => ["-P", "--port [PORT]", "Port to listen on"],
- "baseurl" => ["-b", "--baseurl [URL]", "Base URL"],
+ "ssl_cert" => ["--ssl-cert [CERT]", "X.509 (SSL) certificate."],
+ "host" => ["host", "-H", "--host [HOST]", "Host to bind to"],
+ "open_url" => ["-o", "--open-url", "Launch your site in a browser"],
+ "detach" => ["-B", "--detach", "Run the server in the background"],
+ "ssl_key" => ["--ssl-key [KEY]", "X.509 (SSL) Private Key."],
+ "port" => ["-P", "--port [PORT]", "Port to listen on"],
+ "baseurl" => ["-b", "--baseurl [URL]", "Base URL"],
+ "show_dir_listing" => ["--show-dir-listing",
+ "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
#
def init_with_program(prog)
prog.command(:serve) do |cmd|
@@ -89,10 +91,12 @@
index.cgi
index.xml
)
}
+ opts[:DirectoryIndex] = [] if opts[:JekyllOptions]["show_dir_listing"]
+
enable_ssl(opts)
enable_logging(opts)
opts
end
@@ -101,40 +105,37 @@
private
def file_handler_opts
WEBrick::Config::FileHandler.merge({
:FancyIndexing => true,
:NondisclosureName => [
- '.ht*', '~*'
+ ".ht*", "~*"
]
})
end
#
private
def server_address(server, opts)
- "%{prefix}://%{address}:%{port}%{baseurl}" % {
- :prefix => server.config[:SSLEnable] ? "https" : "http",
+ format("%{prefix}://%{address}:%{port}%{baseurl}", {
+ :prefix => server.config[:SSLEnable] ? "https" : "http",
:baseurl => opts["baseurl"] ? "#{opts["baseurl"]}/" : "",
:address => server.config[:BindAddress],
- :port => server.config[:Port]
- }
+ :port => server.config[:Port]
+ })
end
#
private
def launch_browser(server, opts)
- command =
- if Utils::Platforms.windows?
- "start"
- elsif Utils::Platforms.osx?
- "open"
- else
- "xdg-open"
- end
- system command, server_address(server, opts)
+ address = server_address(server, opts)
+ return system "start", address if Utils::Platforms.windows?
+ return system "xdg-open", address if Utils::Platforms.linux?
+ return system "open", address if Utils::Platforms.osx?
+ Jekyll.logger.error "Refusing to launch browser; " \
+ "Platform launcher unknown."
end
# Keep in our area with a thread or detach the server as requested
# by the user. This method determines what we do based on what you
# ask us to do.
@@ -168,36 +169,41 @@
# Add SSL to the stack if the user triggers --enable-ssl and they
# provide both types of certificates commonly needed. Raise if they
# 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
Jekyll.logger.info("Server running...", "press ctrl-c to stop.")
end
end
end
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
end
end