bridgetown-routes 1.1.0.beta3 → 1.2.0.beta1

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: cafc5bdea40bacaf7d7c2bb6badbc490cf67602dccccf1e1147a8ba9aec601ee
4
- data.tar.gz: da29fd235e26ae5cc22c3ba95150fc1f5f3fb0cb79dbfff63852b8854d228dd4
3
+ metadata.gz: 0e3b03206a50338a4e3ad893d9fda292c90b153d53e55045b2e6d49f551af180
4
+ data.tar.gz: b3537ceca41ac4773208d1675a4b26033b333512df54fac27f0aa4cf8b4a66c5
5
5
  SHA512:
6
- metadata.gz: 1505f55118bba6242048102ea518d988230469e6ab1d5e90278129ad03f4d0e934a46a0b5a4b294df5c760b62f766ce24de02a8c8e51230ba4a8674f9153530c
7
- data.tar.gz: 300c16eceb5cb3ff3a2cf9fca60462d8d7e3b7a508bb4ef13292b4d80ce6f2fccb33636c6c373fbfc868bf8024a1a3539ff14647723555dbcc2519366e8164bf
6
+ metadata.gz: 50788587781e7305668e878421e4553c14236775f30a870ae3a61833fd941d92a3607dc7c00cc9ed75d20d21f9595adb39a5c14b84ebb5014d959c05e576ce3e
7
+ data.tar.gz: 2230eb95ef39cf36ece5125eae4150c6f1d9a050a92342313f53b5912b44921a61648226fd99809665f7c50dfb330eff9906b1a2aac42b1198f3f5007201992a
@@ -14,6 +14,13 @@ Gem::Specification.new do |spec|
14
14
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) }
15
15
  spec.require_paths = ["lib"]
16
16
 
17
+ spec.metadata = {
18
+ "source_code_uri" => "https://github.com/bridgetownrb/bridgetown",
19
+ "bug_tracker_uri" => "https://github.com/bridgetownrb/bridgetown/issues",
20
+ "changelog_uri" => "https://github.com/bridgetownrb/bridgetown/releases",
21
+ "homepage_uri" => spec.homepage,
22
+ }
23
+
17
24
  spec.add_dependency("bridgetown-core", Bridgetown::VERSION)
18
25
  spec.add_dependency("roda-route_list", ">= 2.1")
19
26
  end
@@ -0,0 +1,39 @@
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
@@ -4,42 +4,51 @@ module Bridgetown
4
4
  module Routes
5
5
  module Manifest
6
6
  class << self
7
- # TODO: make this extensible
8
- def routable_extensions
9
- %w(rb md serb erb liquid)
10
- end
11
-
12
- def generate_manifest # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
13
- return @route_manifest if @route_manifest && Bridgetown.env.production?
14
-
15
- routes_dir = Bridgetown::Current.site.in_source_dir("_routes")
16
- # @type [Array]
17
- routes = Dir.glob(
18
- routes_dir + "/**/*.{#{routable_extensions.join(",")}}"
19
- ).filter_map do |file|
20
- if File.basename(file).start_with?("_", ".") ||
21
- File.basename(file, ".*").end_with?(".test")
22
- next
23
- end
24
-
25
- # @type [String]
26
- file_slug = file.delete_prefix("#{routes_dir}/").then do |f|
27
- [File.dirname(f), File.basename(f, ".*")].join("/").delete_prefix("./")
28
- end.delete_suffix("/index")
29
- segment_keys = []
30
- file_slug.gsub!(%r{\[([^/]+)\]}) do |_segment|
31
- segment_keys << Regexp.last_match(1)
32
- ":#{Regexp.last_match(1)}"
7
+ def generate_manifest(site) # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
8
+ return @route_manifest[site.label] if @route_manifest && Bridgetown.env.production?
9
+
10
+ new_manifest = []
11
+ routable_extensions = site.config.routes.extensions.join(",")
12
+
13
+ site.config.routes.source_paths.each do |routes_dir|
14
+ routes_dir = File.expand_path(routes_dir, site.config.source)
15
+
16
+ # @type [Array]
17
+ routes = Dir.glob(
18
+ routes_dir + "/**/*.{#{routable_extensions}}"
19
+ ).filter_map do |file|
20
+ if File.basename(file).start_with?("_", ".") ||
21
+ File.basename(file, ".*").end_with?(".test")
22
+ next
23
+ end
24
+
25
+ # @type [String]
26
+ file_slug = file.delete_prefix("#{routes_dir}/").then do |f|
27
+ [File.dirname(f), File.basename(f, ".*")].join("/").delete_prefix("./")
28
+ end.delete_suffix("/index")
29
+ segment_keys = []
30
+ file_slug.gsub!(%r{\[([^/]+)\]}) do |_segment|
31
+ segment_keys << Regexp.last_match(1)
32
+ ":#{Regexp.last_match(1)}"
33
+ end
34
+
35
+ [file, file_slug, segment_keys]
33
36
  end
34
37
 
35
- [file, file_slug, segment_keys]
38
+ new_manifest += sort_routes!(routes)
36
39
  end
