Module: Kramdown::Options

### Home Classes Methods

### In Files

- lib/kramdown/options.rb

### Sections

- Option Definitions
- Option Validators
- Option definitions

### Namespace

- CLASS Kramdown::Options::Boolean

### Methods

- ::defaults
- ::define
- ::defined?
- ::definitions
- ::merge
- ::parse
- ::simple_array_validator

### Class/Module Index

Quicksearch

- Kramdown
- Kramdown::Converter
- Kramdown::Converter::Base
- Kramdown::Converter::Html
- Kramdown::Converter::Kramdown
- Kramdown::Converter::Latex
- Kramdown::Converter::RemoveHtmlTags
- Kramdown::Converter::Toc
- Kramdown::Document
- Kramdown::Element
- Kramdown::Error
- Kramdown::Options
- Kramdown::Options::Boolean
- Kramdown::Parser
- Kramdown::Parser::Base
- Kramdown::Parser::Html
- Kramdown::Parser::Html::Constants
- Kramdown::Parser::Html::ElementConverter
- Kramdown::Parser::Html::Parser
- Kramdown::Parser::Kramdown
- Kramdown::Parser::Markdown
- Kramdown::Utils
- Kramdown::Utils::Entities
- Kramdown::Utils::Entities::Entity
- Kramdown::Utils::Html
- Kramdown::Utils::OrderedHash

No matching classes.

# 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 ↑ top

This sections contains all option definitions that are used by the included parsers/converters.

## Option Validators ↑ top

This sections contains all pre-defined option validators.

### Public Class Methods

simple_array_validator(val, name, size) click to toggle source

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.

Additionally, the array is checked for the correct size.

    # File lib/kramdown/options.rb, line 138
    def self.simple_array_validator(val, name, size)
      if String === val
        val = val.split(/,/)
      elsif !(Array === val)
        raise Kramdown::Error, "Invalid type #{val.class} for option #{name}"
      end
      if val.size != size
        raise Kramdown::Error, "Option #{name} needs exactly #{size} values"
      end
      val
    end

## Option definitions ↑ top

This sections describes the methods that can be used on the Options module.

### Constants

ALLOWED_TYPES

  Allowed option types.

Definition

  Struct class for storing the definition of an option.

### Public Class Methods

defaults() click to toggle source

Return a Hash with the default values for all options.

    # File lib/kramdown/options.rb, line 81
    def self.defaults
      temp = {}
      @options.each {|n, o| temp[o.name] = o.default}
      temp
    end

define(name, type, default, desc, &block) click to toggle source

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, line 62
    def self.define(name, type, default, desc, &block)
      raise ArgumentError, "Option name #{name} is already used" if @options.has_key?(name)
      raise ArgumentError, "Invalid option type #{type} specified" if !ALLOWED_TYPES.include?(type)
      raise ArgumentError, "Invalid type for default value" if !(type === default) && !default.nil?
      raise ArgumentError, "Missing validator block" if type == Object && block.nil?
      @options[name] = Definition.new(name, type, default, desc, block)
    end

defined?(name) click to toggle source

Return `true` if an option called `name` is defined.

    # File lib/kramdown/options.rb, line 76
    def self.defined?(name)
      @options.has_key?(name)
    end

definitions() click to toggle source

Return all option definitions.

    # File lib/kramdown/options.rb, line 71
    def self.definitions
      @options
    end

merge(hash) click to toggle source

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, line 89
    def self.merge(hash)
      temp = defaults
      hash.each do |k,v|
        next unless @options.has_key?(k)
        temp[k] = parse(k, v)
      end
      temp
    end

parse(name, data) click to toggle source

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, line 103
    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?
               end
      end
      data = @options[name].validator[data] if @options[name].validator
      data
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.