-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdocs_compressor.rb
51 lines (41 loc) · 1.1 KB
/
docs_compressor.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
require 'find'
require 'fileutils'
require 'shellwords'
require 'logging'
require 'running'
# Compresses HTML, JavaScript, and CSS under the given directory, recursively.
#
# We do this to leverage gzip_static in nginx.
class DocsCompressor
include Logging
include Running
EXTENSIONS = %w(.js .html .css)
def initialize(dir)
@dir = dir
end
def compress
log "compressing #@dir"
Find.find(@dir) do |file|
# The directory with content for the Kindle version of the guides has
# HTML files used to build the .mobi file, but they are not served, so
# there is no need to compress them.
if File.basename(file) == 'kindle'
Find.prune
elsif compress_file?(file)
compress_file(file)
end
end
end
private
def gzname(file)
"#{file}.gz"
end
def compress_file(file)
orig = Shellwords.shellescape(file)
dest = Shellwords.shellescape(gzname(file))
log_and_system %(gzip -c -9 #{orig} > #{dest})
end
def compress_file?(file)
EXTENSIONS.include?(File.extname(file)) && !FileUtils.uptodate?(gzname(file), [file])
end
end