-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjekyll-bustify.rb
144 lines (106 loc) · 4.75 KB
/
jekyll-bustify.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
require 'digest/md5'
module Jekyll
module Bustify
DEFAULT_LENGTH = 5
def rename_string_file_extension(source, extension)
dirname = File.dirname(source)
basename = File.basename(source, ".*")
extname = File.extname(source)
if extname == ""
if basename[0,1]=="."
target = dirname + "/." + extension
else
target = source + "." + extension
end
else
target = dirname + "/" + basename + "." + extension
end
target
end
def bustify(input, length=DEFAULT_LENGTH)
file_path = File.join(File.dirname(__FILE__), '..', '_site', input).strip
# the right way would be to use this line below instead, but my lack of knowleadge make me do a hack.
#file_path = File.join(File.dirname(__FILE__), '..', input).strip
# The hack, of reading the asset file in the _site directory, leads to having to run `jekyll build` to times to achive the desired hash result when an asset is modified :// (not always needed, only when a modification is made on an asset dependency of other asset, such as sass dependency file that is called as an import on other sass file)
asset_file_extension = File.extname(file_path)
file_path_of__site = file_path
output_path = input
# if is a SASS CSS file, transform to css (because of the hack of using the _site directory :/ )
if [".scss", ".sass"].include? asset_file_extension
file_path_of__site = rename_string_file_extension(file_path, "css")
output_path = rename_string_file_extension(input, "css")
end
file_content = File.read(file_path_of__site)
# It would be here where to make a rendering of the jekyll asset (I called it Jekyll processing) but I can't make this implementation work.... :/ I have tried
# site = @context.registers[:site]
# content_parsed = site.find_converter_instance(Jekyll::Converters::Identity).convert(file_content.to_s)
# content_parsed1 = Jekyll::Renderer.new(@site, file_path, @payload)
# content_parsed = content_parsed1.render_document
# file_hash = Digest::MD5.hexdigest content_parsed
file_hash = Digest::MD5.hexdigest file_content
# deal with limits of lenght
if length > 32
length = 32
elsif length < 1
length = DEFAULT_LENGTH
end
output_hash = file_hash[-length..-1]
# print cache bust link
"#{output_path}?cb=#{output_hash}"
end
end
end
Liquid::Template.register_filter(Jekyll::Bustify)
require 'digest/md5'
module Jekyll
module Bustify
DEFAULT_LENGTH = 5
def rename_string_file_extension(source, extension)
dirname = File.dirname(source)
basename = File.basename(source, ".*")
extname = File.extname(source)
if extname == ""
if basename[0,1]=="."
target = dirname + "/." + extension
else
target = source + "." + extension
end
else
target = dirname + "/" + basename + "." + extension
end
target
end
def bustify(input, length=DEFAULT_LENGTH)
file_path = File.join(File.dirname(__FILE__), '..', '_site', input).strip
# the right way would be to use this line below instead, but my lack of knowleadge make me do a hack.
#file_path = File.join(File.dirname(__FILE__), '..', input).strip
# The hack, of reading the asset file in the _site directory, leads to having to run `jekyll build` to times to achive the desired hash result when an asset is modified :// (not always needed, only when a modification is made on an asset dependency of other asset, such as sass dependency file that is called as an import on other sass file)
asset_file_extension = File.extname(file_path)
file_path_of__site = file_path
output_path = input
# if is a SASS CSS file, transform to css (because of the hack of using the _site directory :/ )
if [".scss", ".sass"].include? asset_file_extension
file_path_of__site = rename_string_file_extension(file_path, "css")
output_path = rename_string_file_extension(input, "css")
end
file_content = File.read(file_path_of__site)
# It would be here where to make a rendering of the jekyll asset (I called it Jekyll processing) but I can't make this implementation work.... :/ I have tried
# site = @context.registers[:site]
# content_parsed = site.find_converter_instance(Jekyll::Converters::Identity).convert(file_content.to_s)
# content_parsed1 = Jekyll::Renderer.new(@site, file_path, @payload)
# content_parsed = content_parsed1.render_document
# file_hash = Digest::MD5.hexdigest content_parsed
file_hash = Digest::MD5.hexdigest file_content
# deal with limits of length
if length > 32
length = 32
elsif length < 1
length = DEFAULT_LENGTH
end
output_hash = file_hash[-length..-1]
# print cache bust link
"#{output_path}?cb=#{output_hash}"
end
end
end
Liquid::Template.register_filter(Jekyll::Bustify)