bridgetown-core 0.20.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/bridgetown-core.gemspec +0 -1
  3. data/lib/bridgetown-core.rb +11 -22
  4. data/lib/bridgetown-core/cleaner.rb +2 -2
  5. data/lib/bridgetown-core/collection.rb +14 -11
  6. data/lib/bridgetown-core/commands/build.rb +0 -11
  7. data/lib/bridgetown-core/commands/concerns/git_helpers.rb +20 -0
  8. data/lib/bridgetown-core/commands/configure.rb +4 -3
  9. data/lib/bridgetown-core/commands/doctor.rb +1 -19
  10. data/lib/bridgetown-core/commands/new.rb +6 -6
  11. data/lib/bridgetown-core/commands/plugins.rb +14 -13
  12. data/lib/bridgetown-core/commands/serve.rb +0 -14
  13. data/lib/bridgetown-core/commands/webpack.rb +75 -0
  14. data/lib/bridgetown-core/commands/webpack/enable-postcss.rb +12 -0
  15. data/lib/bridgetown-core/commands/webpack/setup.rb +4 -0
  16. data/lib/bridgetown-core/commands/webpack/update.rb +3 -0
  17. data/lib/bridgetown-core/commands/webpack/webpack.config.js +18 -0
  18. data/lib/{site_template/webpack.config.js.erb → bridgetown-core/commands/webpack/webpack.defaults.js.erb} +25 -11
  19. data/lib/bridgetown-core/component.rb +183 -0
  20. data/lib/bridgetown-core/concerns/front_matter_importer.rb +52 -0
  21. data/lib/bridgetown-core/concerns/layout_placeable.rb +1 -1
  22. data/lib/bridgetown-core/concerns/site/configurable.rb +3 -7
  23. data/lib/bridgetown-core/concerns/site/content.rb +2 -3
  24. data/lib/bridgetown-core/concerns/site/localizable.rb +3 -5
  25. data/lib/bridgetown-core/concerns/site/processable.rb +5 -4
  26. data/lib/bridgetown-core/concerns/site/writable.rb +1 -1
  27. data/lib/bridgetown-core/concerns/validatable.rb +1 -5
  28. data/lib/bridgetown-core/configuration.rb +22 -14
  29. data/lib/bridgetown-core/configurations/bt-postcss.rb +6 -6
  30. data/lib/bridgetown-core/configurations/netlify.rb +1 -0
  31. data/lib/bridgetown-core/configurations/tailwindcss.rb +6 -6
  32. data/lib/bridgetown-core/converter.rb +9 -0
  33. data/lib/bridgetown-core/converters/erb_templates.rb +51 -35
  34. data/lib/bridgetown-core/converters/liquid_templates.rb +1 -1
  35. data/lib/bridgetown-core/converters/markdown.rb +1 -1
  36. data/lib/bridgetown-core/converters/markdown/kramdown_parser.rb +2 -38
  37. data/lib/bridgetown-core/converters/ruby_templates.rb +17 -0
  38. data/lib/bridgetown-core/converters/smartypants.rb +3 -1
  39. data/lib/bridgetown-core/core_ext/psych.rb +19 -0
  40. data/lib/bridgetown-core/document.rb +3 -2
  41. data/lib/bridgetown-core/drops/relations_drop.rb +23 -0
  42. data/lib/bridgetown-core/drops/resource_drop.rb +3 -1
  43. data/lib/bridgetown-core/drops/unified_payload_drop.rb +1 -0
  44. data/lib/bridgetown-core/entry_filter.rb +7 -5
  45. data/lib/bridgetown-core/filters.rb +1 -25
  46. data/lib/bridgetown-core/filters/from_liquid.rb +23 -0
  47. data/lib/bridgetown-core/filters/url_filters.rb +12 -0
  48. data/lib/bridgetown-core/frontmatter_defaults.rb +1 -1
  49. data/lib/bridgetown-core/generators/prototype_generator.rb +25 -4
  50. data/lib/bridgetown-core/helpers.rb +48 -9
  51. data/lib/bridgetown-core/layout.rb +52 -20
  52. data/lib/bridgetown-core/model/origin.rb +1 -1
  53. data/lib/bridgetown-core/model/{file_origin.rb → repo_origin.rb} +33 -26
  54. data/lib/bridgetown-core/page.rb +2 -1
  55. data/lib/bridgetown-core/plugin_manager.rb +1 -1
  56. data/lib/bridgetown-core/publisher.rb +2 -2
  57. data/lib/bridgetown-core/reader.rb +13 -11
  58. data/lib/bridgetown-core/readers/data_reader.rb +2 -1
  59. data/lib/bridgetown-core/readers/defaults_reader.rb +1 -1
  60. data/lib/bridgetown-core/readers/layout_reader.rb +1 -1
  61. data/lib/bridgetown-core/readers/page_reader.rb +1 -0
  62. data/lib/bridgetown-core/readers/post_reader.rb +5 -4
  63. data/lib/bridgetown-core/regenerator.rb +1 -1
  64. data/lib/bridgetown-core/related_posts.rb +8 -5
  65. data/lib/bridgetown-core/renderer.rb +1 -1
  66. data/lib/bridgetown-core/resource/base.rb +80 -26
  67. data/lib/bridgetown-core/resource/permalink_processor.rb +1 -1
  68. data/lib/bridgetown-core/resource/relations.rb +132 -0
  69. data/lib/bridgetown-core/resource/taxonomy_term.rb +10 -1
  70. data/lib/bridgetown-core/resource/taxonomy_type.rb +9 -0
  71. data/lib/bridgetown-core/resource/transformer.rb +18 -14
  72. data/lib/bridgetown-core/ruby_template_view.rb +7 -11
  73. data/lib/bridgetown-core/site.rb +4 -5
  74. data/lib/bridgetown-core/tags/highlight.rb +2 -15
  75. data/lib/bridgetown-core/tags/include.rb +1 -1
  76. data/lib/bridgetown-core/tags/post_url.rb +2 -2
  77. data/lib/bridgetown-core/utils.rb +9 -3
  78. data/lib/bridgetown-core/utils/require_gems.rb +60 -0
  79. data/lib/bridgetown-core/utils/ruby_exec.rb +6 -9
  80. data/lib/bridgetown-core/utils/ruby_front_matter.rb +39 -0
  81. data/lib/bridgetown-core/version.rb +2 -2
  82. data/lib/bridgetown-core/watcher.rb +2 -1
  83. data/lib/bridgetown-core/yaml_parser.rb +22 -0
  84. data/lib/site_template/config/.keep +0 -0
  85. data/lib/site_template/package.json.erb +4 -4
  86. data/lib/site_template/plugins/site_builder.rb +1 -1
  87. data/lib/site_template/src/_posts/0000-00-00-welcome-to-bridgetown.md.erb +1 -1
  88. metadata +21 -23
  89. data/lib/bridgetown-core/external.rb +0 -58
  90. data/lib/bridgetown-core/path_manager.rb +0 -31
  91. data/lib/bridgetown-core/readers/static_file_reader.rb +0 -25
  92. data/lib/bridgetown-core/utils/platforms.rb +0 -81
  93. data/lib/bridgetown-core/utils/thread_event.rb +0 -31
