Module: Jekyll::Gzip::Compressor
- Defined in:
- lib/jekyll/gzip/compressor.rb
Overview
The module that does the compressing using Zlib.
Class Method Summary collapse
-
.compress_directory(dir, site) ⇒ Object
Takes a directory path and maps over the files within compressing them in place.
-
.compress_file(file_name, extensions: [], replace_file: false) ⇒ Object
Takes a file name and an array of extensions.
-
.compress_site(site) ⇒ Object
Takes an instance of
Jekyll::Site
and maps over the site files, compressing them in the destination directory.
Class Method Details
.compress_directory(dir, site) ⇒ Object
Takes a directory path and maps over the files within compressing them in place.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/jekyll/gzip/compressor.rb', line 51 def self.compress_directory(dir, site) extensions = zippable_extensions(site).join(',') replace_file = replace_files(site) files = Dir.glob(File.join(dir, "**", "*{#{extensions}}")) files.each do |file| next unless regenerate?(file, site) compress_file( file, extensions: extensions, replace_file: replace_file ) end end |
.compress_file(file_name, extensions: [], replace_file: false) ⇒ Object
Takes a file name and an array of extensions. If the file name extension matches one of the extensions in the array then the file is loaded and compressed using Zlib, outputting the gzipped file under the name of the original file with an extra .gz extension.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jekyll/gzip/compressor.rb', line 82 def self.compress_file(file_name, extensions: [], replace_file: false) return unless extensions.include?(File.extname(file_name)) zipped = zipped(file_name, replace_file) file_content = IO.binread(file_name) Jekyll.logger.debug "Gzip: #{zipped}" Zlib::GzipWriter.open(zipped, Zlib::BEST_COMPRESSION) do |gz| gz.mtime = File.mtime(file_name) gz.orig_name = file_name gz.write file_content end end |
.compress_site(site) ⇒ Object
Takes an instance of Jekyll::Site
and maps over the site files, compressing them in the destination directory.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jekyll/gzip/compressor.rb', line 26 def self.compress_site(site) site.each_site_file do |file| next unless regenerate? file.destination(site.dest), site compress_file( file.destination(site.dest), extensions: zippable_extensions(site), replace_file: replace_files(site) ) end end |