在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:paolochiodi/htmlcompressor开源软件地址:https://github.com/paolochiodi/htmlcompressor开源编程语言:Ruby 82.3%开源软件介绍:HtmlcompressorPut your html on a dietHtmlcompressor provides tools to minify html code. It includes
Please note that Htmlcompressor is still in alpha version and need some additional love. UsageUsing 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_spaces_inside_tags => 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
} Htmlcompressor also ships a rack middleware that can be used with rails, sinatra or rack. However keep in mind that compression is slow (especially when also compressing javascript or css) and can negatively impact the performance of your website. Take your measurements and adopt a different strategy if necessary 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 compressionYou can compress javascript templates that are present in the html.
Setting the Custom preservation rulesIf you need to define custom preservation rules, you can list regular expressions in the options = {
:preserve_patterns => [/<\?php.*?\?>/im]
} CSS and JavaScript CompressionBy 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 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
} StatisticsAs of now the statistic framework hasn't been ported. Refer to original htmlcompressor documentation for statistics on minified pages. LicenseLicensed 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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论