Module: Zen::Markup

Extended by:
Ramaze::Helper::CGI
Defined in:
lib/zen/markup.rb

Overview

Markup is a module that makes it easy to convert markup from one format to another. It can be used to generate HTML using Markdown and Textile or to escape HTML using Ramaze::Helper::CGI.

Using this module is quite easy, simply call the .convert method of and pass the name of the markup engine you'd like to use to it along with some markup. For example, to convert Markdown to HTML you'd call the following:

html = Zen::Markup.convert(:markdown, 'Hello **world**')

When converting Markdown or Textile Zen will automatically install the required gems. For Markdown RDiscount is used, for Textile RedCloth is used.

You may be tempted to call the various methods directly but it is not recommended to do so. For example, you might have the following code:

# Taken from an HTML form
engine = 'markdown'
markup = '...'
html   = Zen::Markdown.send(engine, markup)

This however makes it possible to do some serious damage. Consider the following:

engine = 'instance_eval'
markup = 'eval("....")'
html   = Zen::Markdown.send(engine, markup)

Because of this you should use Markup.convert instead as this method will verify the engine name before calling the method for it.

Adding Markup Engines

Adding a new engine is relatively easy and is done in two steps. First you should add the name of your engine and it's label to REGISTERED. The keys of this hash should be the methods to call, teh values will be displayed in various <select> elements in the administration interface.

Zen::Markup::REGISTERED['my_markup'] = 'My Markup'

In this example the label is hardcoded but it's recommended to use Language.lang instead.

Once the engine has been added to the list you'll have to add a corresponding method. This can be done as following:

module Zen
  module Markup
    private

    def self.my_markup(markup)

    end
  end
end

Each markup method should be a class method and should take a single parameter. This parameter will contain the markup to convert. The return value of these methods should be the converted markup:

module Zen
  module Markup
    private

    def self.my_markup(markup)
      return markup.upcase
    end
  end
end

Since:

Constant Summary

REGISTERED =

Hash containing all the markup engines and their names as they'll be displayed in the backend.

Since:

  • 0.2.5

{
  'markdown' => 'zen_general.markup.markdown',
  'textile'  => 'zen_general.markup.textile',
  'plain'    => 'zen_general.markup.plain',
  'html'     => 'zen_general.markup.html'
}

Class Method Summary (collapse)

Class Method Details

+ (String) convert(engine, markup)

Converts markup using the specified markup engine.

Examples:

Zen::Markup.conver(:markdown, 'Hello **world**')

Parameters:

  • engine (#to_s)

    The markup engine to use.

  • markup (String)

    The markup to convert.

Returns:

  • (String)

Since:

  • 0.3



115
116
117
118
119
120
121
# File 'lib/zen/markup.rb', line 115

def convert(engine, markup)
  unless REGISTERED.keys.include?(engine.to_s)
    raise(ArgumentError, "The specified engine \"#{engine}\" is invalid")
  end

  return send(engine, markup)
end

+ (String) html(markup) (private)

Ignores the markup and just returns it.

Parameters:

  • markup (String)

    The markup to return.

Returns:

  • (String)

    String containing our markup.

Since:

  • 0.2.5



184
185
186
# File 'lib/zen/markup.rb', line 184

def html(markup)
  return markup
end

+ (String) markdown(markup) (private)

Converts the Markdown markup to HTML using RDiscount.

Examples:

Zen::Markup.markdown('Hello **world**')

Parameters:

  • markup (String)

    The Markdown string to convert to HTML.

Returns:

  • (String)

    The HTML returned by RDiscount.

Since:

  • 0.2.5



135
136
137
138
139
140
141
142
143
# File 'lib/zen/markup.rb', line 135

def markdown(markup)
  unless Kernel.const_defined?(:RDiscount)
    Ramaze.setup(:verbose => false) do
      gem 'rdiscount'
    end
  end

  return RDiscount.new(markup).to_html
end

+ (String) plain(markup) (private)

Escapes all HTML using Ramaze::Helper::CGI.

Parameters:

  • markup (String)

    The markup in which all HTML tags should be escaped.

Returns:

  • (String)

    String containing the escaped HTML.

Since:

  • 0.2.5



173
174
175
# File 'lib/zen/markup.rb', line 173

def plain(markup)
  return h(markup)
end

+ (String) textile(markup) (private)

Converts Textile to HTML using RedCloth.

Examples:

Zen::Markup.textile('Hello *world*')

Parameters:

  • markup (String)

    The Textile markup to convert to HTML.

Returns:

  • (String)

    The HTML returned by RedCloth.

Since:

  • 0.2.5



155
156
157
158
159
160
161
162
163
# File 'lib/zen/markup.rb', line 155

def textile(markup)
  unless Kernel.const_defined?(:RedCloth)
    Ramaze.setup(:verbose => false) do
      gem 'RedCloth', :lib => 'redcloth'
    end
  end

  return RedCloth.new(markup).to_html
end

+ (Hash) to_hash

Returns a hash containing the available markup engines and their labels for the current language.

Returns:

  • (Hash)

Since:

  • 15-11-2011



96
97
98
99
100
101
102
# File 'lib/zen/markup.rb', line 96

def to_hash
  hash = {}

  REGISTERED.each { |k, v| hash[k] = lang(v) }

  return hash
end