-
Notifications
You must be signed in to change notification settings - Fork 10
/
js2png
executable file
·77 lines (60 loc) · 1.7 KB
/
js2png
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
#!/usr/bin/env ruby
require 'ftools'
require 'rubygems'
require 'RMagick'
include Magick
files = [
'javascripts/f.js',
'javascripts/p.js',
'javascripts/gh.js',
'javascripts/diff.js',
'javascripts/keyboard.js',
'javascripts/vendors/difflib.js',
'javascripts/vendors/diffview.js'
# 'javascripts/prototype.js'
]
temp_file = 'merge.js'
min_temp_file = 'merge.min.js'
image_file = "code.png"
crushed_image_file = "code.crushed.png"
# Merged and minify scripts
merged_file = ""
files.each {|file|
File.open( file, "r") { |f|
merged_file += f.read + "\n"
}
}
File.open( temp_file , "w") {|f| f.write(merged_file) }
cmd = "java -jar bin/yuicompressor-2.4.2.jar #{temp_file} -o #{min_temp_file}"
js = `#{cmd}`
# debug out put
# File.open( min_temp_file , "w") {|f| f.write(js) }
js = ''
File.open( min_temp_file, "r") { |f| js += f.read + "\n" }
image = Magick::Image.new(1,js.length)
i = 0
while i < js.length do
# color is in format #RRGGBB
color = '#%02X0000' % js[i]
x, y = 0, i
pixel = Magick::Pixel.from_color color
image.pixel_color x, y, pixel
i += 1
end
image.compression = ZipCompression
image.write("png8:"+ image_file)
File.open( temp_file , "r" )
pic = (Image.read(image_file)).first
# puts "Validates Output"
# s = ''
# pic.each_pixel do |px, x, y|
# s += (px.red % 256).chr
# end
# puts s == js
puts "Script Length: #{js.length}"
temp_file = 'merge.js'
min_temp_file = 'merge.min.js'
image_file = "code.png"
[ temp_file, min_temp_file, image_file ].each do |file|
puts "#{"%-20.20s" % file} #{"%30s" % File.stat( file ).size}"
end