bridgetown-core 0.21.1 → 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) 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 +16 -117
  18. data/lib/bridgetown-core/concerns/site/localizable.rb +3 -1
  19. data/lib/bridgetown-core/concerns/site/processable.rb +9 -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/errors.rb +0 -2
  34. data/lib/bridgetown-core/filters/url_filters.rb +3 -1
  35. data/lib/bridgetown-core/frontmatter_defaults.rb +52 -88
  36. data/lib/bridgetown-core/{page.rb → generated_page.rb} +34 -58
  37. data/lib/bridgetown-core/generators/prototype_generator.rb +13 -24
  38. data/lib/bridgetown-core/helpers.rb +7 -2
  39. data/lib/bridgetown-core/layout.rb +15 -4
  40. data/lib/bridgetown-core/log_writer.rb +6 -0
  41. data/lib/bridgetown-core/model/base.rb +10 -2
  42. data/lib/bridgetown-core/model/builder_origin.rb +22 -10
  43. data/lib/bridgetown-core/model/origin.rb +3 -0
  44. data/lib/bridgetown-core/model/plugin_origin.rb +34 -0
  45. data/lib/bridgetown-core/model/repo_origin.rb +1 -1
  46. data/lib/bridgetown-core/plugin_manager.rb +8 -8
  47. data/lib/bridgetown-core/rack/boot.rb +47 -0
  48. data/lib/bridgetown-core/rack/logger.rb +22 -0
  49. data/lib/bridgetown-core/rack/roda.rb +66 -0
  50. data/lib/bridgetown-core/rack/routes.rb +92 -0
  51. data/lib/bridgetown-core/rack/static_indexes.rb +30 -0
  52. data/lib/bridgetown-core/reader.rb +20 -47
  53. data/lib/bridgetown-core/readers/plugin_content_reader.rb +8 -7
  54. data/lib/bridgetown-core/renderer.rb +1 -11
  55. data/lib/bridgetown-core/resource/base.rb +50 -26
  56. data/lib/bridgetown-core/resource/permalink_processor.rb +20 -10
  57. data/lib/bridgetown-core/resource/relations.rb +2 -3
  58. data/lib/bridgetown-core/resource/transformer.rb +1 -1
  59. data/lib/bridgetown-core/ruby_template_view.rb +5 -5
  60. data/lib/bridgetown-core/site.rb +4 -8
  61. data/lib/bridgetown-core/static_file.rb +10 -15
  62. data/lib/bridgetown-core/tags/include.rb +0 -13
  63. data/lib/bridgetown-core/tags/link.rb +4 -0
  64. data/lib/bridgetown-core/tags/live_reload_dev_js.rb +13 -0
  65. data/lib/bridgetown-core/tags/post_url.rb +4 -9
  66. data/lib/bridgetown-core/tasks/bridgetown_tasks.rake +54 -0
  67. data/lib/bridgetown-core/url.rb +2 -1
  68. data/lib/bridgetown-core/utils/aux.rb +57 -0
  69. data/lib/bridgetown-core/utils/ruby_exec.rb +3 -45
  70. data/lib/bridgetown-core/utils/ruby_front_matter.rb +22 -7
  71. data/lib/bridgetown-core/utils.rb +53 -24
  72. data/lib/bridgetown-core/version.rb +2 -2
  73. data/lib/bridgetown-core/watcher.rb +2 -4
  74. data/lib/bridgetown-core.rb +14 -22
  75. data/lib/site_template/Gemfile.erb +6 -2
  76. data/lib/site_template/README.md +6 -6
  77. data/lib/site_template/Rakefile +49 -0
  78. data/lib/site_template/bridgetown.config.yml +2 -3
  79. data/lib/site_template/config/puma.rb +27 -0
  80. data/lib/site_template/config.ru +7 -0
  81. data/lib/site_template/frontend/javascript/index.js.erb +3 -3
  82. data/lib/site_template/package.json.erb +8 -13
  83. data/lib/site_template/server/roda_app.rb +22 -0
  84. data/lib/site_template/server/routes/hello.rb.sample +10 -0
  85. data/lib/site_template/src/_components/head.liquid +2 -1
  86. data/lib/site_template/src/about.md +0 -1
  87. data/lib/site_template/src/posts.md +2 -3
  88. metadata +62 -18
  89. data/lib/bridgetown-core/concerns/data_accessible.rb +0 -20
  90. data/lib/bridgetown-core/concerns/validatable.rb +0 -56
  91. data/lib/bridgetown-core/document.rb +0 -437
  92. data/lib/bridgetown-core/drops/document_drop.rb +0 -80
  93. data/lib/bridgetown-core/drops/excerpt_drop.rb +0 -19
  94. data/lib/bridgetown-core/drops/page_drop.rb +0 -18
  95. data/lib/bridgetown-core/excerpt.rb +0 -200
  96. data/lib/bridgetown-core/readers/data_reader.rb +0 -89
  97. data/lib/bridgetown-core/readers/page_reader.rb +0 -26
  98. data/lib/bridgetown-core/readers/post_reader.rb +0 -109
  99. data/lib/bridgetown-core/regenerator.rb +0 -202
  100. data/lib/bridgetown-core/related_posts.rb +0 -55
  101. data/lib/site_template/config/.keep +0 -0
  102. data/lib/site_template/start.js +0 -17
  103. data/lib/site_template/sync.js +0 -35
@@ -1,16 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bridgetown
4
- # TODO: to be retired once the Resource engine is made official
5
- class Page
6
- include DataAccessible
4
+ class GeneratedPage
7
5
  include LayoutPlaceable
8
6
  include LiquidRenderable
9
7
  include Publishable
10
- include Validatable
11
8
 
12
9
  attr_writer :dir
13
- attr_accessor :site, :paginator, :pager
10
+ attr_accessor :site, :paginator
14
11
  attr_accessor :name, :ext, :basename
15
12
  attr_accessor :data, :content, :output
16
13
 
@@ -25,7 +22,7 @@ module Bridgetown
25
22
  .htm
26
23
  ).freeze
27
24
 
28
- # Initialize a new Page.
25
+ # Initialize a new GeneratedPage.
29
26
  #
30
27
  # site - The Site object.
31
28
  # base - The String path to the source.
@@ -38,23 +35,36 @@ module Bridgetown
38
35
  @base = base
39
36
  @dir = dir
40
37
  @name = name
38
+ @ext = File.extname(name)
39
+ @basename = File.basename(name, ".*")
41
40
  @path = if from_plugin
42
41
  File.join(base, dir, name)
43
42
  else
44
43
  site.in_source_dir(base, dir, name)
45
44
  end
46
45
 
47
- process(name)
48
- read_yaml(File.join(base, dir), name)
46
+ process
49
47
 
50
- data.default_proc = proc do |_, key|
51
- site.frontmatter_defaults.find(relative_path, type, key.to_s)
52
- end
48
+ self.data ||= HashWithDotAccess::Hash.new
53
49
 
54
- Bridgetown::Hooks.trigger :pages, :post_init, self
50
+ Bridgetown::Hooks.trigger :generated_pages, :post_init, self
55
51
  end
56
52
  # rubocop:enable Metrics/ParameterLists
57
53
 
54
+ # Returns the contents as a String.
55
+ def to_s
56
+ output || content || ""
57
+ end
58
+
59
+ # Accessor for data properties by Liquid.
60
+ #
61
+ # property - The String name of the property to retrieve.
62
+ #
63
+ # Returns the String value or nil if the property isn't included.
64
+ def [](property)
65
+ data[property]
66
+ end
67
+
58
68
  # The generated directory into which the page will be placed
59
69
  # upon generation. This is derived from the permalink or, if
60
70
  # permalink is absent, will be '/'
@@ -69,29 +79,15 @@ module Bridgetown
69
79
  end
70
80
  end
71
81
 
72
- def liquid_drop
73
- @liquid_drop ||= begin
74
- defaults = site.frontmatter_defaults.all(relative_path, type)
75
- unless defaults.empty?
76
- Utils.deep_merge_hashes!(data, Utils.deep_merge_hashes!(defaults, data))
77
- end
78
- Drops::PageDrop.new(self)
79
- end
80
- end
81
-
82
- # Public
83
- #
84
82
  # Liquid representation of current page
85
83
  def to_liquid
86
- liquid_drop
84
+ @liquid_drop ||= Drops::GeneratedPageDrop.new(self)
87
85
  end
88
86
 
89
87
  # The full path and filename of the post. Defined in the YAML of the post
90
- # body.
91
- #
92
- # Returns the String permalink or nil if none has been set.
88
+ # body
93
89
  def permalink
94
- data.nil? ? nil : data["permalink"]
90
+ data&.permalink
95
91
  end
96
92
 
97
93
  # The template of the permalink.
@@ -123,33 +119,20 @@ module Bridgetown
123
119
  # desired placeholder replacements. For details see "url.rb"
124
120
  def url_placeholders
125
121
  {
126
- path: qualified_pages_path_for_url,
127
- basename: basename,
122
+ path: @dir,
123
+ basename: @basename,
128
124
  output_ext: output_ext,
129
125
  }
130
126
  end
131
127
 
132
- # Strips _pages prefix off if needed for the url/destination generation
133
- # @return [String]
134
- def qualified_pages_path_for_url
135
- @dir.sub(%r!^/_pages!, "")
136
- end
137
-
138
- # Extract information from the page filename.
139
- #
140
- # name - The String filename of the page file.
141
- #
142
- # NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`)
143
- # Returns nothing.
144
- def process(name)
145
- self.ext = File.extname(name)
146
- self.basename = name[0..-ext.length - 1].gsub(%r!\.*\z!, "")
128
+ # Overide this in subclasses for custom initialization behavior
129
+ def process
130
+ # no-op by default
147
131
  end
148
132
 
149
133
  # The path to the source file
150
- #
151
- # Returns the path to the source file
152
134
  def path
135
+ # TODO: is this trip really necessary?!
153
136
  data.fetch("path") { relative_path }
154
137
  end
155
138
 
@@ -190,7 +173,7 @@ module Bridgetown
190
173
  FileUtils.mkdir_p(File.dirname(path))
191
174
  Bridgetown.logger.debug "Writing:", path
192
175
  File.write(path, output, mode: "wb")
193
- Bridgetown::Hooks.trigger :pages, :post_write, self
176
+ Bridgetown::Hooks.trigger :generated_pages, :post_write, self
194
177
  end
195
178
 
196
179
  # Returns the object as a debug String.
@@ -209,22 +192,15 @@ module Bridgetown
209
192
  end
210
193
 
211
194
  def trigger_hooks(hook_name, *args)
212
- Bridgetown::Hooks.trigger :pages, hook_name, self, *args
195
+ Bridgetown::Hooks.trigger :generated_pages, hook_name, self, *args
213
196
  end
214
197
 
215
198
  def type
216
- :pages
199
+ :generated_pages
217
200
  end
218
201
 
219
202
  def write?
220
203
  true
221
204
  end
222
205
  end
223
-
224
- # set up virtual page class for future compatibility
225
- class GeneratedPage < Page
226
- def read_yaml(_base, _name, _opts = {})
227
- self.data ||= HashWithDotAccess::Hash.new
228
- end
229
- end
230
206
  end
@@ -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,16 +73,16 @@ 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)
81
80
  # @type [String]
82
- search_term = prototype_page.data["prototype"]["term"]
83
- return nil unless search_term.is_a?(String)
81
+ search_term = prototype_page.data["prototype"]["term"].to_s
82
+ return nil unless search_term.present?
84
83
 
85
84
  if prototype_page.data["prototype"]["collection"]
86
- @configured_collection = prototype_page.data["prototype"]["collection"]
85
+ @configured_collection = prototype_page.data["prototype"]["collection"].to_s
87
86
  end
88
87
 
89
88
  unless site.collections[@configured_collection]
@@ -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|
@@ -173,16 +172,17 @@ module Bridgetown
173
172
  # source components _before_ we load any from plugins
174
173
  site.components_load_paths.reverse_each do |load_path|
175
174
  next unless Dir.exist? load_path
176
- next if Zeitwerk::Registry.loaders.find { |loader| loader.manages?(load_path) }
177
175
 
178
- @component_loaders[load_path] = Zeitwerk::Loader.new
179
- @component_loaders[load_path].push_dir(load_path)
180
- @component_loaders[load_path].enable_reloading if load_path.start_with?(site.root_dir)
181
- @component_loaders[load_path].ignore(File.join(load_path, "**", "*.js.rb"))
182
- @component_loaders[load_path].setup
176
+ begin
177
+ @component_loaders[load_path] = Zeitwerk::Loader.new
178
+ @component_loaders[load_path].push_dir(load_path)
179
+ @component_loaders[load_path].enable_reloading if load_path.start_with?(site.root_dir)
180
+ @component_loaders[load_path].ignore(File.join(load_path, "**", "*.js.rb"))
181
+ @component_loaders[load_path].setup
182
+ rescue Zeitwerk::Error # rubocop:disable Lint/SuppressedException
183
+ end
183
184
  end
184
185
  end
185
- # rubocop:enable Metrics/AbcSize
186
186
 
187
187
  def reload_component_loaders
188
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