-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.rb
238 lines (181 loc) · 5.6 KB
/
assets.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Version 2.0.1
require 'fileutils'
$assets = {}
class Stamp
def self.file(absolute_path)
content = File.open(absolute_path).read
md5 = Digest::MD5.new
md5 << content
md5.hexdigest
end
end
class DigestedFile < Jekyll::StaticFile
def write(dest)
# noop
end
end
class GenericAssetTag < Liquid::Tag
def initialize(tag_name, url, tokens)
super
@url = url.strip
end
def render(context)
if production?
site = context.registers[:site]
page = context.registers.fetch(:page, {})
absolute_path = File.join(site.source, url)
digested_url = url.sub(/\.\w+$/) { |match| "-#{Stamp.file(absolute_path)}#{match}" }
add_dependency(site, page, digested_url)
mark_as_digested_asset(site, digested_url)
render_in_production(digested_url)
else
render_in_development
end
end
protected
def render_in_development
fail NotImplementedError
end
def render_in_production(digested_url)
fail NotImplementedError
end
private
attr_reader :url
def production?
ENV.fetch('JEKYLL_ENV', '') == 'production'
end
def add_dependency(site, page, digested_url)
return unless page.key?('path')
site.regenerator.add_dependency(
site.in_source_dir(page['path']),
site.in_source_dir(digested_url)
)
end
def mark_as_digested_asset(site, digested_url)
target_path = site.in_dest_dir(digested_url)
target_dirname = File.dirname(target_path)
target_basename = File.basename(target_path)
asset_file = DigestedFile.new(site, site.source, target_dirname, target_basename)
site.static_files << asset_file
$assets[url] = digested_url
end
end
class AssetUrlTag < GenericAssetTag
def render_in_development
url
end
def render_in_production(digested_url)
digested_url
end
end
class AssetInlineTag < GenericAssetTag
def initialize(tag_name, url, tokens)
super
@type = url =~ /\.js$/ ? 'script' : 'style'
end
def render_in_development
if type == 'script'
%{<script src="#{ url }"></script>}
else
%{<link rel="stylesheet" href="#{ url }"/>}
end
end
def render_in_production(digested_url)
"<!-- inline #{ type }:#{ digested_url } -->"
end
private
attr_reader :type
end
class Stylesheet
def initialize(jekyll, target)
@jekyll = jekyll
@stylesheet_path = jekyll.in_dest_dir(target)
@rewritten = nil
@image_urls = []
end
def rewrite_urls
@rewritten =
File.read(stylesheet_path)
.gsub(URL_PATTERN) {
quote_left = $1
source_url = $2
quote_right = $3
extension = File.extname(source_url)
source_path = File.absolute_path(source_url, File.dirname(stylesheet_path))
stamp = Stamp.file(source_path)
digested_url = source_url.sub(extension, '-' + stamp + extension)
digested_path = source_path.sub(extension, '-' + stamp + extension)
image_urls << [source_path, digested_path]
"url(#{quote_left}#{digested_url}#{quote_right})"
}
self
end
def copy_to_target
File.open(stylesheet_path, 'w') do |f|
f.write(rewritten)
end
self
end
def copy_images_to_target
image_urls.each { |source_path, target_path|
Jekyll.logger.debug('image asset:', "'#{ source_path }' => '#{ target_path }'")
FileUtils.cp(source_path, target_path)
}
self
end
private
attr_reader :image_urls, :jekyll, :rewritten, :stylesheet_path
URL_PATTERN = /url\((["']?)([^)]+)(["']?)\)/m
end
Liquid::Template.register_tag('asset_url', AssetUrlTag)
Liquid::Template.register_tag('asset_inline', AssetInlineTag)
Jekyll::Hooks.register(:site, :post_write) do |jekyll|
next unless ENV.fetch('JEKYLL_ENV', '') == 'production'
$assets.each do |source, target|
source_path = jekyll.in_dest_dir(source)
target_path = jekyll.in_dest_dir(target)
if source =~ /\.js$/
Jekyll.logger.debug('JS asset:', "'#{ source_path }' => '#{ target_path }'")
%x(uglifyjs --compress --mangle --output #{target_path} #{source_path})
elsif source =~ /\.css$/
Jekyll.logger.debug('CSS asset:', "'#{ source_path }' => '#{ target_path }'")
%x(cleancss --output #{target_path} #{source_path})
else
Jekyll.logger.warn('other asset:', "'#{ source_path }' => '#{ target_path }'")
FileUtils.cp(source_path, target_path)
end
end
Jekyll.logger.info('Optimizing assets:', 'done!')
end
Jekyll::Hooks.register(:site, :post_write) do |jekyll|
next unless ENV.fetch('JEKYLL_ENV', '') == 'production'
$assets.each do |source, target|
next unless source =~ /\.css$/
Stylesheet.new(jekyll, target)
.rewrite_urls
.copy_to_target
.copy_images_to_target
end
Jekyll.logger.info('Processing images:', 'done!')
end
Jekyll::Hooks.register(:site, :post_write) do |jekyll|
next unless ENV.fetch('JEKYLL_ENV', '') == 'production'
Dir.glob(File.join(jekyll.dest, '**/*.html')).each do |filename|
Jekyll.logger.debug('HTML file:', "'#{ File.absolute_path(filename) }'")
File.open(filename, 'r+') do |file|
source = file.read.force_encoding('utf-8')
processed = source.gsub(/<!-- inline (script|style):(\S+) -->\n/) do
type = Regexp.last_match[1]
asset_url = Regexp.last_match[2]
asset_path = File.join(jekyll.dest, asset_url)
asset_source = File.read(asset_path)
Jekyll.logger.debug('', "embedding '#{ asset_path }'")
"<#{ type }>#{ asset_source }</#{ type }>\n"
end
file.rewind
file.truncate(0)
file.write(processed)
end
end
Jekyll.logger.info('Embedding assets:', 'done!')
end