class AbstractUrlGenerator
def initialize(controller)
raise 'Controller cannot be nil' if controller.nil?
@controller = controller
end
# Create a link for the given page (or file) name and link text based
# on the render mode in options and whether the page (file) exists
# in the web.
def make_link(name, web, text = nil, options = {})
text = CGI.escapeHTML(text || WikiWords.separate(name))
mode = (options[:mode] || :show).to_sym
link_type = (options[:link_type] || :show).to_sym
if (link_type == :show)
known_page = web.has_page?(name)
else
known_page = web.has_file?(name)
end
case link_type
when :show
page_link(mode, name, text, web.address, known_page)
when :file
file_link(mode, name, text, web.address, known_page)
when :pic
pic_link(mode, name, text, web.address, known_page)
else
raise "Unknown link type: #{link_type}"
end
end
end
class UrlGenerator < AbstractUrlGenerator
private
def file_link(mode, name, text, web_address, known_file)
case mode
when :export
if known_file
%{#{text}}
else
%{#{text}}
end
when :publish
if known_file
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'published',
:id => name
%{#{text}}
else
%{#{text}}
end
else
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'file',
:id => name
if known_file
%{#{text}}
else
%{#{text}?}
end
end
end
def page_link(mode, name, text, web_address, known_page)
case mode
when :export
if known_page
%{#{text}}
else
%{#{text}}
end
when :publish
if known_page
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'published',
:id => name
%{#{text}}
else
%{#{text}}
end
else
if known_page
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'show',
:id => name
%{#{text}}
else
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'new',
:id => name
%{#{text}?}
end
end
end
def pic_link(mode, name, text, web_address, known_pic)
case mode
when :export
if known_pic
%{}
else
%{}
end
when :publish
if known_pic
%{}
else
%{#{text}}
end
else
href = @controller.url_for @controller => 'file', :web => web_address, :action => 'pic',
:id => name
if known_pic
%{}
else
%{#{text}?}
end
end
end
end