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
Constant Summary
- REGISTERED =
Hash containing all the markup engines and their names as they'll be displayed in the backend.
{ 'markdown' => 'zen_general.markup.markdown', 'textile' => 'zen_general.markup.textile', 'plain' => 'zen_general.markup.plain', 'html' => 'zen_general.markup.html' }
Class Method Summary (collapse)
-
+ (String) convert(engine, markup)
Converts markup using the specified markup engine.
-
+ (String) html(markup)
private
Ignores the markup and just returns it.
-
+ (String) markdown(markup)
private
Converts the Markdown markup to HTML using RDiscount.
-
+ (String) plain(markup)
private
Escapes all HTML using Ramaze::Helper::CGI.
-
+ (String) textile(markup)
private
Converts Textile to HTML using RedCloth.
-
+ (Hash) to_hash
Returns a hash containing the available markup engines and their labels for the current language.
Class Method Details
+ (String) convert(engine, markup)
Converts markup using the specified markup engine.
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.
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.
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.
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.
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.
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 |