bridgetown-core 0.19.3 → 0.21.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/bridgetown-core.gemspec +1 -2
  3. data/lib/bridgetown-core.rb +37 -29
  4. data/lib/bridgetown-core/cleaner.rb +9 -3
  5. data/lib/bridgetown-core/collection.rb +177 -78
  6. data/lib/bridgetown-core/commands/base.rb +9 -0
  7. data/lib/bridgetown-core/commands/build.rb +0 -11
  8. data/lib/bridgetown-core/commands/concerns/git_helpers.rb +20 -0
  9. data/lib/bridgetown-core/commands/configure.rb +8 -3
  10. data/lib/bridgetown-core/commands/console.rb +4 -0
  11. data/lib/bridgetown-core/commands/doctor.rb +1 -19
  12. data/lib/bridgetown-core/commands/new.rb +6 -6
  13. data/lib/bridgetown-core/commands/plugins.rb +14 -13
  14. data/lib/bridgetown-core/commands/serve.rb +0 -14
  15. data/lib/bridgetown-core/commands/webpack.rb +75 -0
  16. data/lib/bridgetown-core/commands/webpack/enable-postcss.rb +12 -0
  17. data/lib/bridgetown-core/commands/webpack/setup.rb +4 -0
  18. data/lib/bridgetown-core/commands/webpack/update.rb +3 -0
  19. data/lib/bridgetown-core/commands/webpack/webpack.config.js +18 -0
  20. data/lib/{site_template/webpack.config.js.erb → bridgetown-core/commands/webpack/webpack.defaults.js.erb} +32 -16
  21. data/lib/bridgetown-core/component.rb +183 -0
  22. data/lib/bridgetown-core/concerns/data_accessible.rb +1 -0
  23. data/lib/bridgetown-core/concerns/front_matter_importer.rb +52 -0
  24. data/lib/bridgetown-core/concerns/layout_placeable.rb +1 -1
  25. data/lib/bridgetown-core/concerns/site/configurable.rb +10 -10
  26. data/lib/bridgetown-core/concerns/site/content.rb +56 -15
  27. data/lib/bridgetown-core/concerns/site/localizable.rb +3 -5
  28. data/lib/bridgetown-core/concerns/site/processable.rb +6 -4
  29. data/lib/bridgetown-core/concerns/site/renderable.rb +26 -0
  30. data/lib/bridgetown-core/concerns/site/writable.rb +12 -2
  31. data/lib/bridgetown-core/concerns/validatable.rb +2 -5
  32. data/lib/bridgetown-core/configuration.rb +61 -33
  33. data/lib/bridgetown-core/configurations/bt-postcss.rb +6 -6
  34. data/lib/bridgetown-core/configurations/netlify.rb +1 -0
  35. data/lib/bridgetown-core/configurations/tailwindcss.rb +6 -6
  36. data/lib/bridgetown-core/converter.rb +23 -0
  37. data/lib/bridgetown-core/converters/erb_templates.rb +51 -35
  38. data/lib/bridgetown-core/converters/identity.rb +0 -9
  39. data/lib/bridgetown-core/converters/liquid_templates.rb +1 -1
  40. data/lib/bridgetown-core/converters/markdown.rb +14 -4
  41. data/lib/bridgetown-core/converters/markdown/kramdown_parser.rb +5 -38
  42. data/lib/bridgetown-core/converters/ruby_templates.rb +17 -0
  43. data/lib/bridgetown-core/converters/smartypants.rb +3 -1
  44. data/lib/bridgetown-core/core_ext/psych.rb +19 -0
  45. data/lib/bridgetown-core/current.rb +10 -0
  46. data/lib/bridgetown-core/document.rb +9 -16
  47. data/lib/bridgetown-core/drops/collection_drop.rb +1 -1
  48. data/lib/bridgetown-core/drops/page_drop.rb +4 -0
  49. data/lib/bridgetown-core/drops/relations_drop.rb +23 -0
  50. data/lib/bridgetown-core/drops/resource_drop.rb +83 -0
  51. data/lib/bridgetown-core/drops/site_drop.rb +33 -8
  52. data/lib/bridgetown-core/drops/unified_payload_drop.rb +5 -0
  53. data/lib/bridgetown-core/entry_filter.rb +17 -28
  54. data/lib/bridgetown-core/errors.rb +0 -2
  55. data/lib/bridgetown-core/filters.rb +3 -26
  56. data/lib/bridgetown-core/filters/from_liquid.rb +23 -0
  57. data/lib/bridgetown-core/filters/url_filters.rb +12 -0
  58. data/lib/bridgetown-core/frontmatter_defaults.rb +1 -1
  59. data/lib/bridgetown-core/generators/prototype_generator.rb +59 -20
  60. data/lib/bridgetown-core/helpers.rb +48 -9
  61. data/lib/bridgetown-core/layout.rb +53 -21
  62. data/lib/bridgetown-core/liquid_renderer/file.rb +1 -0
  63. data/lib/bridgetown-core/liquid_renderer/table.rb +1 -0
  64. data/lib/bridgetown-core/model/base.rb +138 -0
  65. data/lib/bridgetown-core/model/builder_origin.rb +40 -0
  66. data/lib/bridgetown-core/model/origin.rb +38 -0
  67. data/lib/bridgetown-core/model/repo_origin.rb +126 -0
  68. data/lib/bridgetown-core/page.rb +11 -2
  69. data/lib/bridgetown-core/plugin_manager.rb +1 -3
  70. data/lib/bridgetown-core/publisher.rb +8 -2
  71. data/lib/bridgetown-core/reader.rb +37 -22
  72. data/lib/bridgetown-core/readers/data_reader.rb +5 -5
  73. data/lib/bridgetown-core/readers/defaults_reader.rb +1 -1
  74. data/lib/bridgetown-core/readers/layout_reader.rb +1 -1
  75. data/lib/bridgetown-core/readers/page_reader.rb +1 -0
  76. data/lib/bridgetown-core/readers/post_reader.rb +5 -4
  77. data/lib/bridgetown-core/regenerator.rb +9 -2
  78. data/lib/bridgetown-core/related_posts.rb +9 -6
  79. data/lib/bridgetown-core/renderer.rb +6 -13
  80. data/lib/bridgetown-core/resource/base.rb +329 -0
  81. data/lib/bridgetown-core/resource/destination.rb +49 -0
  82. data/lib/bridgetown-core/resource/permalink_processor.rb +179 -0
  83. data/lib/bridgetown-core/resource/relations.rb +132 -0
  84. data/lib/bridgetown-core/resource/taxonomy_term.rb +34 -0
  85. data/lib/bridgetown-core/resource/taxonomy_type.rb +56 -0
  86. data/lib/bridgetown-core/resource/transformer.rb +177 -0
  87. data/lib/bridgetown-core/ruby_template_view.rb +11 -11
  88. data/lib/bridgetown-core/site.rb +13 -6
  89. data/lib/bridgetown-core/static_file.rb +33 -10
  90. data/lib/bridgetown-core/tags/highlight.rb +2 -15
  91. data/lib/bridgetown-core/tags/include.rb +1 -1
  92. data/lib/bridgetown-core/tags/post_url.rb +2 -2
  93. data/lib/bridgetown-core/url.rb +1 -0
  94. data/lib/bridgetown-core/utils.rb +49 -43
  95. data/lib/bridgetown-core/utils/require_gems.rb +60 -0
  96. data/lib/bridgetown-core/utils/ruby_exec.rb +6 -9
  97. data/lib/bridgetown-core/utils/ruby_front_matter.rb +39 -0
  98. data/lib/bridgetown-core/version.rb +2 -2
  99. data/lib/bridgetown-core/watcher.rb +2 -1
  100. data/lib/bridgetown-core/yaml_parser.rb +22 -0
  101. data/lib/site_template/config/.keep +0 -0
  102. data/lib/site_template/package.json.erb +4 -4
  103. data/lib/site_template/plugins/site_builder.rb +1 -1
  104. data/lib/site_template/src/_posts/0000-00-00-welcome-to-bridgetown.md.erb +1 -1
  105. metadata +46 -41
  106. data/lib/bridgetown-core/external.rb +0 -58
  107. data/lib/bridgetown-core/page_without_a_file.rb +0 -17
  108. data/lib/bridgetown-core/path_manager.rb +0 -31
  109. data/lib/bridgetown-core/readers/collection_reader.rb +0 -23
  110. data/lib/bridgetown-core/readers/static_file_reader.rb +0 -25
  111. data/lib/bridgetown-core/utils/exec.rb +0 -26
  112. data/lib/bridgetown-core/utils/internet.rb +0 -37
  113. data/lib/bridgetown-core/utils/platforms.rb +0 -80
  114. data/lib/bridgetown-core/utils/thread_event.rb +0 -31
  115. data/lib/bridgetown-core/utils/win_tz.rb +0 -75
