2007-01-19 17:03:47 +00:00
|
|
|
# This file contains redefinitions of and wrappers around various text
|
|
|
|
# filters so they can be used as Haml filters.
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
|
|
|
require 'erb'
|
|
|
|
require 'sass/engine'
|
|
|
|
require 'stringio'
|
|
|
|
|
2007-08-11 21:04:51 +00:00
|
|
|
begin
|
|
|
|
require 'rubygems'
|
|
|
|
rescue LoadError; end
|
2007-01-19 17:03:47 +00:00
|
|
|
|
|
|
|
class ERB; alias_method :render, :result; end
|
|
|
|
|
|
|
|
module Haml
|
|
|
|
module Filters
|
|
|
|
class Plain
|
|
|
|
def initialize(text)
|
|
|
|
@text = text
|
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
@text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Ruby
|
|
|
|
def initialize(text)
|
|
|
|
@text = text
|
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
old_stdout = $stdout
|
|
|
|
$stdout = StringIO.new
|
|
|
|
Object.new.instance_eval(@text)
|
|
|
|
old_stdout, $stdout = $stdout, old_stdout
|
|
|
|
old_stdout.pos = 0
|
|
|
|
old_stdout.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-06 08:08:32 +00:00
|
|
|
class Preserve
|
|
|
|
def initialize(text)
|
|
|
|
@text = text
|
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
Haml::Helpers.preserve(@text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-11 21:04:51 +00:00
|
|
|
class LazyLoaded
|
|
|
|
def initialize(*reqs)
|
|
|
|
reqs[0...-1].each do |req|
|
|
|
|
begin
|
|
|
|
@required = req
|
|
|
|
require @required
|
|
|
|
return
|
|
|
|
rescue LoadError; end # RCov doesn't see this, but it is run
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
@required = reqs[-1]
|
|
|
|
require @required
|
|
|
|
rescue LoadError => e
|
|
|
|
classname = self.class.to_s.gsub(/\w+::/, '')
|
|
|
|
|
|
|
|
if reqs.size == 1
|
|
|
|
raise HamlError.new("Can't run #{classname} filter; required file '#{reqs.first}' not found")
|
|
|
|
else
|
|
|
|
raise HamlError.new("Can't run #{classname} filter; required #{reqs.map { |r| "'#{r}'" }.join(' or ')}, but none were found")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-01-31 06:38:23 +00:00
|
|
|
end
|
2007-08-11 21:04:51 +00:00
|
|
|
|
|
|
|
class RedCloth < LazyLoaded
|
|
|
|
def initialize(text)
|
|
|
|
super('redcloth')
|
|
|
|
@engine = ::RedCloth.new(text)
|
|
|
|
end
|
2007-01-31 06:38:23 +00:00
|
|
|
|
2007-08-11 21:04:51 +00:00
|
|
|
def render
|
|
|
|
@engine.to_html
|
|
|
|
end
|
|
|
|
end
|
2007-01-19 17:03:47 +00:00
|
|
|
|
2007-08-11 21:04:51 +00:00
|
|
|
# Uses RedCloth to provide only Textile (not Markdown) parsing
|
|
|
|
class Textile < RedCloth
|
|
|
|
def render
|
|
|
|
@engine.to_html(:textile)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Uses BlueCloth or RedCloth to provide only Markdown (not Textile) parsing
|
|
|
|
class Markdown < LazyLoaded
|
|
|
|
def initialize(text)
|
|
|
|
super('bluecloth', 'redcloth')
|
|
|
|
|
|
|
|
if @required == 'bluecloth'
|
|
|
|
@engine = ::BlueCloth.new(text)
|
|
|
|
else
|
|
|
|
@engine = ::RedCloth.new(text)
|
2007-01-19 17:03:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-11 21:04:51 +00:00
|
|
|
def render
|
|
|
|
if @engine.is_a?(::BlueCloth)
|
|
|
|
@engine.to_html
|
|
|
|
else
|
|
|
|
@engine.to_html(:markdown)
|
2007-01-19 17:03:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# :startdoc:
|