40
+ @route_manifest ||= {}
41
+ @route_manifest[site.label] = new_manifest
42
+ end
37
43
 
44
+ def sort_routes!(routes)
38
45
  routes.sort! do |route_a, route_b|
39
46
  # @type [String]
40
47
  _, slug_a = route_a
48
+ # @type [String]
41
49
  _, slug_b = route_b
42
50
 
51
+ # @type [Integer]
43
52
  weight1 = slug_a.count("/") <=> slug_b.count("/")
44
53
  if weight1.zero?
45
54
  slug_b.count("/:") <=> slug_a.count("/:")
@@ -47,8 +56,6 @@ module Bridgetown
47
56
  weight1
48
57
  end
49
58
  end.reverse!
50
-
51
- @route_manifest = routes
52
59
  end
53
60
  end
54
61
  end
@@ -1,26 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bridgetown-core/rack/routes"
4
+
3
5
  module Bridgetown
4
6
  module Routes
5
- module RodaRouter
6
- def self.start!(app) # rubocop:todo Metrics/MethodLength
7
- unless Bridgetown::Current.site
7
+ class ManifestRouter < Bridgetown::Rack::Routes
8
+ priority :lowest
9
+
10
+ route do |r|
11
+ unless bridgetown_site
8
12
  Bridgetown.logger.warn(
9
13
  "The `bridgetown_routes` plugin hasn't been configured in the Roda app."
10
14
  )
11
15
  return
12
16
  end
13
17
 
14
- r = app.request
15
- response = app.response
16
-
17
- Bridgetown::Routes::Manifest.generate_manifest.each do |route|
18
+ Bridgetown::Routes::Manifest.generate_manifest(bridgetown_site).each do |route|
18
19
  file, file_slug, segment_keys = route
19
20
 
20
21
  r.on file_slug do |*segment_values|
21
22
  response["X-Bridgetown-SSR"] = "1"
22
23
  # eval_route_file caches when Bridgetown.env.production?
23
- Bridgetown::Routes::CodeBlocks.eval_route_file file, file_slug, app
24
+ Bridgetown::Routes::CodeBlocks.eval_route_file file, file_slug, @_roda_app
24
25
 
25
26
  segment_values.each_with_index do |value, index|
26
27
  r.params[segment_keys[index]] ||= value
@@ -30,7 +31,7 @@ module Bridgetown
30
31
  response.instance_variable_set(
31
32
  :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
32
33
  ) # could be nil
33
- app.instance_exec(r, &route_block)
34
+ @_roda_app.instance_exec(r, &route_block)
34
35
  end
35
36
  end
36
37
 
@@ -1,77 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "flash_additions"
4
+
3
5
  module Bridgetown
4
6
  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
7
+ module ViewHelpers
8
+ # This flash is only used as a stub for views in case there's no Roda flash
9
+ # available in the rendering context
10
+ class Flash < Hash
11
+ include Bridgetown::Routes::FlashHashAdditions
28
12
 
29
- module FlashNowHashIndifferent
30
- def []=(key, val)
31
- super(key.to_s, val)
13
+ def now
14
+ self
15
+ end
32
16
  end
33
17
 
34
- def [](key)
35
- super(key.to_s)
18
+ def roda_app
19
+ view.resource&.roda_app
36
20
  end
37
- end
38
- end
39
- end
40
21
 
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
22
  def request
53
- view.resource&.roda_app&.request
23
+ roda_app&.request
54
24
  end
55
25
  alias_method :r, :request
56
26
 
57
27
  def response
58
- view.resource&.roda_app&.response
28
+ roda_app&.response
59
29
  end
60
30
 
61
31
  def flash
62
- view.resource&.roda_app&.flash || _basic_flash
32
+ roda_app&.flash || _blank_flash
63
33
  end
64
34
 
65
- def _basic_flash
66
- @_basic_flash ||= BlankFlash.new
35
+ def _blank_flash
36
+ @_blank_flash ||= Flash.new
67
37
  end
68
38
 
69
39
  def csrf_tag(...)
70
- request.scope.csrf_tag(...)
40
+ roda_app.csrf_tag(...)
71
41
  end
72
42
 
73
43
  def csrf_token(...)
74
- request.scope.csrf_token(...)
44
+ roda_app.csrf_token(...)
75
45
  end
76
46
  end
77
47
  end
@@ -1,57 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bridgetown-core"
4
- require "bridgetown-core/version"
5
-
6
- require_relative "bridgetown-routes/view_helpers"
7
-
8
- # Roda isn't defined for Bridgetown build-only
9
- require_relative "roda/plugins/bridgetown_routes" if defined?(Roda)
10
4
 
11
5
  module Bridgetown
12
6
  module Routes
13
7
  autoload :CodeBlocks, "bridgetown-routes/code_blocks"
14
8
  autoload :Manifest, "bridgetown-routes/manifest"
15
9
  autoload :RodaRouter, "bridgetown-routes/roda_router"
10
+ end
11
+ end
16
12
 
17
- # rubocop:disable Bridgetown/NoPutsAllowed
18
- def self.print_roda_routes
19
- # TODO: this needs to be fully documented, currently no info on how to generate .routes.json
20
- routes = begin
21
- JSON.parse(File.read("#{Dir.pwd}/.routes.json"))
22
- rescue StandardError
23
- []
24
- end
25
- puts
26
- puts "Routes:"
27
- puts "======="
28
- if routes.blank?
29
- puts "No routes found. Have you commented all of your routes?"
30
- puts "Documentation: https://github.com/jeremyevans/roda-route_list#basic-usage-"
31
- end
13
+ # @param config [Bridgetown::Configuration::ConfigurationDSL]
14
+ Bridgetown.initializer :"bridgetown-routes" do |config|
15
+ config.init :ssr # ensure we already have touchdown!
32
16
 
33
- routes.each do |route|
34
- puts [route["methods"]&.join("|") || "GET", route["path"]].compact.join(" ")
35
- end
36
- end
37
- # rubocop:enable Bridgetown/NoPutsAllowed
17
+ config.routes ||= {}
18
+ config.routes.source_paths ||= ["_routes"]
19
+ config.routes.extensions ||= %w(rb md serb erb liquid)
20
+
21
+ config.only :server do
22
+ require_relative "bridgetown-routes/manifest_router"
23
+ end
24
+
25
+ config.roda do |app|
26
+ app.plugin :bridgetown_routes
38
27
  end
39
- end
40
28
 
41
- module RodaResourceExtension
42
- module RubyResource
43
- def roda_app=(app)
44
- unless app.is_a?(Bridgetown::Rack::Roda)
45
- raise Bridgetown::Errors::FatalException,
46
- "Resource's assigned Roda app must be of type `Bridgetown::Rack::Roda'"
29
+ require_relative "bridgetown-routes/view_helpers"
30
+
31
+ config.builder :BridgetownRoutesBuilder do
32
+ def build
33
+ define_resource_method :roda_app do
34
+ @roda_app
47
35
  end
48
36
 
49
- @roda_app = app
50
- end
37
+ define_resource_method :roda_app= do |app|
38
+ unless app.is_a?(Bridgetown::Rack::Roda)
39
+ raise Bridgetown::Errors::FatalException,
40
+ "Resource's assigned Roda app must be of type `Bridgetown::Rack::Roda'"
41
+ end
51
42
 
52
- def roda_app
53
- @roda_app
43
+ @roda_app = app
44
+ end
54
45
  end
55
46
  end
56
47
  end
57
- Bridgetown::Resource.register_extension RodaResourceExtension
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "roda/plugins/flash"
4
+ require_relative "../../bridgetown-routes/flash_additions"
4
5
 
5
6
  Roda::RodaPlugins::Flash::FlashHash.include Bridgetown::Routes::FlashHashAdditions,
6
7
  Bridgetown::Routes::FlashHashIndifferent
@@ -34,7 +35,7 @@ class Roda
34
35
  def render_with(data: {}) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
35
36
  path = Kernel.caller_locations(1, 1).first.path
36
37
  source_path = Pathname.new(path).relative_path_from(
37
- Bridgetown::Current.site.in_source_dir("_routes")
38
+ bridgetown_site.in_source_dir("_routes")
38
39
  )
39
40
  code = response._route_file_code
40
41
 
@@ -48,7 +49,7 @@ class Roda
48
49
  self, Addressable::URI.encode(source_path.to_s)
49
50
  )
50
51
  ).read do
51
- data[:_collection_] = Bridgetown::Current.site.collections.pages
52
+ data[:_collection_] = bridgetown_site.collections.pages
52
53
  data[:_relative_path_] = source_path
53
54
  data[:_content_] = code
54
55
  data
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.1.0.beta3
4
+ version: 1.2.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-28 00:00:00.000000000 Z
11
+ date: 2022-10-02 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.1.0.beta3
19
+ version: 1.2.0.beta1
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.1.0.beta3
26
+ version: 1.2.0.beta1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: roda-route_list
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -49,14 +49,19 @@ files:
49
49
  - bridgetown-routes.gemspec
50
50
  - lib/bridgetown-routes.rb
51
51
  - lib/bridgetown-routes/code_blocks.rb
52
+ - lib/bridgetown-routes/flash_additions.rb
52
53
  - lib/bridgetown-routes/manifest.rb
53
- - lib/bridgetown-routes/roda_router.rb
54
+ - lib/bridgetown-routes/manifest_router.rb
54
55
  - lib/bridgetown-routes/view_helpers.rb
55
56
  - lib/roda/plugins/bridgetown_routes.rb
56
57
  homepage: https://github.com/bridgetownrb/bridgetown/tree/main/bridgetown-routes
57
58
  licenses:
58
59
  - MIT
59
- metadata: {}
60
+ metadata:
61
+ source_code_uri: https://github.com/bridgetownrb/bridgetown
62
+ bug_tracker_uri: https://github.com/bridgetownrb/bridgetown/issues
63
+ changelog_uri: https://github.com/bridgetownrb/bridgetown/releases
64
+ homepage_uri: https://github.com/bridgetownrb/bridgetown/tree/main/bridgetown-routes
60
65
  post_install_message:
61
66
  rdoc_options: []
62
67
  require_paths: