Module: MultiMarkdown

Defined in:
lib/multi_markdown/version.rb,
lib/multi_markdown/multi_markdown.rb

Constant Summary collapse

VERSION =

multi_markdown version

"0.2.0"
LIBRARIES =

Markdown library names

{
  :bluecloth     => 'bluecloth',
  :kramdown      => 'kramdown',
  :maruku        => 'maruku',
  :rdiscount     => 'rdiscount',
  :redcarpet     => 'redcarpet',
  :rpeg_markdown => 'peg_markdown'
}
CONSTANTS =

Markdown Constants

{
  :bluecloth     => 'BlueCloth',
  :kramdown      => 'Kramdown::Document',
  :maruku        => 'Maruku',
  :rdiscount     => 'RDiscount',
  :redcarpet     => 'RedcarpetCompat',
  :rpeg_markdown => 'PEGMarkdown'
}
PRIORITY =

The loading priority

[
  :redcarpet,
  :rdiscount,
  :kramdown,
  :bluecloth,
  :maruku,
  :rpeg_markdown
]
@@markdown =
nil

Class Method Summary collapse

Class Method Details

.find(library) ⇒ Object

Attempts to find the specific Markdown library.

Parameters:

  • library (Symbol)

    The name of the markdown library.

Raises:

  • (ArgumentError)

    Unknown Markdown library name.

  • (NameError)

    The constant for the Markdown library could not be found.



48
49
50
51
52
53
54
# File 'lib/multi_markdown/multi_markdown.rb', line 48

def self.find(library)
  unless CONSTANTS.has_key?(library)
    raise(ArgumentError,"unknown Markdown library: #{library}")
  end

  eval(CONSTANTS[library])
end

.load#new

Attempts to find or load the first available Markdown library.

Returns:

  • (#new)

    The Markdown class.

Raises:

  • (LoadError)

    None of the supported Markdown libraries could be found or loaded.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/multi_markdown/multi_markdown.rb', line 97

def self.load
  # attempt to find an already loaded markdown library
  PRIORITY.each do |library|
    begin
      return(@@markdown = find(library))
    rescue NameError
    end
  end

  PRIORITY.each do |library|
    begin
      return use(library)
    rescue Gem::LoadError => e
      # re-raise Gem::LoadErrors, as they are a sign of dependency issues
      raise(e)
    rescue LoadError
    end
  end

  raise(LoadError,"could not load any of the markdown libraries")
end

.new(text, options = {}) ⇒ #to_html

Loads the first available Markdown library and creates a Markdown document.

Parameters:

  • text (String)

    Markdown text.

  • options (Hash{Symbol => Object}) (defaults to: {})

    Additional options for the Markdown document.

Returns:

  • (#to_html)

    The Markdown document.



133
134
135
136
137
# File 'lib/multi_markdown/multi_markdown.rb', line 133

def self.new(text,options={})
  load unless @@markdown

  @@markdown.new(text,options)
end

.use(library) ⇒ #new

Uses a specific Markdown library.

Parameters:

  • library (Symbol)

    The name of the markdown library.

Returns:

  • (#new)

    The Markdown class.

Raises:

  • (ArgumentError)

    Unknown Markdown library name.

  • (LoadError)

    The Markdown library could not be loaded.

  • (NameError)

    The constant for the Markdown library could not be found.



76
77
78
79
80
81
82
83
84
# File 'lib/multi_markdown/multi_markdown.rb', line 76

def self.use(library)
  unless LIBRARIES.has_key?(library)
    raise(ArgumentError,"unknown Markdown library: #{library}")
  end

  require LIBRARIES[library]

  @@markdown = find(library)
end