instiki/lib/wiki_content.rb

207 lines
5.9 KiB
Ruby
Raw Normal View History

2007-01-22 14:43:50 +01:00
require 'cgi'
require 'chunks/engines'
require 'chunks/category'
2007-01-22 14:43:50 +01:00
require_dependency 'chunks/include'
require_dependency 'chunks/wiki'
require_dependency 'chunks/literal'
require 'chunks/nowiki'
require 'sanitizer'
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.
#
# 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.
#
# 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:
# * :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
# 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
# => 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
# => 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
ACTIVE_CHUNKS = [ NoWiki, Category, WikiChunk::Link,
WikiChunk::Word ]
2007-01-22 14:43:50 +01:00
HIDE_CHUNKS = [ Literal::Pre, Literal::Tags ]
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-01-22 14:43:50 +01:00
def init_chunk_manager
@chunks_by_type = Hash.new
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-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
# A simplified version of WikiContent. Useful to avoid recursion problems in
2007-01-22 14:43:50 +01:00
# WikiContent.new
class WikiContentStub < String
attr_reader :web, :options
2007-01-22 14:43:50 +01:00
include ChunkManager
def initialize(content, web, options)
2007-01-22 14:43:50 +01:00
super(content)
@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
# 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-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-01-22 14:43:50 +01:00
end
end
class WikiContent < String
include ChunkManager
include Sanitizer
2007-01-22 14:43:50 +01:00
DEFAULT_OPTS = {
:active_chunks => ACTIVE_CHUNKS,
:engine => Engines::MarkdownMML,
2007-01-22 14:43:50 +01:00
:engine_opts => [],
:mode => :show
}.freeze
attr_reader :web, :options, :revision, :not_rendered, :pre_rendered
# 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)
@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?
2007-01-22 14:43:50 +01:00
@not_rendered = @pre_rendered = nil
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.
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)
@url_generator.make_link(name, web, text, @options)
2007-01-22 14:43:50 +01:00
end
def build_chunks
# create and mask Includes and "active_chunks" chunks
Include.apply_to(self)
@options[:active_chunks].each{|chunk_type| chunk_type.apply_to(self)}
# 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.
# 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.
copy = WikiContentStub.new(self, @web, @options)
2007-01-22 14:43:50 +01:00
@options[:engine].apply_to(copy)
copy.inside_chunks(HIDE_CHUNKS) do |id|
@chunks_by_id[id.to_i].revert
end
end
def pre_render!
unless @pre_rendered
2007-01-22 14:43:50 +01:00
@chunks_by_type[Include].each{|chunk| chunk.unmask }
@pre_rendered = String.new(self)
end
@pre_rendered
2007-01-22 14:43:50 +01:00
end
def render!
pre_render!
@options[:engine].apply_to(self)
# unmask in one go. $~[1] is the chunk id
gsub!(MASK_RE[ACTIVE_CHUNKS]) do
2007-01-22 14:43:50 +01:00
chunk = @chunks_by_id[$~[1].to_i]
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]
else
chunk.unmask_text
2007-01-22 14:43:50 +01:00
end
end
self.replace xhtml_sanitize(self)
2007-01-22 14:43:50 +01:00
end
def page_name
@revision.page.name
end
end