@@ -52,7 +52,7 @@ module Bridgetown
52
52
  output = document.content
53
53
  Bridgetown.logger.debug "Rendering Markup:", document.relative_path
54
54
  output = convert(output.to_s, document)
55
- document.content = output
55
+ document.content = output.html_safe
56
56
 
57
57
  if document.place_in_layout?
58
58
  Bridgetown.logger.debug "Rendering Layout:", document.relative_path
@@ -23,13 +23,6 @@ module Bridgetown
23
23
  # @return [String]
24
24
  attr_accessor :content, :untransformed_content, :output
25
25
 
26
- # @!attribute [r] collection
27
- # @return [Bridgetown::Collection] collection of this resource
28
-
29
- # @!attribute [r] relative_path
30
- # @return [Pathname] the relative path of source file or
31
- # file-like origin
32
-
33
26
  DATE_FILENAME_MATCHER = %r!^(?>.+/)*?(\d{2,4}-\d{1,2}-\d{1,2})-([^/]*)(\.[^.]+)$!.freeze
34
27
 
35
28
  # @param site [Bridgetown::Site]
@@ -42,11 +35,31 @@ module Bridgetown
42
35
  trigger_hooks(:post_init)
43
36
  end
44
37
 
38
+ # Collection associated with this resource
39
+ #
45
40
  # @return [Bridgetown::Collection]
