bridgetown-core 0.21.5 → 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/bin/bridgetown +2 -0
  3. data/bridgetown-core.gemspec +3 -0
  4. data/lib/bridgetown-core/cleaner.rb +0 -8
  5. data/lib/bridgetown-core/collection.rb +59 -81
  6. data/lib/bridgetown-core/commands/base.rb +60 -1
  7. data/lib/bridgetown-core/commands/build.rb +26 -6
  8. data/lib/bridgetown-core/commands/concerns/build_options.rb +3 -10
  9. data/lib/bridgetown-core/commands/concerns/configuration_overridable.rb +3 -1
  10. data/lib/bridgetown-core/commands/doctor.rb +3 -3
  11. data/lib/bridgetown-core/commands/new.rb +9 -3
  12. data/lib/bridgetown-core/commands/plugins.rb +1 -2
  13. data/lib/bridgetown-core/commands/serve.rb +14 -12
  14. data/lib/bridgetown-core/commands/start.rb +106 -0
  15. data/lib/bridgetown-core/commands/webpack/webpack.defaults.js.erb +2 -2
  16. data/lib/bridgetown-core/concerns/site/configurable.rb +0 -12
  17. data/lib/bridgetown-core/concerns/site/content.rb +6 -117
  18. data/lib/bridgetown-core/concerns/site/localizable.rb +3 -1
  19. data/lib/bridgetown-core/concerns/site/processable.rb +8 -20
  20. data/lib/bridgetown-core/concerns/site/renderable.rb +19 -30
  21. data/lib/bridgetown-core/concerns/site/ssr.rb +53 -0
  22. data/lib/bridgetown-core/concerns/site/writable.rb +5 -8
  23. data/lib/bridgetown-core/configuration.rb +18 -47
  24. data/lib/bridgetown-core/configurations/minitesting.rb +1 -1
  25. data/lib/bridgetown-core/configurations/turbo.rb +1 -1
  26. data/lib/bridgetown-core/converters/erb_templates.rb +2 -1
  27. data/lib/bridgetown-core/converters/liquid_templates.rb +3 -2
  28. data/lib/bridgetown-core/current.rb +4 -0
  29. data/lib/bridgetown-core/drops/collection_drop.rb +1 -1
  30. data/lib/bridgetown-core/drops/generated_page_drop.rb +23 -0
  31. data/lib/bridgetown-core/drops/resource_drop.rb +3 -3
  32. data/lib/bridgetown-core/drops/site_drop.rb +3 -47
  33. data/lib/bridgetown-core/filters/url_filters.rb +3 -1
  34. data/lib/bridgetown-core/frontmatter_defaults.rb +44 -80
  35. data/lib/bridgetown-core/{page.rb → generated_page.rb} +34 -58
  36. data/lib/bridgetown-core/generators/prototype_generator.rb +10 -21
  37. data/lib/bridgetown-core/helpers.rb +7 -2
  38. data/lib/bridgetown-core/layout.rb +15 -4
  39. data/lib/bridgetown-core/log_writer.rb +6 -0
  40. data/lib/bridgetown-core/model/base.rb +10 -2
  41. data/lib/bridgetown-core/model/builder_origin.rb +22 -10
  42. data/lib/bridgetown-core/model/origin.rb +3 -0
  43. data/lib/bridgetown-core/model/plugin_origin.rb +34 -0
  44. data/lib/bridgetown-core/model/repo_origin.rb +1 -1
  45. data/lib/bridgetown-core/plugin_manager.rb +0 -2
  46. data/lib/bridgetown-core/rack/boot.rb +47 -0
  47. data/lib/bridgetown-core/rack/logger.rb +22 -0
  48. data/lib/bridgetown-core/rack/roda.rb +66 -0
  49. data/lib/bridgetown-core/rack/routes.rb +92 -0
  50. data/lib/bridgetown-core/rack/static_indexes.rb +30 -0
  51. data/lib/bridgetown-core/reader.rb +16 -48
  52. data/lib/bridgetown-core/readers/plugin_content_reader.rb +8 -7
  53. data/lib/bridgetown-core/renderer.rb +1 -11
  54. data/lib/bridgetown-core/resource/base.rb +33 -10
  55. data/lib/bridgetown-core/resource/permalink_processor.rb +20 -10
  56. data/lib/bridgetown-core/resource/relations.rb +2 -3
  57. data/lib/bridgetown-core/resource/transformer.rb +1 -1
  58. data/lib/bridgetown-core/ruby_template_view.rb +5 -5
  59. data/lib/bridgetown-core/site.rb +4 -8
  60. data/lib/bridgetown-core/static_file.rb +10 -15
  61. data/lib/bridgetown-core/tags/include.rb +0 -13
  62. data/lib/bridgetown-core/tags/link.rb +4 -0
  63. data/lib/bridgetown-core/tags/live_reload_dev_js.rb +13 -0
  64. data/lib/bridgetown-core/tags/post_url.rb +4 -9
  65. data/lib/bridgetown-core/tasks/bridgetown_tasks.rake +54 -0
  66. data/lib/bridgetown-core/url.rb +2 -1
  67. data/lib/bridgetown-core/utils/aux.rb +57 -0
  68. data/lib/bridgetown-core/utils/ruby_exec.rb +3 -45
  69. data/lib/bridgetown-core/utils/ruby_front_matter.rb +22 -7
  70. data/lib/bridgetown-core/utils.rb +37 -2
  71. data/lib/bridgetown-core/version.rb +2 -2
  72. data/lib/bridgetown-core/watcher.rb +2 -4
  73. data/lib/bridgetown-core.rb +14 -22
  74. data/lib/site_template/Gemfile.erb +6 -2
  75. data/lib/site_template/README.md +6 -6
  76. data/lib/site_template/Rakefile +49 -0
  77. data/lib/site_template/bridgetown.config.yml +2 -3
  78. data/lib/site_template/config/puma.rb +27 -0
  79. data/lib/site_template/config.ru +7 -0
  80. data/lib/site_template/package.json.erb +1 -9
  81. data/lib/site_template/server/roda_app.rb +22 -0
  82. data/lib/site_template/server/routes/hello.rb.sample +10 -0
  83. data/lib/site_template/src/_components/head.liquid +2 -1
  84. data/lib/site_template/src/about.md +0 -1
  85. data/lib/site_template/src/posts.md +2 -3
  86. metadata +62 -18
  87. data/lib/bridgetown-core/concerns/data_accessible.rb +0 -20
  88. data/lib/bridgetown-core/concerns/validatable.rb +0 -56
  89. data/lib/bridgetown-core/document.rb +0 -437
  90. data/lib/bridgetown-core/drops/document_drop.rb +0 -80
  91. data/lib/bridgetown-core/drops/excerpt_drop.rb +0 -19
  92. data/lib/bridgetown-core/drops/page_drop.rb +0 -18
  93. data/lib/bridgetown-core/excerpt.rb +0 -200
  94. data/lib/bridgetown-core/readers/data_reader.rb +0 -89
  95. data/lib/bridgetown-core/readers/page_reader.rb +0 -26
  96. data/lib/bridgetown-core/readers/post_reader.rb +0 -109
  97. data/lib/bridgetown-core/regenerator.rb +0 -202
  98. data/lib/bridgetown-core/related_posts.rb +0 -55
  99. data/lib/site_template/config/.keep +0 -0
  100. data/lib/site_template/start.js +0 -17
  101. data/lib/site_template/sync.js +0 -35
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Handles Generated Pages
4
- Bridgetown::Hooks.register :pages, :post_init, reloadable: false do |page|
4
+ Bridgetown::Hooks.register :generated_pages, :post_init, reloadable: false do |page|
5
5
  if page.class != Bridgetown::PrototypePage && page.data["prototype"].is_a?(Hash)
