-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
113 lines (94 loc) · 2.28 KB
/
app.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'bundler/setup'
require 'sinatra'
require 'slim'
require 'bigdecimal/math'
class Debug < BasicObject
def initialize(obj)
@obj = obj
end
def method_missing(*args, &b)
::Kernel.puts args.inspect
@obj.send(*args, &b)
end
end
helpers do
def render(engine, template, options = {}, locals = {}, &block)
# Slim calls the option :streaming, but Sinatra has a #stream method
# so we also support the :stream option.
options[:streaming] ||= options.delete(:stream)
# We are not streaming. Call super implementation
return super unless options[:streaming]
# Engine specific stuff, currently only :slim
case engine
when :slim
# We use the Temple generator without preamble and postamble
# which is suitable for streaming.
options[:generator] = Temple::Generator
else
raise "Streaming is not supported for #{engine}"
end
# There is an output buffer present. We are already streaming, continue!
if @_out_buf
# Check if we are really streaming...
raise 'You are trying to stream from within a unstreamed layout' unless @_out_buf.is_a? Sinatra::Helpers::Stream
return super
end
# Create a new stream
stream do |out|
@_out_buf = Debug.new(out)
if options[:layout] == false
# No layout given, start rendering template
super
else
# Layout given
layout = options[:layout] == nil || options[:layout] == true ? :layout : options[:layout]
# Invert layout and template rendering order
super engine, layout, options.merge(layout: false), locals do
super engine, template, options.merge(layout: false), locals, &block
end
end
end
end
end
get '/' do
slim :pi, stream: true
end
get '/nolayout' do
slim :pi, stream: true, layout: false
end
get '/stream' do
stream do |out|
out << 'Sleep...'
sleep 1
out << 'Awake!'
end
end
get '/simple' do
slim :simple
end
__END__
@@ layout
html
head
title Computing PI...
body
= yield
@@ headline
| Computing PI
@@ ellipsis
- 3.times do
- sleep 0.5
= '.'
@@ pi
h1
== slim :headline
== slim :ellipsis, stream: true
- i = 1
- loop do
= BigMath.PI(i).to_s('F')[i-1]
- i += 1
- sleep 0.2
- if i % 100 == 0
br
@@ simple
| Hello world!