46
41
  def collection
47
42
  model.collection
48
43
  end
49
44
 
45
+ # Layout associated with this resource
46
+ # This will output a warning if the layout can't be found.
47
+ #
48
+ # @return [Bridgetown::Layout]
49
+ def layout
50
+ return @layout if @layout
51
+ return if no_layout?
52
+
53
+ @layout = site.layouts[data.layout].tap do |layout|
54
+ unless layout
55
+ Bridgetown.logger.warn "Resource:", "Layout '#{data.layout}' " \
56
+ "requested via #{relative_path} does not exist."
57
+ end
58
+ end
59
+ end
60
+
61
+ # The relative path of source file or file-like origin
62
+ #
50
63
  # @return [Pathname]
51
64
  def relative_path
52
65
  model.origin.relative_path
@@ -57,6 +70,11 @@ module Bridgetown
57
70
  @transformer ||= Bridgetown::Resource::Transformer.new(self)
58
71
  end
59
72
 
73
+ # @return [Bridgetown::Resource::Relations]
74
+ def relations
75
+ @relations ||= Bridgetown::Resource::Relations.new(self)
76
+ end
77
+
60
78
  # @param new_data [HashWithDotAccess::Hash]
61
79
  def data=(new_data)
62
80
  unless new_data.is_a?(HashWithDotAccess::Hash)
@@ -80,9 +98,11 @@ module Bridgetown
80
98
 
81
99
  unless collection.data?
82
100
  self.untransformed_content = content
83
- determine_slug_and_date
84
101
  normalize_categories_and_tags
85
102
  import_taxonomies_from_data
103
+ ensure_default_data
104
+ transformer.execute_inline_ruby!
105
+ set_date_from_string(data.date)
86
106
  end
87
107
 
88
108
  @destination = Destination.new(self) if requires_destination?
@@ -108,6 +128,7 @@ module Bridgetown
108
128
  trigger_hooks :"post_#{hook_suffix}"
109
129
  end
110
130
 
131
+ # @return [String]
111
132
  def relative_path_basename_without_prefix
112
133
  return_path = Pathname.new("")
113
134
  relative_path.each_filename do |filename|
@@ -121,6 +142,7 @@ module Bridgetown
121
142
  (return_path.dirname + return_path.basename(".*")).to_s
122
143
  end
123
144
 
145
+ # @return [String]
124
146
  def basename_without_ext
125
147
  relative_path.basename(".*").to_s
126
148
  end
@@ -135,21 +157,28 @@ module Bridgetown
135
157
  data&.permalink
136
158
  end
137
159
 
160
+ # @return [String]
138
161
  def path
139
162
  (model.origin.respond_to?(:original_path) ? model.origin.original_path : relative_path).to_s
140
163
  end
141
164
 
165
+ # @return [String]
142
166
  def absolute_url
143
167
  format_url destination&.absolute_url
144
168
  end
145
169
 
170
+ # @return [String]
146
171
  def relative_url
147
172
  format_url destination&.relative_url
148
173
  end
149
- alias_method :id, :relative_url
174
+
175
+ # @return [String]
176
+ def id
177
+ model.origin.id
178
+ end
150
179
 
151
180
  def date
