jekyll-3rd-party-libraries 0.0.1

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: 361a737b10f9ff08913d8793919ec1e27cedcbf2482a2b0ac78d9b5bb16b316b
4
+ data.tar.gz: 39c752119d38572055d59b7a897f2242751aff97bc931b384a97a160be23d7e7
5
+ SHA512:
6
+ metadata.gz: 793a0db5e969f40c5b885fa6ad644a37d2a4fd897b9d16fc4b226fa266d2e990805223a3a6195c064dff347d908b80b709d017d2f733dcbd7995beb1569b82ef
7
+ data.tar.gz: 3238c2a74537197032083aefa8d28cca5e5f6d0f436bc922fd2a62a8cb7a929ffbc959be497ccc14436f2fd90c998057c81b69998c3768fabd813a5dd9c8f188
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module ThirdPartyLibraries
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,253 @@
1
+ Jekyll::Hooks.register :site, :after_init do |site|
2
+ require 'css_parser'
3
+ require 'digest'
4
+ require 'fileutils'
5
+ require 'nokogiri'
6
+ require 'open-uri'
7
+ require 'uri'
8
+
9
+ font_file_types = ['otf', 'ttf', 'woff', 'woff2']
10
+ image_file_types = ['.gif', '.jpg', '.jpeg', '.png', '.webp']
11
+
12
+ def download_and_change_rule_set_url(rule_set, rule, dest, dirname, config, file_types)
13
+ # check if the rule has a url
14
+ if rule_set[rule].include?('url(')
15
+ # get the file url
16
+ url = rule_set[rule].split('url(').last.split(')').first
17
+
18
+ # remove quotes from the url
19
+ if url.start_with?('"') || url.start_with?("'")
20
+ url = url[1..-2]
21
+ end
22
+
23
+ file_name = url.split('/').last.split('?').first
24
+
25
+ # verify if the file is of the correct type
26
+ if file_name.end_with?(*file_types)
27
+ # fix the url if it is not an absolute url
28
+ unless url.start_with?('https://')
29
+ url = URI.join(url, url).to_s
30
+ end
31
+
32
+ # download the file
33
+ download_file(url, File.join(dest, file_name))
34
+
35
+ # change the url to the local file, considering baseurl
36
+ previous_rule = rule_set[rule]
37
+ if config['baseurl']
38
+ # add rest of the src attribute if it exists
39
+ if rule_set[rule].split(' ').length > 1
40
+ rule_set[rule] = "url(#{File.join(config['baseurl'], 'assets', 'libs', dirname, file_name)}) #{rule_set[rule].split(' ').last}"
41
+ else
42
+ rule_set[rule] = "url(#{File.join(config['baseurl'], 'assets', 'libs', dirname, file_name)})"
43
+ end
44
+ else
45
+ # add rest of the src attribute if it exists
46
+ if rule_set[rule].split(' ').length > 1
47
+ rule_set[rule] = "url(#{File.join('/assets', 'libs', dirname, file_name)}) #{rule_set[rule].split(' ').last}"
48
+ else
49
+ rule_set[rule] = "url(#{File.join('/assets', 'libs', dirname, file_name)})"
50
+ end
51
+ end
52
+ puts "Changed #{previous_rule} to #{rule_set[rule]}"
53
+ end
54
+ end
55
+ end
56
+
57
+ def download_file(url, dest)
58
+ # only try to download the file if url doesn't start with | for security reasons
59
+ if url.start_with?('|')
60
+ return
61
+ end
62
+
63
+ # create the directory if it doesn't exist
64
+ dir = File.dirname(dest)
65
+ unless File.directory?(dir)
66
+ FileUtils.mkdir_p(dir)
67
+ end
68
+
69
+ # download the file if it doesn't exist
70
+ unless File.file?(dest)
71
+ puts "Downloading #{url} to #{dest}"
72
+ File.open(dest, "wb") do |saved_file|
73
+ URI(url).open("rb") do |read_file|
74
+ saved_file.write(read_file.read)
75
+ end
76
+ end
77
+
78
+ # check if the file was downloaded successfully
79
+ unless File.file?(dest)
80
+ raise "Failed to download #{url} to #{dest}"
81
+ end
82
+ end
83
+ end
84
+
85
+ def download_fonts(url, dest, file_types)
86
+ # only try to download the file if url doesn't start with | for security reasons
87
+ if url.start_with?('|')
88
+ return
89
+ end
90
+
91
+ # only download fonts if the directory doesn't exist or is empty
92
+ unless File.directory?(dest) && !Dir.empty?(dest)
93
+ puts "Downloading fonts from #{url} to #{dest}"
94
+ # get available fonts from the url
95
+ doc = Nokogiri::HTML(URI(url).open("User-Agent" => "Ruby/#{RUBY_VERSION}"))
96
+ doc.css('a').each do |link|
97
+ # get the file name from the url
98
+ file_name = link['href'].split('/').last.split('?').first
99
+
100
+ # verify if the file is a font file
101
+ if file_name.end_with?(*file_types)
102
+ # download the file and change the url to the local file
103
+ download_file(URI.join(url, link['href']).to_s, File.join(dest, file_name))
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ def download_images(url, dest, file_types)
110
+ # only try to download the file if url doesn't start with | for security reasons
111
+ if url.start_with?('|')
112
+ return
113
+ end
114
+
115
+ # only download images if the directory doesn't exist or is empty
116
+ unless File.directory?(dest) && !Dir.empty?(dest)
117
+ puts "Downloading images from #{url} to #{dest}"
118
+ # get available fonts from the url
119
+ doc = Nokogiri::HTML(URI(url).open("User-Agent" => "Ruby/#{RUBY_VERSION}"))
120
+ doc.xpath('/html/body/div/div[3]/table/tbody/tr/td[1]/a').each do |link|
121
+ # get the file name from the url
122
+ file_name = link['href'].split('/').last.split('?').first
123
+
124
+ # verify if the file is a font file
125
+ if file_name.end_with?(*file_types)
126
+ # download the file and change the url to the local file
127
+ download_file(URI.join(url, link['href']).to_s, File.join(dest, file_name))
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ def download_fonts_from_css(config, url, dest, lib_name, file_types)
134
+ # only try to download the file if url doesn't start with | for security reasons
135
+ if url.start_with?('|')
136
+ return
137
+ end
138
+
139
+ # get the file name from the url
140
+ file_name = url.split('/').last.split('?').first
141
+
142
+ if file_name == 'css'
143
+ file_name = 'google-fonts.css'
144
+ end
145
+
146
+ # only download the css file if it doesn't exist
147
+ unless File.file?(File.join(dest, file_name))
148
+ puts "Downloading fonts from #{url} to #{dest}"
149
+ # download the css file with a fake user agent to force downloading woff2 fonts instead of ttf
150
+ # user agent from https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome
151
+ doc = Nokogiri::HTML(URI(url).open("User-Agent" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"))
152
+ css = CssParser::Parser.new
153
+ css.load_string! doc.document.text
154
+
155
+ # get the font-face rules
156
+ css.each_rule_set do |rule_set|
157
+ # check if the rule set has a url
158
+ download_and_change_rule_set_url(rule_set, 'src', File.join(dest, 'fonts'), File.join(lib_name, 'fonts'), config, file_types)
159
+ end
160
+
161
+ # save the modified css file
162
+ puts "Saving modified css file to #{File.join(dest, file_name)}"
163
+ File.write(File.join(dest, file_name), css.to_s)
164
+ end
165
+
166
+ return file_name
167
+ end
168
+
169
+ # replace {{version}} with the version number in all 3rd party libraries urls
170
+ site.config['third_party_libraries'].each do |key, value|
171
+ if key != 'download'
172
+ value['url'].each do |type, url|
173
+ # check if url is a dictionary
174
+ if url.is_a?(Hash)
175
+ url.each do |type2, url2|
176
+ # replace {{version}} with the version number if it exists
177
+ if url2.include?('{{version}}')
178
+ site.config['third_party_libraries'][key]['url'][type][type2] = url2.gsub('{{version}}', site.config['third_party_libraries'][key]['version'])
179
+ end
180
+ end
181
+ else
182
+ # replace {{version}} with the version number if it exists
183
+ if url.include?('{{version}}')
184
+ site.config['third_party_libraries'][key]['url'][type] = url.gsub('{{version}}', site.config['third_party_libraries'][key]['version'])
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ # download 3rd party libraries if required
192
+ if site.config['third_party_libraries']['download']
193
+ site.config['third_party_libraries'].each do |key, value|
194
+ if key != 'download'
195
+ value['url'].each do |type, url|
196
+ # check if url is a dictionary
197
+ if url.is_a?(Hash)
198
+ url.each do |type2, url2|
199
+ # get the file name from the url
200
+ file_name = url2.split('/').last.split('?').first
201
+ # download the file and change the url to the local file
202
+ dest = File.join(site.source, 'assets', 'libs', key, file_name)
203
+ download_file(url2, dest)
204
+ # change the url to the local file, considering baseurl
205
+ if site.config['baseurl']
206
+ site.config['third_party_libraries'][key]['url'][type][type2] = File.join(site.config['baseurl'], 'assets', 'libs', key, file_name)
207
+ else
208
+ site.config['third_party_libraries'][key]['url'][type][type2] = File.join('/assets', 'libs', key, file_name)
209
+ end
210
+ end
211
+
212
+ else
213
+ if type == 'fonts'
214
+ # get the file name from the url
215
+ file_name = url.split('/').last.split('?').first
216
+
217
+ if file_name.end_with?('css')
218
+ # if the file is a css file, download the css file, the fonts from it, and change information on the css file
219
+ file_name = download_fonts_from_css(site.config, url, File.join(site.source, 'assets', 'libs', key), key, font_file_types)
220
+ # change the url to the local file, considering baseurl
221
+ if site.config['baseurl']
222
+ site.config['third_party_libraries'][key]['url'][type] = File.join(site.config['baseurl'], 'assets', 'libs', key, file_name)
223
+ else
224
+ site.config['third_party_libraries'][key]['url'][type] = File.join('/assets', 'libs', key, file_name)
225
+ end
226
+ else
227
+ # download the font files and change the url to the local file
228
+ download_fonts(url, File.join(site.source, 'assets', 'libs', key, site.config['third_party_libraries'][key]['local'][type]), font_file_types)
229
+ end
230
+
231
+ elsif type == 'images'
232
+ # download the image files and change the url to the local file
233
+ download_images(url, File.join(site.source, 'assets', 'libs', key, site.config['third_party_libraries'][key]['local'][type]), image_file_types)
234
+
235
+ else
236
+ # get the file name from the url
237
+ file_name = url.split('/').last.split('?').first
238
+ # download the file and change the url to the local file
239
+ dest = File.join(site.source, 'assets', 'libs', key, file_name)
240
+ download_file(url, dest)
241
+ # change the url to the local file, considering baseurl
242
+ if site.config['baseurl']
243
+ site.config['third_party_libraries'][key]['url'][type] = File.join(site.config['baseurl'], 'assets', 'libs', key, file_name)
244
+ else
245
+ site.config['third_party_libraries'][key]['url'][type] = File.join('/assets', 'libs', key, file_name)
246
+ end
247
+ end
248
+ end
249
+ end
250
+ end
251
+ end
252
+ end
253
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-3rd-party-libraries
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - George Corrêa de Araújo
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-01-23 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: jekyll
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '3.6'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '5.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '3.6'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '5.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: css_parser
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '1.6'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '1.6'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '2.0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: nokogiri
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '1.8'
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - - "<"
70
+ - !ruby/object:Gem::Version
71
+ version: '2.0'
72
+ description: Force updating cached files and resources in a Jekyll site by adding
73
+ a hash.
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/jekyll-3rd-party-libraries.rb
79
+ - lib/jekyll-3rd-party-libraries/version.rb
80
+ homepage: https://github.com/george-gca/jekyll-3rd-party-libraries
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ source_code_uri: https://github.com/george-gca/jekyll-3rd-party-libraries
85
+ bug_tracker_uri: https://github.com/george-gca/jekyll-3rd-party-libraries/issues
86
+ changelog_uri: https://github.com/george-gca/jekyll-3rd-party-libraries/releases
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.3.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.6.3
102
+ specification_version: 4
103
+ summary: Force updating cached files and resources in a Jekyll site.
104
+ test_files: []