instiki/lib/chunks/engines.rb

137 lines
5 KiB
Ruby
Raw Normal View History

2007-01-22 14:43:50 +01:00
$: << File.dirname(__FILE__) + "../../lib"
require_dependency 'chunks/chunk'
require 'instiki_stringsupport'
require 'maruku'
require 'maruku/ext/math'
require_dependency 'rdocsupport'
require 'redcloth'
require 'oldredcloth'
2007-01-22 14:43:50 +01:00
# 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
def mask
@content.as_utf8
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
html = redcloth.to_html(:textile)
2007-01-22 14:43:50 +01:00
end
end
class Markdown < AbstractEngine
def mask
2011-08-08 08:54:06 +02:00
text = @content.as_utf8.to_str.delete("\r").to_utf8
# If the request is for S5, call Maruku accordingly (without math)
if @content.options[:mode] == :s5
2011-08-08 08:54:06 +02:00
my_content = Maruku.new(text,
{:math_enabled => false, :content_only => true,
:author => @content.options[:engine_opts][:author],
:title => @content.options[:engine_opts][:title]})
@content.options[:renderer].s5_theme = my_content.s5_theme
else
2011-08-08 08:54:06 +02:00
html = Maruku.new(text, {:math_enabled => false}).to_html
html.gsub(/\A<div class="maruku_wrapper_div">\n?(.*?)\n?<\/div>\Z/m, '\1')
end
2007-01-22 14:43:50 +01:00
end
end
2007-01-22 15:36:51 +01:00
class MarkdownMML < AbstractEngine
def mask
2011-08-08 08:54:06 +02:00
text = @content.as_utf8.to_str.delete("\r").to_utf8
# If the request is for S5, call Maruku accordingly
if @content.options[:mode] == :s5
2011-08-08 08:54:06 +02:00
my_content = Maruku.new(text,
{:math_enabled => true,
:math_numbered => ['\\[','\\begin{equation}'],
:content_only => true,
:author => @content.options[:engine_opts][:author],
:title => @content.options[:engine_opts][:title]})
@content.options[:renderer].s5_theme = my_content.s5_theme
my_content.to_s5
else
2011-08-08 08:54:06 +02:00
(t = Time.now; nil)
puts "text is #{text.class}"
puts "text responds to concat_with_safety" if text.respond_to?(:concat_with_safety)
html = Maruku.new(text,
{:math_enabled => true,
:math_numbered => ['\\[','\\begin{equation}']}).to_html
(ApplicationController.logger.info("Maruku took " + (Time.now-t).to_s + " seconds."); nil)
html.gsub(/\A<div class="maruku_wrapper_div">\n?(.*?)\n?<\/div>\Z/m, '\1')
end
2007-01-22 15:36:51 +01:00
end
end
class MarkdownPNG < AbstractEngine
def mask
2011-08-08 08:54:06 +02:00
text = @content.as_utf8.to_str.delete("\r").to_utf8
# If the request is for S5, call Maruku accordingly
if @content.options[:mode] == :s5
2011-08-08 08:54:06 +02:00
my_content = Maruku.new(text,
{:math_enabled => true,
:math_numbered => ['\\[','\\begin{equation}'],
:html_math_output_mathml => false,
:html_math_output_png => true,
:html_png_engine => 'blahtex',
:html_png_dir => @content.web.files_path.join('pngs').to_s,
:html_png_url => @content.options[:png_url],
:content_only => true,
:author => @content.options[:engine_opts][:author],
:title => @content.options[:engine_opts][:title]})
@content.options[:renderer].s5_theme = my_content.s5_theme
my_content.to_s5
else
2011-08-08 08:54:06 +02:00
html = Maruku.new(text,
{:math_enabled => true,
:math_numbered => ['\\[','\\begin{equation}'],
:html_math_output_mathml => false,
:html_math_output_png => true,
:html_png_engine => 'blahtex',
:html_png_dir => @content.web.files_path.join('pngs').to_s,
:html_png_url => @content.options[:png_url]}).to_html
html.gsub(/\A<div class="maruku_wrapper_div">\n?(.*?)\n?<\/div>\Z/m, '\1')
end
end
end
2007-01-22 14:43:50 +01:00
class Mixed < AbstractEngine
def mask
@content.as_utf8
redcloth = OldRedCloth.new(@content.to_str, @content.options[:engine_opts])
2007-01-22 14:43:50 +01:00
redcloth.filter_html = false
redcloth.no_span_caps = false
html = redcloth.to_html
2007-01-22 14:43:50 +01:00
end
end
class RDoc < AbstractEngine
def mask
html = RDocSupport::RDocFormatter.new(@content.as_utf8.to_str).to_html
2007-01-22 14:43:50 +01:00
end
end
MAP = { :textile => Textile, :markdown => Markdown, :markdownMML => MarkdownMML, :markdownPNG => MarkdownPNG, :mixed => Mixed, :rdoc => RDoc }
MAP.default = MarkdownMML
2007-01-22 14:43:50 +01:00
end