152
- data["date"] ||= site.time # TODO: this doesn't reflect documented behavior
181
+ data["date"] ||= site.time
153
182
  end
154
183
 
155
184
  # @return [Hash<String, Hash<String => Bridgetown::Resource::TaxonomyType,
@@ -189,13 +218,36 @@ module Bridgetown
189
218
 
190
219
  # Create a Liquid-understandable version of this resource.
191
220
  #
192
- # @return [Drops::DocumentDrop] represents this resource's data.
221
+ # @return [Drops::ResourceDrop] represents this resource's data.
193
222
  def to_liquid
194
223
  @to_liquid ||= Drops::ResourceDrop.new(self)
195
224
  end
196
225
 
226
+ def to_h
227
+ {
228
+ id: id,
229
+ absolute_url: absolute_url,
230
+ relative_path: relative_path,
231
+ relative_url: relative_url,
232
+ date: date,
233
+ data: data,
234
+ taxonomies: taxonomies,
235
+ untransformed_content: untransformed_content,
236
+ content: content,
237
+ output: output,
238
+ }
239
+ end
240
+
241
+ def as_json(*)
242
+ to_h
243
+ end
244
+
245
+ ruby2_keywords def to_json(*options)
246
+ as_json(*options).to_json(*options)
247
+ end
248
+
197
249
  def inspect
198
- "#<#{self.class} [#{collection.label}] #{relative_path}>"
250
+ "#<#{self.class} #{id}>"
199
251
  end
200
252
 
201
253
  # Compare this document against another document.
@@ -222,30 +274,32 @@ module Bridgetown
222
274
  alias_method :next_doc, :next_resource
223
275
 
224
276
  def previous_resource
225
- pos = collection.docs.index { |item| item.equal?(self) }
277
+ pos = collection.resources.index { |item| item.equal?(self) }
226
278
  collection.resources[pos - 1] if pos&.positive?
227
279
  end
228
280
  alias_method :previous_doc, :previous_resource
229
281
 
230
282
  private
231
283
 
232
- def determine_slug_and_date
233
- return unless relative_path.to_s =~ DATE_FILENAME_MATCHER
284
+ def ensure_default_data
285
+ slug = if matches = relative_path.to_s.match(DATE_FILENAME_MATCHER) # rubocop:disable Lint/AssignmentInCondition
286
+ set_date_from_string(matches[1]) unless data.date
287
+ matches[2]
288
+ else
289
+ basename_without_ext
290
+ end
234
291
 
235
- new_date, slug = Regexp.last_match.captures
236
- modify_date(new_date)
237
-
238
- slug.gsub!(%r!\.*\z!, "")
239
292
  data.slug ||= slug
293
+ data.title ||= Bridgetown::Utils.titleize_slug(slug)
240
294
  end
241
295
 
242
- def modify_date(new_date)
243
- if !data.date || data.date.to_i == site.time.to_i
244
- data.date = Utils.parse_date(
245
- new_date,
246
- "Document '#{relative_path}' does not have a valid date in the #{model}."
247
- )
248
- end
296
+ def set_date_from_string(new_date) # rubocop:disable Naming/AccessorMethodName
297
+ return unless new_date.is_a?(String)
298
+
299
+ data.date = Bridgetown::Utils.parse_date(
300
+ new_date,
301
+ "Document '#{relative_path}' does not have a valid date in the #{model}."
302
+ )
249
303
  end
250
304
 
251
305
  def normalize_categories_and_tags
@@ -32,7 +32,7 @@ module Bridgetown
32
32
 
33
33
  # Strip out file extension and process each segment of a URL to swap out
34
34
  # placeholders such as :categories or :title
35
- url_segments = permalink.sub(%r{\.[^/]*$}, "").split("/")
35
+ url_segments = Bridgetown::Filters::URLFilters.strip_extname(permalink).split("/")
36
36
  new_url = url_segments.map do |segment|
37
37
  segment.starts_with?(":") ? process_segment(segment.sub(%r{^:}, "")) : segment
