require 'singleton'
require 'redcloth'
require 'cgi'
require 'facet/classinherit'
require 'glue/sanitize'
module Glue
# Generalised Markup transformations.
#
# The expand methods evaluate (expand) the markup
# code to produce the final content. The compact
# methods reverse this process to create the original
# markup code. Not all markup transformations are
# reversible.
#
# When this library is included, the default PropertyUtils
# implementation is overriden to add markup support.
#
# === Examples
#
# Define your custom markup methods like this:
#
# module Markup
# def markup_simple
# ...
# end
# def markup_special
# ...
# end
#
# # maps the {{..}} macro
# alias_method :sanitize, :markup_simple
# # maps the {|..|} macro
# alias_method :markup, :markup_special
# end
#
# here comes the #{obj.body} # => prints the expanded version.
#
# obj.body = markup(@params['body'])
module Markup
# The default markup method. You should override this method
# in your application to call your custom markup
# methods.
def expand(str)
if str
xstr = str.dup
xstr.gsub!(/, '<')
xstr.gsub!(/>/, '>')
xstr.gsub!(/\r/, '
')
return String.sanitize(xstr)
end
return nil
end
alias_method :sanitize, :expand
# ...
def expand_redcloth(str)
if str
return RedCloth.new(expand(str)).to_html
end
return nil
end
alias_method :markup, :expand_redcloth
# Compact (reverse) the content to the origial markup
# code. Not all markup transformations are reversible.
# You should override this method in your application
# to call your custom markup methods.
#
# NOT IMPLEMENTED.
def compact(str, meth = nil)
end
# Remove markup code from the input string.
#
# NOT IMPLEMENTED.
def clear(str)
end
def escape(str)
return nil unless str
CGI.escape(str.gsub(/ /, '_'))
end
def unescape(str)
CGI.unescape(str.gsub(/_/, ' '))
end
class_inherit do
# Helper method for manipulating the sanitize transformation.
def setup_sanitize_transform(&block)
self.send :define_method, :sanitize, block
end
alias_method :setup_sanitize_transformation, :setup_sanitize_transform
# Helper method for manipulating the markup transformation.
def setup_markup_transform(&block)
self.send :define_method, :markup, block
end
alias_method :setup_markup_transformation, :setup_markup_transform
end
end
# An abstract Markup class.
class MarkupKit
extend Markup
include Markup
end
end
# * George Moschovitis