bridgetown-core 0.15.0.beta4 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +14 -0
  3. data/bridgetown-core.gemspec +3 -1
  4. data/lib/bridgetown-core.rb +7 -1
  5. data/lib/bridgetown-core/commands/concerns/actions.rb +2 -1
  6. data/lib/bridgetown-core/commands/console.rb +4 -4
  7. data/lib/bridgetown-core/concerns/data_accessible.rb +19 -0
  8. data/lib/bridgetown-core/concerns/layout_placeable.rb +17 -0
  9. data/lib/bridgetown-core/concerns/liquid_renderable.rb +20 -0
  10. data/lib/bridgetown-core/concerns/publishable.rb +10 -0
  11. data/lib/bridgetown-core/concerns/site/configurable.rb +66 -31
  12. data/lib/bridgetown-core/concerns/site/content.rb +88 -29
  13. data/lib/bridgetown-core/concerns/site/extensible.rb +15 -12
  14. data/lib/bridgetown-core/concerns/site/localizable.rb +20 -0
  15. data/lib/bridgetown-core/concerns/site/processable.rb +12 -10
  16. data/lib/bridgetown-core/concerns/site/renderable.rb +21 -2
  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 +5 -2
  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/site_drop.rb +1 -1
  26. data/lib/bridgetown-core/errors.rb +2 -0
  27. data/lib/bridgetown-core/excerpt.rb +1 -6
  28. data/lib/bridgetown-core/filters.rb +3 -48
  29. data/lib/bridgetown-core/filters/condition_helpers.rb +56 -0
  30. data/lib/bridgetown-core/frontmatter_defaults.rb +17 -0
  31. data/lib/bridgetown-core/layout.rb +24 -1
  32. data/lib/bridgetown-core/liquid_renderer/file_system.rb +1 -1
  33. data/lib/bridgetown-core/page.rb +33 -24
  34. data/lib/bridgetown-core/plugin_manager.rb +10 -2
  35. data/lib/bridgetown-core/reader.rb +1 -0
  36. data/lib/bridgetown-core/readers/collection_reader.rb +1 -0
  37. data/lib/bridgetown-core/readers/data_reader.rb +1 -0
  38. data/lib/bridgetown-core/readers/defaults_reader.rb +27 -0
  39. data/lib/bridgetown-core/readers/layout_reader.rb +1 -0
  40. data/lib/bridgetown-core/readers/page_reader.rb +1 -0
  41. data/lib/bridgetown-core/readers/post_reader.rb +1 -0
  42. data/lib/bridgetown-core/readers/static_file_reader.rb +1 -0
  43. data/lib/bridgetown-core/regenerator.rb +1 -1
  44. data/lib/bridgetown-core/renderer.rb +38 -12
  45. data/lib/bridgetown-core/ruby_template_view.rb +102 -0
  46. data/lib/bridgetown-core/site.rb +2 -0
  47. data/lib/bridgetown-core/tags/class_map.rb +90 -0
  48. data/lib/bridgetown-core/tags/find.rb +86 -0
  49. data/lib/bridgetown-core/tags/t.rb +14 -0
  50. data/lib/bridgetown-core/tags/webpack_path.rb +19 -22
  51. data/lib/bridgetown-core/utils.rb +55 -2
  52. data/lib/bridgetown-core/version.rb +2 -2
  53. data/lib/site_template/src/_layouts/{default.html → default.liquid} +0 -0
  54. data/lib/site_template/src/_layouts/{home.html → home.liquid} +0 -0
  55. data/lib/site_template/src/_layouts/{page.html → page.liquid} +0 -0
  56. data/lib/site_template/src/_layouts/{post.html → post.liquid} +0 -0
  57. metadata +50 -10
  58. data/lib/bridgetown-core/concerns/convertible.rb +0 -235
@@ -68,12 +68,20 @@ module Bridgetown
68
68
  # If that exact package hasn't been installed, execute yarn add
69
69
  #
70
70
  # Returns nothing.
71
- def self.install_yarn_dependencies(required_gems)
71
+ def self.install_yarn_dependencies(required_gems, single_gemname = nil)
72
72
  return unless File.exist?("package.json")
73
73
 
74
74
  package_json = JSON.parse(File.read("package.json"))
75
75
 
76
- required_gems.each do |loaded_gem|
76
+ gems_to_search = if single_gemname
77
+ required_gems.select do |loaded_gem|
78
+ loaded_gem.to_spec&.name == single_gemname.to_s
79
+ end
80
+ else
81
+ required_gems
82
+ end
83
+
84
+ gems_to_search.each do |loaded_gem|
77
85
  yarn_dependency = find_yarn_dependency(loaded_gem)
78
86
  next unless add_yarn_dependency?(yarn_dependency, package_json)
79
87
 
@@ -13,6 +13,7 @@ module Bridgetown
13
13
  # Returns nothing.
14
14
  # rubocop:disable Metrics/AbcSize
15
15
  def read
16
+ @site.defaults_reader.read
16
17
  @site.layouts = LayoutReader.new(site).read
17
18
  read_directories
18
19
  read_included_excludes
@@ -5,6 +5,7 @@ module Bridgetown
5
5
  SPECIAL_COLLECTIONS = %w(posts data).freeze
6
6
 
7
7
  attr_reader :site, :content
8
+
8
9
  def initialize(site)
9
10
  @site = site
10
11
  @content = {}
@@ -3,6 +3,7 @@
3
3
  module Bridgetown
4
4
  class DataReader
5
5
  attr_reader :site, :content
6
+
6
7
  def initialize(site)
7
8
  @site = site
8
9
  @content = ActiveSupport::HashWithIndifferentAccess.new
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ class DefaultsReader
5
+ attr_reader :site, :path_defaults
6
+
7
+ def initialize(site)
8
+ @site = site
9
+ @path_defaults = ActiveSupport::HashWithIndifferentAccess.new
10
+ end
11
+
12
+ def read
13
+ return unless File.directory?(site.source)
14
+
15
+ entries = Dir.chdir(site.source) do
16
+ Dir["**/_defaults.{yaml,yml,json}"]
17
+ end
18
+
19
+ entries.each do |entry|
20
+ path = @site.in_source_dir(entry)
21
+ @path_defaults[File.dirname(path) + File::SEPARATOR] = SafeYAML.load_file(path)
22
+ end
23
+
24
+ @path_defaults
25
+ end
26
+ end
27
+ end
@@ -3,6 +3,7 @@
3
3
  module Bridgetown
4
4
  class LayoutReader
5
5
  attr_reader :site
6
+
6
7
  def initialize(site)
7
8
  @site = site
8
9
  @layouts = {}
@@ -3,6 +3,7 @@
3
3
  module Bridgetown
4
4
  class PageReader
5
5
  attr_reader :site, :dir, :unfiltered_content
6
+
6
7
  def initialize(site, dir)
7
8
  @site = site
8
9
  @dir = dir
@@ -3,6 +3,7 @@
3
3
  module Bridgetown
4
4
  class PostReader
5
5
  attr_reader :site, :unfiltered_content
6
+
6
7
  def initialize(site)
7
8
  @site = site
8
9
  end
@@ -3,6 +3,7 @@
3
3
  module Bridgetown
4
4
  class StaticFileReader
5
5
  attr_reader :site, :dir, :unfiltered_content
6
+
6
7
  def initialize(site, dir)
7
8
  @site = site
8
9
  @dir = dir
@@ -163,7 +163,7 @@ module Bridgetown
163
163
  end
164
164
 
165
165
  def regenerate_page?(document)
166
- document.asset_file? || document.data["regenerate"] ||
166
+ document.data["regenerate"] ||
167
167
  source_modified_or_dest_missing?(
168
168
  site.in_source_dir(document.relative_path), document.destination(@site.dest)
169
169
  )
@@ -58,6 +58,7 @@ module Bridgetown
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!
@@ -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,102 @@
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
+
24
+ def t(*args)
25
+ I18n.send :t, *args
26
+ end
27
+ end
28
+
29
+ attr_reader :layout, :page, :site, :content
30
+
31
+ def initialize(convertible)
32
+ if convertible.is_a?(Layout)
33
+ @layout = convertible
34
+ @page = layout.current_document
35
+ @content = layout.current_document_output
36
+ else
37
+ @page = convertible
38
+ end
39
+ @site = page.site
40
+ end
41
+
42
+ def partial(_partial_name, _options = {})
43
+ raise "Must be implemented in a subclass"
44
+ end
45
+
46
+ def site_drop
47
+ site.site_payload.site
48
+ end
49
+
50
+ def liquid_render(component, options = {})
51
+ render_statement = _render_statement(component, options)
52
+
53
+ template = site.liquid_renderer.file(
54
+ "#{page.path}.#{Digest::SHA2.hexdigest(render_statement)}"
55
+ ).parse(render_statement)
56
+ template.warnings.each do |e|
57
+ Bridgetown.logger.warn "Liquid Warning:",
58
+ LiquidRenderer.format_error(e, path || document.relative_path)
59
+ end
60
+ template.render!(options.deep_stringify_keys, _liquid_context)
61
+ end
62
+
63
+ def helpers
64
+ @helpers ||= Helpers.new(@site)
65
+ end
66
+
67
+ def method_missing(method, *args, &block)
68
+ if helpers.respond_to?(method.to_sym)
69
+ helpers.send method.to_sym, *args, &block
70
+ else
71
+ super
72
+ end
73
+ end
74
+
75
+ def respond_to_missing?(method, include_private = false)
76
+ helpers.respond_to?(method.to_sym, include_private) || super
77
+ end
78
+
79
+ private
80
+
81
+ def _render_statement(component, options)
82
+ render_statement = ["{% render \"#{component}\""]
83
+ unless options.empty?
84
+ render_statement << ", " + options.keys.map { |k| "#{k}: #{k}" }.join(", ")
85
+ end
86
+ render_statement << " %}"
87
+ render_statement.join
88
+ end
89
+
90
+ def _liquid_context
91
+ {
92
+ registers: {
93
+ site: site,
94
+ page: page,
95
+ cached_partials: Bridgetown::Renderer.cached_partials,
96
+ },
97
+ strict_filters: site.config["liquid"]["strict_filters"],
98
+ strict_variables: site.config["liquid"]["strict_variables"],
99
+ }
100
+ end
101
+ end
102
+ end
@@ -7,6 +7,7 @@ module Bridgetown
7
7
  include Configurable
8
8
  include Content
9
9
  include Extensible
10
+ include Localizable
10
11
  include Processable
11
12
  include Renderable
12
13
  include Writable
@@ -25,6 +26,7 @@ module Bridgetown
25
26
  # config - A Hash containing site configuration details.
26
27
  def initialize(config)
27
28
  self.config = config
29
+ locale
28
30
 
29
31
  @plugin_manager = PluginManager.new(self)
30
32
  @cleaner = Cleaner.new(self)
@@ -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)
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Tags
5
+ class Find < Liquid::Tag
6
+ include Bridgetown::Filters::ConditionHelpers
7
+ include Bridgetown::LiquidExtensions
8
+
9
+ SYNTAX = %r!^(.*?) (where|in) (.*?),(.*)$!.freeze
10
+ CONDITIONS_SEP = "~FINDSEP~"
11
+
12
+ def initialize(tag_name, markup, tokens)
13
+ super
14
+ if markup.strip =~ SYNTAX
15
+ @new_var_name = Regexp.last_match(1).strip
16
+ @single_or_group = Regexp.last_match(2)
17
+ @arr_name = Regexp.last_match(3).strip
18
+ @conditions = process_conditions(Regexp.last_match(4).strip)
19
+ else
20
+ raise SyntaxError, <<~MSG
21
+ Syntax Error in tag 'find' while parsing the following markup:
22
+
23
+ #{markup}
24
+
25
+ Valid syntax: find <varname> where|in <array>, <condition(s)>
26
+ MSG
27
+ end
28
+ end
29
+
30
+ def render(context)
31
+ @group = lookup_variable(context, @arr_name)
32
+ return "" unless @group.respond_to?(:select)
33
+
34
+ @group = @group.values if @group.is_a?(Hash)
35
+
36
+ expression = @conditions.split(CONDITIONS_SEP).map do |condition|
37
+ "__find_tag_item__.#{condition.strip}"
38
+ end.join(" and ")
39
+ @liquid_condition = parse_condition(expression)
40
+
41
+ context[@new_var_name] = if @single_or_group == "where"
42
+ group_evaluate(context)
43
+ else
44
+ single_evaluate(context)
45
+ end
46
+
47
+ ""
48
+ end
49
+
50
+ private
51
+
52
+ def process_conditions(conditions)
53
+ processed_conditions = +""
54
+ in_quotes = false
55
+
56
+ conditions.each_char do |c|
57
+ in_quotes = !in_quotes if c == '"'
58
+
59
+ processed_conditions << (c == "," && !in_quotes ? CONDITIONS_SEP : c)
60
+ end
61
+
62
+ processed_conditions
63
+ end
64
+
65
+ def group_evaluate(context)
66
+ context.stack do
67
+ @group.select do |object|
68
+ context["__find_tag_item__"] = object
69
+ @liquid_condition.evaluate(context)
70
+ end
71
+ end || []
72
+ end
73
+
74
+ def single_evaluate(context)
75
+ context.stack do
76
+ @group.find do |object|
77
+ context["__find_tag_item__"] = object
78
+ @liquid_condition.evaluate(context)
79
+ end
80
+ end || nil
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ Liquid::Template.register_tag("find", Bridgetown::Tags::Find)