-
Notifications
You must be signed in to change notification settings - Fork 2
/
font_grabber.rb
executable file
·46 lines (40 loc) · 1.01 KB
/
font_grabber.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
#!/usr/bin/ruby
# encoding: utf-8
# Grab google web fonts and embed them as base64 data URIs
# <http://brettterpstra.com/2015/03/14/embedding-google-web-fonts/>
require 'base64'
if ARGV.length > 0
input = ARGV
elsif STDIN.stat.size > 0
input = STDIN.read.strip.split(/\n+/)
else
$stderr.puts "No input"
Process.exit 1
end
output = ""
input.each {|line|
if line =~ /^\s*<link.*?>\s*$/
url = line.match(/href='(.*?)'/)
if url
css_url = url[1]
else
$stderr.puts "Error matching url"
end
css = %x{curl -sS '#{css_url}'}.strip
css.gsub!(/(src: .*?, url\()(.*?)(\) format\(')(.*?)('\);)/).each {|src|
pre = $1
font_url = $2
mid = $3
font_fmt = $4
post = $5
font_ext = font_url.match(/\.(\w+)$/)[1]
font_src = %x{curl -sS '#{font_url}'}
enc = Base64.encode64(font_src).strip.gsub(/\n/,'')
%Q{src:url("data:font/#{font_ext};base64,#{enc}") format('#{font_fmt}');}
}
output += css + "\n"
else
next
end
}
$stdout.puts output