bridgetown-builder 1.0.0 → 1.1.0.beta3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57644330313669593a0ef79303b8bb692fd2ea7d69fbed8c878d5d33918d4bed
4
- data.tar.gz: 3a79d7726250d9c477b1afb214a6b1aff3fd45b5d400467ba73844d9b608bdc6
3
+ metadata.gz: bbc911c29c7452724a7aac033623d55d478c210e0c0016b17cc615bd81ca3432
4
+ data.tar.gz: 3ef397e52602f72936aacecda5ba216a00a74108f8032fcc1548483d5490ddd6
5
5
  SHA512:
6
- metadata.gz: b31cae7e13054287b31cc4eaac0246370bd366ce8c8537d5a220ebde1daaf0ad27204ab40e9e09b8e47361cd06b4f6e646acc14da00833eea8ac84e3c4223d8c
7
- data.tar.gz: a165e07527cf5c2302e66729648445ec7cd84a030c9fa06bb89ca97e57a7a6c3c96981a8fb9838adcb1e5b1dacabf7c193ec8c58afeea3994a71d55b6b3c65b4
6
+ metadata.gz: 91f62d820a391edad6b285bd1411648de5177a3c66b27c5e5d4151b46b979cffc84c648bf74cd6a143f503a23ac79b75a14be3f3ab0020ebd1531ab3d6d6855c
7
+ data.tar.gz: 8fa3a72a20b0b6008fbbc3f6cee4436a953541c08ba77aea9d49dfa6570e61dd697e46f83eb51d24141c4ff15d772f232b9900912bea6a090a1117b66b622fee
@@ -10,9 +10,7 @@ module Bridgetown
10
10
 
11
11
  class << self
12
12
  def register
13
- Bridgetown::Hooks.register_one :site, :pre_read, reloadable: false do |site|
14
- new(name, site).build_with_callbacks
15
- end
13
+ Bridgetown::Builders::PluginBuilder.plugin_registrations << self
16
14
  end
17
15
 
18
16
  def before_build(*args, **kwargs, &block)
@@ -7,13 +7,16 @@ module Bridgetown
7
7
  def generator(method_name = nil, &block)
8
8
  block = method(method_name) if method_name.is_a?(Symbol)
9
9
  local_name = name # pull the name method into a local variable
10
+ builder_priority = self.class.instance_variable_get(:@priority)
10
11
 
11
- new_gen = Class.new(Bridgetown::Generator) do
12
+ anon_generator = Class.new(Bridgetown::Generator) do
12
13
  define_method(:_builder_block) { block }
13
14
  define_singleton_method(:custom_name) { local_name }
14
15
 
15
16
  attr_reader :site
16
17
 
18
+ priority builder_priority || :low
19
+
17
20
  def inspect
18
21
  "#<#{self.class.custom_name} (Generator)>"
19
22
  end
@@ -23,10 +26,10 @@ module Bridgetown
23
26
  end
24
27
  end
25
28
 
26
- first_low_priority_index = site.generators.find_index { |gen| gen.class.priority == :low }
27
- site.generators.insert(first_low_priority_index || 0, new_gen.new(site.config))
29
+ site.generators << anon_generator.new(site.config)
30
+ site.generators.sort!
28
31
 
29
- functions << { name: name, generator: new_gen }
32
+ functions << { name: name, generator: anon_generator }
30
33
  end
31
34
  end
32
35
  end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Builders
