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
@@ -65,7 +65,7 @@ create_file "test/test_homepage.rb" do
65
65
  class TestHomepage < Minitest::Test
66
66
  context "homepage" do
67
67
  setup do
68
- page = site.pages.find { |doc| doc.url == "/" }
68
+ page = site.collections.pages.resources.find { |doc| doc.relative_url == "/" }
69
69
  document_root page
70
70
  end
71
71
  should "exist" do
@@ -13,4 +13,4 @@ javascript_import do
13
13
  end
14
14
 
15
15
  say "Turbo successfully added", :green
16
- say 'For further reading, check out "https://turbo.hotwire.dev/"', :blue
16
+ say 'For further reading, check out "https://turbo.hotwired.dev/"', :blue
@@ -104,7 +104,8 @@ module Bridgetown
104
104
  # Logic to do the ERB content conversion.
105
105
  #
106
106
  # @param content [String] Content of the file (without front matter).
107
- # @param convertible [Bridgetown::Page, Bridgetown::Document, Bridgetown::Layout]
107
+ # @param convertible [
108
+ # Bridgetown::GeneratedPage, Bridgetown::Resource::Base, Bridgetown::Layout]
108
109
  # The instantiated object which is processing the file.
109
110
  #
110
111
  # @return [String] The converted content.
@@ -18,7 +18,8 @@ module Bridgetown
18
18
  # Logic to do the Liquid content conversion.
19
19
  #
20
20
  # @param content [String] Content of the file (without front matter).
21
- # @param convertible [Bridgetown::Page, Bridgetown::Document, Bridgetown::Layout]
21
+ # @param convertible [
22
+ # Bridgetown::GeneratedPage, Bridgetown::Resource::Base, Bridgetown::Layout]
22
23
  # The instantiated object which is processing the file.
23
24
  #
24
25
  # @return [String] The converted content.
@@ -84,7 +85,7 @@ module Bridgetown
84
85
  def configure_payload(content = nil)
85
86
  payload["page"] = document.to_liquid
86
87
  payload["paginator"] = document.respond_to?(:paginator) ? document.paginator.to_liquid : nil
87
- payload["layout"] = @layout ? @layout.data : {}
88
+ payload["layout"] = @layout ? @layout.to_liquid.merge({ data: @layout.data }) : {}
88
89
  payload["content"] = content
89
90
  end
90
91
 
@@ -6,5 +6,9 @@ module Bridgetown
6
6
  # @return [Bridgetown::Site]
7
7
 
8
8
  attribute :site
9
+
10
+ # @!method self.preloaded_configuration
11
+ # @return [Bridgetown::Configuration]
12
+ attribute :preloaded_configuration
9
13
  end
10
14
  end
@@ -8,7 +8,7 @@ module Bridgetown
8
8
  mutable false
9
9
 
10
10
  def_delegator :@obj, :write?, :output
11
- def_delegators :@obj, :label, :docs, :files, :relative_path, :resources, :static_files
11
+ def_delegators :@obj, :label, :files, :relative_path, :resources, :static_files
12
12
 
13
13
  private def_delegator :@obj, :metadata, :fallback_data
14
14
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class GeneratedPageDrop < Drop
6
+ extend Forwardable
7
+
8
+ mutable false
9
+
10
+ def_delegators :@obj,
11
+ :data,
12
+ :content,
13
+ :dir,
14
+ :name,
15
+ :path,
16
+ :url,
17
+ :relative_url,
18
+ :relative_path
19
+
20
+ private def_delegator :@obj, :data, :fallback_data
21
+ end
22
+ end
23
+ end
@@ -11,7 +11,6 @@ module Bridgetown
11
11
 
12
12
  mutable false
13
13
 
14
- def_delegator :@obj, :relative_path, :path
15
14
  def_delegators :@obj,
16
15
  :id,
17
16
  :data,
@@ -20,6 +19,7 @@ module Bridgetown
20
19
  :summary,
21
20
  :to_s,
22
21
  :absolute_url,
22
+ :path,
23
23
  :relative_path,
24
24
  :relative_url,
25
25
  :date,
@@ -32,8 +32,8 @@ module Bridgetown
32
32
  @collection ||= @obj.collection.to_liquid
33
33
  end
34
34
 
