From e99a9e5821a7ba16ce42a1f5de378012f22acae0 Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Thu, 12 Feb 2015 21:53:13 -0500 Subject: [PATCH 1/2] Custom WEBrick FileHandler for stripping .html extension This first performs the normal static file check at the exact location. If no file is found, attempt the check again with an ".html" suffix. See the following for base class search_file implementation: https://github.com/ruby/ruby/blob/4607f95/lib/webrick/httpservlet/filehandler.rb#L363-L383 --- lib/jekyll/commands/serve.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index 6467c5ab..912f4f62 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -40,7 +40,7 @@ module Jekyll s.mount( options['baseurl'], - WEBrick::HTTPServlet::FileHandler, + FileHandler, destination, file_handler_options ) @@ -131,6 +131,19 @@ module Jekyll end + # Custom WEBrick FileHandler servlet for serving "/file.html" at "/file" + # when no exact match is found. This mirrors the behavior of GitHub Pages + # and many static web server configs. + require 'webrick' + class FileHandler < ::WEBrick::HTTPServlet::FileHandler + def search_file(req, res, basename) + if file = super + file + else + super(req, res, "#{basename}.html") + end + end + end end end end From 491cce7a99976a443e058b4aaa6655882d8f8427 Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Thu, 12 Feb 2015 22:05:30 -0500 Subject: [PATCH 2/2] Avoid requiring webrick at boot time This is a bit uglier but allows deferring loading webrick until the serve command is invoked as opposed to when it's required. --- lib/jekyll/commands/serve.rb | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index 912f4f62..111d6709 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -40,7 +40,7 @@ module Jekyll s.mount( options['baseurl'], - FileHandler, + custom_file_handler, destination, file_handler_options ) @@ -99,6 +99,21 @@ module Jekyll opts end + # Custom WEBrick FileHandler servlet for serving "/file.html" at "/file" + # when no exact match is found. This mirrors the behavior of GitHub + # Pages and many static web server configs. + def custom_file_handler + Class.new WEBrick::HTTPServlet::FileHandler do + def search_file(req, res, basename) + if file = super + file + else + super(req, res, "#{basename}.html") + end + end + end + end + def start_callback(detached) unless detached Proc.new { Jekyll.logger.info "Server running...", "press ctrl-c to stop." } @@ -131,19 +146,6 @@ module Jekyll end - # Custom WEBrick FileHandler servlet for serving "/file.html" at "/file" - # when no exact match is found. This mirrors the behavior of GitHub Pages - # and many static web server configs. - require 'webrick' - class FileHandler < ::WEBrick::HTTPServlet::FileHandler - def search_file(req, res, basename) - if file = super - file - else - super(req, res, "#{basename}.html") - end - end - end end end end