5
+ module DSL
6
+ module Inspectors
7
+ # Add a couple familar DOM API features
8
+ module QuerySelection
9
+ def query_selector(selector)
10
+ css(selector).first
11
+ end
12
+
13
+ def query_selector_all(selector)
14
+ css(selector)
15
+ end
16
+ end
17
+
18
+ # HTML inspector type
19
+ module HTML
20
+ # Are there inspectors available? Is it an .htm* file?
21
+ def self.can_run?(resource, inspectors)
22
+ inspectors &&
23
+ resource.output_ext&.starts_with?(".htm") &&
24
+ !resource.data.bypass_inspectors
25
+ end
26
+
27
+ # Process the resource with the available inspectors and return the output HTML
28
+ #
29
+ # @return [String] transformed HTML
30
+ def self.call(resource, inspectors)
31
+ doc = Nokogiri.HTML5(resource.output)
32
+
33
+ inspectors.each do |block|
34
+ block.call(doc, resource)
35
+ end
36
+
37
+ doc.to_html
38
+ end
39
+ end
40
+
41
+ # XML inspector type
42
+ module XML
43
+ # Strip the resource's initial extension dot. `.rss` => `rss`
44
+ def self.resource_ext(resource)
45
+ resource.output_ext&.delete_prefix(".")
46
+ end
47
+
48
+ # Are there any inspectors available which match the resource extension?
49
+ def self.can_run?(resource, inspectors)
50
+ inspectors &&
51
+ inspectors[resource_ext(resource)] &&
52
+ !resource.data.bypass_inspectors
53
+ end
54
+
55
+ # Process the resource with the available inspectors and return the output XML
56
+ #
57
+ # @return [String] transformed XML
58
+ def self.call(resource, inspectors)
59
+ doc = Nokogiri::XML(resource.output)
60
+
61
+ inspectors[resource_ext(resource)].each do |block|
62
+ block.call(doc, resource)
63
+ end
64
+
65
+ doc.to_xml
66
+ end
67
+ end
68
+
69
+ class << self
70
+ # Require the Nokogiri gem if necessary and add the `QuerySelection` mixin
71
+ def setup_nokogiri
72
+ unless defined?(Nokogiri)
73
+ Bridgetown::Utils::RequireGems.require_with_graceful_fail "nokogiri"
74
+ end
75
+
76
+ return if Nokogiri::XML::Node <= QuerySelection
77
+
78
+ Nokogiri::XML::Node.include QuerySelection
79
+ end
80
+
81
+ # Shorthand for `HTML.call`
82
+ def process_html(...)
83
+ HTML.call(...)
84
+ end
85
+
86
+ # Shorthand for `XML.call`
87
+ def process_xml(...)
88
+ XML.call(...)
89
+ end
90
+ end
91
+
92
+ # Set up an inspector to review or manipulate HTML resources
93
+ # @yield the block to be called after the resource has been rendered
94
+ # @yieldparam [Nokogiri::HTML5::Document] the Nokogiri document
95
+ def inspect_html(&block)
96
+ unless @_html_inspectors
97
+ @_html_inspectors = []
98
+
99
+ Inspectors.setup_nokogiri
100
+
101
+ hook :resources, :post_render do |resource|
102
+ next unless HTML.can_run?(resource, @_html_inspectors)
103
+
104
+ resource.output = Inspectors.process_html(resource, @_html_inspectors)
105
+ end
106
+
107
+ hook :generated_pages, :post_render do |page|
108
+ next unless HTML.can_run?(page, @_html_inspectors)
109
+
110
+ page.output = Inspectors.process_html(page, @_html_inspectors)
111
+ end
112
+ end
113
+
114
+ @_html_inspectors << block
115
+ end
116
+
117
+ # Set up an inspector to review or manipulate XML resources
118
+ # @param extension [String] defaults to `xml`
119
+ # @yield the block to be called after the resource has been rendered
120
+ # @yieldparam [Nokogiri::XML::Document] the Nokogiri document
121
+ def inspect_xml(extension = "xml", &block)
122
+ unless @_xml_inspectors
123
+ @_xml_inspectors = {}
124
+
125
+ Inspectors.setup_nokogiri
126
+
127
+ hook :resources, :post_render do |resource|
128
+ next unless Inspectors::XML.can_run?(resource, @_xml_inspectors)
129
+
130
+ resource.output = Inspectors.process_xml(resource, @_xml_inspectors)
131
+ end
132
+
133
+ hook :generated_pages, :post_render do |page|
134
+ next unless Inspectors::XML.can_run?(page, @_xml_inspectors)
135
+
136
+ page.output = Inspectors.process_xml(page, @_xml_inspectors)
137
+ end
138
+ end
139
+
140
+ (@_xml_inspectors[extension.to_s] ||= []).tap do |arr|
141
+ arr << block
142
+ end
143
+
144
+ @_xml_inspectors
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -3,21 +3,40 @@
3
3
  require "bridgetown-builder/dsl/generators"
