Skip to content

Commit

Permalink
Merge pull request #35 from le0pard/v5-support
Browse files Browse the repository at this point in the history
Support middleman v5
  • Loading branch information
tdreyno authored Aug 16, 2020
2 parents 22fb636 + f48052c commit 6808039
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 52 deletions.
19 changes: 12 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
dist: xenial
language: ruby

cache:
bundler: true

rvm:
- ruby-head
- jruby-head
- jruby-19mode
- 2.2
- 2.1
- 2.0
- 2.7
- 2.6
- 2.5
os:
- linux
- osx
matrix:

jobs:
fast_finish: true
allow_failures:
- rvm: ruby-head
- rvm: jruby-19mode
- rvm: jruby-head

script: "bundle exec rake test"

env: TEST=true
env: TEST=true
21 changes: 12 additions & 9 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem "middleman-cli", :github => "middleman/middleman", :branch => "master"
gem "middleman-core", :github => "middleman/middleman", :branch => "master"
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem "middleman-cli", github: "middleman/middleman", branch: "master"
gem "middleman-core", github: "middleman/middleman", branch: "master"

# Specify your gem's dependencies in middleman-minify-html.gemspec
gemspec

gem "rake", "~> 10.0.3", :require => false
gem "yard", "~> 0.8.0", :require => false
gem "rake", ">= 10.0.3", require: false
gem "yard", ">= 0.8.0", require: false

gem "cucumber", "~> 1.3.1"
gem "cucumber", ">= 1.3.1"
gem "capybara", ">= 0"
gem "fivemat"
gem "aruba", "~> 0.5.1"
gem "rspec", "~> 2.12"
gem "builder", "~> 3.0.0"
gem "aruba", ">= 0.5.1"
gem "rspec", ">= 2.12"
gem "builder", ">= 3.0.0"
15 changes: 0 additions & 15 deletions Gemfile-v3

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ activate :minify_html, remove_input_attributes: false
These are the default settings, as listed in the [Htmlcompressor documentation](https://github.com/paolochiodi/htmlcompressor#usage):
```ruby
activate :minify_html do |html|
html.ignore = [] # Patterns to avoid minifying
html.content_types = [text/html] # Content types of resources that contain HTML
html.remove_multi_spaces = true # Remove multiple spaces
html.remove_comments = true # Remove comments
html.remove_intertag_spaces = false # Remove inter-tag spaces
Expand Down
18 changes: 12 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ require 'bundler'
Bundler::GemHelper.install_tasks

require 'cucumber/rake/task'
require 'rake/clean'

Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
ENV["TEST"] = "true"
exempt_tags = ""
exempt_tags << "--tags ~@nojava" if RUBY_PLATFORM == "java"
t.cucumber_opts = "--color --tags ~@wip #{exempt_tags} --strict --format #{ENV['CUCUMBER_FORMAT'] || 'pretty'}"
end

require 'rake/clean'
exempt_tags = ''
exempt_tags = "#{exempt_tags} --tags 'not @nojava'" if RUBY_PLATFORM == "java"
t.cucumber_opts = "--color --tags 'not @wip' #{exempt_tags} --strict --format #{ENV['CUCUMBER_FORMAT'] || 'pretty'}"
end

task :test => ["cucumber"]
task :test => [:cucumber]

desc "Build HTML documentation"
task :doc do
sh 'bundle exec yard'
end

task('default').clear

desc 'Run all tests'
task default: [:test]
12 changes: 6 additions & 6 deletions features/minify.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: Minify HTML

Scenario: Preview HTML with minify_html disabled
Given a fixture app "basic-app"
And a file named "config.rb" with:
Expand All @@ -8,7 +8,7 @@ Feature: Minify HTML
And the Server is running at "basic-app"
When I go to "/index.html"
Then I should see "13" lines

Scenario: Preview HTML with minify_html enabled
Given the Server is running at "basic-app"
When I go to "/index.html"
Expand All @@ -27,12 +27,12 @@ Feature: Minify HTML
Given a fixture app "basic-app"
And a file named "config.rb" with:
"""
activate :minify_html, :preserve_line_breaks => true
activate :minify_html, preserve_line_breaks: true
"""
And the Server is running at "basic-app"
When I go to "/index.html"
Then I should see "13" lines

Scenario: Build HTML with minify_html disabled
Given a fixture app "basic-app"
And a file named "config.rb" with:
Expand All @@ -41,7 +41,7 @@ Feature: Minify HTML
And a successfully built app at "basic-app"
When I cd to "build"
Then the file "index.html" should contain " <h1>"

Scenario: Build HTML with minify_html enabled
Given a fixture app "basic-app"
And a successfully built app at "basic-app"
Expand All @@ -54,4 +54,4 @@ Feature: Minify HTML
// a comment
echo "bar";
?>
"""
"""
41 changes: 36 additions & 5 deletions lib/middleman-minify-html/extension.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require 'htmlcompressor'

module Middleman
class MinifyHtmlExtension < Extension
class MinifyHtmlExtension < ::Middleman::Extension
option :ignore, [], 'Patterns to avoid minifying'
option :content_types, %w[text/html], 'Content types of resources that contain HTML'
# compressor options
option :remove_multi_spaces, true, 'Remove multiple spaces'
option :remove_comments, true, 'Remove comments'
option :remove_intertag_spaces, false, 'Remove inter-tag spaces'
Expand All @@ -17,13 +22,39 @@ class MinifyHtmlExtension < Extension
option :simple_boolean_attributes, true, 'Use simple boolean attributes'
option :preserve_patterns, nil, 'Patterns to preserve'

def initialize(*)
def initialize(app, _options_hash = ::Middleman::EMPTY_HASH, &block)
super
require 'htmlcompressor'

@ignore = Array(options[:ignore])
compressor_options = options.to_h.reject do |k, _|
[:ignore, :content_types].include?(k)
end
@compressor = HtmlCompressor::Compressor.new(compressor_options)
end

def after_configuration
app.use ::HtmlCompressor::Rack, options.to_h
def manipulate_resource_list_container!(resource_list)
resource_list.by_binary(false).each do |r|
type = r.content_type.try(:slice, /^[^;]*/)
if minifiable?(type) && !ignore?(r.destination_path)
r.add_filter method(:minify)
end
end
end

def minifiable?(content_type)
options[:content_types].include?(content_type)
end
memoize :minifiable?

def ignore?(path)
@ignore.any? { |ignore| ::Middleman::Util.path_match(ignore, path) }
end
memoize :ignore?

def minify(content)
@compressor.compress(content)
end
memoize :minify

end
end
2 changes: 1 addition & 1 deletion lib/middleman-minify-html/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Middleman
module MinifyHtml
VERSION = "3.4.1"
VERSION = "4.0.0"
end
end
6 changes: 3 additions & 3 deletions middleman-minify-html.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ Gem::Specification.new do |s|
s.files = `git ls-files -z`.split("\0")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ["lib"]
s.add_runtime_dependency("middleman-core", [">= 3.2"])
s.add_runtime_dependency("htmlcompressor", ["~> 0.2.0"])
end
s.add_runtime_dependency("middleman-core", [">= 5.0.0.rc.1"])
s.add_runtime_dependency("htmlcompressor", ["~> 0"])
end

0 comments on commit 6808039

Please sign in to comment.