38
38
  end.select(&:present?).join("/")
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Resource
5
+ class Relations
6
+ # @return [Bridgetown::Resource::Base]
7
+ attr_reader :resource
8
+
9
+ # @return [Bridgetown::Site]
10
+ attr_reader :site
11
+
12
+ # @param resource [Bridgetown::Resource::Base]
13
+ def initialize(resource)
14
+ @resource = resource
15
+ @site = resource.site
16
+ end
17
+
18
+ # @return [HashWithDotAccess::Hash]
19
+ def relation_schema
20
+ resource.collection.metadata.relations
21
+ end
22
+
23
+ # @return [Array<String>]
24
+ def relation_types
25
+ @relation_types ||= begin
26
+ types = []
27
+ relation_schema&.each do |_relation_type, collections|
28
+ types << collections
29
+ types << Array(collections).map { |item| ActiveSupport::Inflector.pluralize(item) }
30
+ end
31
+ types.flatten.uniq
32
+ end
33
+ end
34
+
35
+ # @param type [Symbol]
36
+ # @return [Bridgetown::Resource::Base, Array<Bridgetown::Resource::Base>]
37
+ def resources_for_type(type)
38
+ relation_kind = kind_of_relation_for_type(type)
39
+ return [] unless relation_kind
40
+
41
+ case relation_kind.to_sym
42
+ when :belongs_to
43
+ belongs_to_relation_for_type(type)
44
+ when :has_many
45
+ has_many_relation_for_type(type)
46
+ when :has_one
47
+ has_one_relation_for_type(type)
48
+ end
49
+ end
50
+
51
+ def method_missing(type, *args)
52
+ return super unless type.to_s.in?(relation_types)
53
+
54
+ resources_for_type(type)
55
+ end
56
+
57
+ def respond_to_missing?(type, *_args)
58
+ type.to_s.in?(relation_types)
59
+ end
60
+
61
+ def to_liquid
62
+ @to_liquid ||= Drops::RelationsDrop.new(self)
63
+ end
64
+
65
+ private
66
+
67
+ # @param type [Symbol]
68
+ # @return [String]
69
+ def kind_of_relation_for_type(type)
70
+ relation_schema&.each do |relation_type, collections|
71
+ collections = Array(collections).yield_self do |collections_arr|
72
+ collections_arr +
73
+ collections_arr.map { |item| ActiveSupport::Inflector.pluralize(item) }
74
+ end.flatten.uniq
75
+ return relation_type if collections.include?(type.to_s)
76
+ end
77
+ end
78
+
79
+ # @param type [Symbol]
80
+ # @return [Bridgetown::Collection]
81
+ def other_collection_for_type(type)
82
+ site.collections[type] || site.collections[ActiveSupport::Inflector.pluralize(type)]
83
+ end
84
+
85
+ # @return [Array<String>]
86
+ def collection_labels
87
+ [
88
+ resource.collection.label,
89
+ ActiveSupport::Inflector.singularize(resource.collection.label),
90
+ ]
91
+ end
92
+
93
+ # @param type [Symbol]
94
+ # @return [Bridgetown::Resource::Base, Array<Bridgetown::Resource::Base>]
95
+ def belongs_to_relation_for_type(type)
96
+ if resource.data[type].is_a?(Array)
97
+ other_collection_for_type(type).resources.select do |other_resource|
98
+ other_resource.data.slug.in?(resource.data[type])
99
+ end
100
+ else
101
+ other_collection_for_type(type).resources.find do |other_resource|
102
+ other_resource.data.slug == resource.data[type]
103
+ end
104
+ end
105
+ end
106
+
107
+ # @param type [Symbol]
108
+ # @return [Array<Bridgetown::Resource::Base>]
109
+ def has_many_relation_for_type(type) # rubocop:disable Naming/PredicateName
110
+ label, singular_label = collection_labels
111
+
112
+ other_collection_for_type(type).resources.select do |other_resource|
113
+ resource.data.slug.in?(
114
+ Array(other_resource.data[label] || other_resource.data[singular_label])
115
+ )
116
+ end
117
+ end
118
+
119
+ # @param type [Symbol]
120
+ # @return [Bridgetown::Resource::Base]
121
+ def has_one_relation_for_type(type) # rubocop:disable Naming/PredicateName
122
+ label, singular_label = collection_labels
123
+
124
+ other_collection_for_type(type).resources.find do |other_resource|
125
+ resource.data.slug.in?(
126
+ Array(other_resource.data[label] || other_resource.data[singular_label])
127
+ )
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -17,9 +17,18 @@ module Bridgetown
17
17
 
18
18
  def to_liquid
19
19
  {
20
- label: label,
20
+ "label" => label,
21
21
  }
22
22
  end
23
+ alias_method :to_h, :to_liquid
24
+
25
+ def as_json(*)
26
+ to_h
27
+ end
28
+
29
+ ruby2_keywords def to_json(*options)
30
+ as_json(*options).to_json(*options)
31
+ end
23
32
  end
24
33
  end
25
34
  end
@@ -42,6 +42,15 @@ module Bridgetown
42
42
  "metadata" => metadata,
43
43
  }
44
44
  end
45
+ alias_method :to_h, :to_liquid
46
+
47
+ def as_json(*)
48
+ to_h
49
+ end
50
+
51
+ ruby2_keywords def to_json(*options)
52
+ as_json(*options).to_json(*options)
53
+ end
45
54
  end
46
55
  end
47
56
  end
@@ -12,18 +12,21 @@ module Bridgetown
12
12
  # @return [Bridgetown::Site]
13
13
  attr_reader :site
14
14
 
15
- # @return [String]
16
- attr_reader :output_ext
17
-
18
15
  def initialize(resource)
19
16
  @resource = resource
20
17
  @site = resource.site
21
- execute_inline_ruby
22
- @output_ext = output_ext_from_converters
18
+ @conversions = []
19
+ end
20
+
21
+ # @return [String]
22
+ def output_ext
23
+ @output_ext ||= output_ext_from_converters
23
24
  end
24
25
 
25
26
  # @return [String]
26
27
  def final_ext
28
+ output_ext # we always need this to get run
29
+
27
30
  permalink_ext || output_ext
28
31
  end
29
32
 
@@ -35,6 +38,12 @@ module Bridgetown
35
38
  end
36
39
  end
37
40
 
41
+ def execute_inline_ruby!
42
+ return unless site.config.should_execute_inline_ruby?
43
+
44
+ Bridgetown::Utils::RubyExec.search_data_for_ruby_code(resource, self)
45
+ end
46
+
38
47
  def inspect
39
48
  "#<#{self.class} Conversion Steps: #{conversions.length}>"
40
49
  end
@@ -102,13 +111,7 @@ module Bridgetown
102
111
 
103
112
  ### Transformation Actions
104
113
 
105
- def execute_inline_ruby
106
- return unless site.config.should_execute_inline_ruby?
107
-
108
- Bridgetown::Utils::RubyExec.search_data_for_ruby_code(resource, self)
109
- end
110
-
111
- def run_conversions # rubocop:disable Metrics/AbcSize
114
+ def run_conversions # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
112
115
  input = resource.content.to_s
113
116
 
114
117
  # @param content [String]
@@ -123,9 +126,10 @@ module Bridgetown
123
126
  type: :content,
124
127
  converter: converter,
125
128
  output: Bridgetown.env.production? ? nil : output,
126
- output_ext: conversions[index][:output_ext],
129
+ output_ext: conversions[index]&.dig(:output_ext) ||
130
+ converter.output_ext(resource.extname),
127
131
  }
128
- output
132
+ output.html_safe
129
133
  rescue StandardError => e
130
134
  Bridgetown.logger.error "Conversion error:",
131
135
  "#{converter.class} encountered an error while "\