bridgetown-core 0.15.0.beta2 → 0.16.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +14 -0
  3. data/bridgetown-core.gemspec +3 -0
  4. data/lib/bridgetown-core.rb +6 -1
  5. data/lib/bridgetown-core/commands/concerns/actions.rb +41 -19
  6. data/lib/bridgetown-core/commands/console.rb +12 -2
  7. data/lib/bridgetown-core/commands/serve.rb +5 -0
  8. data/lib/bridgetown-core/concerns/data_accessible.rb +19 -0
  9. data/lib/bridgetown-core/concerns/layout_placeable.rb +17 -0
  10. data/lib/bridgetown-core/concerns/liquid_renderable.rb +20 -0
  11. data/lib/bridgetown-core/concerns/publishable.rb +10 -0
  12. data/lib/bridgetown-core/concerns/site/configurable.rb +62 -31
  13. data/lib/bridgetown-core/concerns/site/content.rb +88 -29
  14. data/lib/bridgetown-core/concerns/site/extensible.rb +15 -12
  15. data/lib/bridgetown-core/concerns/site/processable.rb +12 -10
  16. data/lib/bridgetown-core/concerns/site/renderable.rb +23 -4
  17. data/lib/bridgetown-core/concerns/site/writable.rb +16 -2
  18. data/lib/bridgetown-core/concerns/validatable.rb +59 -0
  19. data/lib/bridgetown-core/configuration.rb +1 -0
  20. data/lib/bridgetown-core/converter.rb +34 -0
  21. data/lib/bridgetown-core/converters/erb_templates.rb +78 -0
  22. data/lib/bridgetown-core/converters/markdown.rb +6 -23
  23. data/lib/bridgetown-core/converters/smartypants.rb +0 -10
  24. data/lib/bridgetown-core/document.rb +8 -52
  25. data/lib/bridgetown-core/drops/document_drop.rb +9 -1
  26. data/lib/bridgetown-core/errors.rb +2 -0
  27. data/lib/bridgetown-core/excerpt.rb +5 -7
  28. data/lib/bridgetown-core/filters.rb +2 -0
  29. data/lib/bridgetown-core/layout.rb +24 -1
  30. data/lib/bridgetown-core/liquid_renderer/file.rb +1 -4
  31. data/lib/bridgetown-core/liquid_renderer/file_system.rb +1 -1
  32. data/lib/bridgetown-core/page.rb +34 -25
  33. data/lib/bridgetown-core/plugin_manager.rb +36 -14
  34. data/lib/bridgetown-core/regenerator.rb +1 -1
  35. data/lib/bridgetown-core/renderer.rb +41 -15
  36. data/lib/bridgetown-core/ruby_template_view.rb +98 -0
  37. data/lib/bridgetown-core/tags/class_map.rb +90 -0
  38. data/lib/bridgetown-core/tags/include.rb +2 -0
  39. data/lib/bridgetown-core/tags/render_content.rb +14 -2
  40. data/lib/bridgetown-core/tags/webpack_path.rb +19 -22
  41. data/lib/bridgetown-core/utils.rb +97 -0
  42. data/lib/bridgetown-core/version.rb +2 -2
  43. data/lib/site_template/bridgetown.config.yml +5 -3
  44. data/lib/site_template/src/_components/{footer.html → footer.liquid} +0 -0
  45. data/lib/site_template/src/_components/{head.html → head.liquid} +0 -0
  46. data/lib/site_template/src/_components/{navbar.html → navbar.liquid} +0 -0
  47. data/lib/site_template/src/_layouts/{default.html → default.liquid} +1 -1
  48. data/lib/site_template/src/_layouts/{home.html → home.liquid} +0 -0
  49. data/lib/site_template/src/_layouts/{page.html → page.liquid} +0 -0
  50. data/lib/site_template/src/_layouts/{post.html → post.liquid} +0 -0
  51. metadata +59 -10
  52. data/lib/bridgetown-core/concerns/convertible.rb +0 -238
@@ -53,18 +53,19 @@ module Bridgetown
53
53
 
54
54
  # Prepare payload and render the document
55
55
  #
56
- # Returns String rendered document output
56
+ # Returns nothing
57
57
  def run
58
58
  Bridgetown.logger.debug "Rendering:", document.relative_path
59
59
 
60
60
  assign_pages!