35
- def excerpt
36
- fallback_data["excerpt"].to_s
35
+ def relative_path
36
+ @relative_path ||= @obj.relative_path.to_s
37
37
  end
38
38
 
39
39
  def <=>(other)
@@ -13,7 +13,6 @@ module Bridgetown
13
13
  :data,
14
14
  :locale,
15
15
  :time,
16
- :pages,
17
16
  :generated_pages,
18
17
  :static_files,
19
18
  :tags,
@@ -25,51 +24,10 @@ module Bridgetown
25
24
 
26
25
  attr_writer :current_document
27
26
 
28
- def [](key)
29
- if !@obj.uses_resource? && !%w(posts data).freeze.include?(key) &&
30
- @obj.collections.key?(key)
31
- return @obj.collections[key].docs
32
- end
33
-
34
- super(key)
35
- end
36
-
37
- def key?(key)
38
- (!@obj.uses_resource? && key != "posts" && @obj.collections.key?(key)) || super
39
- end
40
-
41
27
  def uses_resource
42
28
  @obj.uses_resource?
43
29
  end
44
30
 
45
- def posts
46
- unless @obj.uses_resource?
47
- @site_posts ||= @obj.collections.posts.docs.sort { |a, b| b <=> a }
48
- end
49
- end
50
-
51
- # TODO: deprecate before v1.0
52
- def html_pages
53
- @site_html_pages ||= @obj.pages.select do |page|
54
- page.html? || page.url.end_with?("/")
55
- end
56
- end
57
-
58
- # TODO: deprecate before v1.0
59
- def collections
60
- @site_collections ||= @obj.collections.values.sort_by(&:label).map(&:to_liquid)
61
- end
62
-
63
- # `Site#documents` cannot be memoized so that `Site#docs_to_write` can access the
64
- # latest state of the attribute.
65
- #
66
- # Since this method will be called after `Site#pre_render` hook, the `Site#documents`
67
- # array shouldn't thereafter change and can therefore be safely memoized to prevent
68
- # additional computation of `Site#documents`.
69
- def documents
70
- @documents ||= @obj.documents
71
- end
72
-
73
31
  def resources
74
32
  @resources ||= @obj.resources
75
33
  end
@@ -82,11 +40,9 @@ module Bridgetown
82
40
  @site_metadata ||= @obj.data["site_metadata"]
83
41
  end
84
42
 
85
- # TODO: change this so you *do* use site.config...aka site.config.timezone,
86
- # not site.timezone
87
- #
88
- # return nil for `{{ site.config }}` even if --config was passed via CLI
89
- def config; end
43
+ def config
44
+ @obj.config
45
+ end
90
46
  end
91
47
  end
92
48
  end
@@ -49,8 +49,9 @@ module Bridgetown
49
49
 
50
50
  private
51
51
 
52
- def compute_absolute_url(input)
52
+ def compute_absolute_url(input) # rubocop:todo Metrics/CyclomaticComplexity
53
53
  return if input.nil?
54
+ return input.absolute_url if input.respond_to?(:absolute_url)
54
55
 
55
56
  input = input.url if input.respond_to?(:url)
56
57
  return input if Addressable::URI.parse(input.to_s).absolute?
@@ -66,6 +67,7 @@ module Bridgetown
66
67
 
67
68
  def compute_relative_url(input)
68
69
  return if input.nil?
70
+ return input.relative_url if input.respond_to?(:relative_url)
69
71
 
70
72
  input = input.url if input.respond_to?(:url)
71
73
  return input if Addressable::URI.parse(input.to_s).absolute?
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bridgetown
4
- # This class handles custom defaults for YAML frontmatter variables.
4
+ # This class handles custom defaults for front matter settings.
5
5
  # It is exposed via the frontmatter_defaults method on the site class.
6
- # TODO: needs simplification/refactoring.
7
6
  class FrontmatterDefaults
8
7
  # @return [Bridgetown::Site]
9
8
  attr_reader :site
@@ -16,24 +15,6 @@ module Bridgetown
16
15
  @glob_cache = {}
17
16
  end
18
17
 
