require 'redcloth' require 'glue/property' module Nitro #-- # Override the default PropertyUtils implementation to # add markup support. #++ module PropertyUtils # Override to add markup code. # def self.prop_setter(prop) s = prop.symbol if markup = prop.meta[:markup] # if true, set to default Markup markup = Nitro::Markup if true == markup code = %{ def #{s}=(val) } if Property.type_checking code << %{ unless String == val.class raise "Invalid type, expected '#{prop.klass}', is '\#\{val.class\}'." end } end code << %{ @#{s} = #{markup}.expand(val) end def compact_#{s} #{markup}.compact(@#{s}) end } return code else return %{ def #{s}=(val) @#{s} = val end } end end end # 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 # # here comes the #{obj.body} # => prints the expanded version. # # obj.body = Nitro::Markup.expand(@params['body']) module Markup def self.expand_html!(str) return unless str str.gsub!(/"/, '"') str.gsub!(/'/, ''') str.gsub!(//, '>') str.gsub!(/\r\n/, '
') return str end def self.compact_html!(str) return unless str str.gsub!(/"/, '"') str.gsub!(/'/, "'") str.gsub!(/</, '<') str.gsub!(/>/, '>') # gmosx: SOS! double quotes ARE needed for \r\n!! str.gsub!(/\s
/, "\r\n") return str end # Expand the markup code to produce the # actual content. You should override this method # in your application to call your custom markup # methods. def self.expand(str) if str xstr = str.dup expand_html!(xstr) return xstr end return nil end # 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. def self.compact(str) if str xstr = str.dup compact_html!(xstr) return xstr end return nil end # Remove markup code from the input string. # NOT IMPLEMENTED. def self.clear(str) end end # Markup shader. # # === Examples # #

#{{Welcome}}

class MarkupShader < Shader def process(hash, text) # handle strings text.gsub!(/\#\{\{(.*?)\}\}/, '#{RedCloth.new(\1).to_html}') process_next(hash, text) end end end # * George Moschovitis