61
+ # TODO: this can be eliminated I think:
61
62
  assign_current_document!
62
63
  assign_highlighter_options!
63
64
  assign_layout_data!
64
65
 
65
66
  document.trigger_hooks(:pre_render, payload)
66
-
67
- render_document
67
+ document.output = render_document
68
+ document.trigger_hooks(:post_render)
68
69
  end
69
70
 
70
71
  # Render the document.
@@ -83,7 +84,7 @@ module Bridgetown
83
84
  end
84
85
 
85
86
  Bridgetown.logger.debug "Rendering Markup:", document.relative_path
86
- output = convert(output.to_s)
87
+ output = convert(output.to_s, document)
87
88
  document.content = output
88
89
 
89
90
  if document.place_in_layout?
@@ -140,9 +141,13 @@ module Bridgetown
140
141
  # Convert the document using the converters which match this renderer's document.
141
142
  #
142
143
  # Returns String the converted content.
143
- def convert(content)
144
+ def convert(content, document)
144
145
  converters.reduce(content) do |output, converter|
145
- converter.convert output
146
+ if converter.method(:convert).arity == 1
147
+ converter.convert output
148
+ else
149
+ converter.convert output, document
150
+ end
146
151
  rescue StandardError => e
147
152
  Bridgetown.logger.error "Conversion error:",
148
153
  "#{converter.class} encountered an error while "\
@@ -202,17 +207,38 @@ module Bridgetown
202
207
  # Render layout content into document.output
203
208
  #
204
209
  # Returns String rendered content
210
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
205
211
  def render_layout(output, layout, liquid_context)
206
- payload["content"] = output
207
- payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})
208
-
209
- render_liquid(
210
- layout.content,
211
- payload,
212
- liquid_context,
213
- layout.path
214
- )
212
+ if layout.render_with_liquid?
213
+ payload["content"] = output
214
+ payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})
215
+
216
+ render_liquid(
217
+ layout.content,
218
+ payload,
219
+ liquid_context,
220
+ layout.path
221
+ )
222
+ else
223
+ layout_converters ||= site.converters.select { |c| c.matches(layout.ext) }.sort
224
+
225
+ layout_content = layout.content.dup
226
+ layout_converters.reduce(layout_content) do |layout_output, converter|
227
+ next(layout_output) unless converter.method(:convert).arity == 2
228
+
229
+ layout.current_document = document
230
+ layout.current_document_output = output
231
+ converter.convert layout_output, layout
232
+ rescue StandardError => e
233
+ Bridgetown.logger.error "Conversion error:",
234
+ "#{converter.class} encountered an error while "\
235
+ "converting '#{document.relative_path}':"
236
+ Bridgetown.logger.error("", e.to_s)
237
+ raise e
238
+ end
239
+ end
215
240
  end
241
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
216
242
 
217
243
  def add_regenerator_dependencies(layout)
