roger 1.7.2 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90bed681641cf5722ab1358fb98eb699beeec2db
4
- data.tar.gz: f96a2a9b467568731dd0e3dcb3d60729bde47ad2
3
+ metadata.gz: 16132ae1857a2fc40e046c297848b5f3683dd249
4
+ data.tar.gz: b82228479d276c41069da98f8bf473a0f5478a85
5
5
  SHA512:
6
- metadata.gz: 86c610fe97e1e7f8a2080676c1d87731750b20faf9294e75205b2638e72da37fa38bf4af536f7c564270b3ca17655263768914e4efd94ba5989102ea1c6ad26d
7
- data.tar.gz: c7234601296a0d70c2b5c1ead72008399e922b1ae2daad1f291741978717d4f0d93ebb6afd397b4e65bf74f5feb7a7381a927839a857f39dc60122b57fa08faf
6
+ metadata.gz: d5f59c20f09f5cbab909ceaa865c1f6b1d6bdac61c863bf9e3e3df6c549c4d0effd212d379a9e215d09a41d32e5e83f8269ece9f423194b6e95f83c0db57a81c
7
+ data.tar.gz: 54ddecc3563773870ee17ae3c0e712e180f0f6b4969a83ff8fa5afd17055e7fd00fb1598f2723317a58291cc1a09ab3b47cde23947d7e797d3e13a479f886769
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.8.0
4
+ * Roger Rack application now adheres to match/skip options. Pass them in the rogerfile as follows:
5
+ ```
6
+ roger.server do |s|
7
+ s.application_options = { match: [], skip: [] }
8
+ end
9
+ ```
10
+
3
11
  ## Version 1.7.2
4
12
  * Return from partials instead of rendering partial content as string. This allows us to have code in ruby blocks instead of just more ERB.
5
13
 
@@ -42,7 +50,7 @@
42
50
 
43
51
  ## Version 1.5.0
44
52
  * Roger won't be tested on Ruby 1.9.x anymore
45
- * The way we render templates has been revamped. It is now possible to have multi-pass templates so you can do `.md.erb` which will first be processed by `erb` and then by the `md` tilt template handler.
53
+ * The way we render templates has been revamped. It is now possible to have multi-pass templates so you can do `.md.erb` which will first be processed by `erb` and then by the `md` tilt template handler.
46
54
  * The way templates are searched has changed to be more predictable. You may notice the change when you have the same filenames with different extensions.
47
55
  * You can now use local partials by just using an underscore as prefix. So `<%= partial('bla') %>` will look for `_bla.*` relative to the current template first and `bla.*` in the partials directory second (current behaviour).
48
56
  * Template recursion will be detected and prevented instead of giving a stack overflow.
@@ -102,11 +110,11 @@ This is a dud. It's identical to 1.3.1. Don't use.
102
110
  * Fix missing variables in release
103
111
 
104
112
  ## Version 1.2.1
105
- * Fix missing env (https://github.com/DigitPaint/roger/issues/24)
113
+ * Fix missing env (https://github.com/DigitPaint/roger/issues/24)
106
114
 
107
115
  ## Version 1.2.0
108
116
  * `roger.project` is now always in `env` when running as a project or as a release. Use this in favor of `env["MOCKUP_PROJECT"]`
109
- * All Roger code now is linted by Rubocop. More tests have been added and more documentation as well. Still not everything may have been covered by the tests.
117
+ * All Roger code now is linted by Rubocop. More tests have been added and more documentation as well. Still not everything may have been covered by the tests.
110
118
 
111
119
 
112
120
  ## Version 1.1.3
@@ -21,6 +21,8 @@ Sass::Plugin.options[:css_location] = "./html/stylesheets"
21
21
  # Server is a regular Rack interface.
22
22
  roger.serve do |server|
23
23
  server.use :sass
24
+
25
+ server.application_options = {match: ["**/*.html.erb"]} # Config for Rack application (call is optional)
24
26
  end
25
27
 
26
28
  # Define tests
@@ -2,20 +2,36 @@ module Roger
2
2
  module Helpers
3
3
  # Helper to include the get_files method
4
4
  module GetFiles
5
+ GLOB_OPTIONS = File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
6
+
5
7
  # Get files from a path, skipping excludes.
6
8
  #
7
9
  # @param [Array] globs an array of file path globs that will be globbed
8
10
  # against the project path
9
11
  # @param [Array] excludes an array of regexps[!] that will be excluded
10
12
  # from the result.
11
- # @param [String, Pathname] Path to search files in
12
- def get_files(globs, excludes = [], path = nil)
13
+ def get_files(globs, excludes = [])
13
14
  path = Pathname.new(get_files_default_path)
14
- files = globs.map { |g| Dir.glob(path + g) }.flatten
15
+ files = globs.map { |g| Dir.glob(path + g, GLOB_OPTIONS) }.flatten
15
16
  files.reject! { |file| excludes.detect { |e| file.match(e) } } if excludes.any?
16
17
  files.select { |file| File.file?(file) }
17
18
  end
18
19
 
20
+ # See if a file matches globs/excludes
21
+ #
22
+ # @param [.to_s] path the path to match
23
+ # @param [Array] globs an array of file path globs that will be matched against path
24
+ # @param [Array] exclude an array of regexps[!] that will be matched negatively against path
25
+ #
26
+ # @return [Boolean] Did the passed path match against the globs and excludes?
27
+ def match_path(path, globs, excludes = [])
28
+ path = path.to_s
29
+ match = globs.detect { |glob| File.fnmatch?(glob, path, GLOB_OPTIONS) }
30
+ return false unless match # No need to check excludes if we don't match anyway
31
+
32
+ !excludes.find { |e| path.match(e) }
33
+ end
34
+
19
35
  protected
20
36
 
21
37
  # The default path to use when calling get_files
@@ -9,10 +9,24 @@ module Roger
9
9
  module Rack
10
10
  # Roger middleware that processe roger templates
11
11
  class Roger
12
- attr_reader :project
12
+ include ::Roger::Helpers::GetFiles
13
+ attr_reader :project, :options
13
14
 
14
- def initialize(project)
15
+ def default_options
16
+ {
17
+ match: ["**/*.{html,md,html.erb}"],
18
+ skip: []
19
+ }
20
+ end
21
+
22
+ # Initialize the Rack app
23
+ #
24
+ # @param [Hash] options Options to use
25
+ #
26
+ # @option options
27
+ def initialize(project, options = {})
15
28
  @project = project
29
+ @options = default_options.update(options)
16
30
  @docroot = project.html_path
17
31
 
18
32
  @resolver = Resolver.new(@docroot)
@@ -23,7 +37,8 @@ module Roger
23
37
  url = env["PATH_INFO"]
24
38
  env["MOCKUP_PROJECT"] = env["roger.project"] || @project
25
39
 
26
- if template_path = @resolver.url_to_path(url)
40
+ template_path = @resolver.url_to_path(url)
41
+ if process_path_by_roger?(template_path)
27
42
  env["rack.errors"].puts "Rendering template #{template_path.inspect} (#{url.inspect})"
28
43
  build_response(template_path, env).finish
29
44
  else
@@ -34,6 +49,16 @@ module Roger
34
49
 
35
50
  protected
36
51
 
52
+ # Wether or not we should process this
53
+ # path by the Roger renderer.
54
+ def process_path_by_roger?(path)
55
+ return false unless path
56
+
57
+ return false unless match_path(path, @options[:match], @options[:skip])
58
+
59
+ ::Roger::Renderer.will_render?(path)
60
+ end
61
+
37
62
  def build_response(template_path, env)
38
63
  renderer = ::Roger::Renderer.new(
39
64
  env,
@@ -43,6 +68,7 @@ module Roger
43
68
  mime = ::Rack::Mime.mime_type(File.extname(template_path), "text/html")
44
69
  ::Rack::Response.new do |res|
45
70
  res.headers["Content-Type"] = mime if mime
71
+ res.headers["X-Handled-By"] = "Roger"
46
72
  res.status = 200
47
73
  res.write renderer.render(template_path, @project.options[:renderer] || {})
48
74
  end
@@ -22,6 +22,11 @@ module Roger
22
22
  @helpers || []
23
23
  end
24
24
 
25
+ # Will the renderer render this path to something meaningful?
26
+ def will_render?(path)
27
+ Tilt.templates_for(path.to_s).any?
28
+ end
29
+
25
30
  # Try to infer the final extension of the output file.
26
31
  def target_extension_for(path)
27
32
  if type = MIME::Types[target_mime_type_for(path)].first
@@ -28,6 +28,11 @@ module Roger
28
28
 
29
29
  attr_accessor :port, :handler, :host, :auto_port
30
30
 
31
+ # @attr_accessor [Hash] Options pass to ::Roger::Rack::Roger initialization
32
+ #
33
+ # @see ::Roger::Rack:Roger
34
+ attr_accessor :application_options
35
+
31
36
  def initialize(project, options = {})
32
37
  @project = project
33
38
 
@@ -143,7 +148,7 @@ module Roger
143
148
  def application
144
149
  return @app if @app
145
150
 
146
- @stack.run Rack::Roger.new(project)
151
+ @stack.run Rack::Roger.new(project, @application_options || {})
147
152
 
148
153
  @app = @stack
149
154
  end
@@ -1,4 +1,4 @@
1
1
  # Roger main namespace
2
2
  module Roger
3
- VERSION = "1.7.2".freeze
3
+ VERSION = "1.8.0".freeze
4
4
  end
@@ -0,0 +1 @@
1
+ <% yield.each do |value| %><%= value %><% end %>
@@ -34,6 +34,7 @@ module Roger
34
34
  files = [
35
35
  "a.js",
36
36
  "1.js",
37
+ "2.js",
37
38
  "1.html",
38
39
  "dir/2.js",
39
40
  "dir/3.js",
@@ -55,6 +56,12 @@ module Roger
55
56
  assert_array_contains(expect, files)
56
57
  end
57
58
 
59
+ def test_glob_extended
60
+ files = @object.get_files(["**/{1,2}.js"])
61
+ expect = @files.grep(/[12]\.js\Z/)
62
+ assert_array_contains(expect, files)
63
+ end
64
+
58
65
  def test_get_only_files
59
66
  dir = @object.construct.directory "evil.js"
60
67
  files = @object.get_files(["*.js"])
@@ -67,6 +74,34 @@ module Roger
67
74
  assert_array_contains(expect, files)
68
75
  end
69
76
 
77
+ def test_match_path_with_glob_in_root
78
+ path = "file.js"
79
+ assert @object.match_path(path, ["*"])
80
+ assert @object.match_path(path, ["**/*"])
81
+ assert @object.match_path(path, ["**/*.js"])
82
+ end
83
+
84
+ def test_match_path_with_glob_in_subdir
85
+ path = "dir/subdir/4.js"
86
+ assert !@object.match_path(path, [])
87
+ assert !@object.match_path(path, ["*"])
88
+ assert @object.match_path(path, ["**/*"])
89
+ assert @object.match_path(path, ["**/*.js"])
90
+ assert !@object.match_path(path, ["**/*.html"])
91
+ assert @object.match_path(path, ["**/*.html", "**/*.js"])
92
+ end
93
+
94
+ def test_match_path_with_extended_glob
95
+ assert @object.match_path("4.js", ["{3,4}.js"])
96
+ assert !@object.match_path("4.js", ["{5}.js"])
97
+ end
98
+
99
+ def test_match_path_with_excludes
100
+ path = "dir/subdir/4.js"
101
+ assert @object.match_path(path, ["**/*"], [/\.html\Z/])
102
+ assert !@object.match_path(path, ["**/*"], [/\.js\Z/])
103
+ end
104
+
70
105
  protected
71
106
 
72
107
  def assert_array_contains(expect, result)
@@ -19,6 +19,25 @@ module Roger
19
19
  response = request.get("/erb")
20
20
 
21
21
  assert response.body.include?("ERB format")
22
+ assert_equal "Roger", response.headers["X-Handled-By"]
23
+ end
24
+
25
+ def test_middleware_does_not_render_unrenderables
26
+ @project.construct.file "html/myjpeg.jpg", "JPG"
27
+ request = ::Rack::MockRequest.new(@app)
28
+ response = request.get("/myjpeg.jpg")
29
+
30
+ assert response.body.include?("JPG")
31
+ assert_equal nil, response.headers["X-Handled-By"]
32
+ end
33
+
34
+ def test_middleware_does_not_render_unwanteds
35
+ @project.construct.file "html/mysass.scss", ".scss{}"
36
+ request = ::Rack::MockRequest.new(@app)
37
+ response = request.get("/mysass.scss")
38
+
39
+ assert response.body.include?(".scss{}")
40
+ assert_equal nil, response.headers["X-Handled-By"]
22
41
  end
23
42
 
24
43
  def test_renderer_options_are_passed
@@ -59,5 +59,26 @@ module Roger
59
59
  # This is a bit of a clunky comparison but it suffices for now
60
60
  assert_equal @project.object_id.to_s, request.get("/").body
61
61
  end
62
+
63
+ def test_application_options
64
+ @server.application_options = { test_option: true }
65
+
66
+ test = Class.new do
67
+ def initialize(app)
68
+ @app = app
69
+ end
70
+
71
+ def call(_env)
72
+ [200, {}, [@app.options[:test_option].to_s]]
73
+ end
74
+ end
75
+
76
+ @server.use test
77
+
78
+ request = ::Rack::MockRequest.new(@server.send(:application))
79
+
80
+ # This is a bit of a clunky comparison but it suffices for now
81
+ assert_equal "true", request.get("/").body
82
+ end
62
83
  end
63
84
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flurin Egger
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-08-09 00:00:00.000000000 Z
13
+ date: 2017-10-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: thor
@@ -302,6 +302,7 @@ files:
302
302
  - test/project/partials/formats/erb.html.erb
303
303
  - test/project/partials/partials-test.html.erb
304
304
  - test/project/partials/test/_underscored.html.erb
305
+ - test/project/partials/test/array.html.erb
305
306
  - test/project/partials/test/deep_recursive.html.erb
306
307
  - test/project/partials/test/erb.html.erb
307
308
  - test/project/partials/test/front_matter.html.erb
@@ -418,6 +419,7 @@ test_files:
418
419
  - test/project/partials/formats/erb.html.erb
419
420
  - test/project/partials/partials-test.html.erb
420
421
  - test/project/partials/test/_underscored.html.erb
422
+ - test/project/partials/test/array.html.erb
421
423
  - test/project/partials/test/deep_recursive.html.erb
422
424
  - test/project/partials/test/erb.html.erb
423
425
  - test/project/partials/test/front_matter.html.erb