Class: Jekyll::Commands::Serve

Inherits:
Jekyll::Command show all
Defined in:
lib/jekyll/commands/serve.rb,
lib/jekyll/commands/serve/servlet.rb

Defined Under Namespace

Classes: Servlet

Constant Summary collapse

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"],
  "skip_initial_build" => ["skip_initial_build", "--skip-initial-build",
    "Skips the initial site build which occurs before the server is started."]
}

Class Method Summary collapse

Methods inherited from Jekyll::Command

add_build_options, configuration_from_options, inherited, process_site, subclasses

Class Method Details

.adminObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll/commands/serve.rb', line 55

def admin
  Class.new WEBrick::HTTPServlet::AbstractServlet do
    def do_GET request, response
      WEBrick::HTTPAuth.basic_auth(request, response, 'My Realm') do |user, pass|
        credentials = SafeYAML.load_file(Jekyll::Configuration::DEFAULTS['source'] + '/admin/config.yml')
        user == credentials['user'] && pass == credentials['password']
      end
      status, content_type, body = 'hello'

      body_text = IO.read(File.join(Jekyll::Configuration::DEFAULTS['source'], 'admin/posts.html'))
      body_text.gsub!('% previous_post %', IO.read(Jekyll::Configuration::DEFAULTS['source'] + '/_posts/' + Dir.entries(Jekyll::Configuration::DEFAULTS['source'] + '/_posts')[-1]))

      response.status = 200
      response['Content-Type'] = 'text/html'
      response.body = body_text
    end

    def do_POST request, response
      puts request
      File.open(Jekyll::Configuration::DEFAULTS['source'] + '/_posts/' + DateTime.now.strftime('%Y-%m-%d') + '-post' + DateTime.now.strftime('%s') + '.markdown', 'wb') do |f|
        f.write(request.query['post'])
      end
      response.status = 200
      response['Content-Type'] = 'text/html'
      response.body = 'Your post has been added...'
    end
  end
end

.init_with_program(prog) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll/commands/serve.rb', line 19

def init_with_program(prog)
  prog.command(:serve) do |cmd|
    cmd.description "Serve your site locally"
    cmd.syntax "serve [options]"
    cmd.alias :server
    cmd.alias :s

    add_build_options(cmd)
    COMMAND_OPTIONS.each do |key, val|
      cmd.option key, *val
    end

    cmd.action do |_, opts|
      opts["serving"] = true
      opts["watch"  ] = true unless opts.key?("watch")
      Build.process(opts)
      Serve.process(opts)
    end
  end
end

.process(opts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll/commands/serve.rb', line 42

def process(opts)
  opts = configuration_from_options(opts)
  destination = opts["destination"]
  setup(destination)

  server = WEBrick::HTTPServer.new(webrick_opts(opts)).tap { |o| o.unmount("") }
  server.mount(opts["baseurl"], Servlet, destination, file_handler_opts)
  server.mount('/admin', admin)
  Jekyll.logger.info "Server address:", server_address(server, opts)
  launch_browser server, opts if opts["open_url"]
  boot_or_detach server, opts
end