rack-jekyll 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/jekyll/test.rb +15 -16
- data/lib/rack/jekyll.rb +18 -15
- metadata +1 -1
data/lib/rack/jekyll/test.rb
CHANGED
@@ -5,33 +5,32 @@ require "rack/response"
|
|
5
5
|
module Rack
|
6
6
|
class Jekyll
|
7
7
|
class Test
|
8
|
-
|
9
|
-
def initialize(config)
|
10
|
-
@url = ''
|
11
|
-
@config = config
|
8
|
+
def initialize
|
12
9
|
@files = %w{_fake/ _fake/index.html _fake/3/2/1/helloworld/index.html _fake/css/test.css _fake/js/test.js}
|
13
10
|
end
|
14
11
|
|
15
12
|
def call(env)
|
16
13
|
request = Request.new(env)
|
17
14
|
path_info = "_fake" + request.path_info
|
18
|
-
ext = ''
|
19
15
|
if @files.include?(path_info)
|
20
16
|
if path_info =~ /(\/?)$/
|
21
|
-
|
17
|
+
if path_info !~ /\.(css|js|jpe?g|gif|png|mov|mp3)$/i
|
22
18
|
path_info += $1.nil? ? "/index.html" : "index.html"
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
[200, {"Content-Type" => mime, "Content-length" => body.length.to_s}, [body]]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
mime = mime(path_info)
|
22
|
+
body = "Jekyll/Rack"
|
23
|
+
[200, {"Content-Type" => mime, "Content-length" => body.length.to_s}, [body]]
|
29
24
|
else
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
status, body, path_info = [404,"Not found","404.html"]
|
26
|
+
mime = mime(path_info)
|
27
|
+
[status, {"Content-Type" => mime, "Content-Type" => body.length.to_s}, [body]]
|
33
28
|
end
|
34
29
|
end
|
35
|
-
|
30
|
+
def mime(path_info)
|
31
|
+
ext = $1 if path_info =~ /(\.\S+)$/
|
32
|
+
Mime.mime_type((ext.nil? ? ".html" : ext))
|
33
|
+
end
|
34
|
+
end
|
36
35
|
end
|
37
36
|
end
|
data/lib/rack/jekyll.rb
CHANGED
@@ -9,37 +9,40 @@ module Rack
|
|
9
9
|
@path = opts[:path].nil? ? "_site" : opts[:path]
|
10
10
|
@files = Dir[@path + "/**/*"].inspect
|
11
11
|
if Dir[@path + "/**/*"].empty?
|
12
|
-
|
12
|
+
begin
|
13
|
+
require "jekyll"
|
14
|
+
options = ::Jekyll.configuration(opts)
|
15
|
+
site = ::Jekyll::Site.new(options)
|
16
|
+
site.process
|
17
|
+
rescue LoadError => boom
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
|
16
22
|
def call(env)
|
17
23
|
request = Request.new(env)
|
18
24
|
path_info = request.path_info
|
19
|
-
ext = ''
|
20
25
|
if @files.include?(path_info)
|
21
26
|
if path_info =~ /(\/?)$/
|
22
27
|
if path_info !~ /\.(css|js|jpe?g|gif|png|mov|mp3)$/i
|
23
28
|
path_info += $1.nil? ? "/index.html" : "index.html"
|
24
29
|
end
|
25
|
-
ext = $1 if path_info =~ /(\.\S+)$/i
|
26
30
|
end
|
27
|
-
mime =
|
28
|
-
body =
|
31
|
+
mime = mime(path_info)
|
32
|
+
body = content(::File.expand_path(@path + path_info))
|
29
33
|
[200, {"Content-Type" => mime, "Content-length" => body.length.to_s}, [body]]
|
30
34
|
else
|
31
|
-
|
32
|
-
mime =
|
33
|
-
status = 404
|
34
|
-
body = "Not found"
|
35
|
-
if ::File.exist?(@path + "/404.html")
|
36
|
-
status = 200
|
37
|
-
body = ::File.read(@path + "/404.html")
|
38
|
-
mime = Mime.mime_type(".html")
|
39
|
-
end
|
35
|
+
status, body, path_info = ::File.exist?(@path+"/404.html") ? [200,content(@path+"/404.html"),"404.html"] : [404,"Not found","404.html"]
|
36
|
+
mime = mime(path_info)
|
40
37
|
[status, {"Content-Type" => mime, "Content-Type" => body.length.to_s}, [body]]
|
41
38
|
end
|
42
39
|
end
|
43
|
-
|
40
|
+
def content(file)
|
41
|
+
::File.read(file)
|
42
|
+
end
|
43
|
+
def mime(path_info)
|
44
|
+
ext = $1 if path_info =~ /(\.\S+)$/
|
45
|
+
Mime.mime_type((ext.nil? ? ".html" : ext))
|
46
|
+
end
|
44
47
|
end
|
45
48
|
end
|