2007-01-22 14:43:50 +01:00
|
|
|
require 'cgi'
|
2007-06-14 06:05:15 +02:00
|
|
|
require 'chunks/engines'
|
|
|
|
require 'chunks/category'
|
2007-01-22 14:43:50 +01:00
|
|
|
require_dependency 'chunks/include'
|
2009-06-03 05:17:15 +02:00
|
|
|
require_dependency 'chunks/redirect'
|
2007-01-22 14:43:50 +01:00
|
|
|
require_dependency 'chunks/wiki'
|
|
|
|
require_dependency 'chunks/literal'
|
2007-06-14 06:05:15 +02:00
|
|
|
require 'chunks/nowiki'
|
2008-05-22 16:36:23 +02:00
|
|
|
require 'sanitizer'
|
2010-06-09 18:47:39 +02:00
|
|
|
require 'instiki_stringsupport'
|
2008-05-22 16:36:23 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# Wiki content is just a string that can process itself with a chain of
|
|
|
|
# actions. The actions can modify wiki content so that certain parts of
|
|
|
|
# it are protected from being rendered by later actions.
|
|
|
|
#
|
2007-10-06 23:04:11 +02:00
|
|
|
# When wiki content is rendered, it can be interrogated to find out
|
|
|
|
# which chunks were rendered. This means things like categories, wiki
|
2007-01-22 14:43:50 +01:00
|
|
|
# links, can be determined.
|
|
|
|
#
|
2007-10-06 23:04:11 +02:00
|
|
|
# Exactly how wiki content is rendered is determined by a number of
|
2007-01-22 14:43:50 +01:00
|
|
|
# settings that are optionally passed in to a constructor. The current
|
|
|
|
# options are:
|
2007-10-06 23:04:11 +02:00
|
|
|
# * :engine
|
2007-01-22 14:43:50 +01:00
|
|
|
# => The structural markup engine to use (Textile, Markdown, RDoc)
|
|
|
|
# * :engine_opts
|
|
|
|
# => A list of options to pass to the markup engines (safe modes, etc)
|
|
|
|
# * :pre_engine_actions
|
|
|
|
# => A list of render actions or chunks to be processed before the
|
2007-10-06 23:04:11 +02:00
|
|
|
# markup engine is applied. By default this is:
|
|
|
|
# Category, Include, URIChunk, WikiChunk::Link, WikiChunk::Word
|
2007-01-22 14:43:50 +01:00
|
|
|
# * :post_engine_actions
|
2007-10-06 23:04:11 +02:00
|
|
|
# => A list of render actions or chunks to apply after the markup
|
|
|
|
# engine. By default these are:
|
2007-01-22 14:43:50 +01:00
|
|
|
# Literal::Pre, Literal::Tags
|
|
|
|
# * :mode
|
2007-10-06 23:04:11 +02:00
|
|
|
# => How should the content be rendered? For normal display (show),
|
2007-01-22 14:43:50 +01:00
|
|
|
# publishing (:publish) or export (:export)?
|
|
|
|
|
|
|
|
module ChunkManager
|
|
|
|
attr_reader :chunks_by_type, :chunks_by_id, :chunks, :chunk_id
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2009-06-03 05:17:15 +02:00
|
|
|
ACTIVE_CHUNKS = [ NoWiki, Category, Redirect, WikiChunk::Link,
|
2007-10-06 23:04:11 +02:00
|
|
|
WikiChunk::Word ]
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-01-14 23:11:07 +01:00
|
|
|
HIDE_CHUNKS = [ Literal::Pre, Literal::Tags, Literal::Math ]
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-10-06 23:04:11 +02:00
|
|
|
MASK_RE = {
|
2007-01-22 14:43:50 +01:00
|
|
|
ACTIVE_CHUNKS => Chunk::Abstract.mask_re(ACTIVE_CHUNKS),
|
|
|
|
HIDE_CHUNKS => Chunk::Abstract.mask_re(HIDE_CHUNKS)
|
|
|
|
}
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def init_chunk_manager
|
|
|
|
@chunks_by_type = Hash.new
|
2007-10-06 23:04:11 +02:00
|
|
|
Chunk::Abstract::derivatives.each{|chunk_type|
|
|
|
|
@chunks_by_type[chunk_type] = Array.new
|
2007-01-22 14:43:50 +01:00
|
|
|
}
|
|
|
|
@chunks_by_id = Hash.new
|
|
|
|
@chunks = []
|
|
|
|
@chunk_id = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_chunk(c)
|
|
|
|
@chunks_by_type[c.class] << c
|
|
|
|
@chunks_by_id[c.object_id] = c
|
|
|
|
@chunks << c
|
|
|
|
@chunk_id += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_chunk(c)
|
|
|
|
@chunks_by_type[c.class].delete(c)
|
|
|
|
@chunks_by_id.delete(c.object_id)
|
|
|
|
@chunks.delete(c)
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_chunks(other)
|
|
|
|
other.chunks.each{|c| add_chunk(c)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def scan_chunkid(text)
|
|
|
|
text.scan(MASK_RE[ACTIVE_CHUNKS]){|a| yield a[0] }
|
|
|
|
end
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def find_chunks(chunk_type)
|
|
|
|
@chunks.select { |chunk| chunk.kind_of?(chunk_type) and chunk.rendered? }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-10-06 23:04:11 +02:00
|
|
|
# A simplified version of WikiContent. Useful to avoid recursion problems in
|
2007-01-22 14:43:50 +01:00
|
|
|
# WikiContent.new
|
|
|
|
class WikiContentStub < String
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2008-07-26 11:14:41 +02:00
|
|
|
attr_reader :web, :options
|
2007-01-22 14:43:50 +01:00
|
|
|
include ChunkManager
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2008-07-26 11:14:41 +02:00
|
|
|
def initialize(content, web, options)
|
2007-01-22 14:43:50 +01:00
|
|
|
super(content)
|
2008-07-26 11:14:41 +02:00
|
|
|
@web = web
|
2007-01-22 14:43:50 +01:00
|
|
|
@options = options
|
|
|
|
init_chunk_manager
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detects the mask strings contained in the text of chunks of type chunk_types
|
|
|
|
# and yields the corresponding chunk ids
|
2007-10-06 23:04:11 +02:00
|
|
|
# example: content = "chunk123categorychunk <pre>chunk456categorychunk</pre>"
|
2007-01-22 14:43:50 +01:00
|
|
|
# inside_chunks(Literal::Pre) ==> yield 456
|
|
|
|
def inside_chunks(chunk_types)
|
|
|
|
chunk_types.each{|chunk_type| chunk_type.apply_to(self) }
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
chunk_types.each{|chunk_type| @chunks_by_type[chunk_type].each{|hide_chunk|
|
|
|
|
scan_chunkid(hide_chunk.text){|id| yield id }
|
|
|
|
}
|
2007-10-06 23:04:11 +02:00
|
|
|
}
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-25 19:45:45 +02:00
|
|
|
class WikiContent < ActiveSupport::SafeBuffer
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
include ChunkManager
|
2008-05-21 09:06:31 +02:00
|
|
|
include Sanitizer
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
DEFAULT_OPTS = {
|
|
|
|
:active_chunks => ACTIVE_CHUNKS,
|
2009-01-16 19:51:43 +01:00
|
|
|
:hide_chunks => HIDE_CHUNKS,
|
2007-09-14 17:43:03 +02:00
|
|
|
:engine => Engines::MarkdownMML,
|
2007-01-22 14:43:50 +01:00
|
|
|
:engine_opts => [],
|
|
|
|
:mode => :show
|
|
|
|
}.freeze
|
|
|
|
|
2010-05-27 07:27:49 +02:00
|
|
|
attr_reader :web, :options, :revision, :not_rendered, :pre_rendered, :url_generator
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# Create a new wiki content string from the given one.
|
|
|
|
# The options are explained at the top of this file.
|
|
|
|
def initialize(revision, url_generator, options = {})
|
|
|
|
@revision = revision
|
|
|
|
@url_generator = url_generator
|
|
|
|
@web = @revision.page.web
|
|
|
|
|
|
|
|
@options = DEFAULT_OPTS.dup.merge(options)
|
2007-10-06 23:04:11 +02:00
|
|
|
@options[:engine] = Engines::MAP[@web.markup]
|
2007-01-22 14:43:50 +01:00
|
|
|
@options[:engine_opts] = [:filter_html, :filter_styles] if @web.safe_mode?
|
|
|
|
@options[:active_chunks] = (ACTIVE_CHUNKS - [WikiChunk::Word] ) if @web.brackets_only?
|
2009-01-16 19:51:43 +01:00
|
|
|
@options[:hide_chunks] = (HIDE_CHUNKS - [Literal::Math] ) unless
|
|
|
|
[Engines::MarkdownMML, Engines::MarkdownPNG].include?(@options[:engine])
|
2010-01-26 00:55:31 +01:00
|
|
|
if @options[:engine] == Engines::MarkdownPNG
|
|
|
|
@options[:png_url] =
|
|
|
|
@options[:mode] == :export ? 'files/pngs/' :
|
2010-01-26 05:01:10 +01:00
|
|
|
(@url_generator.url_for :controller => 'file', :web => @web.address,
|
|
|
|
:action => 'file', :id => 'pngs', :only_path => true) + '/'
|
2010-01-26 00:55:31 +01:00
|
|
|
end
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
@not_rendered = @pre_rendered = nil
|
2007-10-06 23:04:11 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
super(@revision.content)
|
|
|
|
init_chunk_manager
|
|
|
|
build_chunks
|
|
|
|
@not_rendered = String.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Call @web.page_link using current options.
|
2007-10-06 23:04:11 +02:00
|
|
|
def page_link(web_name, name, text, link_type)
|
|
|
|
web = Web.find_by_name(web_name) || Web.find_by_address(web_name) || @web
|
2007-01-22 14:43:50 +01:00
|
|
|
@options[:link_type] = (link_type || :show)
|
2009-09-04 06:09:10 +02:00
|
|
|
@url_generator.make_link(@web, name, web, text, @options)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_chunks
|
|
|
|
# create and mask Includes and "active_chunks" chunks
|
2008-12-21 06:24:50 +01:00
|
|
|
NoWiki.apply_to(self) if @options[:active_chunks].include?(NoWiki)
|
2007-01-22 14:43:50 +01:00
|
|
|
Include.apply_to(self)
|
2008-12-21 06:24:50 +01:00
|
|
|
@options[:active_chunks].each{|chunk_type| chunk_type.apply_to(self) unless chunk_type == NoWiki}
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# Handle hiding contexts like "pre" and "code" etc..
|
|
|
|
# The markup (textile, rdoc etc) can produce such contexts with its own syntax.
|
|
|
|
# To reveal them, we work on a copy of the content.
|
2007-10-06 23:04:11 +02:00
|
|
|
# The copy is rendered and used to detect the chunks that are inside protecting context
|
2007-01-22 14:43:50 +01:00
|
|
|
# These chunks are reverted on the original content string.
|
|
|
|
|
2008-07-26 11:14:41 +02:00
|
|
|
copy = WikiContentStub.new(self, @web, @options)
|
2007-01-22 14:43:50 +01:00
|
|
|
@options[:engine].apply_to(copy)
|
|
|
|
|
2009-01-16 19:51:43 +01:00
|
|
|
copy.inside_chunks(@options[:hide_chunks]) do |id|
|
2010-07-22 21:08:21 +02:00
|
|
|
@chunks_by_id[id.to_i].revert if @chunks_by_id[id.to_i]
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 06:58:04 +02:00
|
|
|
def delete_chunks!(types)
|
|
|
|
types.each do |t|
|
|
|
|
@chunks_by_type[t].each do |c|
|
|
|
|
@pre_rendered.sub!(c.mask, '') if @pre_rendered
|
|
|
|
@chunks.delete(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def pre_render!
|
2007-10-06 23:04:11 +02:00
|
|
|
unless @pre_rendered
|
2007-01-22 14:43:50 +01:00
|
|
|
@chunks_by_type[Include].each{|chunk| chunk.unmask }
|
2009-01-16 05:42:09 +01:00
|
|
|
@pre_rendered = String.new(self)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-10-06 23:04:11 +02:00
|
|
|
@pre_rendered
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def render!
|
|
|
|
pre_render!
|
|
|
|
@options[:engine].apply_to(self)
|
2009-12-08 16:08:25 +01:00
|
|
|
as_utf8
|
2007-01-22 14:43:50 +01:00
|
|
|
# unmask in one go. $~[1] is the chunk id
|
2011-06-15 07:43:38 +02:00
|
|
|
text = self.to_str
|
|
|
|
text.gsub!(MASK_RE[ACTIVE_CHUNKS]) do
|
2007-01-22 14:43:50 +01:00
|
|
|
chunk = @chunks_by_id[$~[1].to_i]
|
2007-10-06 23:04:11 +02:00
|
|
|
if chunk.nil?
|
2007-01-22 14:43:50 +01:00
|
|
|
# if we match a chunkmask that existed in the original content string
|
|
|
|
# just keep it as it is
|
|
|
|
$~[0]
|
2007-10-06 23:04:11 +02:00
|
|
|
else
|
|
|
|
chunk.unmask_text
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-15 07:43:38 +02:00
|
|
|
self.replace xhtml_sanitize(text)
|
2010-05-25 19:45:45 +02:00
|
|
|
self.html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def page_name
|
|
|
|
@revision.page.name
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|