6
6
  Bridgetown::PrototypeGenerator.add_matching_template(page)
7
7
  end
@@ -38,8 +38,7 @@ module Bridgetown
38
38
  # @param site [Bridgetown::Site]
39
39
  def generate(site)
40
40
  @site = site
41
- @configured_collection = "posts" unless site.uses_resource?
42
- page_list = site.uses_resource? ? site.collections.pages.resources : site.pages
41
+ page_list = site.collections.pages.resources
43
42
 
44
43
  prototype_pages = self.class.matching_templates.select do |page|
45
44
  page_list.include?(page)
@@ -74,7 +73,7 @@ module Bridgetown
74
73
 
75
74
  # Check incoming prototype configuration and normalize options.
76
75
  #
77
- # @param prototype_page [Bridgetown::Page, Bridgetown::Resource::Base]
76
+ # @param prototype_page [Bridgetown::GeneratedPage, Bridgetown::Resource::Base]
78
77
  #
79
78
  # @return [String, nil]
80
79
  def validate_search_term(prototype_page)
@@ -110,11 +109,7 @@ module Bridgetown
110
109
  #
111
110
  # @return [Array<String>]
112
111
  def terms_matching_pages(search_term)
113
- pages_list = if site.uses_resource?
114
- site.collections[@configured_collection].resources
115
- else
116
- site.collections[@configured_collection].docs
117
- end
112
+ pages_list = site.collections[@configured_collection].resources
118
113
 
