Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 'jekyll serve' command. #2269

Merged
merged 1 commit into from
Apr 27, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 45 additions & 22 deletions lib/jekyll/commands/serve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,22 @@ def init_with_program(prog)
# Boot up a WEBrick server which points to the compiled site's root.
def process(options)
options = configuration_from_options(options)

require 'webrick'

destination = options['destination']

FileUtils.mkdir_p(destination)

# monkey patch WEBrick using custom 404 page (/404.html)
if File.exist?(File.join(destination, '404.html'))
WEBrick::HTTPResponse.class_eval do
def create_error_page
@body = IO.read(File.join(@config[:DocumentRoot], '404.html'))
end
end
end

# recreate NondisclosureName under utf-8 circumstance
fh_option = WEBrick::Config::FileHandler
fh_option[:NondisclosureName] = ['.ht*','~*']
setup(destination)

s = WEBrick::HTTPServer.new(webrick_options(options))
s.unmount("")

s.config.store(:DirectoryIndex, s.config[:DirectoryIndex] << "index.xml")
s.mount(
options['baseurl'],
WEBrick::HTTPServlet::FileHandler,
destination,
file_handler_options
)

s.mount(options['baseurl'], WEBrick::HTTPServlet::FileHandler, destination, fh_option)
Jekyll.logger.info "Server address:", server_address(s, options)

Jekyll.logger.info "Server address:", "http://#{s.config[:BindAddress]}:#{s.config[:Port]}"
p s

if options['detach'] # detach the server
pid = Process.fork { s.start }
Expand All @@ -68,14 +57,30 @@ def create_error_page
end
end

def setup(destination)
require 'webrick'

FileUtils.mkdir_p(destination)

# monkey patch WEBrick using custom 404 page (/404.html)
if File.exist?(File.join(destination, '404.html'))
WEBrick::HTTPResponse.class_eval do
def create_error_page
@body = IO.read(File.join(@config[:DocumentRoot], '404.html'))
end
end
end
end

def webrick_options(config)
opts = {
:DocumentRoot => config['destination'],
:Port => config['port'],
:BindAddress => config['host'],
:MimeTypes => mime_types,
:DoNotReverseLookup => true,
:StartCallback => start_callback(config['detach'])
:StartCallback => start_callback(config['detach']),
:DirectoryIndex => %w(index.html index.htm index.cgi index.rhtml index.xml)
}

if !config['verbose']
Expand All @@ -99,6 +104,24 @@ def mime_types
WEBrick::HTTPUtils::load_mime_types(mime_types_file)
end

def server_address(server, options)
baseurl = "#{options['baseurl']}/" if options['baseurl']
[
"http://",
server.config[:BindAddress],
":",
server.config[:Port],
baseurl || ""
].map(&:to_s).join("")
end

# recreate NondisclosureName under utf-8 circumstance
def file_handler_options
fh_option = WEBrick::Config::FileHandler
fh_option[:NondisclosureName] = ['.ht*','~*']
fh_option
end

end

end
Expand Down