218
244
  return unless document.write?
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "active_support/core_ext/hash/keys"
5
+
6
+ module Bridgetown
7
+ class RubyTemplateView
8
+ class Helpers
9
+ include Bridgetown::Filters
10
+
11
+ Context = Struct.new(:registers)
12
+
13
+ def initialize(site)
14
+ @site = site
15
+
16
+ # duck typing for Liquid context
17
+ @context = Context.new({ site: @site })
18
+ end
19
+
20
+ def webpack_path(asset_type)
21
+ Bridgetown::Utils.parse_webpack_manifest_file(@site, asset_type.to_s)
22
+ end
23
+ end
24
+
25
+ attr_reader :layout, :page, :site, :content
26
+
27
+ def initialize(convertible)
28
+ if convertible.is_a?(Layout)
29
+ @layout = convertible
30
+ @page = layout.current_document
31
+ @content = layout.current_document_output
32
+ else
33
+ @page = convertible
34
+ end
35
+ @site = page.site
36
+ end
37
+
38
+ def partial(_partial_name, _options = {})
39
+ raise "Must be implemented in a subclass"
40
+ end
41
+
42
+ def site_drop
43
+ site.site_payload.site
44
+ end
45
+
46
+ def liquid_render(component, options = {})
47
+ render_statement = _render_statement(component, options)
48
+
49
+ template = site.liquid_renderer.file(
50
+ "#{page.path}.#{Digest::SHA2.hexdigest(render_statement)}"
51
+ ).parse(render_statement)
52
+ template.warnings.each do |e|
53
+ Bridgetown.logger.warn "Liquid Warning:",
54
+ LiquidRenderer.format_error(e, path || document.relative_path)
55
+ end
56
+ template.render!(options.deep_stringify_keys, _liquid_context)
57
+ end
58
+
59
+ def helpers
60
+ @helpers ||= Helpers.new(@site)
61
+ end
62
+
63
+ def method_missing(method, *args, &block)
64
+ if helpers.respond_to?(method.to_sym)
65
+ helpers.send method.to_sym, *args, &block
66
+ else
67
+ super
68
+ end
69
+ end
70
+
71
+ def respond_to_missing?(method, include_private = false)
72
+ helpers.respond_to?(method.to_sym, include_private) || super
73
+ end
74
+
75
+ private
76
+
77
+ def _render_statement(component, options)
78
+ render_statement = ["{% render \"#{component}\""]
79
+ unless options.empty?
80
+ render_statement << ", " + options.keys.map { |k| "#{k}: #{k}" }.join(", ")
81
+ end
82
+ render_statement << " %}"
83
+ render_statement.join
84
+ end
85
+
86
+ def _liquid_context
87
+ {
88
+ registers: {
89
+ site: site,
90
+ page: page,
91
+ cached_partials: Bridgetown::Renderer.cached_partials,
92
+ },
93
+ strict_filters: site.config["liquid"]["strict_filters"],
94
+ strict_variables: site.config["liquid"]["strict_variables"],
95
+ }
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Bridgetown
6
+ module Tags
7
+ # A ClassMap class is meant to take a hash and append styles based on if the
8
+ # value is truthy or falsy
9
+ #
10
+ # @example
11
+ # center-var = true
12
+ # small-var = nil
13
+ #
14
+ # # input
15
+ # <div class="{% class_map has-centered-text: center-var, is-small: small-var %}">
16
+ # Text
17
+ # </div>
18
+ #
19
+ # # output
20
+ # <div class="has-centered-text">
21
+ # Text
22
+ # </div>
23
+ class ClassMap < Liquid::Tag
24
+ # @see https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html
25
+ FALSE_VALUES = [
26
+ nil, "nil", "NIL", false, 0, "0", :"0", "f", :f, "F", :F, "false",
27
+ false, "FALSE", :FALSE,
28
+ ].to_set.freeze
29
+
30
+ # @param tag_name [String] The name to use for the tag
31
+ # @param input [String] The input to the tag
32
+ # @param tokens [Hash] A hash of config tokens for Liquid.
33
+ #
34
+ #
35
+ # @return [ClassMap] Returns a ClassMap object
36
+ def initialize(tag_name, input, tokens)
37
+ super
38
+ @input = input
39
+ end
40
+
41
+ def render(context)
42
+ class_map(@input, context)
43
+ end
44
+
45
+ private
46
+
47
+ def class_map(string, context)
48
+ ary = []
49
+
50
+ string.split(%r!,\s+!).each do |item|
51
+ kv_pair = item.split(%r!:\s+!)
52
+ klass = kv_pair[0]
53
+ variable = kv_pair[1]
54
+
55
+ # Check if a user wants the opposite of the variable
56
+ if variable[0] == "!"
57
+ check_opposite = true
58
+ variable.slice!(1..-1)
59
+ end
60
+
61
+ variable = find_variable(context, variable)
62
+
63
+ if check_opposite
64
+ ary.push(klass) if FALSE_VALUES.include?(variable)
65
+ else
66
+ ary.push(klass) unless FALSE_VALUES.include?(variable)
67
+ end
68
+ end
69
+
70
+ ary.join(" ")
71
+
72
+ # Gracefully handle if syntax is improper
73
+ rescue NoMethodError
74
+ "invalid-class-map"
75
+ end
76
+
77
+ def find_variable(context, variable)
78
+ lookup = context
79
+
80
+ variable.split(".").each do |value|
81
+ lookup = lookup[value.strip]
82
+ end
83
+
84
+ lookup || nil
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ Liquid::Template.register_tag("class_map", Bridgetown::Tags::ClassMap)
@@ -212,7 +212,9 @@ module Bridgetown
212
212
  else
