bridgetown-routes 1.0.0.alpha3 → 1.0.0.alpha7

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: 824080d36bab52ac4f6ebf7a3e3d50ec2d7d289cd4727594125ba17475fbc445
4
- data.tar.gz: 4c656040ae9fc9945c00240b019e7e2cd4be6d5df6b20b674e931cec2d3c1f3a
3
+ metadata.gz: dcd02f991a4e93157657cdcc8a8bb8fbb5dd31b8f7362115c35b93eb4e0f4440
4
+ data.tar.gz: 0ffd18f98b4a0b5d4e2fbd65cf539ad86c3d450351e5a9bc180501c4ca84877e
5
5
  SHA512:
6
- metadata.gz: 538394a2ea353c743abcd06939a00bef9af7252e7a02a5184e1aefe989acdec3f885a1c55f233ef6e8119d333e8baa066da589ab9c261c7fed3775145d2bd289
7
- data.tar.gz: '06932279655aa3e401ab61eac7b0f537d1bb5b52318f96f44c5a18b5d1072b52525467f5d9475ece1806147e8b5f22a4adb4a4912a638983670e1ec7606f2e86'
6
+ metadata.gz: 1a337e4d65fa2c54b449c6a2838214891d646061929dceb3285f10184826fd25e67de6c1960f7f82d7ba6af5957677c707034555ffa04581f99d2c48404fe3db
7
+ data.tar.gz: 39137b4297418ed2edec3ba8087449e4d0e8118787aa373ba7cf29bb04ae15be8f392ca718358388f3dc978a63c93cf0a9fc839938a963c52a011226714e9861
@@ -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,9 +12,11 @@ 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
- routes = Dir.glob(routes_dir + "/**/*.{#{routable_extensions.join(",")}}").map do |file|
17
+ routes = Dir.glob(
18
+ routes_dir + "/**/*.{#{routable_extensions.join(",")}}"
19
+ ).filter_map do |file|
17
20
  if File.basename(file).start_with?("_", ".") ||
18
21
  File.basename(file, ".*").end_with?(".test")
19
22
  next
@@ -30,7 +33,7 @@ module Bridgetown
30
33
  end
31
34
 
32
35
  [file, file_slug, segment_keys]
33
- end.compact
36
+ end
34
37
 
35
38
  routes.sort! do |route_a, route_b|
36
39
  # @type [String]
@@ -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,53 +31,58 @@ 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
 
47
- ruby2_keywords def render(*args, &block)
48
- view.render(*args, &block)
62
+ def render(...)
63
+ view.render(...)
49
64
  end
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.alpha3
4
+ version: 1.0.0.alpha7
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-18 00:00:00.000000000 Z
11
+ date: 2021-11-05 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.alpha3
19
+ version: 1.0.0.alpha7
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.alpha3
26
+ version: 1.0.0.alpha7
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