Module: Kramdown::Options

Defined in:
lib/burr/kramdown_ext/options.rb

Class Method Summary collapse

Class Method Details

.parse(name, data) ⇒ Object

Parse the given value data as if it was a value for the option name and return the parsed value with the correct type.

If data already has the correct type, it is just returned. Otherwise it is converted to a String and then to the correct type.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/burr/kramdown_ext/options.rb', line 9

def self.parse(name, data)
  raise ArgumentError, "No option named #{name} defined" if !@options.has_key?(name)
  if !(@options[name].type === data)
    data = data.to_s
    data = if @options[name].type == String
             data
           elsif @options[name].type == Integer
             Integer(data) rescue raise Kramdown::Error, "Invalid integer value for option '#{name}': '#{data}'"
           elsif @options[name].type == Float
             Float(data) rescue raise Kramdown::Error, "Invalid float value for option '#{name}': '#{data}'"
           elsif @options[name].type == Symbol
             data.strip!
             data = data[1..-1] if data[0] == ?:
             (data.empty? || data == 'nil' ? nil : data.to_sym)
           elsif @options[name].type == Boolean
             data.downcase.strip != 'false' && !data.empty?
           else
             data
           end
  end
  data = @options[name].validator[data] if @options[name].validator
  data
end