2007-01-22 14:43:50 +01:00
|
|
|
require 'chunks/wiki'
|
|
|
|
|
|
|
|
# Includes the contents of another page for rendering.
|
|
|
|
# The include command looks like this: "[[!include PageName]]".
|
|
|
|
# It is a WikiReference since it refers to another page (PageName)
|
|
|
|
# and the wiki content using this command must be notified
|
|
|
|
# of changes to that page.
|
|
|
|
# If the included page could not be found, a warning is displayed.
|
|
|
|
|
|
|
|
class Include < WikiChunk::WikiReference
|
|
|
|
|
2008-12-24 20:11:53 +01:00
|
|
|
INCLUDE_PATTERN = /\[\[!include\s+([^\]\s][^\]]*?)\s*\]\]/i
|
2007-01-22 14:43:50 +01:00
|
|
|
def self.pattern() INCLUDE_PATTERN end
|
|
|
|
|
|
|
|
def initialize(match_data, content)
|
|
|
|
super
|
|
|
|
@page_name = match_data[1].strip
|
|
|
|
rendering_mode = content.options[:mode] || :show
|
2008-12-24 18:09:05 +01:00
|
|
|
add_to_include_list
|
2007-01-22 14:43:50 +01:00
|
|
|
@unmask_text = get_unmask_text_avoiding_recursion_loops(rendering_mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_unmask_text_avoiding_recursion_loops(rendering_mode)
|
2008-12-24 18:09:05 +01:00
|
|
|
if refpage
|
|
|
|
return "<em>Recursive include detected: #{@content.page_name} " +
|
|
|
|
"→ #{@content.page_name}</em>\n" if self_inclusion(refpage)
|
2007-01-22 14:43:50 +01:00
|
|
|
renderer = PageRenderer.new(refpage.current_revision)
|
2008-12-21 09:47:45 +01:00
|
|
|
included_content =
|
|
|
|
case rendering_mode
|
2007-01-22 14:43:50 +01:00
|
|
|
when :show then renderer.display_content
|
|
|
|
when :publish then renderer.display_published
|
|
|
|
when :export then renderer.display_content_for_export
|
2009-01-16 05:42:09 +01:00
|
|
|
when :s5 then renderer.display_s5
|
2008-12-21 09:47:45 +01:00
|
|
|
else
|
|
|
|
raise "Unsupported rendering mode #{@mode.inspect}"
|
|
|
|
end
|
|
|
|
@content.merge_chunks(included_content)
|
2008-12-24 18:09:05 +01:00
|
|
|
clear_include_list
|
2008-12-21 09:47:45 +01:00
|
|
|
return included_content.pre_rendered
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
2008-12-24 18:09:05 +01:00
|
|
|
clear_include_list
|
2007-01-22 14:43:50 +01:00
|
|
|
return "<em>Could not include #{@page_name}</em>\n"
|
|
|
|
end
|
|
|
|
end
|
2008-12-24 18:09:05 +01:00
|
|
|
|
|
|
|
# We track included pages in a thread-local variable.
|
2008-12-26 00:41:35 +01:00
|
|
|
# This allows a multi-threaded Rails to handle multiple
|
|
|
|
# simultaneous requests (one request/thread), without
|
|
|
|
# getting confused.
|
2008-12-24 18:09:05 +01:00
|
|
|
|
|
|
|
def add_to_include_list
|
2008-12-26 00:41:35 +01:00
|
|
|
Thread.current[:chunk_included_by] ?
|
|
|
|
Thread.current[:chunk_included_by].push(@content.page_name) :
|
|
|
|
Thread.current[:chunk_included_by] = [@content.page_name]
|
2008-12-24 18:09:05 +01:00
|
|
|
end
|
2008-12-26 00:41:35 +01:00
|
|
|
|
|
|
|
def clear_include_list
|
|
|
|
Thread.current[:chunk_included_by] = []
|
|
|
|
end
|
|
|
|
|
2008-12-24 18:09:05 +01:00
|
|
|
def self_inclusion(refpage)
|
2008-12-26 00:41:35 +01:00
|
|
|
if Thread.current[:chunk_included_by].include?(refpage.page.name)
|
2008-12-24 18:09:05 +01:00
|
|
|
@content.delete_chunk(self)
|
|
|
|
clear_include_list
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
end
|