2009-01-10 09:00:26 +01:00
|
|
|
require 'stringsupport'
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
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.
|
2009-09-04 06:09:10 +02:00
|
|
|
def make_link(current_web, asked_name, web, text = nil, options = {})
|
|
|
|
@web = current_web
|
2007-01-22 14:43:50 +01:00
|
|
|
mode = (options[:mode] || :show).to_sym
|
|
|
|
link_type = (options[:link_type] || :show).to_sym
|
|
|
|
|
|
|
|
if (link_type == :show)
|
2009-06-03 05:17:15 +02:00
|
|
|
page_exists = web.has_page?(asked_name)
|
|
|
|
known_page = page_exists || web.has_redirect_for?(asked_name)
|
|
|
|
if known_page && !page_exists
|
2009-06-07 04:08:08 +02:00
|
|
|
name = web.page_that_redirects_for(asked_name).name
|
2009-06-03 05:17:15 +02:00
|
|
|
else
|
|
|
|
name = asked_name
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
2009-06-03 05:17:15 +02:00
|
|
|
name = asked_name
|
2007-01-22 14:43:50 +01:00
|
|
|
known_page = web.has_file?(name)
|
2007-10-09 09:51:38 +02:00
|
|
|
description = web.description(name)
|
2009-01-10 09:00:26 +01:00
|
|
|
description = description.unescapeHTML.escapeHTML if description
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-06-03 05:17:15 +02:00
|
|
|
if (text == asked_name)
|
2007-10-09 18:02:44 +02:00
|
|
|
text = description || text
|
|
|
|
else
|
|
|
|
text = text || description
|
|
|
|
end
|
2009-06-03 05:17:15 +02:00
|
|
|
text = (text || WikiWords.separate(asked_name)).unescapeHTML.escapeHTML
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
case link_type
|
|
|
|
when :show
|
|
|
|
page_link(mode, name, text, web.address, known_page)
|
|
|
|
when :file
|
2007-10-10 06:56:55 +02:00
|
|
|
file_link(mode, name, text, web.address, known_page, description)
|
2007-01-22 14:43:50 +01:00
|
|
|
when :pic
|
2007-10-09 18:02:44 +02:00
|
|
|
pic_link(mode, name, text, web.address, known_page)
|
2009-03-03 19:17:14 +01:00
|
|
|
when :audio
|
|
|
|
media_link(mode, name, text, web.address, known_page, 'audio')
|
2009-03-02 09:32:25 +01:00
|
|
|
when :video
|
2009-03-03 19:17:14 +01:00
|
|
|
media_link(mode, name, text, web.address, known_page, 'video')
|
2008-12-30 10:03:02 +01:00
|
|
|
when :delete
|
|
|
|
delete_link(mode, name, web.address, known_page)
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
raise "Unknown link type: #{link_type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class UrlGenerator < AbstractUrlGenerator
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2007-10-10 06:56:55 +02:00
|
|
|
def file_link(mode, name, text, web_address, known_file, description)
|
2007-01-22 14:43:50 +01:00
|
|
|
case mode
|
|
|
|
when :export
|
|
|
|
if known_file
|
2009-12-16 07:59:33 +01:00
|
|
|
%{<a class="existingWikiWord" title="#{description}" href="#{CGI.escape(name)}.#{html_ext}">#{text}</a>}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}</span>}
|
|
|
|
end
|
|
|
|
when :publish
|
|
|
|
if known_file
|
2009-07-28 05:49:12 +02:00
|
|
|
href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file',
|
|
|
|
:id => name, :only_path => true
|
2007-10-10 06:56:55 +02:00
|
|
|
%{<a class="existingWikiWord" title="#{description}" href="#{href}">#{text}</a>}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}</span>}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file',
|
2009-07-28 05:49:12 +02:00
|
|
|
:id => name, :only_path => true
|
2007-01-22 14:43:50 +01:00
|
|
|
if known_file
|
2007-10-10 06:56:55 +02:00
|
|
|
%{<a class="existingWikiWord" title="#{description}" href="#{href}">#{text}</a>}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}<a href="#{href}">?</a></span>}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def page_link(mode, name, text, web_address, known_page)
|
2009-06-13 02:47:56 +02:00
|
|
|
return %{<span class='wikilink-error'><b>Illegal link (target contains a '.'):</b> #{name}</span>} if name.include?('.')
|
2007-01-22 14:43:50 +01:00
|
|
|
case mode
|
|
|
|
when :export
|
|
|
|
if known_page
|
2009-12-16 07:59:33 +01:00
|
|
|
%{<a class="existingWikiWord" href="#{CGI.escape(name)}.#{html_ext}">#{text}</a>}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}</span>}
|
|
|
|
end
|
|
|
|
when :publish
|
|
|
|
if known_page
|
2009-10-23 00:57:13 +02:00
|
|
|
wikilink_for(mode, name, text, web_address)
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}</span>}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if known_page
|
2009-10-23 00:57:13 +02:00
|
|
|
wikilink_for(mode, name, text, web_address)
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'new',
|
2009-07-28 05:49:12 +02:00
|
|
|
:id => name, :only_path => true
|
2007-01-22 14:43:50 +01:00
|
|
|
%{<span class="newWikiWord">#{text}<a href="#{href}">?</a></span>}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-10-09 18:02:44 +02:00
|
|
|
def pic_link(mode, name, text, web_address, known_pic)
|
2008-12-20 20:56:50 +01:00
|
|
|
href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file',
|
2009-07-28 05:49:12 +02:00
|
|
|
:id => name, :only_path => true
|
2007-01-22 14:43:50 +01:00
|
|
|
case mode
|
|
|
|
when :export
|
|
|
|
if known_pic
|
2007-10-09 18:02:44 +02:00
|
|
|
%{<img alt="#{text}" src="#{CGI.escape(name)}" />}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<img alt="#{text}" src="no image" />}
|
|
|
|
end
|
|
|
|
when :publish
|
|
|
|
if known_pic
|
2008-12-20 20:56:50 +01:00
|
|
|
%{<img alt="#{text}" src="#{href}" />}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}</span>}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if known_pic
|
2007-10-09 18:02:44 +02:00
|
|
|
%{<img alt="#{text}" src="#{href}" />}
|
2007-01-22 14:43:50 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}<a href="#{href}">?</a></span>}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-03 19:17:14 +01:00
|
|
|
def media_link(mode, name, text, web_address, known_media, media_type)
|
2009-03-02 09:32:25 +01:00
|
|
|
href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file',
|
2009-07-28 05:49:12 +02:00
|
|
|
:id => name, :only_path => true
|
2009-03-02 09:32:25 +01:00
|
|
|
case mode
|
|
|
|
when :export
|
2009-03-03 19:17:14 +01:00
|
|
|
if known_media
|
|
|
|
%{<#{media_type} src="#{CGI.escape(name)}" controls="controls">#{text}</#{media_type}>}
|
2009-03-02 09:32:25 +01:00
|
|
|
else
|
|
|
|
text
|
|
|
|
end
|
|
|
|
when :publish
|
2009-03-03 19:17:14 +01:00
|
|
|
if known_media
|
|
|
|
%{<#{media_type} src="#{href}" controls="controls">#{text}</#{media_type}>}
|
2009-03-02 09:32:25 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}</span>}
|
|
|
|
end
|
|
|
|
else
|
2009-03-03 19:17:14 +01:00
|
|
|
if known_media
|
|
|
|
%{<#{media_type} src="#{href}" controls="controls">#{text}</#{media_type}>}
|
2009-03-02 09:32:25 +01:00
|
|
|
else
|
|
|
|
%{<span class="newWikiWord">#{text}<a href="#{href}">?</a></span>}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-30 10:03:02 +01:00
|
|
|
def delete_link(mode, name, web_address, known_file)
|
|
|
|
href = @controller.url_for :controller => 'file', :web => web_address,
|
2009-07-28 05:49:12 +02:00
|
|
|
:action => 'delete', :id => name, :only_oath => true
|
2008-12-30 10:03:02 +01:00
|
|
|
if mode == :show and known_file
|
|
|
|
%{<span class="deleteWikiWord"><a href="#{href}">Delete #{name}</a></span>}
|
|
|
|
else
|
|
|
|
%{<span class="deleteWikiWord">[[#{name}:delete]]</span>}
|
|
|
|
end
|
|
|
|
end
|
2009-10-23 00:57:13 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def wikilink_for(mode, name, text, web_address)
|
|
|
|
web = Web.find_by_address(web_address)
|
2009-10-28 06:03:25 +01:00
|
|
|
action = web.published? && (web != @web || [:publish, :s5].include?(mode) ) ? 'published' : 'show'
|
2009-10-23 00:57:13 +02:00
|
|
|
href = @controller.url_for :controller => 'wiki', :web => web_address, :action => action,
|
|
|
|
:id => name, :only_path => true
|
|
|
|
%{<a class="existingWikiWord" href="#{href}">#{text}</a>}
|
2009-12-16 07:59:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def html_ext
|
|
|
|
@html_ext ||= @controller.method(:html_ext).call
|
|
|
|
# Why method().call ? A Ruby 1.9.2preview1 bug:
|
|
|
|
# http://redmine.ruby-lang.org/issues/show/1802
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|