jekyll-gem-resolver 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 74d9ea3d879cc2078bf4ca0e6b68eaacf59ec41f0d558106b4e05db9ade46642
4
+ data.tar.gz: d6b0244738b348355b8a02454ab55a7a85e19e34a2deeecc44ed9389fb1d8b0a
5
+ SHA512:
6
+ metadata.gz: 79f4e904fb806be39b7768bf11c4b5c0669eab0fc8d7d8fd3e1d3d8310c71333ba8d711efdb3fbc7b8b88b81bdf5c7dbb98c9a05330e24c8aef21803ad7afc25
7
+ data.tar.gz: 7d84e011b33b4495ba69d1baefd8549e769bb12927935696ad1877f4cf3f4d852cafc9dd89426d7eafe7392a6a76abdf169df0cdb5292690d8ee7b8e8c12b292
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Lukas Waslowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,196 @@
1
+ # jekyll-gem-resolver
2
+
3
+ Simple Jekyll plugin to allow including resources from non-Jekyll gems into the site build.
4
+
5
+ ### Purpose
6
+
7
+ When enabled, the plugin scans certain, configurable parts of your `_config.yml` file for references of the form `"gem:package:path/in/package"` (or `"gem:package/path/in/package"`) and replaces them with absolute paths to the correct Gem directory/file, using the Gem version specified in your `Gemfile` / `Gemfile.lock`.
8
+
9
+ ### History
10
+
11
+ The origin of this plugin is that I wanted to build [Bootstrap][bootstrap-website]
12
+ from source using Jekyll's SASS processor and the [boostrap gem][bootstrap-rubygem]. But there is no easy way to reference the external files of the `bootstrap` gem
13
+ in your `_config.yml`, except for using absolute paths, which feels unsatisfying for a number of reasons.
14
+ This plugin was born to resolve this problem.
15
+
16
+ ## Installation
17
+
18
+ This plugin is available as a [RubyGem][published-ruby-gem].
19
+
20
+ Add this line to your application's `Gemfile`:
21
+
22
+ ```
23
+ gem 'jekyll-gem-resoler'
24
+ ```
25
+
26
+ And then execute the `bundle` command to install the gem.
27
+
28
+ Alternatively, you can also manually install the gem using the following command:
29
+
30
+ ```
31
+ $ gem install jekyll-gem-resolver
32
+ ```
33
+
34
+ After the plugin has been installed successfully, add the following lines to your `_config.yml` in order to tell Jekyll to use the plugin:
35
+
36
+ ```yaml
37
+ plugins:
38
+ - jekyll-gem-resolver
39
+
40
+ gem_resolver:
41
+ transform:
42
+ - sass.load_paths
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ A typical configuration could look like this:
48
+
49
+ ```ruby
50
+ # Excerpt from <your_directory>/Gemfile
51
+ source "https://rubygems.org"
52
+
53
+ # Or whatever version of Jekyll you are using
54
+ gem "jekyll", "~> 4.3.3"
55
+
56
+ # Declare the gems that you want to reference
57
+ gem "bootstrap", "~> 5.3.2"
58
+
59
+ group :jekyll_plugins do
60
+ gem "jekyll-gem-resolver", "~> 1.0"
61
+ end
62
+ ```
63
+
64
+ ```yaml
65
+ # Excerpt from <your_directory>/_config.yml
66
+ plugins:
67
+ - jekyll-gem-resolver
68
+
69
+ # A configuration for another plugin that references files and/or folders from a Gem
70
+ sass:
71
+ load_paths:
72
+ - 'gem:bootstrap/assets/stylesheets'
73
+
74
+
75
+ # Tell jekyll-gem-resolver where in the configuration to look for
76
+ # "gem:" references. The format is explained below.
77
+ gem_resolver:
78
+ transform:
79
+ - sass.load_paths"
80
+ ```
81
+
82
+ The usage of the plugin is split into two parts:
83
+
84
+ 1. Specifying where in the configuration `gem:` paths might appear
85
+ using a JSONPath-like language.
86
+
87
+ 2. Using a `gem:<..>` reference in those places in the configuration.
88
+
89
+ ### Gem Reference Syntax
90
+
91
+ Gem references are written like this `"gem:package_name:path/in/package"` (or `"gem:package_name/path/in/package"`, if `package_name` does not contain a `'/'` character), which is resolved to something like:
92
+
93
+ ```
94
+ /home/<your_username>/.gems/gems/<package_name>@<version_from_gemfile>/<path_in_package>
95
+ ```
96
+
97
+ Note that you have to use **double or single quotes** around the `gem:` references in your YAML file,
98
+ because without it, the `gem:` prefix would erroneously be parsed as a hash by the YAML parer.
99
+
100
+ ### Path Syntax
101
+
102
+ A path consists of multiple segments separated by a `'.'` character.
103
+ Each segment is either:
104
+
105
+ - an identifier (e.g. `myplugin.config_setting`, for hash elements),
106
+ - a number (e.g. `myplugin.items.1`, for array elements), or
107
+ - the string `*` (e.g. `myplugin.items.*.ident`, meaning "all elements of this array or hash").
108
+
109
+ Each segment can optionally be wrapped in `[brackets]`,
110
+ which is just syntatic sugar for the same thing without brackets. By convention, we write array indidces and `*` in brackets, and hash elements without:
111
+
112
+ ```
113
+ my_plugin.array.[1]
114
+ my_plugin.array.[*]
115
+ my_plugin.hash.something
116
+ my_plugin.hash.[*]
117
+ ```
118
+
119
+ When referring to all items of an array as the last segment of a path (e.g. `my_plugin.array.[*]`), you can omit the final `*` and just write it as: `my_plugin.array`.
120
+
121
+ #### Some more examples
122
+
123
+ ```yaml
124
+ # You can process global settings
125
+ global_setting: "gem:example" # Matched by: "global-setting"
126
+
127
+ # You process items in arrays
128
+ global_array:
129
+ - "gem:package1/assets/js" # Matched by "global_array", "global_array.[*]" and "global_array.[0]"
130
+ - "gem:package2/assets/css" # Matched by "global_array", "global_array.[*]" and "global_array.[1]"
131
+ - "gem:package3/assets/js" # Matched by "global_array", "global_array.[*]" and "global_array.[2]"
132
+
133
+ # You can transform items in nested arrays as well
134
+ my_plugin:
135
+ - name: "I don't know"
136
+ children:
137
+ # Matched by:
138
+ # "my_plugin.[*].children"
139
+ # "my_plugin.[*].children.[*]"
140
+ # "my_plugin.[*].children.[1]
141
+ # "my_plugin.[0].children.[*]
142
+ # "my_plugin.[0].children.[1]"
143
+ - "gem:package1/assets/js"
144
+
145
+ - name: "I don't know either"
146
+ children:
147
+ # Matched by:
148
+ # "my_plugin.[*].children"
149
+ # "my_plugin.[*].children.[*]"
150
+ # "my_plugin.[*].children.[0]
151
+ # "my_plugin.[1].children.[*]
152
+ # "my_plugin.[1].children.[0]"
153
+ - "gem:package2/assets/css"
154
+ # Matched by:
155
+ # "my_plugin.[*].children"
156
+ # "my_plugin.[*].children.[*]"
157
+ # "my_plugin.[*].children.[1]
158
+ # "my_plugin.[1].children.[*]
159
+ # "my_plugin.[1].children.[1]"
160
+ - "gem:package3/assets/js"
161
+
162
+ # You can even process items in hashes
163
+ categories:
164
+ category_a:
165
+ # Matched by:
166
+ # "categories.category_a.template_styles
167
+ # "categories.[*].template_styles
168
+ template_styles: "gem:package4/templates/"
169
+ category_b:
170
+ # Matched by:
171
+ # "categories.category_a.template_styles
172
+ # "categories.[*].template_styles
173
+ template_styles: "gem:package4/templates/"
174
+
175
+ # Note that omission of the final ".[*]" only works for arrays,
176
+ # not for hashes:
177
+ my_other_plugin:
178
+ config_dict:
179
+ # The string values below are matched by "my_other_plugin.config_dict.[*]",
180
+ # but NOT by "my_other_plugin.config_dict".
181
+ entry_a: "gem:package5/abc"
182
+ entry_b: "gem:package5/def"
183
+ entry_c: "gem:package5/ghi"
184
+ ```
185
+
186
+ # Contribute
187
+ Fork this repository, make your changes and then issue a pull request. If you find bugs or have new ideas that you do not want to implement yourself, file a bug report.
188
+
189
+ # Copyright
190
+ Copyright (c) 2024 Lukas Waslowski.
191
+
192
+ License: MIT
193
+
194
+ [bootstrap-website]: https://getbootstrap.com
195
+ [bootstrap-rubygem]: https://github.com/twbs/bootstrap-rubygem
196
+ [published-ruby-gem]: https://rubygems.org/gems/jekyll-gem-resolver
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module GemResolver
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,77 @@
1
+ module JekyllGemResolver
2
+ def self.transform_config(site)
3
+ options = site.config['gem_resolver'] || {}
4
+ (options['transform'] || []).each { |path_to_transform| transform_config_node(site.config, path_to_transform.split('.')) }
5
+ end
6
+
7
+ private
8
+
9
+ def self.transform_config_node(node, path, use_fallbacks = true)
10
+ # Have we arrived at the last segment of the path?
11
+ if path.nil? || path.empty?
12
+ # Special behavior: If this node is an array, behave as if path contains an additional '.*'
13
+ return transform_config_node(node, ['*'], false) if node.is_a?(Array) && use_fallbacks
14
+
15
+ # Not an array: Directly process this node
16
+ return transform_config_value(node)
17
+ end
18
+
19
+ # The path has at least one remaining segment
20
+ key = strip_brackets(path[0])
21
+ remaining_path = path[1..-1]
22
+
23
+ if node.is_a? Array
24
+ if key == '*'
25
+ # Transform each value in this array recursively
26
+ node.map! { |value| transform_config_node(value, remaining_path) }
27
+ else
28
+ # Recurse into the specified array item
29
+ index = Integer(key)
30
+ node[index] = transform_config_node(node[index], remaining_path)
31
+ end
32
+ elsif node.is_a? Hash
33
+ if key == '*'
34
+ # Transform each value in this hash recursively
35
+ node.transform_values! { |value| transform_config_node(value, remaining_path) }
36
+ else
37
+ # Recurse into the specified hash value
38
+ node[key] = transform_config_node(node[key], remaining_path)
39
+ end
40
+ else
41
+ # The path has remaining segments, but this is a leaf node.
42
+ # This means that the specified path does not match, and we therefore
43
+ # do not modify or transform this node.
44
+ end
45
+
46
+ return node
47
+ end
48
+
49
+ def self.strip_brackets(text)
50
+ return text unless text
51
+ return text.gsub(/^\[(.+)\]$/, '\1')
52
+ end
53
+
54
+ def self.transform_config_value(value)
55
+ return value unless value.is_a? String
56
+
57
+ result = if /^gem:(?<gem_name>[^:]+):(?<relative_path>.*)/ =~ value
58
+ resolve_gem(gem_name, "/#{relative_path}")
59
+ elsif /^gem:(?<gem_name>[^\/]+)(?<relative_path>.*)/ =~ value
60
+ resolve_gem(gem_name, relative_path)
61
+ else
62
+ nil
63
+ end
64
+
65
+ Jekyll.logger.info("Resolved #{value} to #{result}") unless result.nil?
66
+ result
67
+ end
68
+
69
+ def self.resolve_gem(gem_name, relative_path)
70
+ spec = Gem::Specification.find_by_name(gem_name)
71
+ spec.gem_dir + relative_path
72
+ end
73
+
74
+ Jekyll::Hooks.register :site, :after_init do |site|
75
+ transform_config(site)
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-gem-resolver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Waslowksi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.3.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.3.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ description: Gem resolver for Jekyll
56
+ email:
57
+ - cr7pt0gr4ph7@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/jekyll/gem_resolver/version.rb
65
+ - lib/jekyll_gem_resolver.rb
66
+ homepage: https://github.com/cr7pt0gr4ph7/jekyll-gem-resolver
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '3.0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.2.33
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: This plugin provides simple support for resolving paths relative to the directory
89
+ of specific gems.
90
+ test_files: []