Code Monkey home page Code Monkey logo

htmlcompressor's Introduction

Htmlcompressor

Build Status

Put your html on a diet

Htmlcompressor provides tools to minify html code. It includes

  • HtmlCompressor::Compressor class which is a raw port of google's htmlcompressor
  • HtmlCompressor::Rack a rack middleware to compress html pages on the fly

Please note that Htmlcompressor is still in alpha version and need some additional love.

Usage

Using the compressor class is straightforward:

  compressor = HtmlCompressor::Compressor.new
  compressor.compress('<html><body><div id="compress_me"></div></body></html>')

The compressor ships with basic and safe default options that may be overwritten passing the options hash to the constructor:

options = {
  :enabled => true,
  :remove_multi_spaces => true,
  :remove_comments => true,
  :remove_intertag_spaces => false,
  :remove_quotes => false,
  :compress_css => false,
  :compress_javascript => false,
  :simple_doctype => false,
  :remove_script_attributes => false,
  :remove_style_attributes => false,
  :remove_link_attributes => false,
  :remove_form_attributes => false,
  :remove_input_attributes => false,
  :remove_javascript_protocol => false,
  :remove_http_protocol => false,
  :remove_https_protocol => false,
  :preserve_line_breaks => false,
  :simple_boolean_attributes => false,
  :compress_js_templates => false
}

Using rack middleware (in rails) is as easy as:

config.middleware.use HtmlCompressor::Rack, options

And in sinatra:

use HtmlCompressor::Rack, options

The middleware uses a little more aggressive options by default:

options = {
  :enabled => true,
  :remove_multi_spaces => true,
  :remove_comments => true,
  :remove_intertag_spaces => false,
  :remove_quotes => true,
  :compress_css => false,
  :compress_javascript => false,
  :simple_doctype => false,
  :remove_script_attributes => true,
  :remove_style_attributes => true,
  :remove_link_attributes => true,
  :remove_form_attributes => false,
  :remove_input_attributes => true,
  :remove_javascript_protocol => true,
  :remove_http_protocol => false,
  :remove_https_protocol => false,
  :preserve_line_breaks => false,
  :simple_boolean_attributes => true
}

Rails 2.3 users may need to add

require 'htmlcompressor'

Javascript template compression

You can compress javascript templates that are present in the html. Setting the :compress_js_templates options to true will by default compress the content of script tags marked with type="text/x-jquery-tmpl". For compressing other types of templates, you can pass a string (or an array of strings) containing the type: :compress_js_templates => ['text/html']. Please note that activating template compression will disable the removal of quotes from attributes values, as this could lead to unexpected errors with compiled templates.

Custom preservation rules

If you need to define custom preservation rules, you can list regular expressions in the preserve_patterns option. For example, to preserve PHP blocks you might want to define:

options = {
  :preserve_patterns => [/<\?php.*?\?>/im]
}

CSS and JavaScript Compression

By default CSS/JS compression is disabled. In order to minify in page javascript and css, you need to supply a compressor in the options hash. A compressor can be :yui or :closure or any object that responds to :compress. E.g.: compressed = compressor.compress(source)

class MyCompressor

  def compress(source)
    return 'minified'
  end

end

options = {
  :compress_css => true,
  :css_compressor => MyCompressor.new,
  :compress_javascript => true,
  :javascript_compressor => MyCompressor.new
}

Please note that in order to use yui or closure compilers you need to manually add them to the Gemfile

gem 'yui-compressor'

...

options = {
  :compress_javascript => true,
  :javascript_compressor => :yui,
  :compress_css => true,
  :css_compressor => :yui
}
gem 'closure-compiler'

...

options = {
  :compress_javascript => true,
  :javascript_compressor => :closure
}

Statistics

As of now the statistic framework hasn't been ported. Refer to original htmlcompressor documentation for statistics on minified pages.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

htmlcompressor's People

Contributors

paolochiodi avatar whitehat101 avatar andrewkvalheim avatar le0pard avatar bhollis avatar scudelletti avatar gustavolobo avatar bolmaster2 avatar rmoriz avatar mc-jordan-andree avatar strcmp avatar

Watchers

James Cloos avatar HRoR avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.