@@ -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
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Resource
5
+ class TaxonomyTerm
6
+ attr_reader :resource
7
+
8
+ attr_reader :label
9
+
10
+ attr_reader :type
11
+
12
+ def initialize(resource:, label:, type:)
13
+ @resource = resource
14
+ @label = label
15
+ @type = type
16
+ end
17
+
18
+ def to_liquid
19
+ {
20
+ "label" => label,
21
+ }
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
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Resource
5
+ class TaxonomyType
6
+ # @return [Bridgetown::Site]
7
+ attr_reader :site
8
+
9
+ # @return [String] aka `category`, `tag`, `region`, etc.
10
+ attr_reader :label
11
+
12
+ # @return [String] the key used in front matter
13
+ attr_reader :key
14
+
15
+ # @return [HashWithDotAccess::Hash] any associated metadata
16
+ attr_reader :metadata
17
+
18
+ # @param site [Bridgetown::Site]
19
+ # @param label [String]
20
+ # @param key [String]
21
+ def initialize(site:, label:, key:, metadata:)
22
+ @site = site
23
+ @label = label
24
+ @key = key
25
+ @metadata = metadata
26
+ end
27
+
28
+ def terms
29
+ site.resources.map do |resource|
30
+ resource.taxonomies[label].terms
31
+ end.flatten.group_by(&:label).with_dot_access
32
+ end
33
+
34
+ def inspect
35
+ "#<#{self.class} label=#{label}>"
36
+ end
37
+
38
+ def to_liquid
39
+ {
40
+ "label" => label,
41
+ "key" => key,
42
+ "metadata" => metadata,
43
+ }
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
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Resource
5
+ class Transformer
6
+ # @return [Array<Hash>]
7
+ attr_reader :conversions
8
+
9
+ # @return [Bridgetown::Resource::Base]
10
+ attr_reader :resource
11
+
12
+ # @return [Bridgetown::Site]
13
+ attr_reader :site
14
+
15
+ def initialize(resource)
16
+ @resource = resource
17
+ @site = resource.site
18
+ @conversions = []
19
+ end
20
+
21
+ # @return [String]
22
+ def output_ext
23
+ @output_ext ||= output_ext_from_converters
24
+ end
25
+
26
+ # @return [String]
27
+ def final_ext
28
+ output_ext # we always need this to get run
29
+
30
+ permalink_ext || output_ext
31
+ end
32
+
33
+ def process!
34
+ Bridgetown.logger.debug "Transforming:", resource.relative_path
35
+ resource.around_hook :render do
36
+ run_conversions
37
+ resource.place_in_layout? ? place_into_layouts : resource.output = resource.content.dup
38
+ end
39
+ end
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
+
47
+ def inspect
48
+ "#<#{self.class} Conversion Steps: #{conversions.length}>"
49
+ end
50
+
51
+ private
52
+
53
+ ### Utilities
54
+
55
+ def permalink_ext
56
+ resource_permalink = resource.permalink
57
+ if resource_permalink &&
58
+ !resource_permalink.end_with?("/") &&
59
+ !resource_permalink.end_with?(".*")
60
+ permalink_ext = File.extname(resource_permalink)
61
+ permalink_ext unless permalink_ext.empty?
62
+ end
63
+ end
64
+
65
+ # @return [Array<Bridgetown::Converter>]
66
+ def converters
67
+ @converters ||= site.matched_converters_for_convertible(resource)
68
+ end
69
+
70
+ # @return [String]
71
+ def output_ext_from_converters
72
+ @conversions = converters.map do |converter|
73
+ {
74
+ converter: converter,
75
+ output_ext: converter.output_ext(resource.extname),
76
+ }
77
+ end
78
+
79
+ conversions
80
+ .reverse
81
+ .find do |conversion|
82
+ conversions.length == 1 ||
83
+ !conversion[:converter].is_a?(Bridgetown::Converters::Identity)
84
+ end
85
+ .fetch(:output_ext)
86
+ end
87
+
88
+ # @return [Array<Bridgetown::Layout>]
89
+ def validated_layouts
90
+ layout = site.layouts[resource.data.layout]
91
+ warn_on_missing_layout layout, resource.data.layout
92
+
93
+ layout_list = Set.new([layout])
94
+ while layout
95
+ layout_name = layout.data.layout
96
+ layout = site.layouts[layout_name]
97
+ warn_on_missing_layout layout, layout_name
98
+
99
+ layout_list << layout
100
+ end
101
+
102
+ layout_list.to_a.compact
103
+ end
104
+
105
+ def warn_on_missing_layout(layout, layout_name)
106
+ if layout.nil? && layout_name
107
+ Bridgetown.logger.warn "Build Warning:", "Layout '#{layout_name}' " \
108
+ "requested via #{resource.relative_path} does not exist."
109
+ end
110
+ end
111
+
112
+ ### Transformation Actions
113
+
114
+ def run_conversions # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
115
+ input = resource.content.to_s
116
+
117
+ # @param content [String]
118
+ # @param converter [Bridgetown::Converter]
119
+ resource.content = converters.each_with_index.inject(input) do |content, (converter, index)|
120
+ output = if converter.method(:convert).arity == 1
121
+ converter.convert content
122
+ else
123
+ converter.convert content, resource
124
+ end
125
+ conversions[index] = {
126
+ type: :content,
127
+ converter: converter,
128
+ output: Bridgetown.env.production? ? nil : output,
129
+ output_ext: conversions[index]&.dig(:output_ext) ||
130
+ converter.output_ext(resource.extname),
131
+ }
132
+ output.html_safe
133
+ rescue StandardError => e
134
+ Bridgetown.logger.error "Conversion error:",
135
+ "#{converter.class} encountered an error while "\
136
+ "converting `#{resource.relative_path}'"
137
+ raise e
138
+ end
139
+ end
140
+
141
+ def place_into_layouts
142
+ Bridgetown.logger.debug "Placing in Layouts:", resource.relative_path
143
+ output = resource.content.dup
144
+ validated_layouts.each do |layout|
145
+ output = run_layout_conversions layout, output
146
+ end
147
+ resource.output = output
148
+ end
149
+
150
+ def run_layout_conversions(layout, output)
151
+ layout_converters = site.matched_converters_for_convertible(layout)
152
+ layout_input = layout.content.dup
153
+
154
+ layout_converters.inject(layout_input) do |content, converter|
155
+ next(content) unless [2, -2].include?(converter.method(:convert).arity)
156
+
157
+ layout.current_document = resource
158
+ layout.current_document_output = output
159
+ layout_output = converter.convert content, layout
160
+
161
+ conversions << {
162
+ type: :layout,
163
+ layout: layout,
164
+ converter: converter,
165
+ output: Bridgetown.env.production? ? nil : layout_output,
166
+ }
167
+ layout_output
168
+ rescue StandardError => e
169
+ Bridgetown.logger.error "Conversion error:",
170
+ "#{converter.class} encountered an error while "\
171
+ "converting `#{resource.relative_path}'"
172
+ raise e
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -7,6 +7,7 @@ module Bridgetown
7
7
  require "bridgetown-core/helpers"
8
8
 
9
9
  attr_reader :layout, :page, :paginator, :site, :content
10
+ alias_method :resource, :page
10
11
 
11
12
  def initialize(convertible)
12
13
  if convertible.is_a?(Layout)
@@ -27,20 +28,17 @@ module Bridgetown
27
28
 
28
29
  def render(item, options = {}, &block)
29
30
  if item.respond_to?(:render_in)
30
- previous_buffer_state = @_erbout
31
- @_erbout = Bridgetown::ERBBuffer.new
32
-
33
- @in_view_component ||= defined?(::ViewComponent::Base) && item.is_a?(::ViewComponent::Base)
34
31
  result = item.render_in(self, &block)
35
- @in_view_component = false
36
-
37
- @_erbout = previous_buffer_state
38
- result
32
+ result&.html_safe
39
33
  else
40
- partial(item, options, &block)
34
+ partial(item, options, &block)&.html_safe
41
35
  end
42
36
  end
43
37
 
38
+ def collections
39
+ site.collections
40
+ end
41
+
44
42
  def site_drop
45
43
  site.site_payload.site
46
44
  end
@@ -56,14 +54,15 @@ module Bridgetown
56
54
  Bridgetown.logger.warn "Liquid Warning:",
57
55
  LiquidRenderer.format_error(e, path || document.relative_path)
58
56
  end
59
- template.render!(options.deep_stringify_keys, _liquid_context)
57
+ template.render!(options.deep_stringify_keys, _liquid_context).html_safe
60
58
  end
61
59
 
62
60
  def helpers
63
61
  @helpers ||= Helpers.new(self, site)
64
62
  end
65
63
 
66
- def method_missing(method, *args, &block)
64
+ # rubocop:disable Style/MissingRespondToMissing
65
+ ruby2_keywords def method_missing(method, *args, &block)
67
66
  if helpers.respond_to?(method.to_sym)
68
67
  helpers.send method.to_sym, *args, &block
69
68
  else
@@ -74,6 +73,7 @@ module Bridgetown
74
73
  def respond_to_missing?(method, include_private = false)
75
74
  helpers.respond_to?(method.to_sym, include_private) || super
76
75
  end
76
+ # rubocop:enable Style/MissingRespondToMissing
77
77
 
78
78
  private
79
79