Module: MultiTypograf

Defined in:
lib/multi_typograf.rb,
lib/multi_typograf/version.rb,
lib/multi_typograf/adapters/typograf.rb,
lib/multi_typograf/adapters/typography.rb,
lib/multi_typograf/adapters/typographer.rb,
lib/multi_typograf/adapters/art_typograf.rb,
lib/multi_typograf/adapters/typographica.rb

Defined Under Namespace

Modules: Adapters Classes: Error

Constant Summary collapse

REQUIREMENT_MAP =
[
  ["art_typograf", :art_typograf],
  ["typographer/parsers/basic",  :typographer],
  ["typographica", :typographica],
  ["typograf", :typograf]
]
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.adapterObject Also known as: engine

Get the current adapter class.



48
49
50
51
52
# File 'lib/multi_typograf.rb', line 48

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

.current_adapter(options) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/multi_typograf.rb', line 87

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/multi_typograf.rb', line 27

def default_adapter
  return :art_typograf if defined?(::ArtTypograf)
  return :typographer if defined?(::TypographerHelper::Parsers::Basic)
  return :typographica if defined?(::Typographica)
  return :typograf if defined?(::Typograf)

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

  :typograf
end

.load_adapter(new_adapter) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/multi_typograf.rb', line 71

def load_adapter(new_adapter)
  case new_adapter
  when String, Symbol
    require "multi_typograf/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_typograf/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

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

Minify CSS



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/multi_typograf.rb', line 96

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

  adapter = current_adapter(options)
  if defined?(adapter::ParseError)
    begin
      adapter.process(string, options)
    rescue adapter::ParseError => exception
      raise ::MultiTypograf::Error.new(exception.message, exception.backtrace)
    end
  else
    adapter.process(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:

  • :typograf

  • :art_typograf

  • :typographer

  • :typography

  • :typographica



64
65
66
# File 'lib/multi_typograf.rb', line 64

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