213
213
  File.join(site.config["collections_dir"], page_payload["path"])
214
214
  end
215
+ # rubocop:disable Performance/DeleteSuffix
215
216
  resource_path.sub!(%r!/#excerpt\z!, "")
217
+ # rubocop:enable Performance/DeleteSuffix
216
218
  site.in_source_dir File.dirname(resource_path)
217
219
  end
218
220
  end
@@ -3,9 +3,12 @@
3
3
  module Bridgetown
4
4
  module Tags
5
5
  class BlockRenderTag < Liquid::Block
6
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
6
7
  def render(context)
7
8
  context.stack({}) do
8
- content = super.gsub(%r!^[ \t]+!, "") # unindent the incoming text
9
+ # unindent the incoming text
10
+ content = Bridgetown::Utils.reindent_for_markdown(super)
11
+
9
12
  regions = gather_content_regions(context)
10
13
 
11
14
  site = context.registers[:site]
@@ -17,7 +20,15 @@ module Bridgetown
17
20
  unless regions.empty?
18
21
  regions.each do |region_name, region_content|
19
22
  region_name = region_name.sub("content_with_region_", "")
20
- context[region_name] = converter.convert(region_content.gsub(%r!^[ \t]+!, ""))
23
+
24
+ if region_name.end_with? ":markdown"
25
+ region_name.sub!(%r!:markdown$!, "")
26
+ context[region_name] = converter.convert(
27
+ Bridgetown::Utils.reindent_for_markdown(region_content)
28
+ )
29
+ else
30
+ context[region_name] = region_content
31
+ end
21
32
  render_params.push "#{region_name}: #{region_name}"
22
33
  end
23
34
  end
@@ -26,6 +37,7 @@ module Bridgetown
26
37
  .render_tag(context, +"")
27
38
  end
28
39
  end
40
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
29
41
 
30
42
  private
31
43
 
@@ -2,38 +2,35 @@
2
2
 
3
3
  module Bridgetown
4
4
  module Tags
5
+ # A helper class to help find the path to webpack asset inside of a webpack
6
+ # manifest file.
5
7
  class WebpackPath < Liquid::Tag
6
- include Bridgetown::Filters::URLFilters
7
-
8
- def initialize(tag_name, asset_type, tokens)
8
+ # @param tag_name [String] Name of the tag
9
+ # @param asset_type [String] The type of asset to parse (js, css)
10
+ # @param options [Hash] An options hash
11
+ # @return [void]
12
+ # @see {https://www.rdoc.info/github/Shopify/liquid/Liquid/Tag#initialize-instance_method}
13
+ def initialize(tag_name, asset_type, options)
9
14
  super
10
15
 
11
16
  # js or css
12
17
  @asset_type = asset_type.strip
13
18
  end
14
19
 
20
+ # Render an asset path based on the Webpack manifest file
21
+ # @param context [Liquid::Context] Context passed to the tag
22
+ #
23
+ # @return [String] Returns "MISSING_WEBPACK_MANIFEST" if the manifest
24
+ # file isn't found
25
+ # @return [String] Returns a blank string if the asset isn't found
26
+ # @return [String] Returns the path to the asset if no issues parsing
27
+ #
28
+ # @raise [WebpackAssetError] if unable to find css or js in the manifest
29
+ # file
15
30
  def render(context)
16
31
  @context = context
17
32
  site = context.registers[:site]
18
-
19
- frontend_path = relative_url("_bridgetown/static")
20
-
21
- manifest_file = site.in_root_dir(".bridgetown-webpack", "manifest.json")
22
- if File.exist?(manifest_file)
23
- manifest = JSON.parse(File.read(manifest_file))
24
- if @asset_type == "js"
25
- js_path = manifest["main.js"].split("/").last
26
- [frontend_path, "js", js_path].join("/")
27
- elsif @asset_type == "css"
28
- css_path = manifest["main.css"].split("/").last
29
- [frontend_path, "css", css_path].join("/")
30
- else
31
- Bridgetown.logger.error("Unknown Webpack asset type", @asset_type)
32
- nil
33
- end
34
- else
35
- "MISSING_WEBPACK_MANIFEST"
36
- end
33
+ Bridgetown::Utils.parse_webpack_manifest_file(site, @asset_type) || ""
37
34
  end
38
35
  end
39
36
  end
@@ -287,6 +287,103 @@ module Bridgetown
287
287
  merged
288
288
  end
289
289
 
290
+ # Returns a string that's been reindented so that Markdown's four+ spaces =
291
+ # code doesn't get triggered for nested Liquid components
292
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
293
+ def reindent_for_markdown(input)
294
+ lines = input.lines
295
+ return input if lines.first.nil?
296
+
297
+ starting_indentation = lines.find { |line| line != "\n" }&.match(%r!^ +!)
298
+ return input unless starting_indentation
299
+
300
+ starting_indent_length = starting_indentation[0].length
301
+
302
+ skip_pre_lines = false
303
+ lines.map do |line|
304
+ continue_processing = !skip_pre_lines
305
+
306
+ if skip_pre_lines
307
+ skip_pre_lines = false if line.include?("</pre>")
308
+ end
309
+ if line.include?("<pre")
310
+ skip_pre_lines = true
311
+ continue_processing = false
312
+ end
313
+
314
+ if continue_processing
315
+ line_indentation = line.match(%r!^ +!).yield_self do |indent|
316
+ indent.nil? ? "" : indent[0]
317
+ end
318
+ new_indentation = line_indentation.rjust(starting_indent_length, " ")
319
+
320
+ if %r!^ +!.match?(line)
321
+ line
322
+ .sub(%r!^ {1,#{starting_indent_length}}!, new_indentation)
323
+ .sub(%r!^#{new_indentation}!, "")
324
+ else
325
+ line
326
+ end
327
+ else
328
+ line
329
+ end
330
+ end.join("")
331
+ end
332
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
333
+
334
+ # Return an asset path based on the Webpack manifest file
335
+ # @param site [Bridgetown::Site] The current site object
336
+ # @param asset_type [String] js or css
337
+ #
338
+ # @return [String] Returns "MISSING_WEBPACK_MANIFEST" if the manifest
339
+ # file isnt found
340
+ # @return [nil] Returns nil if the asset isnt found
341
+ # @return [String] Returns the path to the asset if no issues parsing
342
+ #
343
+ # @raise [WebpackAssetError] if unable to find css or js in the manifest
344
+ # file
345
+ def parse_webpack_manifest_file(site, asset_type)
346
+ manifest_file = site.in_root_dir(".bridgetown-webpack", "manifest.json")
347
+ return "MISSING_WEBPACK_MANIFEST" unless File.exist?(manifest_file)
348
+
349
+ manifest = JSON.parse(File.read(manifest_file))
350
+
351
+ known_assets = %w(js css)
352
+ if known_assets.include?(asset_type)
353
+ asset_path = manifest["main.#{asset_type}"]
354
+
355
+ log_webpack_asset_error(asset_type) && return if asset_path.nil?
356
+
357
+ asset_path = asset_path.split("/").last
358
+ return [static_frontend_path(site), asset_type, asset_path].join("/")
359
+ end
360
+
361
+ Bridgetown.logger.error("Unknown Webpack asset type", asset_type)
362
+ nil
363
+ end
364
+
365
+ def static_frontend_path(site)
366
+ path_parts = [site.config["baseurl"].to_s.chomp("/"), "_bridgetown/static"]
367
+ path_parts[0] = "/#{path_parts[0]}" unless path_parts[0].empty?
368
+ Addressable::URI.parse(path_parts.join("/")).normalize.to_s
369
+ end
370
+
371
+ def log_webpack_asset_error(asset_type)
372
+ error_message = "There was an error parsing your #{asset_type} files. \
373
+ Please check your #{asset_type} for any errors."
374
+
375
+ Bridgetown.logger.warn(Errors::WebpackAssetError, error_message)
376
+ end
377
+
378
+ def default_github_branch_name(repo_url)
379
+ repo_match = Bridgetown::Commands::Actions::GITHUB_REPO_REGEX.match(repo_url)
380
+ api_endpoint = "https://api.github.com/repos/#{repo_match[1]}"
381
+ JSON.parse(Faraday.get(api_endpoint).body)["default_branch"] || "master"
382
+ rescue StandardError => e
383
+ Bridgetown.logger.warn("Unable to connect to GitHub API: #{e.message}")
384
+ "master"
385
+ end
386
+
290
387
  private
291
388
 
292
389
  def merge_values(target, overwrite)