4
4
  require "bridgetown-builder/dsl/helpers"
5
5
  require "bridgetown-builder/dsl/hooks"
6
+ require "bridgetown-builder/dsl/inspectors"
6
7
  require "bridgetown-builder/dsl/http"
7
8
  require "bridgetown-builder/dsl/liquid"
8
9
  require "bridgetown-builder/dsl/resources"
10
+
9
11
  module Bridgetown
10
12
  module Builders
11
13
  class PluginBuilder
14
+ include Bridgetown::Prioritizable
15
+
16
+ self.priorities = {
17
+ highest: 100,
18
+ high: 10,
19
+ normal: 0,
20
+ low: -10,
21
+ lowest: -100,
22
+ }.freeze
23
+
12
24
  include DSL::Generators
13
25
  include DSL::Helpers
14
26
  include DSL::Hooks
27
+ include DSL::Inspectors
15
28
  include DSL::HTTP
16
29
  include DSL::Liquid
17
30
  include DSL::Resources
18
31
 
19
32
  attr_accessor :functions, :name, :site, :config
20
33
 
34
+ class << self
35
+ def plugin_registrations
36
+ @plugin_registrations ||= Set.new
37
+ end
38
+ end
39
+
21
40
  def initialize(name = nil, current_site = nil)
22
41
  self.functions = Set.new
23
42
  self.name = name || self.class.name
@@ -12,12 +12,14 @@ module Bridgetown
12
12
  end
13
13
 
14
14
  Bridgetown::Hooks.register_one :site, :pre_read, priority: :low, reloadable: false do |site|
15
+ builders = Bridgetown::Builders::PluginBuilder.plugin_registrations.to_a
16
+
15
17
  # SiteBuilder is the superclass sites can subclass to create any number of
16
18
  # builders, but if the site hasn't defined it explicitly, this is a no-op
17
- if defined?(SiteBuilder)
18
- SiteBuilder.descendants.map do |c|
19
- c.new(c.name, site).build_with_callbacks
20
- end
19
+ builders += SiteBuilder.descendants if defined?(SiteBuilder)
20
+
21
+ builders.sort.map do |c|
22
+ c.new(c.name, site).build_with_callbacks
21
23
  end
22
24
  end
23
25
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-07 00:00:00.000000000 Z
11
+ date: 2022-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0
19
+ version: 1.1.0.beta3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.0
26
+ version: 1.1.0.beta3
27
27
  description:
28
28
  email: maintainers@bridgetownrb.com
29
29
  executables: []
@@ -39,6 +39,7 @@ files:
39
39
  - lib/bridgetown-builder/dsl/helpers.rb
40
40
  - lib/bridgetown-builder/dsl/hooks.rb
41
41
  - lib/bridgetown-builder/dsl/http.rb
42
+ - lib/bridgetown-builder/dsl/inspectors.rb
42
43
  - lib/bridgetown-builder/dsl/liquid.rb
43
44
  - lib/bridgetown-builder/dsl/resources.rb
44
45
  - lib/bridgetown-builder/plugin.rb
@@ -57,9 +58,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
58
  version: '0'
58
59
  required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  requirements:
60
- - - ">="
61
+ - - ">"
61
62
  - !ruby/object:Gem::Version
62
- version: '0'
63
+ version: 1.3.1
63
64
  requirements: []
64
65
  rubygems_version: 3.1.4
65
66
  signing_key: