serve 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +11 -0
- data/Gemfile +17 -14
- data/Gemfile.lock +53 -26
- data/LICENSE +1 -1
- data/QUICKSTART.rdoc +137 -27
- data/README.rdoc +115 -23
- data/VERSION +1 -1
- data/bin/serve +3 -3
- data/lib/serve.rb +2 -5
- data/lib/serve/application.rb +7 -6
- data/lib/serve/handlers/dynamic_handler.rb +40 -42
- data/lib/serve/handlers/less_handler.rb +19 -0
- data/lib/serve/handlers/sass_handler.rb +5 -0
- data/lib/serve/out.rb +19 -1
- data/lib/serve/project.rb +2 -2
- data/lib/serve/rack.rb +58 -18
- data/lib/serve/router.rb +43 -0
- data/lib/serve/templates/config.ru +14 -10
- data/lib/serve/view_helpers.rb +36 -24
- data/spec/fixtures/directory/index.html +1 -0
- data/spec/fixtures/hello.html +1 -0
- data/spec/fixtures/stylesheets/application.sass +3 -0
- data/spec/router_spec.rb +60 -0
- data/spec/spec_helper.rb +0 -1
- metadata +50 -39
- data/lib/serve/file_resolver.rb +0 -48
- data/lib/serve/handlers/markdown_handler.rb +0 -10
- data/lib/serve/handlers/static_handler.rb +0 -22
- data/lib/serve/handlers/textile_handler.rb +0 -10
- data/lib/serve/rails.rb +0 -4
- data/lib/serve/rails/configuration.rb +0 -69
- data/lib/serve/rails/mount.rb +0 -38
- data/lib/serve/rails/routing.rb +0 -25
- data/lib/serve/rails/serve_controller.rb +0 -52
- data/lib/serve/response_cache.rb +0 -170
- data/spec/response_cache_spec.rb +0 -248
@@ -6,28 +6,32 @@ gem 'serve'
|
|
6
6
|
require 'serve'
|
7
7
|
require 'serve/rack'
|
8
8
|
|
9
|
-
require 'sass/plugin/rack'
|
10
|
-
require 'compass'
|
11
|
-
|
12
9
|
# The project root directory
|
13
10
|
root = ::File.dirname(__FILE__)
|
14
11
|
|
15
|
-
#
|
16
|
-
|
17
|
-
|
12
|
+
# Compile Sass on the fly with the Sass plugin. Some production environments
|
13
|
+
# don't allow you to write to the file system on the fly (like Heroku).
|
14
|
+
# Remove this conditional if you want to compile Sass in production.
|
15
|
+
if ENV['RACK_ENV'] != 'production'
|
16
|
+
require 'sass/plugin/rack'
|
17
|
+
require 'compass'
|
18
|
+
|
19
|
+
Compass.add_project_configuration(root + '/compass.config')
|
20
|
+
Compass.configure_sass_plugin!
|
21
|
+
|
22
|
+
use Sass::Plugin::Rack # Sass Middleware
|
23
|
+
end
|
18
24
|
|
19
|
-
# Rack Middleware
|
25
|
+
# Other Rack Middleware
|
20
26
|
use Rack::ShowStatus # Nice looking 404s and other messages
|
21
27
|
use Rack::ShowExceptions # Nice looking errors
|
22
|
-
use Sass::Plugin::Rack # Compile Sass on the fly
|
23
28
|
|
24
29
|
# Rack Application
|
25
30
|
if ENV['SERVER_SOFTWARE'] =~ /passenger/i
|
26
31
|
# Passendger only needs the adapter
|
27
32
|
run Serve::RackAdapter.new(root + '/views')
|
28
33
|
else
|
29
|
-
#
|
30
|
-
# assets
|
34
|
+
# Use Rack::Cascade and Rack::Directory on other platforms for static assets
|
31
35
|
run Rack::Cascade.new([
|
32
36
|
Serve::RackAdapter.new(root + '/views'),
|
33
37
|
Rack::Directory.new(root + '/public')
|
data/lib/serve/view_helpers.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Serve #:nodoc:
|
2
|
-
# Many of the methods here have been extracted from Rails
|
2
|
+
# Many of the methods here have been extracted in some form from Rails
|
3
3
|
|
4
4
|
module EscapeHelpers
|
5
5
|
HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
|
@@ -35,26 +35,8 @@ module Serve #:nodoc:
|
|
35
35
|
end
|
36
36
|
|
37
37
|
module ContentHelpers
|
38
|
-
def capture_erb(&block)
|
39
|
-
buffer = _erbout
|
40
|
-
pos = buffer.length
|
41
|
-
block.call
|
42
|
-
|
43
|
-
# extract the block
|
44
|
-
data = buffer[pos..-1]
|
45
|
-
|
46
|
-
# replace it in the original with empty string
|
47
|
-
buffer[pos..-1] = ''
|
48
|
-
|
49
|
-
data
|
50
|
-
end
|
51
|
-
|
52
38
|
def content_for(symbol, &block)
|
53
|
-
|
54
|
-
set_content_for(symbol, capture_haml(&block))
|
55
|
-
else
|
56
|
-
set_content_for(symbol, capture_erb(&block))
|
57
|
-
end
|
39
|
+
set_content_for(symbol, capture(&block))
|
58
40
|
end
|
59
41
|
|
60
42
|
def content_for?(symbol)
|
@@ -72,6 +54,36 @@ module Serve #:nodoc:
|
|
72
54
|
def set_content_for(symbol, value)
|
73
55
|
instance_variable_set("@content_for_#{symbol}", value)
|
74
56
|
end
|
57
|
+
|
58
|
+
def capture_erb(&block)
|
59
|
+
buffer = ""
|
60
|
+
old_buffer, @_out_buf = @_out_buf, buffer
|
61
|
+
yield
|
62
|
+
buffer
|
63
|
+
ensure
|
64
|
+
@_out_buf = old_buffer
|
65
|
+
end
|
66
|
+
alias capture_rhtml capture_erb
|
67
|
+
alias capture_erubis capture_erb
|
68
|
+
|
69
|
+
def capture(&block)
|
70
|
+
capture_method = "capture_#{script_extension}"
|
71
|
+
if respond_to? capture_method
|
72
|
+
send capture_method, &block
|
73
|
+
else
|
74
|
+
raise "Capture not supported for `#{script_extension}' template (#{engine_name})"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def engine_name
|
81
|
+
Tilt[script_extension].to_s
|
82
|
+
end
|
83
|
+
|
84
|
+
def script_extension
|
85
|
+
parser.script_extension
|
86
|
+
end
|
75
87
|
end
|
76
88
|
|
77
89
|
module FlashHelpers
|
@@ -110,7 +122,7 @@ module Serve #:nodoc:
|
|
110
122
|
template = options.delete(:template)
|
111
123
|
case
|
112
124
|
when partial
|
113
|
-
render_partial(partial)
|
125
|
+
render_partial(partial, options)
|
114
126
|
when template
|
115
127
|
render_template(template)
|
116
128
|
else
|
@@ -118,8 +130,8 @@ module Serve #:nodoc:
|
|
118
130
|
end
|
119
131
|
end
|
120
132
|
|
121
|
-
def render_partial(partial)
|
122
|
-
render_template(partial, :partial => true)
|
133
|
+
def render_partial(partial, options={})
|
134
|
+
render_template(partial, options.merge(:partial => true))
|
123
135
|
end
|
124
136
|
|
125
137
|
def render_template(template, options={})
|
@@ -130,7 +142,7 @@ module Serve #:nodoc:
|
|
130
142
|
end
|
131
143
|
filename = template_filename(File.join(path, template), :partial => options.delete(:partial))
|
132
144
|
if File.file?(filename)
|
133
|
-
parser.parse_file(filename)
|
145
|
+
parser.parse_file(filename, options[:locals])
|
134
146
|
else
|
135
147
|
raise "File does not exist #{filename.inspect}"
|
136
148
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<html><body>Directory Index</body></html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<html><body>Hello World</body></html>
|
data/spec/router_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
require 'serve/router'
|
3
|
+
|
4
|
+
describe Serve::Router do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@root = File.dirname(__FILE__) + '/fixtures'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should not resolve bad file names' do
|
11
|
+
resolve('404').should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should resolve file names' do
|
15
|
+
resolve('hello.html').should == 'hello.html'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should resolve filenames without extensions' do
|
19
|
+
resolve('hello').should == 'hello.html'
|
20
|
+
resolve('hello/').should == 'hello.html'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should resolve directory indexes' do
|
24
|
+
resolve('directory').should == 'directory/index.html'
|
25
|
+
resolve('directory/').should == 'directory/index.html'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should resolve references to css files to sass if css does not exist' do
|
29
|
+
resolve('stylesheets/application.css').should == 'stylesheets/application.sass'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should not resolve paths that attempt to climb up the directory tree' do
|
33
|
+
resolve('../CHANGELOG.rdoc').should be_nil
|
34
|
+
resolve('directory/../hello.html').should be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should resolve files with various extensions' do
|
38
|
+
@root = File.dirname(__FILE__) + '/../tmp/'
|
39
|
+
random_extension = %w(a b c d e f g).shuffle[1..4].join
|
40
|
+
path = "test"
|
41
|
+
full_path = path + "." + random_extension
|
42
|
+
FileUtils.touch(@root + full_path)
|
43
|
+
resolve(path).should == full_path
|
44
|
+
resolve(path + "/").should == full_path
|
45
|
+
FileUtils.rm(@root + full_path)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should resolve files without case sensitivity' do
|
49
|
+
resolve('HELLO').should == 'hello.html'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should resolve directories without case sensitivity' do
|
53
|
+
resolve('DIRECTORY').should == 'directory/index.html'
|
54
|
+
end
|
55
|
+
|
56
|
+
def resolve(name)
|
57
|
+
Serve::Router.resolve(@root, name)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serve
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John W. Long
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date:
|
20
|
+
date: 2011-05-25 00:00:00 -04:00
|
21
21
|
default_executable: serve
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
@@ -28,78 +28,94 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ~>
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
hash:
|
31
|
+
hash: 27
|
32
32
|
segments:
|
33
33
|
- 1
|
34
|
-
-
|
35
|
-
-
|
36
|
-
version: 1.
|
34
|
+
- 3
|
35
|
+
- 0
|
36
|
+
version: 1.3.0
|
37
37
|
type: :runtime
|
38
38
|
version_requirements: *id001
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
|
-
name:
|
40
|
+
name: tilt
|
41
41
|
prerelease: false
|
42
42
|
requirement: &id002 !ruby/object:Gem::Requirement
|
43
43
|
none: false
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
hash:
|
47
|
+
hash: 25
|
48
48
|
segments:
|
49
|
+
- 1
|
49
50
|
- 3
|
50
|
-
- 0
|
51
51
|
- 1
|
52
|
-
version: 3.
|
52
|
+
version: 1.3.1
|
53
53
|
type: :runtime
|
54
54
|
version_requirements: *id002
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: activesupport
|
57
57
|
prerelease: false
|
58
58
|
requirement: &id003 !ruby/object:Gem::Requirement
|
59
59
|
none: false
|
60
60
|
requirements:
|
61
61
|
- - ~>
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
hash:
|
63
|
+
hash: 9
|
64
64
|
segments:
|
65
|
-
- 0
|
66
65
|
- 3
|
67
|
-
-
|
68
|
-
|
66
|
+
- 0
|
67
|
+
- 7
|
68
|
+
version: 3.0.7
|
69
69
|
type: :runtime
|
70
70
|
version_requirements: *id003
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
|
-
name:
|
72
|
+
name: tzinfo
|
73
73
|
prerelease: false
|
74
74
|
requirement: &id004 !ruby/object:Gem::Requirement
|
75
75
|
none: false
|
76
76
|
requirements:
|
77
77
|
- - ~>
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
hash:
|
79
|
+
hash: 37
|
80
80
|
segments:
|
81
81
|
- 0
|
82
|
-
-
|
83
|
-
-
|
84
|
-
version: 0.
|
82
|
+
- 3
|
83
|
+
- 27
|
84
|
+
version: 0.3.27
|
85
85
|
type: :runtime
|
86
86
|
version_requirements: *id004
|
87
87
|
- !ruby/object:Gem::Dependency
|
88
|
-
name:
|
88
|
+
name: i18n
|
89
89
|
prerelease: false
|
90
90
|
requirement: &id005 !ruby/object:Gem::Requirement
|
91
91
|
none: false
|
92
92
|
requirements:
|
93
93
|
- - ~>
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
hash:
|
95
|
+
hash: 7
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
- 6
|
99
|
+
- 0
|
100
|
+
version: 0.6.0
|
101
|
+
type: :runtime
|
102
|
+
version_requirements: *id005
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rspec
|
105
|
+
prerelease: false
|
106
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ~>
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 23
|
96
112
|
segments:
|
97
113
|
- 2
|
114
|
+
- 6
|
98
115
|
- 0
|
99
|
-
|
100
|
-
version: 2.0.1
|
116
|
+
version: 2.6.0
|
101
117
|
type: :development
|
102
|
-
version_requirements: *
|
118
|
+
version_requirements: *id006
|
103
119
|
description: Serve is a small Rack-based web server that makes it easy to serve ERB or HAML from any directory. Serve is an ideal tool for building HTML prototypes of Rails applications. Serve can also handle SASS, Textile, and Markdown if the appropriate gems are installed.
|
104
120
|
email: me@johnwlong.com
|
105
121
|
executables:
|
@@ -121,25 +137,17 @@ files:
|
|
121
137
|
- bin/serve
|
122
138
|
- lib/serve.rb
|
123
139
|
- lib/serve/application.rb
|
124
|
-
- lib/serve/file_resolver.rb
|
125
140
|
- lib/serve/handlers/dynamic_handler.rb
|
126
141
|
- lib/serve/handlers/email_handler.rb
|
127
142
|
- lib/serve/handlers/file_type_handler.rb
|
128
|
-
- lib/serve/handlers/
|
143
|
+
- lib/serve/handlers/less_handler.rb
|
129
144
|
- lib/serve/handlers/redirect_handler.rb
|
130
145
|
- lib/serve/handlers/sass_handler.rb
|
131
|
-
- lib/serve/handlers/static_handler.rb
|
132
|
-
- lib/serve/handlers/textile_handler.rb
|
133
146
|
- lib/serve/javascripts.rb
|
134
147
|
- lib/serve/out.rb
|
135
148
|
- lib/serve/project.rb
|
136
149
|
- lib/serve/rack.rb
|
137
|
-
- lib/serve/
|
138
|
-
- lib/serve/rails/configuration.rb
|
139
|
-
- lib/serve/rails/mount.rb
|
140
|
-
- lib/serve/rails/routing.rb
|
141
|
-
- lib/serve/rails/serve_controller.rb
|
142
|
-
- lib/serve/response_cache.rb
|
150
|
+
- lib/serve/router.rb
|
143
151
|
- lib/serve/templates/LICENSE
|
144
152
|
- lib/serve/templates/README.markdown
|
145
153
|
- lib/serve/templates/_layout.html.erb
|
@@ -154,8 +162,11 @@ files:
|
|
154
162
|
- lib/serve/view_helpers.rb
|
155
163
|
- rails/init.rb
|
156
164
|
- spec/application_spec.rb
|
165
|
+
- spec/fixtures/directory/index.html
|
166
|
+
- spec/fixtures/hello.html
|
167
|
+
- spec/fixtures/stylesheets/application.sass
|
157
168
|
- spec/project_spec.rb
|
158
|
-
- spec/
|
169
|
+
- spec/router_spec.rb
|
159
170
|
- spec/spec_helper.rb
|
160
171
|
has_rdoc: true
|
161
172
|
homepage: http://github.com/jlong/serve
|
@@ -194,5 +205,5 @@ summary: Serve is a small web server that makes it easy to serve ERB or HAML fro
|
|
194
205
|
test_files:
|
195
206
|
- spec/application_spec.rb
|
196
207
|
- spec/project_spec.rb
|
197
|
-
- spec/
|
208
|
+
- spec/router_spec.rb
|
198
209
|
- spec/spec_helper.rb
|
data/lib/serve/file_resolver.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
module Serve
|
2
|
-
mattr_accessor :file_resolver
|
3
|
-
|
4
|
-
class FileResolver
|
5
|
-
cattr_accessor :alternate_extensions
|
6
|
-
self.alternate_extensions = %w(htm html txt text xml atom rss rdf haml erb rhtml html.erb html.haml textile markdown email redirect)
|
7
|
-
|
8
|
-
def resolve(root, path)
|
9
|
-
return nil if path.nil?
|
10
|
-
path = File.join(path) # path may be array
|
11
|
-
return nil if path =~ /\.\./
|
12
|
-
path = path.sub(%r{/\Z}, '')
|
13
|
-
if path =~ /\.css\Z/ && !File.file?(File.join(root, path)) # if .css not found, try .scss, .sass:
|
14
|
-
alternates = %w{.scss .sass}.map { |ext| path.sub(/\.css\Z/, ext) }
|
15
|
-
sass_path = alternates.find do |p|
|
16
|
-
File.file?(File.join(root, p))
|
17
|
-
end
|
18
|
-
elsif File.directory?(File.join(root, path))
|
19
|
-
resolve(root, File.join(path, 'index'))
|
20
|
-
else
|
21
|
-
resolve_with_extension(root, path)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def resolve_with_extension(root, path)
|
26
|
-
full_path = File.join(root, path)
|
27
|
-
if File.file?(full_path)
|
28
|
-
path
|
29
|
-
else
|
30
|
-
if extension = find_extension(alternate_extensions, full_path)
|
31
|
-
"#{path}.#{extension}"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def find_extension(extensions_to_try, full_path)
|
37
|
-
extensions_to_try.find do |ext|
|
38
|
-
File.file?("#{full_path}.#{ext}")
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
self.file_resolver = FileResolver.new
|
44
|
-
|
45
|
-
def self.resolve_file(root, path)
|
46
|
-
file_resolver.resolve(root, path)
|
47
|
-
end
|
48
|
-
end
|