Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Last active May 10, 2022 01:56
Show Gist options
  • Save mytharcher/2758691 to your computer and use it in GitHub Desktop.
Save mytharcher/2758691 to your computer and use it in GitHub Desktop.
A jekyll plugin for compressing HTML, JavaScript files when rendering.
#
# File compressor plugin for jekyll
# =================================
#
# By [mytharcher](https://github.com/mytharcher)
# 2012-05-20
#
# Updated by [nicoespeon](https://github.com/nicoespeon)
# 2013-04-12
#
# This plugin for compressing text files, including
# HTML and JavaScript of jekyll site. NOT support CSS
# file yet.
#
# The JavaScript compressing requires 'packr', a
# third-party lib. I'm not sure if GitHub has this
# extension. But the compressor for HTML files works OK.
#
# For HTML files it uses Alan Moore's regexp :
# http://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter
# It removes spaces between HTML, excepted within
# <textarea> and <pre> code, so you don't get into trouble!
#
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
@jelmerdemaat
Copy link

Works great! Is there a way to exclude a specific section of HTML from being compressed? Part of my page layout relies on text-align: justify which needs a space in between elements.

@GlenDC
Copy link

GlenDC commented Aug 25, 2014

Thanks man! Works great! :)

@kimar
Copy link

kimar commented Dec 14, 2014

Thanks for the nice plugin. However I've made a small improvement because this plugin always fucked up my rss.xml. https://gist.github.com/kimar/cc2cc5fd86d6abe54042#file-compressor-rb-L45-L49

@jalcine
Copy link

jalcine commented Feb 5, 2015

@jelmerdemaat why not use a &nbsp; ?

@sguha-work
Copy link

great program....Fulfill my purpose 100%...

@jsntn
Copy link

jsntn commented Oct 18, 2015

Good Job! Thanks so much man!!

@vszakats
Copy link

vszakats commented Dec 4, 2017

Fixes an @@mtime error, static files being copied into a subdirectory and some whitespace/URL updates:

--- compressor-old.rb
+++ compressor-new.rb
@@ -1,26 +1,26 @@
-# 
+#
 # File compressor plugin for jekyll
 # =================================
-# 
+#
 # By [mytharcher](https://github.com/mytharcher)
 # 2012-05-20
-# 
+#
 # Updated by [nicoespeon](https://github.com/nicoespeon)
 # 2013-04-12
-# 
+#
 # This plugin for compressing text files, including
 # HTML and JavaScript of jekyll site. NOT support CSS
 # file yet.
-# 
+#
 # The JavaScript compressing requires 'packr', a
 # third-party lib. I'm not sure if GitHub has this
 # extension. But the compressor for HTML files works OK.
-# 
-# For HTML files it uses Alan Moore's regexp : 
-# http://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter
+#
+# For HTML files it uses Alan Moore's regexp :
+# https://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter
 # It removes spaces between HTML, excepted within
 # <textarea> and <pre> code, so you don't get into trouble!
-# 
+#
 
 require 'packr'
 
@@ -41,7 +41,7 @@
     end
 
     def output_html(dest, content)
-      path = self.destination(dest)
+      path = dest
       self.output_file(path, compress_html(content))
     end
 
@@ -62,7 +62,7 @@
     include Compressor
 
     def write(dest)
-      self.output_html(dest, self.output)
+      self.output_html(self.destination(dest), self.output)
     end
 
   end
@@ -74,7 +74,7 @@
     include Compressor
 
     def write(dest)
-      self.output_html(dest, self.output)
+      self.output_html(self.destination(dest), self.output)
     end
 
   end
@@ -84,12 +84,12 @@
   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
+      self.class.mtimes[path] = mtime
 
       case File.extname(dest_path)
         when '.html'
@@ -106,4 +106,4 @@
 
   end
 
-end
\ No newline at end of file
+end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment