bridgetown-routes 1.0.0.alpha2 → 1.0.0.alpha6

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
  SHA256:
3
- metadata.gz: 7d99af4d6cf66c15ad4ca1da7044ca24142f45dbf6035696573dbb8e5033f8c7
4
- data.tar.gz: 8418e77c8b4bd9263468149ba6d231d5a51b6922ffe299291374b8abb8e2ba80
3
+ metadata.gz: 862c93a20ddca076132f99fece6aad13aa7832540251160f1b701675a0751b48
4
+ data.tar.gz: a25355e13e76aeff83c6566329d3e27ceb6cd7b5b8ea0380a9b1671deb1b1dac
5
5
  SHA512:
6
- metadata.gz: 5f0528d922fbf73a90edf3f62fd584f8fba34853337228b2fa16dcb3e2f31a927d0c6e30ebe549a4762aa56f9649812c03d4c9be5b5f9e1597fac4e00d8c25ff
7
- data.tar.gz: b17f234af39b21ec58721c842eb6f4bbd6294c579f0c4a9cf0884ad0a891aaf873e33b7a01d34efef9abd7d496281dd9ab42a5454ce6648320046ef555e2555d
6
+ metadata.gz: 5f33bf8a6cf93ae662dcc0114467d107a01ccce02fccbf2fec98ef05ec02774d9db91b91c7f0a1c584a8e7879f6267cadf92b61fac5be06d53356778dc3d37bb
7
+ data.tar.gz: 659e1198862118b090f41618e2e326dc9a633bf3860a64f35c11946bd42764cb368c29453b04c86494bf49faf23180e30e6e9fd8d262e075eb5e5fd9b94a7b69
@@ -21,6 +21,29 @@ module Bridgetown
21
21
  def route_block(name)
22
22
  blocks[name] if route_defined?(name)
23
23
  end
24
+
25
+ def eval_route_file(file, file_slug, app) # rubocop:disable Lint/UnusedMethodArgument
26
+ if Bridgetown.env.production? && Bridgetown::Routes::CodeBlocks.route_defined?(file_slug)
27
+ # we don't need to re-eval the file in production because it won't be changing underfoot
28
+ return
29
+ end
30
+
31
+ code = File.read(file)
32
+ code_postmatch = nil
33
+ ruby_content = code.match(Bridgetown::FrontMatterImporter::RUBY_BLOCK)
34
+ if ruby_content
35
+ code = ruby_content[1]
36
+ code_postmatch = ruby_content.post_match
37
+ end
38
+
39
+ code = <<~RUBY
40
+ r = app.request
41
+ add_route(#{file_slug.inspect}, #{code_postmatch.inspect}) do
42
+ #{code}
43
+ end
44
+ RUBY
45
+ instance_eval(code, file, -1)
46
+ end
24
47
  end
25
48
  end
26
49
  end
@@ -4,6 +4,7 @@ module Bridgetown
4
4
  module Routes
5
5
  module Manifest
6
6
  class << self
7
+ # TODO: make this extensible
7
8
  def routable_extensions
8
9
  %w(rb md serb erb liquid)
9
10
  end
@@ -11,7 +12,7 @@ module Bridgetown
11
12
  def generate_manifest # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
12
13
  return @route_manifest if @route_manifest && Bridgetown.env.production?
13
14
 
14
- routes_dir = File.expand_path("src/_routes", Dir.pwd)
15
+ routes_dir = Bridgetown::Current.site.in_source_dir("_routes")
15
16
  # @type [Array]
16
17
  routes = Dir.glob(routes_dir + "/**/*.{#{routable_extensions.join(",")}}").map do |file|
17
18
  if File.basename(file).start_with?("_", ".") ||
@@ -3,7 +3,7 @@
3
3
  module Bridgetown
4
4
  module Routes
5
5
  module RodaRouter
6
- def self.start!(app) # rubocop:disable Metrics/MethodLength
6
+ def self.start!(app)
7
7
  r = app.request
8
8
  response = app.response
9
9
 
@@ -12,17 +12,15 @@ module Bridgetown
12
12
 
13
13
  r.on file_slug do |*segment_values|
14
14
  response["X-Bridgetown-SSR"] = "1"
15
- unless Bridgetown.env.production? &&
16
- Bridgetown::Routes::CodeBlocks.route_defined?(file_slug)
17
- eval_route_file file, file_slug, app
18
- end
15
+ # eval_route_file caches when Bridgetown.env.production?
16
+ Bridgetown::Routes::CodeBlocks.eval_route_file file, file_slug, app
19
17
 
20
18
  segment_values.each_with_index do |value, index|
21
19
  r.params[segment_keys[index]] ||= value
22
20
  end
23
21
 
24
22
  route_block = Bridgetown::Routes::CodeBlocks.route_block(file_slug)
25
- app.instance_variable_set(
23
+ response.instance_variable_set(
26
24
  :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
27
25
  ) # could be nil
28
26
  app.instance_exec(&route_block)
@@ -31,24 +29,6 @@ module Bridgetown
31
29
 
32
30
  nil
33
31
  end
34
-
35
- def self.eval_route_file(file, file_slug, app) # rubocop:disable Lint/UnusedMethodArgument
36
- code = File.read(file)
37
- code_postmatch = nil
38
- ruby_content = code.match(Bridgetown::FrontMatterImporter::RUBY_BLOCK)
39
- if ruby_content
40
- code = ruby_content[1]
41
- code_postmatch = ruby_content.post_match
42
- end
43
-
44
- code = <<~RUBY
45
- r = app.request
46
- Bridgetown::Routes::CodeBlocks.add_route(#{file_slug.inspect}, #{code_postmatch.inspect}) do
47
- #{code}
48
- end
49
- RUBY
50
- instance_eval(code, file, -1)
51
- end
52
32
  end
53
33
  end
54
34
  end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Routes
5
+ module FlashHashAdditions
6
+ def info
7
+ self["info"]
8
+ end
9
+
10
+ def info=(val)
11
+ self["info"] = val
12
+ end
13
+
14
+ def alert
15
+ self["alert"]
16
+ end
17
+
18
+ def alert=(val)
19
+ self["alert"] = val
20
+ end
21
+ end
22
+
23
+ module FlashHashIndifferent
24
+ def []=(key, val)
25
+ @next[key.to_s] = val
26
+ end
27
+ end
28
+
29
+ module FlashNowHashIndifferent
30
+ def []=(key, val)
31
+ super(key.to_s, val)
32
+ end
33
+
34
+ def [](key)
35
+ super(key.to_s)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ module Bridgetown
42
+ module Routes
43
+ class BlankFlash < Hash
44
+ include Bridgetown::Routes::FlashHashAdditions
45
+
46
+ def now
47
+ self
48
+ end
49
+ end
50
+
51
+ module ViewHelpers
52
+ def request
53
+ view.resource&.roda_app&.request
54
+ end
55
+ alias_method :r, :request
56
+
57
+ def response
58
+ view.resource&.roda_app&.response
59
+ end
60
+
61
+ def flash
62
+ view.resource&.roda_app&.flash || _basic_flash
63
+ end
64
+
65
+ def _basic_flash
66
+ @_basic_flash ||= BlankFlash.new
67
+ end
68
+
69
+ # def csrf_tag(...)
70
+ # request.scope.csrf_tag(...)
71
+ # end
72
+
73
+ # def csrf_token(...)
74
+ # request.scope.csrf_token(...)
75
+ # end
76
+ end
77
+ end
78
+ end
79
+
80
+ Bridgetown::RubyTemplateView::Helpers.include Bridgetown::Routes::ViewHelpers
@@ -3,9 +3,10 @@
3
3
  require "bridgetown-core"
4
4
  require "bridgetown-core/version"
5
5
 
6
- require_relative "roda/plugins/bridgetown_routes"
6
+ require_relative "bridgetown-routes/view_helpers"
7
7
 
8
- require_relative "bridgetown-routes/helpers"
8
+ # Roda isn't defined for Bridgetown build-only
9
+ require_relative "roda/plugins/bridgetown_routes" if defined?(Roda)
9
10
 
10
11
  module Bridgetown
11
12
  module Routes
@@ -35,3 +36,21 @@ module Bridgetown
35
36
  # rubocop:enable Bridgetown/NoPutsAllowed
36
37
  end
37
38
  end
39
+
40
+ module RodaResourceExtension
41
+ module RubyResource
42
+ def roda_app=(app)
43
+ unless app.is_a?(Bridgetown::Rack::Roda)
44
+ raise Bridgetown::Errors::FatalException,
45
+ "Resource's assigned Roda app must be of type `Bridgetown::Rack::Roda'"
46
+ end
47
+
48
+ @roda_app = app
49
+ end
50
+
51
+ def roda_app
52
+ @roda_app
53
+ end
54
+ end
55
+ end
56
+ Bridgetown::Resource.register_extension RodaResourceExtension
@@ -2,6 +2,17 @@
2
2
 
3
3
  require "roda/plugins/flash"
4
4
 
5
+ Roda::RodaPlugins::Flash::FlashHash.include Bridgetown::Routes::FlashHashAdditions,
6
+ Bridgetown::Routes::FlashHashIndifferent
7
+ Roda::RodaPlugins::Flash::FlashHash.class_eval do
8
+ def initialize(hash = {})
9
+ super(hash || {})
10
+ now.singleton_class.include Bridgetown::Routes::FlashHashAdditions,
11
+ Bridgetown::Routes::FlashNowHashIndifferent
12
+ @next = {}
13
+ end
14
+ end
15
+
5
16
  class Roda
6
17
  module RodaPlugins
7
18
  module BridgetownRoutes
@@ -20,27 +31,31 @@ class Roda
20
31
  end
21
32
 
22
33
  module InstanceMethods
23
- def render_with(data: {}) # rubocop:todo Metrics/AbcSize
34
+ def render_with(data: {}) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
24
35
  path = Kernel.caller_locations(1, 1).first.absolute_path
25
- source_path = Pathname.new(path).relative_path_from(Bridgetown::Current.site.source)
26
- code = @_route_file_code
36
+ source_path = Pathname.new(path).relative_path_from(
37
+ Bridgetown::Current.site.in_source_dir("_routes")
38
+ )
39
+ code = response._route_file_code
27
40
 
28
41
  unless code.present?
29
42
  raise Bridgetown::Errors::FatalException,
30
43
  "`render_with' method must be called from a template-based file in `src/_routes'"
31
44
  end
32
45
 
33
- data = Bridgetown::Model::BuilderOrigin.new("builder://#{source_path}").read do
46
+ data = Bridgetown::Model::BuilderOrigin.new(
47
+ Bridgetown::Model::BuilderOrigin.id_for_builder_path(
48
+ self, Addressable::URI.encode(source_path.to_s)
49
+ )
50
+ ).read do
34
51
  data[:_collection_] = Bridgetown::Current.site.collections.pages
52
+ data[:_relative_path_] = source_path
35
53
  data[:_content_] = code
36
54
  data
37
55
  end
38
56
 
39
57
  Bridgetown::Model::Base.new(data).to_resource.tap do |resource|
40
- resource.roda_data[:request] = request
41
- resource.roda_data[:response] = response
42
- resource.roda_data[:flash] = nil
43
- # resource.roda_data[:flash] = flash
58
+ resource.roda_app = self
44
59
  end.read!.transform!.output
45
60
  end
46
61
 
@@ -50,23 +65,24 @@ class Roda
50
65
 
51
66
  def view(view_class: Bridgetown::ERBView)
52
67
  response._fake_resource_view(
53
- view_class: view_class, request: request, bridgetown_site: bridgetown_site
68
+ view_class: view_class, roda_app: self, bridgetown_site: bridgetown_site
54
69
  )
55
70
  end
56
71
  end
57
72
 
58
73
  module ResponseMethods
59
- def _fake_resource_view(view_class:, request:, bridgetown_site:)
74
+ # template string provided, if available, by the saved code block
75
+ def _route_file_code
76
+ @_route_file_code
77
+ end
78
+
79
+ def _fake_resource_view(view_class:, roda_app:, bridgetown_site:)
60
80
  @_fake_resource_views ||= {}
61
81
  @_fake_resource_views[view_class] ||= view_class.new(
62
82
  # TODO: use a Stuct for better performance...?
63
83
  HashWithDotAccess::Hash.new({
64
84
  data: {},
65
- roda_data: {
66
- request: request,
67
- response: self,
68
- flash: nil, # flash,
69
- },
85
+ roda_app: roda_app,
70
86
  site: bridgetown_site,
71
87
  })
72
88
  )
@@ -77,30 +93,3 @@ class Roda
77
93
  register_plugin :bridgetown_routes, BridgetownRoutes
78
94
  end
79
95
  end
80
-
81
- module RodaResourceExtension
82
- module RubyResource
83
- def roda_data
84
- @roda_data ||= HashWithDotAccess::Hash.new
85
- end
86
- end
87
- end
88
- Bridgetown::Resource.register_extension RodaResourceExtension
89
-
90
- Roda::RodaPlugins::Flash::FlashHash.class_eval do
91
- def info
92
- self["info"]
93
- end
94
-
95
- def info=(val)
96
- self["info"] = val
97
- end
98
-
99
- def alert
100
- self["alert"]
101
- end
102
-
103
- def alert=(val)
104
- self["alert"] = val
105
- end
106
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha2
4
+ version: 1.0.0.alpha6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-16 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0.alpha2
19
+ version: 1.0.0.alpha6
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.0.alpha2
26
+ version: 1.0.0.alpha6
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: roda-route_list
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -49,9 +49,9 @@ files:
49
49
  - bridgetown-routes.gemspec
50
50
  - lib/bridgetown-routes.rb
51
51
  - lib/bridgetown-routes/code_blocks.rb
52
- - lib/bridgetown-routes/helpers.rb
53
52
  - lib/bridgetown-routes/manifest.rb
54
53
  - lib/bridgetown-routes/roda_router.rb
54
+ - lib/bridgetown-routes/view_helpers.rb
55
55
  - lib/roda/plugins/bridgetown_routes.rb
56
56
  homepage: https://github.com/bridgetownrb/bridgetown/tree/main/bridgetown-routes
57
57
  licenses:
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bridgetown
4
- module Routes
5
- module ViewHelpers
6
- def request
7
- view.resource&.roda_data&.request
8
- end
9
- alias_method :r, :request
10
-
11
- def response
12
- view.resource&.roda_data&.response
13
- end
14
-
15
- def flash
16
- view.resource&.roda_data&.flash
17
- end
18
-
19
- # def csrf_tag(...)
20
- # request.scope.csrf_tag(...)
21
- # end
22
-
23
- # def csrf_token(...)
24
- # request.scope.csrf_token(...)
25
- # end
26
- end
27
- end
28
- end
29
-
30
- Bridgetown::RubyTemplateView::Helpers.include Bridgetown::Routes::ViewHelpers