module Kramdown::Options
This module defines all options that are used by parsers and/or converters as well as providing methods to deal with the options.
Option Definitions
↑ topConstants
- SMART_QUOTES_ENTITIES
- SMART_QUOTES_STR
- TOC_LEVELS_ARRAY
- TOC_LEVELS_RANGE
Option Validators
↑ topPublic Class Methods
Ensures that the option value val
for the option called name
is a valid array. The parameter val
can be
-
a comma separated string which is split into an array of values
-
or an array.
Optionally, the array is checked for the correct size.
# File lib/kramdown/options.rb 141 def self.simple_array_validator(val, name, size = nil) 142 if String === val 143 val = val.split(",") 144 elsif !(Array === val) 145 raise Kramdown::Error, "Invalid type #{val.class} for option #{name}" 146 end 147 if size && val.size != size 148 raise Kramdown::Error, "Option #{name} needs exactly #{size} values" 149 end 150 val 151 end
Ensures that the option value val
for the option called name
is a valid hash. The parameter val
can be
-
a hash in YAML format
-
or a Ruby Hash object.
# File lib/kramdown/options.rb 158 def self.simple_hash_validator(val, name) 159 if String === val 160 begin 161 val = YAML.safe_load(val) 162 rescue RuntimeError, ArgumentError, SyntaxError 163 raise Kramdown::Error, "Invalid YAML value for option #{name}" 164 end 165 end 166 raise Kramdown::Error, "Invalid type #{val.class} for option #{name}" unless Hash === val 167 val 168 end
Option definitions
↑ topConstants
- ALLOWED_TYPES
Allowed option types.
- Definition
Struct class for storing the definition of an option.
Public Class Methods
Return a Hash with the default values for all options.
# File lib/kramdown/options.rb 71 def self.defaults 72 @defaults ||= 73 begin 74 temp = {} 75 @options.each {|_n, o| temp[o.name] = o.default } 76 temp.freeze 77 end 78 end
Define a new option called name
(a Symbol) with the given type
(String, Integer, Float, Symbol, Boolean
, Object), default value default
and the description desc
. If a block is specified, it should validate the value and either raise an error or return a valid value.
The type ‘Object’ should only be used for complex types for which none of the other types suffices. A block needs to be specified when using type ‘Object’ and it has to cope with a value given as string and as the opaque type.
# File lib/kramdown/options.rb 50 def self.define(name, type, default, desc, &block) 51 name = name.to_sym 52 raise ArgumentError, "Option name #{name} is already used" if @options.key?(name) 53 raise ArgumentError, "Invalid option type #{type} specified" unless ALLOWED_TYPES.include?(type) 54 raise ArgumentError, "Invalid type for default value" if !(type === default) && !default.nil? 55 raise ArgumentError, "Missing validator block" if type == Object && block.nil? 56 @options[name] = Definition.new(name, type, default, desc, block) 57 @defaults = nil 58 end
Return true
if an option called name
is defined.
# File lib/kramdown/options.rb 66 def self.defined?(name) 67 @options.key?(name.to_sym) 68 end
Return all option definitions.
# File lib/kramdown/options.rb 61 def self.definitions 62 @options 63 end
Merge the defaults Hash with the parsed options from the given Hash, i.e. only valid option names are considered and their value is run through the parse method.
# File lib/kramdown/options.rb 82 def self.merge(hash) 83 temp = defaults.dup 84 hash.each do |k, v| 85 k = k.to_sym 86 temp[k] = @options.key?(k) ? parse(k, v) : v 87 end 88 temp 89 end
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.
# File lib/kramdown/options.rb 96 def self.parse(name, data) 97 name = name.to_sym 98 raise ArgumentError, "No option named #{name} defined" unless @options.key?(name) 99 unless @options[name].type === data 100 data = data.to_s 101 data = if @options[name].type == String 102 data 103 elsif @options[name].type == Integer 104 Integer(data) rescue raise Kramdown::Error, "Invalid integer value for option '#{name}': '#{data}'" 105 elsif @options[name].type == Float 106 Float(data) rescue raise Kramdown::Error, "Invalid float value for option '#{name}': '#{data}'" 107 elsif @options[name].type == Symbol 108 str_to_sym(data) 109 elsif @options[name].type == Boolean 110 data.downcase.strip != 'false' && !data.empty? 111 end 112 end 113 data = @options[name].validator[data] if @options[name].validator 114 data 115 end
Converts the given String data
into a Symbol or nil
with the following provisions:
-
A leading colon is stripped from the string.
-
An empty value or a value equal to “nil” results in
nil
.
# File lib/kramdown/options.rb 122 def self.str_to_sym(data) 123 data = data.strip 124 data = data[1..-1] if data[0] == ':' 125 (data.empty? || data == 'nil' ? nil : data.to_sym) 126 end