Created
August 3, 2014 13:03
-
-
Save DarrylDias/a0979ba5cf8ccf855288 to your computer and use it in GitHub Desktop.
HTML/Javascript minifier for MiniSoda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # HTML/Javascript minifier for MiniSoda | |
| # gem install packr --no-ri --no-rdoc | |
| require 'packr' | |
| module Jekyll | |
| module Compressor | |
| def compress_html(content) | |
| content.gsub(/(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!\/?(?:textarea|pre)\b))*+)(?:<(?>textarea|pre)\b|\z))/ix, '') | |
| end | |
| # Really writing process | |
| def output_file(dest, content) | |
| FileUtils.mkdir_p(File.dirname(dest)) | |
| File.open(dest, 'w') do |f| | |
| f.write(content) | |
| end | |
| end | |
| def output_html(dest, content) | |
| path = self.destination(dest) | |
| self.output_file(path, compress_html(content)) | |
| end | |
| def output_js(dest, content) | |
| self.output_file(dest, Packr.pack(content, | |
| :shrink_vars => true | |
| )) | |
| end | |
| end | |
| # Overwrite old methods to insert a hook | |
| class Post | |
| include Compressor | |
| def write(dest) | |
| self.output_html(dest, self.output) | |
| end | |
| end | |
| class Page | |
| include Compressor | |
| def write(dest) | |
| self.output_html(dest, self.output) | |
| end | |
| end | |
| class StaticFile | |
| include Compressor | |
| def write(dest) | |
| dest_path = self.destination(dest) | |
| return false if File.exist?(dest_path) and !self.modified? | |
| @@mtimes[path] = mtime | |
| case File.extname(dest_path) | |
| when '.html' | |
| self.output_html(dest_path, File.read(path)) | |
| when '.js' | |
| self.output_js(dest_path, File.read(path)) | |
| else | |
| FileUtils.mkdir_p(File.dirname(dest_path)) | |
| FileUtils.cp(path, dest_path) | |
| end | |
| true | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment