6
6
require 'open-uri'
7
7
require 'uri'
8
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
+
9
57
def download_file ( url , dest )
10
58
# only try to download the file if url doesn't start with | for security reasons
11
59
if url . start_with? ( '|' )
@@ -34,7 +82,7 @@ def download_file(url, dest)
34
82
end
35
83
end
36
84
37
- def download_fonts ( url , dest )
85
+ def download_fonts ( url , dest , file_types )
38
86
# only try to download the file if url doesn't start with | for security reasons
39
87
if url . start_with? ( '|' )
40
88
return
@@ -50,15 +98,39 @@ def download_fonts(url, dest)
50
98
file_name = link [ 'href' ] . split ( '/' ) . last . split ( '?' ) . first
51
99
52
100
# verify if the file is a font file
53
- if file_name . end_with? ( '.woff' , '.woff2' , '.ttf' , '.otf' )
101
+ if file_name . end_with? ( * file_types )
54
102
# download the file and change the url to the local file
55
103
download_file ( URI . join ( url , link [ 'href' ] ) . to_s , File . join ( dest , file_name ) )
56
104
end
57
105
end
58
106
end
59
107
end
60
108
61
- def download_fonts_from_css ( config , url , dest )
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 . open ( url , "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 )
62
134
# only try to download the file if url doesn't start with | for security reasons
63
135
if url . start_with? ( '|' )
64
136
return
@@ -83,48 +155,11 @@ def download_fonts_from_css(config, url, dest)
83
155
# get the font-face rules
84
156
css . each_rule_set do |rule_set |
85
157
# check if the rule set has a url
86
- if rule_set [ 'src' ] . include? ( 'url(' )
87
- # get the font file url
88
- font_url = rule_set [ 'src' ] . split ( 'url(' ) . last . split ( ')' ) . first
89
-
90
- # remove quotes from the url
91
- if font_url . start_with? ( '"' ) || font_url . start_with? ( "'" )
92
- font_url = font_url [ 1 ..-2 ]
93
- end
94
-
95
- font_file_name = font_url . split ( '/' ) . last . split ( '?' ) . first
96
-
97
- # verify if the file is a font file
98
- if font_file_name . end_with? ( '.woff' , '.woff2' , '.ttf' , '.otf' )
99
- # fix the font url if it is not an absolute url
100
- unless font_url . start_with? ( 'https://' )
101
- font_url = URI . join ( url , font_url ) . to_s
102
- end
103
-
104
- # download the file
105
- download_file ( font_url , File . join ( dest , 'fonts' , font_file_name ) )
106
-
107
- # change the font url to the local file, considering baseurl
108
- if config [ 'baseurl' ]
109
- # add rest of the src attribute if it exists
110
- if rule_set [ 'src' ] . split ( ' ' ) . length > 1
111
- rule_set [ 'src' ] = "url(#{ File . join ( config [ 'baseurl' ] , 'assets' , 'libs' , 'fonts' , font_file_name ) } ) #{ rule_set [ 'src' ] . split ( ' ' ) . last } "
112
- else
113
- rule_set [ 'src' ] = "url(#{ File . join ( config [ 'baseurl' ] , 'assets' , 'libs' , 'fonts' , font_file_name ) } )"
114
- end
115
- else
116
- # add rest of the src attribute if it exists
117
- if rule_set [ 'src' ] . split ( ' ' ) . length > 1
118
- rule_set [ 'src' ] = "url(#{ File . join ( '/assets' , 'libs' , 'fonts' , font_file_name ) } ) #{ rule_set [ 'src' ] . split ( ' ' ) . last } "
119
- else
120
- rule_set [ 'src' ] = "url(#{ File . join ( '/assets' , 'libs' , 'fonts' , font_file_name ) } )"
121
- end
122
- end
123
- end
124
- end
158
+ download_and_change_rule_set_url ( rule_set , 'src' , File . join ( dest , 'fonts' ) , File . join ( lib_name , 'fonts' ) , config , file_types )
125
159
end
126
160
127
161
# save the modified css file
162
+ puts "Saving modified css file to #{ File . join ( dest , file_name ) } "
128
163
File . write ( File . join ( dest , file_name ) , css . to_s )
129
164
end
130
165
@@ -164,13 +199,13 @@ def download_fonts_from_css(config, url, dest)
164
199
# get the file name from the url
165
200
file_name = url2 . split ( '/' ) . last . split ( '?' ) . first
166
201
# download the file and change the url to the local file
167
- dest = File . join ( site . source , 'assets' , 'libs' , file_name )
202
+ dest = File . join ( site . source , 'assets' , 'libs' , key , file_name )
168
203
download_file ( url2 , dest )
169
204
# change the url to the local file, considering baseurl
170
205
if site . config [ 'baseurl' ]
171
- site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] [ type2 ] = File . join ( site . config [ 'baseurl' ] , 'assets' , 'libs' , file_name )
206
+ site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] [ type2 ] = File . join ( site . config [ 'baseurl' ] , 'assets' , 'libs' , key , file_name )
172
207
else
173
- site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] [ type2 ] = File . join ( '/assets' , 'libs' , file_name )
208
+ site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] [ type2 ] = File . join ( '/assets' , 'libs' , key , file_name )
174
209
end
175
210
end
176
211
@@ -181,29 +216,33 @@ def download_fonts_from_css(config, url, dest)
181
216
182
217
if file_name . end_with? ( 'css' )
183
218
# if the file is a css file, download the css file, the fonts from it, and change information on the css file
184
- file_name = download_fonts_from_css ( site . config , url , File . join ( site . source , 'assets' , 'libs' ) )
219
+ file_name = download_fonts_from_css ( site . config , url , File . join ( site . source , 'assets' , 'libs' , key ) , key , font_file_types )
185
220
# change the url to the local file, considering baseurl
186
221
if site . config [ 'baseurl' ]
187
- site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( site . config [ 'baseurl' ] , 'assets' , 'libs' , file_name )
222
+ site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( site . config [ 'baseurl' ] , 'assets' , 'libs' , key , file_name )
188
223
else
189
- site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( '/assets' , 'libs' , file_name )
224
+ site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( '/assets' , 'libs' , key , file_name )
190
225
end
191
226
else
192
227
# download the font files and change the url to the local file
193
- download_fonts ( url , File . join ( site . source , 'assets' , 'libs' , site . config [ 'third_party_libraries' ] [ key ] [ 'local' ] [ 'fonts' ] ) )
228
+ download_fonts ( url , File . join ( site . source , 'assets' , 'libs' , key , site . config [ 'third_party_libraries' ] [ key ] [ 'local' ] [ type ] ) , font_file_types )
194
229
end
195
230
231
+ elsif type == 'images'
232
+ # download the font 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
+
196
235
else
197
236
# get the file name from the url
198
237
file_name = url . split ( '/' ) . last . split ( '?' ) . first
199
238
# download the file and change the url to the local file
200
- dest = File . join ( site . source , 'assets' , 'libs' , file_name )
239
+ dest = File . join ( site . source , 'assets' , 'libs' , key , file_name )
201
240
download_file ( url , dest )
202
241
# change the url to the local file, considering baseurl
203
242
if site . config [ 'baseurl' ]
204
- site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( site . config [ 'baseurl' ] , 'assets' , 'libs' , file_name )
243
+ site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( site . config [ 'baseurl' ] , 'assets' , 'libs' , key , file_name )
205
244
else
206
- site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( '/assets' , 'libs' , file_name )
245
+ site . config [ 'third_party_libraries' ] [ key ] [ 'url' ] [ type ] = File . join ( '/assets' , 'libs' , key , file_name )
207
246
end
208
247
end
209
248
end
0 commit comments