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:

  • The name of the markdown library.

Raises:

  • Unknown Markdown library name.

  • The constant for the Markdown library could not be found.

API:

  • semipublic



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:

  • The Markdown class.

Raises:

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

API:

  • semipublic



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:

  • Markdown text.

  • (defaults to: {})

    Additional options for the Markdown document.

Returns:

  • The Markdown document.

API:

  • public



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:

  • The name of the markdown library.

Returns:

  • The Markdown class.

Raises:

  • Unknown Markdown library name.

  • The Markdown library could not be loaded.

  • The constant for the Markdown library could not be found.

API:

  • semipublic



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