forked from jruby/jruby.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
79 lines (66 loc) · 1.87 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
# coding: utf-8
# -*- ruby -*-
#
# This rackup file runs the equivalent of jekyll --server --auto, but
# uses Rack to add a couple of rewrite rules to make it equivalent to the
# Apache configuration.
require 'rack'
require 'rack/file'
### This section copied from bin/jekyll file
require 'jekyll'
options = Jekyll.configuration({})
source = options['source']
destination = options['destination']
# Files to watch
def globs(source)
Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) }
dirs -= ['_site']
dirs = dirs.map { |x| "#{x}/**/*" }
dirs += ['*']
end
end
# Create the Site
site = Jekyll::Site.new(options)
# Watch for updates
require 'directory_watcher'
puts "Auto-regenerating enabled: #{source} -> #{destination}"
dw = DirectoryWatcher.new(source)
dw.interval = 1
dw.glob = globs(source)
dw.add_observer do |*args|
t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
puts "[#{t}] regeneration: #{args.size} files changed"
site.process
end
dw.start
### Rackup server stuff starts here
# Emulate the following Apache config:
# * / => /index.html
# * RewriteRule ([^.]+)$ $1.html
# * ErrorDocument 404 /404.html
class DotHtmlRewriter
def initialize(app, fourohfour)
@app = app
@fourohfour = fourohfour
end
def call(env)
env["PATH_INFO"] += "index.html" if env["PATH_INFO"] =~ /\/$/
result = @app.call(env)
if result[0] == 404 && env["PATH_INFO"] !~ /\.html$/
env["PATH_INFO"] += ".html"
second_result = @app.call(env)
if second_result[0] != 404
result = second_result
else
result[2] = [File.read(@fourohfour)]
result[1]["Content-Type"] = "text/html"
result[1]["Content-Length"] = result[2][0].length.to_s
end
end
result
end
end
puts "Server running on http://localhost:9292"
use DotHtmlRewriter, "#{destination}/404.html"
run Rack::File.new(destination)