-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp_servlet_handler.rb
43 lines (35 loc) · 1.08 KB
/
http_servlet_handler.rb
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
require 'open3'
class JadeHandler
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] =~ /\/$/
env["PATH_INFO"] += "index.jade"
end
if env["PATH_INFO"] =~ /\.jade$/
path = env["PATH_INFO"][1..-1]
base = File.basename(path)
dir = File.dirname(path) + "/src"
body = nil
Dir.chdir(dir) do
if File.exists?('c:/users/etblue/appdata/roaming/npm/node_modules/jade/bin/jade')
jade_cmd = 'node c:/users/etblue/appdata/roaming/npm/node_modules/jade/bin/jade --path . -O "{require: require}" -P '
else
jade_cmd = 'jade --path . -O "{require: require}" -P'
end
body = Open3.popen3(jade_cmd ) do |stdin, stdout, stderr|
template = open(base,'r'){|f| f.read}
stdin.write template
stdin.close
stdout.read + stderr.read.gsub(/\n/, '<br>')
end
end
[200, {"Content-Type" => "text/html"}, [body]]
else
status, headers, body = @app.call(env)
[status, headers, body]
end
end
end
use JadeHandler