Module: MultiHtml

Defined in:
lib/multi_html.rb,
lib/multi_html/version.rb,
lib/multi_html/adapters/htmlmin.rb,
lib/multi_html/adapters/html_min.rb,
lib/multi_html/adapters/html_press.rb,
lib/multi_html/adapters/html_minifier.rb,
lib/multi_html/adapters/htmlcompressor.rb,
lib/multi_html/adapters/html_compressor.rb

Defined Under Namespace

Modules: Adapters Classes: ParseError

Constant Summary collapse

REQUIREMENT_MAP =
[
  ["html_minifier", :html_minifier],
  # ["html_compressor", :html_compressor],
  # ["html_min", :html_min],
  # ["htmlmin", :htmlmin],
  # ["htmlcompressor", :htmlcompressor],
  ["html_press", :html_press]
]
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.adapterObject Also known as: engine

Get the current adapter class.



52
53
54
55
56
# File 'lib/multi_html.rb', line 52

def adapter
  return @adapter if @adapter
  self.use self.default_adapter
  @adapter
end

.current_adapter(options) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/multi_html.rb', line 91

def current_adapter(options)
  if new_adapter = (options || {}).delete(:adapter)
    load_adapter(new_adapter)
  else
    adapter
  end
end

.default_adapterObject Also known as: default_engine

The default adapter based on what you currently have loaded and installed. First checks to see if any adapters are already loaded, then checks to see which are installed if none are loaded.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/multi_html.rb', line 29

def default_adapter
  return :html_minifier if defined?(::HtmlMinifier)
  # return :html_compressor if defined?(::HtmlCompressor)
  # return :html_min if defined?(::HTMLMin)
  # return :htmlmin if defined?(::HTMLMin)
  # return :htmlcompressor if defined?(::HtmlCompressor)
  return :html_press if defined?(::HtmlPress)

  REQUIREMENT_MAP.each do |(library, adapter)|
    begin
      require library
      return adapter
    rescue LoadError
      next
    end
  end

  :html_press
end

.load_adapter(new_adapter) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/multi_html.rb', line 75

def load_adapter(new_adapter)
  case new_adapter
  when String, Symbol
    require "multi_html/adapters/#{new_adapter}"
    self::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when NilClass, FalseClass
    default_adapter = self.default_adapter
    require "multi_html/adapters/#{default_adapter}"
    self::Adapters.const_get(:"#{default_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when Class
    new_adapter
  else
    raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
  end
end

.min(string, options = {}) ⇒ Object

Minify HTML



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/multi_html.rb', line 100

def min(string, options={})
  string = string.read if string.respond_to?(:read)

  adapter = current_adapter(options)
  if defined?(adapter::ParseError)
    begin
      adapter.min(string, options)
    rescue adapter::ParseError => exception
      raise ::MultiHtml::ParseError.new(exception.message, exception.backtrace)
    end
  else
    adapter.min(string, options)
  end
end

.use(new_adapter) ⇒ Object Also known as: adapter=, engine=

Set the adapter utilizing a symbol, string, or class. Supported by default are:

  • :html_press

  • :cssminify

  • :yuicssmin

  • :yui_compressor

  • :rainpress



68
69
70
# File 'lib/multi_html.rb', line 68

def use(new_adapter)
  @adapter = load_adapter(new_adapter)
end