2007-01-22 14:43:50 +01:00
|
|
|
$: << File.dirname(__FILE__) + "../../lib"
|
|
|
|
|
|
|
|
require_dependency 'chunks/chunk'
|
|
|
|
|
|
|
|
# The markup engines are Chunks that call the one of RedCloth
|
|
|
|
# or RDoc to convert text. This markup occurs when the chunk is required
|
|
|
|
# to mask itself.
|
|
|
|
module Engines
|
|
|
|
class AbstractEngine < Chunk::Abstract
|
|
|
|
|
|
|
|
# Create a new chunk for the whole content and replace it with its mask.
|
|
|
|
def self.apply_to(content)
|
|
|
|
new_chunk = self.new(content)
|
|
|
|
content.replace(new_chunk.mask)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Never create engines by constructor - use apply_to instead
|
|
|
|
def initialize(content)
|
|
|
|
@content = content
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Textile < AbstractEngine
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'sanitize'
|
2007-02-22 08:06:53 +01:00
|
|
|
include Sanitize
|
2007-01-22 14:43:50 +01:00
|
|
|
def mask
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'redcloth'
|
2007-01-22 14:43:50 +01:00
|
|
|
redcloth = RedCloth.new(@content, [:hard_breaks] + @content.options[:engine_opts])
|
|
|
|
redcloth.filter_html = false
|
|
|
|
redcloth.no_span_caps = false
|
2007-02-20 06:15:39 +01:00
|
|
|
html = redcloth.to_html(:textile)
|
2007-05-26 03:52:27 +02:00
|
|
|
sanitize_xhtml(html)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Markdown < AbstractEngine
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'sanitize'
|
2007-02-22 08:06:53 +01:00
|
|
|
include Sanitize
|
2007-01-22 14:43:50 +01:00
|
|
|
def mask
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'maruku'
|
|
|
|
require 'maruku/ext/math'
|
2007-06-06 00:13:44 +02:00
|
|
|
html = sanitize_rexml(Maruku.new(@content.delete("\r\x01-\x08\x0B\x0C\x0E-\x1F"),
|
|
|
|
{:math_enabled => false}).to_html_tree)
|
2007-06-14 06:05:15 +02:00
|
|
|
html.gsub(/\A<div class="maruku_wrapper_div">\n?(.*?)\n?<\/div>\Z/m, '\1')
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 15:36:51 +01:00
|
|
|
class MarkdownMML < AbstractEngine
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'sanitize'
|
2007-02-22 08:06:53 +01:00
|
|
|
include Sanitize
|
2007-01-22 15:36:51 +01:00
|
|
|
def mask
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'maruku'
|
|
|
|
require 'maruku/ext/math'
|
2007-06-06 00:13:44 +02:00
|
|
|
html = sanitize_rexml(Maruku.new(@content.delete("\r\x01-\x08\x0B\x0C\x0E-\x1F"),
|
|
|
|
{:math_enabled => true, :math_numbered => ['\\[','\\begin{equation}']}).to_html_tree)
|
2007-06-14 06:05:15 +02:00
|
|
|
html.gsub(/\A<div class="maruku_wrapper_div">\n?(.*?)\n?<\/div>\Z/m, '\1')
|
2007-01-22 15:36:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
class Mixed < AbstractEngine
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'sanitize'
|
2007-02-22 08:06:53 +01:00
|
|
|
include Sanitize
|
2007-01-22 14:43:50 +01:00
|
|
|
def mask
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'redcloth'
|
2007-01-22 14:43:50 +01:00
|
|
|
redcloth = RedCloth.new(@content, @content.options[:engine_opts])
|
|
|
|
redcloth.filter_html = false
|
|
|
|
redcloth.no_span_caps = false
|
2007-02-20 15:18:48 +01:00
|
|
|
html = redcloth.to_html
|
2007-05-26 03:52:27 +02:00
|
|
|
sanitize_xhtml(html)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RDoc < AbstractEngine
|
2007-06-13 08:56:44 +02:00
|
|
|
require 'sanitize'
|
2007-02-22 08:06:53 +01:00
|
|
|
include Sanitize
|
2007-01-22 14:43:50 +01:00
|
|
|
def mask
|
|
|
|
require_dependency 'rdocsupport'
|
2007-02-20 15:18:48 +01:00
|
|
|
html = RDocSupport::RDocFormatter.new(@content).to_html
|
2007-05-26 03:52:27 +02:00
|
|
|
sanitize_xhtml(html)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 15:36:51 +01:00
|
|
|
MAP = { :textile => Textile, :markdown => Markdown, :markdownMML => MarkdownMML, :mixed => Mixed, :rdoc => RDoc }
|
2007-01-22 14:43:50 +01:00
|
|
|
MAP.default = Textile
|
|
|
|
end
|