forked from PowerPointLabs/PowerPointLabs-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
68 lines (63 loc) · 2.25 KB
/
Rakefile
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
require 'fileutils'
def write_out_file(filename, new_html)
File.open(filename, "w") do |file|
file.write(new_html)
puts "\t#{filename} successfully created!\n"
end
end
task default: %w[build]
desc "Run image optimizer"
task :imageOptimize do
puts "\nCompressing images... Go grab a coffee!"
Dir.chdir(Rake.original_dir)
sh "image_optim -r --no-pngout --no-svgo ./_site"
end
desc "Merge all css and js into one app html file"
task :inline do |t, args|
Dir.chdir(Rake.original_dir)
Dir.chdir('_site')
app_files = FileList['*.html']
app_files.each do |app_file|
new_html = ""
local_file_link_found = false
puts("\nProcessing #{app_file}...")
IO.foreach(app_file) do |line|
# If the line refers to script content contained in a local file, concatenate. Regex below finds for local script tag that does not with src starting with http,//, *.*
if !(line =~ /<script.*src="http*:/) && !(line =~ /<script.*src="\/\/*/) && line =~ /<script.*src="(.*)"/
script_file = $1
puts "\tConcatenating #{script_file}..."
new_html << "<script>\n"
lines = IO.readlines(script_file)
first_js = lines.first
lines.each do |l|
new_html << ' ' << l.to_s.gsub(/\n/, "\n ")
end
new_html << "\n</script>\n"
local_file_link_found = true
# or if the line pulls in a CSS stylesheet from a local file, concatenate. Regex below find for local link tag that does not start with href starting with http,//, *.css
elsif (line !~ /<link.*href="http?:\/\//) && !(line =~ /<link.*href="\/\/*/) && (line =~ /<link.*href="(.*\.css)"/)
stylesheet_name = $1
puts "\tConcatenating #{stylesheet_name}..."
new_html << "<style>\n"
lines = IO.readlines(stylesheet_name)
lines.each do |l|
new_html << ' ' << l.to_s.gsub(/\n/, "\n ")
end
new_html << "\n</style>\n"
local_file_link_found = true
else
new_html << line
end
end
if local_file_link_found
output_filename = app_file.sub(/\.template\.html$/, '#{app_file}.html')
write_out_file(output_filename, new_html)
else
puts "\tNo concatenation needed!"
end
end
end
task :build do
Rake::Task['rake:inline'].invoke
Rake::Task['rake:imageOptimize'].invoke
end