119
114
  Bridgetown::Paginate::PaginationIndexer.index_documents_by(
120
115
  pages_list, search_term
@@ -123,7 +118,7 @@ module Bridgetown
123
118
  end
124
119
 
125
120
  class PrototypePage < GeneratedPage
126
- # @return [Bridgetown::Page, Bridgetown::Resource::Base]
121
+ # @return [Bridgetown::GeneratedPage, Bridgetown::Resource::Base]
127
122
  attr_reader :prototyped_page
128
123
 
129
124
  # @param prototyped_page [Bridgetown::Page, Bridgetown::Resource::Base]
@@ -135,23 +130,17 @@ module Bridgetown
135
130
  @site = prototyped_page.site
136
131
  @url = ""
137
132
  @name = "index.html"
138
- @path = prototyped_page.path
139
-
140
- process(@name)
133
+ @ext = ".html"
134
+ @basename = "index"
135
+ @dir = Pathname.new(prototyped_page.relative_path).dirname.to_s.sub(%r{^_pages}, "")
136
+ @path = site.in_source_dir(@dir, @name)
141
137
 
142
138
  self.data = Bridgetown::Utils.deep_merge_hashes prototyped_page.data, {}
143
139
  self.content = prototyped_page.content
144
140
 
145
- # Perform some validation that is also performed in Bridgetown::Page
146
- validate_data! prototyped_page.path
147
- validate_permalink! prototyped_page.path
148
-
149
- @dir = Pathname.new(prototyped_page.relative_path).dirname.to_s.sub(%r{^_pages}, "")
150
- @path = site.in_source_dir(@dir, @name)
151
-
152
141
  process_prototype_page_data(collection, search_term, term)
153
142
 
154
- Bridgetown::Hooks.trigger :pages, :post_init, self
143
+ Bridgetown::Hooks.trigger :generated_pages, :post_init, self
155
144
  end
156
145
 
157
146
  def process_prototype_page_data(collection, search_term, term)
@@ -26,6 +26,10 @@ module Bridgetown
26
26
  Bridgetown::Utils.parse_webpack_manifest_file(site, asset_type.to_s)
27
27
  end
28
28
 
29
+ def live_reload_dev_js
30
+ Bridgetown::Utils.live_reload_js(site)
31
+ end
32
+
29
33
  # @param pairs [Hash] A hash of key/value pairs.
30
34
  #
31
35
  # @return [String] Space-separated keys where the values are truthy.
@@ -56,7 +60,7 @@ module Bridgetown
56
60
  return safe(relative_path.relative_url) # new resource engine
57
61
  elsif relative_path.respond_to?(:url)
58
62
  return safe(relative_url(relative_path.url)) # old legacy engine
59
- elsif relative_path.start_with?("/", "http")
63
+ elsif relative_path.to_s.start_with?("/", "http")
60
64
  return safe(relative_path)
61
65
  end
62
66
 
@@ -69,7 +73,8 @@ module Bridgetown
69
73
  # @raise [ArgumentError] if the file cannot be found
70
74
  def find_relative_url_for_path(relative_path)
71
75
  site.each_site_file do |item|
72
- if item.relative_path == relative_path || item.relative_path == "/#{relative_path}"
76
+ if item.relative_path.to_s == relative_path ||
77
+ item.relative_path.to_s == "/#{relative_path}"
73
78
  return safe(item.respond_to?(:relative_url) ? item.relative_url : relative_url(item))
74
79
  end
75
80
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  module Bridgetown
4
4
  class Layout
5
- include DataAccessible
6
5
  include FrontMatterImporter
7
6
  include LiquidRenderable
8
7
 
@@ -44,9 +43,7 @@ module Bridgetown
44
43
  # @param file [String]
45
44
  # @return [String]
46
45
  def self.label_for_file(file)
47
- # TODO: refactor this so multi-extension layout filenames don't leak
48
- # middle extensions into layout label
49
- file.split(".")[0..-2].join(".")
46
+ file.split(".").first
50
47
  end
51
48
 
52
49
  # Initialize a new Layout.
@@ -93,6 +90,20 @@ module Bridgetown
93
90
  end
94
91
  end
95
92
 
93
+ # Returns the contents as a String.
94
+ def to_s
95
+ output || content || ""
96
+ end
97
+
98
+ # Accessor for data properties by Liquid.
99
+ #
100
+ # property - The String name of the property to retrieve.
101
+ #
102
+ # Returns the String value or nil if the property isn't included.
103
+ def [](property)
104
+ data[property]
105
+ end
106
+
96
107
  # The label of the layout (should match what would used in front matter
97
108
  # references).
98
109
  #
@@ -12,6 +12,12 @@ module Bridgetown
12
12
  end
13
13
  end
14
14
 
15
+ def enable_prefix
16
+ @formatter = proc do |_, _, _, msg|
17
+ "\e[32m[Bridgetown]\e[0m #{msg}"
18
+ end
19
+ end
20
+
15
21
  def add(severity, message = nil, progname = nil)
16
22
  severity ||= UNKNOWN
17
23
  @logdev = logdevice(severity)
@@ -46,8 +46,10 @@ module Bridgetown
46
46
  end
47
47
 
48
48
  class << self
49
- def build(collection_name, path, data)
50
- data = Bridgetown::Model::BuilderOrigin.new("builder://#{path}").read do
49
+ def build(builder, collection_name, path, data)
50
+ data = Bridgetown::Model::BuilderOrigin.new(
51
+ Bridgetown::Model::BuilderOrigin.id_for_builder_path(builder, path)
52
+ ).read do
51
53
  data[:_collection_] = Bridgetown::Current.site.collections[collection_name]
52
54
  data
53
55
  end
@@ -79,11 +81,17 @@ module Bridgetown
79
81
  Bridgetown::Resource::Base.new(model: self)
80
82
  end
81
83
 
84
+ # @return [Bridgetown::Resource::Base]
82
85
  def as_resource_in_collection
83
86
  collection.resources << to_resource.read!
84
87
  collection.resources.last
85
88
  end
86
89
 
90
+ # @return [Bridgetown::Resource::Base]
91
+ def render_as_resource
92
+ to_resource.read!.transform!
93
+ end
94
+
87
95
  # override if need be
88
96
  # @return [Bridgetown::Site]
89
97
  def site
@@ -2,29 +2,29 @@
2
2
 
3
3
  module Bridgetown
4
4
  module Model
5
- # Abstract Superclass
6
5
  class BuilderOrigin < Origin
7
6
  # @return [Pathname]
8
7
  attr_reader :relative_path
9
8
 
10
- # Override in subclass
11
9
  def self.handle_scheme?(scheme)
12
10
  scheme == "builder"
13
11
  end
14
12
 
13
+ def self.id_for_builder_path(builder, path)
14
+ "builder://#{builder.class.name.gsub("::", ".")}/#{path}"
15
+ end
16
+
15
17
  def initialize(id)
16
18
  self.id = id
17
- @relative_path = Pathname.new(id.delete_prefix("builder://"))
19
+ @relative_path = Pathname.new(url.path.delete_prefix("/"))
20
+ end
21
+
22
+ def url
23
+ @url ||= URI.parse(id)
18
24
  end
19
25
 
20
26
  def read
21
- @data = if block_given?
22
- yield
23
- elsif defined?(SiteBuilder) && SiteBuilder.respond_to?(:data_for_id)
24
- SiteBuilder.data_for_id(id)
25
- else
26
- raise "No builder exists which can read #{id}"
27
- end
27
+ @data = block_given? ? yield : read_data_from_builder
28
28
  @data[:_id_] = id
29
29
  @data[:_origin_] = self
30
30
  @relative_path = Pathname.new(@data[:_relative_path_]) if @data[:_relative_path_]
@@ -35,6 +35,18 @@ module Bridgetown
35
35
  def exists?
36
36
  false
37
37
  end
38
+
39
+ def read_data_from_builder
40
+ builder = Kernel.const_get(url.host.gsub(".", "::"))
41
+ raise NameError unless builder.respond_to?(:resource_data_for_id)
42
+
43
+ builder.resource_data_for_id(id)
44
+ rescue NameError
45
+ raise(
46
+ Bridgetown::Errors::FatalException,
47
+ "Builder not found which can read #{id}"
48
+ )
49
+ end
38
50
  end
39
51
  end
40
52
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # See bottom of file for specific origin requires...
4
+
3
5
  module Bridgetown
4
6
  module Model
5
7
  # Abstract Superclass
@@ -36,3 +38,4 @@ end
36
38
 
37
39
  require "bridgetown-core/model/builder_origin"
38
40
  require "bridgetown-core/model/repo_origin"
41
+ require "bridgetown-core/model/plugin_origin"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Model
5
+ class PluginOrigin < RepoOrigin
6
+ class << self
7
+ def handle_scheme?(scheme)
8
+ scheme == "plugin"
9
+ end
10
+ end
11
+
12
+ def manifest
13
+ @manifest ||= begin
14
+ manifest_origin = Addressable::URI.unescape(url.path.delete_prefix("/")).split("/").first
15
+ Bridgetown::PluginManager.source_manifests.find do |manifest|
16
+ manifest.origin.to_s == manifest_origin
17
+ end.tap do |manifest| # rubocop:disable Style/MultilineBlockChain
18
+ raise "Unable to locate a source manifest for #{manifest_origin}" unless manifest
19
+ end
20
+ end
21
+ end
22
+
23
+ def relative_path
24
+ @relative_path ||= Pathname.new(
25
+ Addressable::URI.unescape(url.path.delete_prefix("/")).split("/")[1..-1].join("/")
26
+ )
27
+ end
28
+
29
+ def original_path
30
+ @original_path ||= relative_path.expand_path(manifest.content)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -47,7 +47,7 @@ module Bridgetown
47
47
  end
48
48
 
49
49
  def url
50
- @url = URI.parse(id)
50
+ @url ||= URI.parse(id)
51
51
  end
52
52
 
53
53
  def relative_path
@@ -160,7 +160,6 @@ module Bridgetown
160
160
  end
161
161
  end
162
162
 
163
- # rubocop:disable Metrics/AbcSize
164
163
  def setup_component_loaders
165
164
  unless @component_loaders.keys.empty?
166
165
  @component_loaders.each do |_path, loader|
@@ -184,7 +183,6 @@ module Bridgetown
184
183
  end
185
184
  end
186
185
  end
187
- # rubocop:enable Metrics/AbcSize
188
186
 
189
187
  def reload_component_loaders
190
188
  @component_loaders.each do |path, loader|
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ require "roda"
5
+ require "json"
6
+ require "roda/plugins/public"
7
+
8
+ Bridgetown::Current.preloaded_configuration ||= Bridgetown.configuration
9
+
10
+ require_relative "logger"
11
+ require_relative "roda"
12
+ require_relative "routes"
13
+ require_relative "static_indexes"
14
+
15
+ module Bridgetown
16
+ module Rack
17
+ def self.boot
18
+ autoload_server_folder(root: Dir.pwd)
19
+ RodaApp.opts[:bridgetown_preloaded_config] = Bridgetown::Current.preloaded_configuration
20
+ end
21
+
22
+ def self.autoload_server_folder(root:)
23
+ server_folder = File.join(root, "server")
24
+ loader = Zeitwerk::Loader.new
25
+ loader.push_dir server_folder
26
+ loader.enable_reloading unless ENV["BRIDGETOWN_ENV"] == "production"
27
+ loader.setup
28
+ loader.eager_load
29
+ loader.do_not_eager_load(File.join(server_folder, "roda_app.rb"))
30
+
31
+ unless ENV["BRIDGETOWN_ENV"] == "production"
32
+ begin
33
+ Listen.to(server_folder) do |_modified, _added, _removed|
34
+ loader.reload
35
+ loader.eager_load
36
+ Bridgetown::Rack::Routes.reload_subclasses
37
+ end.start
38
+ # interrupt isn't handled well by the listener
39
+ rescue ThreadError # rubocop:disable Lint/SuppressedException
40
+ end
41
+ end
42
+ rescue Zeitwerk::Error
43
+ # We assume if there's an error it's because Zeitwerk already registered this folder,
44
+ # so it's fine to swallow the error
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module Bridgetown
6
+ module Rack
7
+ class Logger < Logger
8
+ def self.message_with_prefix(msg)
9
+ return if msg.include?("/_bridgetown/live_reload")
10
+
11
+ "\e[35m[Server]\e[0m #{msg}"
12
+ end
13
+
14
+ def initialize(*)
15
+ super
16
+ @formatter = proc do |_, _, _, msg|
17
+ self.class.message_with_prefix(msg)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/indifferent"
4
+
5
+ class Roda
6
+ module RodaPlugins
7
+ module BridgetownSSR
8
+ def self.configure(app, _opts = {}, &block)
9
+ app.opts[:bridgetown_site] = Bridgetown::Site.start_ssr!(&block)
10
+ end
11
+ end
12
+
13
+ register_plugin :bridgetown_ssr, BridgetownSSR
14
+ end
15
+ end
16
+
17
+ module Bridgetown
18
+ module Rack
19
+ class Roda < ::Roda
20
+ plugin :hooks
21
+ plugin :common_logger, Bridgetown::Rack::Logger.new($stdout), method: :info
22
+ plugin :json
23
+ plugin :json_parser
24
+ plugin :cookies
25
+ plugin :public, root: Bridgetown::Current.preloaded_configuration.destination
26
+ plugin :not_found do
27
+ output_folder = Bridgetown::Current.preloaded_configuration.destination
28
+ File.read(File.join(output_folder, "404.html"))
29
+ rescue Errno::ENOENT
30
+ "404 Not Found"
31
+ end
32
+ plugin :error_handler do |e|
33
+ puts "\n#{e.class} (#{e.message}):\n\n"
34
+ puts e.backtrace
35
+ output_folder = Bridgetown::Current.preloaded_configuration.destination
36
+ File.read(File.join(output_folder, "500.html"))
37
+ rescue Errno::ENOENT
38
+ "500 Internal Server Error"
39
+ end
40
+
41
+ def _roda_run_main_route(r) # rubocop:disable Naming/MethodParameterName
42
+ if self.class.opts[:bridgetown_site]
43
+ # The site had previously been initialized via the bridgetown_ssr plugin
44
+ Bridgetown::Current.site ||= self.class.opts[:bridgetown_site]
45
+ end
46
+ Bridgetown::Current.preloaded_configuration ||=
47
+ self.class.opts[:bridgetown_preloaded_config]
48
+
49
+ r.public
50
+
51
+ r.root do
52
+ output_folder = Bridgetown::Current.preloaded_configuration.destination
53
+ File.read(File.join(output_folder, "index.html"))
54
+ end
55
+
56
+ super
57
+ end
58
+
59
+ # Helper shorthand for Bridgetown::Current.site
60
+ # @return [Bridgetown::Site]
61
+ def bridgetown_site
62
+ Bridgetown::Current.site
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Rack
5
+ class Routes
6
+ class << self
7
+ attr_accessor :tracked_subclasses
8
+
9
+ def inherited(base)
10
+ Bridgetown::Rack::Routes.track_subclass base
11
+ super
12
+ end
13
+
14
+ def track_subclass(klass)
15
+ Bridgetown::Rack::Routes.tracked_subclasses ||= {}
16
+ Bridgetown::Rack::Routes.tracked_subclasses[klass.name] = klass
17
+ end
18
+
19
+ def reload_subclasses
20
+ Bridgetown::Rack::Routes.tracked_subclasses&.each_key do |klassname|
21
+ Kernel.const_get(klassname)
22
+ rescue NameError
23
+ Bridgetown::Rack::Routes.tracked_subclasses.delete klassname
24
+ end
25
+ end
26
+
27
+ attr_accessor :router_block
28
+
29
+ def route(&block)
30
+ self.router_block = block
31
+ end
32
+
33
+ def merge(roda_app)
34
+ return unless router_block
35
+
36
+ new(roda_app).handle_routes
37
+ end
38
+
39
+ def start!(roda_app)
40
+ if Bridgetown.env.development? &&
41
+ !Bridgetown::Current.preloaded_configuration.skip_live_reload
42
+ setup_live_reload roda_app.request
43
+ end
44
+
45
+ Bridgetown::Rack::Routes.tracked_subclasses&.each_value do |klass|
46
+ klass.merge roda_app
47
+ end
48
+
49
+ if defined?(Bridgetown::Routes::RodaRouter)
50
+ Bridgetown::Routes::RodaRouter.start!(roda_app)
51
+ end
52
+
53
+ nil
54
+ end
55
+
56
+ def setup_live_reload(request)
57
+ request.get "_bridgetown/live_reload" do
58
+ {
59
+ last_mod: File.stat(
60
+ File.join(Bridgetown::Current.preloaded_configuration.destination, "index.html")
61
+ ).mtime.to_i,
62
+ }
63
+ rescue StandardError => e
64
+ { last_mod: 0, error: e.message }
65
+ end
66
+ end
67
+ end
68
+
69
+ def initialize(roda_app)
70
+ @_roda_app = roda_app
71
+ end
72
+
73
+ def handle_routes
74
+ instance_exec(@_roda_app.request, &self.class.router_block)
75
+ end
76
+
77
+ # rubocop:disable Style/MissingRespondToMissing
78
+ ruby2_keywords def method_missing(method_name, *args, &block)
79
+ if @_roda_app.respond_to?(method_name.to_sym)
80
+ @_roda_app.send method_name.to_sym, *args, &block
81
+ else
82
+ super
83
+ end
84
+ end
85
+
86
+ def respond_to_missing?(method_name, include_private = false)
87
+ @_roda_app.respond_to?(method_name.to_sym, include_private) || super
88
+ end
89
+ # rubocop:enable Style/MissingRespondToMissing
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "roda/plugins/public"
4
+
5
+ Roda::RodaPlugins::Public::RequestMethods.module_eval do
6
+ SPLIT = Regexp.union(*[File::SEPARATOR, File::ALT_SEPARATOR].compact)
7
+ def public_path_segments(path) # rubocop:disable Metrics/CyclomaticComplexity
8
+ segments = []
9
+
10
+ path.split(SPLIT).each do |seg|
11
+ next if seg.empty? || seg == "."
12
+
13
+ seg == ".." ? segments.pop : segments << seg
14
+ end
15
+
16
+ path = ::File.join(roda_class.opts[:public_root], *segments)
17
+ unless ::File.file?(path)
18
+ path = ::File.join(path, "index.html")
19
+ if ::File.file?(path)
20
+ segments << "index.html"
21
+ else
22
+ segments[segments.size - 1] = "#{segments.last}.html"
23
+ end
24
+ end
25
+
26
+ segments
27
+ rescue IndexError
28
+ nil
29
+ end
30
+ end