19
- def update_deprecated_types(set)
20
- return set unless set.key?("scope") && set["scope"].key?("type")
21
-
22
- set["scope"]["type"] =
23
- case set["scope"]["type"]
24
- when "page"
25
- Deprecator.defaults_deprecate_type("page", "pages")
26
- "pages"
27
- when "post"
28
- Deprecator.defaults_deprecate_type("post", "posts")
29
- "posts"
30
- else
31
- set["scope"]["type"]
32
- end
33
-
34
- set
35
- end
36
-
37
18
  def ensure_time!(set)
38
19
  return set unless set.key?("values") && set["values"].key?("date")
39
20
  return set if set["values"]["date"].is_a?(Time)
@@ -45,44 +26,19 @@ module Bridgetown
45
26
  set
46
27
  end
47
28
 
48
- # TODO: deprecated. See `all` method instead
29
+ # Collects a hash with all default values for a resource
49
30
  #
50
- # path - the path (relative to the source) of the page or
51
- # post the default is used in
52
- # type - a symbol indicating whether a :page,
53
- # a :post calls this method
31
+ # @param path [String] the relative path of the resource
32
+ # @param collection [Symbol] :posts, :pages, etc.
54
33
  #
55
- # Returns the default value or nil if none was found
56
- def find(path, type, setting)
57
- merged_data = {}
58
- merge_data_cascade_for_path(path, merged_data)
59
- return merged_data[setting] if merged_data.key?(setting)
60
-
61
- value = nil
62
- old_scope = nil
63
-
64
- matching_sets(path, type).each do |set|
65
- if set["values"].key?(setting) && has_precedence?(old_scope, set["scope"])
66
- value = set["values"][setting]
67
- old_scope = set["scope"]
68
- end
69
- end
70
- value
71
- end
72
-
73
- # Collects a hash with all default values for a page or post
74
- #
75
- # path - the relative path of the page or post
76
- # type - a symbol indicating the type (:post or :page)
77
- #
78
- # Returns a hash with all default values (an empty hash if there are none)
79
- def all(path, type)
34
+ # @returns [Hash] all default values (an empty hash if there are none)
35
+ def all(path, collection)
80
36
  defaults = {}
81
37
 
82
38
  merge_data_cascade_for_path(path, defaults)
83
39
 
84
40
  old_scope = nil
85
- matching_sets(path, type).each do |set|
41
+ matching_sets(path, collection).each do |set|
86
42
  if has_precedence?(old_scope, set["scope"])
87
43
  defaults = Utils.deep_merge_hashes(defaults, set["values"])
88
44
  old_scope = set["scope"]
@@ -105,22 +61,22 @@ module Bridgetown
105
61
  end
106
62
  end
107
63
 
108
- # Checks if a given default setting scope matches the given path and type
64
+ # Checks if a given default setting scope matches the given path and collection
109
65
  #
110
66
  # scope - the hash indicating the scope, as defined in bridgetown.config.yml
111
67
  # path - the path to check for
112
- # type - the type (:post or :page) to check for
68
+ # collection - the collection (:posts or :pages) to check for
113
69
  #
114
- # Returns true if the scope applies to the given type and path
115
- def applies?(scope, path, type)
116
- applies_type?(scope, type) && applies_path?(scope, path)
70
+ # Returns true if the scope applies to the given collection and path
71
+ def applies?(scope, path, collection)
72
+ applies_collection?(scope, collection) && applies_path?(scope, path)
117
73
  end
118
74
 
119
75
  def applies_path?(scope, path)
120
76
  rel_scope_path = scope["path"]
121
77
  return true if !rel_scope_path.is_a?(String) || rel_scope_path.empty?
122
78
 
123
- sanitized_path = sanitize_path(path)
79
+ sanitized_path = strip_collections_dir(sanitize_path(path))
124
80
 
125
81
  if rel_scope_path.include?("*")
126
82
  glob_scope(sanitized_path, rel_scope_path)
@@ -159,26 +115,24 @@ module Bridgetown
159
115
  path.sub(slashed_coll_dir, "")
160
116
  end
161
117
 
162
- # Determines whether the scope applies to type.
163
- # The scope applies to the type if:
164
- # 1. no 'type' is specified
165
- # 2. the 'type' in the scope is the same as the type asked about
118
+ # Determines whether the scope applies to collection.
119
+ # The scope applies to the collection if:
120
+ # 1. no 'collection' is specified
121
+ # 2. the 'collection' in the scope is the same as the collection asked about
166
122
  #
167
- # scope - the Hash defaults set being asked about application
168
- # type - the type of the document being processed / asked about
169
- # its defaults.
123
+ # @param scope [Hash] the defaults set being asked about
124
+ # @param collection [Symbol] the collection of the resource being processed
170
125
  #
171
- # Returns true if either of the above conditions are satisfied,
172
- # otherwise returns false
173
- def applies_type?(scope, type)
174
- !scope.key?("type") || scope["type"].eql?(type.to_s)
126
+ # @returns [Boolean] whether either of the above conditions are satisfied
127
+ def applies_collection?(scope, collection)
128
+ !scope.key?("collection") || scope["collection"].eql?(collection.to_s)
175
129
  end
176
130
 
177
131
  # Checks if a given set of default values is valid
178
132
  #
179
- # set - the default value hash, as defined in bridgetown.config.yml
133
+ # @param set [Hash] the default value hash as defined in bridgetown.config.yml
180
134
  #
181
- # Returns true if the set is valid and can be used in this class
135
+ # @returns [Boolean] if the set is valid and can be used
182
136
  def valid?(set)
183
137
  set.is_a?(Hash) && set["values"].is_a?(Hash)
184
138
  end
@@ -198,23 +152,23 @@ module Bridgetown
198
152
 
199
153
  if new_path.length != old_path.length
200
154
  new_path.length >= old_path.length
201
- elsif new_scope.key?("type")
155
+ elsif new_scope.key?("collection")
202
156
  true
203
157
  else
204
- !old_scope.key? "type"
158
+ !old_scope.key? "collection"
205
159
  end
206
160
  end
207
161
  # rubocop: enable Naming/PredicateName
208
162
 
209
- # Collects a list of sets that match the given path and type
163
+ # Collects a list of sets that match the given path and collection
210
164
  #
211
- # Returns an array of hashes
212
- def matching_sets(path, type)
165
+ # @return [Array<Hash>]
166
+ def matching_sets(path, collection)
213
167
  @matched_set_cache ||= {}
214
168
  @matched_set_cache[path] ||= {}
215
- @matched_set_cache[path][type] ||= begin
169
+ @matched_set_cache[path][collection] ||= begin
216
170
  valid_sets.select do |set|
217
- !set.key?("scope") || applies?(set["scope"], path, type)
171
+ !set.key?("scope") || applies?(set["scope"], path, collection)
218
172
  end
219
173
  end
220
174
  end
@@ -224,14 +178,16 @@ module Bridgetown
224
178
  # This is not cached to allow plugins to modify the configuration
225
179
  # and have their changes take effect
226
180
  #
227
- # Returns an array of hashes
181
+ # @return [Array<Hash>]
228
182
  def valid_sets
229
183
  sets = site.config["defaults"]
230
184
  return [] unless sets.is_a?(Array)
231
185
 
232
186
  sets.map do |set|
233
187
  if valid?(set)
234
- ensure_time!(update_deprecated_types(set))
188
+ massage_scope!(set)
189
+ # TODO: is this trip really necessary?
190
+ ensure_time!(set)
235
191
  else
236
192
  Bridgetown.logger.warn "Defaults:", "An invalid front-matter default set was found:"
237
193
  Bridgetown.logger.warn set.to_s
@@ -240,10 +196,18 @@ module Bridgetown
240
196
  end.compact
241
197
  end
242
198
 
243
- # Sanitizes the given path by removing a leading and adding a trailing slash
199
+ # Set path to blank if not specified and alias older type to collection
200
+ def massage_scope!(set)
201
+ set["scope"] ||= {}
202
+ set["scope"]["path"] ||= ""
203
+ if set["scope"]["type"] && !set["scope"]["collection"]
204
+ set["scope"]["collection"] = set["scope"]["type"]
205
+ end
206
+ end
244
207
 
245
208
  SANITIZATION_REGEX = %r!\A/|(?<=[^/])\z!.freeze
246
209
 
210
+ # Sanitizes the given path by removing a leading and adding a trailing slash
247
211
  def sanitize_path(path)
248
212
  if path.nil? || path.empty?
249
213
  ""
@@ -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