Rehashed URL generation once again - templates should not use web.make_link anymore, there is link_for_page helper instead

This commit is contained in:
Alexey Verkhovsky 2005-02-13 18:53:49 +00:00
parent fd1d0ccc1e
commit 9c04ed3461
10 changed files with 62 additions and 51 deletions

View file

@ -33,4 +33,11 @@ module ApplicationHelper
html_options.join("\n") html_options.join("\n")
end end
def link_to_page(page_name, web = @web, text = nil, options = {})
raise 'Web not defined' if web.nil?
home_page_url = url_for :web => web.address, :action => 'show', :id => 'HomePage', :only_path => true
base_url = home_page_url.sub(%r-/show/HomePage/?$-, '')
web.make_link(page_name, text, options.merge(:base_url => base_url))
end
end end

View file

@ -15,7 +15,7 @@ module WikiChunk
def initialize(*args) def initialize(*args)
super super
@link_type = 'show' @link_type = :show
end end
def self.apply_to(content) def self.apply_to(content)
@ -124,7 +124,7 @@ module WikiChunk
link_type_match = LINK_TYPE_SEPARATION.match(@page_name) link_type_match = LINK_TYPE_SEPARATION.match(@page_name)
if link_type_match if link_type_match
@link_text = @page_name = link_type_match[1] @link_text = @page_name = link_type_match[1]
@link_type = link_type_match[2..3].compact[0] @link_type = link_type_match[2..3].compact[0].to_sym
end end
end end

View file

@ -2,6 +2,8 @@
module PageLock module PageLock
LOCKING_PERIOD = 30 * 60 # 30 minutes LOCKING_PERIOD = 30 * 60 # 30 minutes
attr_reader :locked_by
def lock(time, locked_by) def lock(time, locked_by)
@locked_at, @locked_by = time, locked_by @locked_at, @locked_by = time, locked_by
end end
@ -18,7 +20,4 @@ module PageLock
@locked_at + LOCKING_PERIOD > comparison_time unless @locked_at.nil? @locked_at + LOCKING_PERIOD > comparison_time unless @locked_at.nil?
end end
def locked_by_link
web.make_link(@locked_by)
end
end end

View file

@ -46,20 +46,20 @@ class Web
wiki.file_yard(self).has_file?(name) wiki.file_yard(self).has_file?(name)
end end
def make_file_link(mode, name, text) def make_file_link(mode, name, text, base_url)
link = CGI.escape(name) link = CGI.escape(name)
case mode case mode
when :export when :export
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>" if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
else "<span class=\"newWikiWord\">#{text}</span>" end else "<span class=\"newWikiWord\">#{text}</span>" end
when :publish when :publish
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"../published/#{link}\">#{text}</a>" if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{base_url}/published/#{link}\">#{text}</a>"
else "<span class=\"newWikiWord\">#{text}</span>" end else "<span class=\"newWikiWord\">#{text}</span>" end
else else
if has_file?(name) if has_file?(name)
"<a class=\"existingWikiWord\" href=\"../file/#{link}\">#{text}</a>" "<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
else else
"<span class=\"newWikiWord\">#{text}<a href=\"../file/#{link}\">?</a></span>" "<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
end end
end end
end end
@ -67,43 +67,46 @@ class Web
# Create a link for the given page name and link text based # Create a link for the given page name and link text based
# on the render mode in options and whether the page exists # on the render mode in options and whether the page exists
# in the this web. # in the this web.
# The links a relative, and will work only if displayed on another WikiPage.
# It should not be used in menus, templates and such - instead, use link_to_page helper
def make_link(name, text = nil, options = {}) def make_link(name, text = nil, options = {})
text = CGI.escapeHTML(text || WikiWords.separate(name)) text = CGI.escapeHTML(text || WikiWords.separate(name))
mode = options[:mode] mode = options[:mode] || :show
link_type = options[:link_type] || 'show' base_url = options[:base_url] || '..'
case link_type link_type = options[:link_type] || :show
when 'show' case link_type.to_sym
make_page_link(mode, name, text) when :show
when 'file' make_page_link(mode, name, text, base_url)
make_file_link(mode, name, text) when :file
when 'pic' make_file_link(mode, name, text, base_url)
make_pic_link(mode, name, text) when :pic
make_pic_link(mode, name, text, base_url)
else else
raise "Unknown link type: #{link_type}" raise "Unknown link type: #{link_type}"
end end
end end
def make_page_link(mode, name, text) def make_page_link(mode, name, text, base_url)
link = CGI.escape(name) link = CGI.escape(name)
case mode case mode.to_sym
when :export when :export
if has_page?(name) then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>} if has_page?(name) then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>}
else %{<span class="newWikiWord">#{text}</span>} end else %{<span class="newWikiWord">#{text}</span>} end
when :publish when :publish
if has_page?(name) then %{<a class="existingWikiWord" href="../published/#{link}">#{text}</a>} if has_page?(name) then %{<a class="existingWikiWord" href="#{base_url}/published/#{link}">#{text}</a>}
else %{<span class="newWikiWord">#{text}</span>} end else %{<span class="newWikiWord">#{text}</span>} end
else else
if has_page?(name) if has_page?(name)
%{<a class="existingWikiWord" href="../show/#{link}">#{text}</a>} %{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
else else
%{<span class="newWikiWord">#{text}<a href="../show/#{link}">?</a></span>} %{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
end end
end end
end end
def make_pic_link(mode, name, text) def make_pic_link(mode, name, text, base_url)
link = CGI.escape(name) link = CGI.escape(name)
case mode case mode.to_sym
when :export when :export
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />} if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
else %{<img alt="#{text}" src="no image" />} end else %{<img alt="#{text}" src="no image" />} end
@ -111,8 +114,8 @@ class Web
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />} if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
else %{<span class="newWikiWord">#{text}</span>} end else %{<span class="newWikiWord">#{text}</span>} end
else else
if has_file?(name) then %{<img alt="#{text}" src="../pic/#{link}" />} if has_file?(name) then %{<img alt="#{text}" src="#{base_url}/pic/#{link}" />}
else %{<span class="newWikiWord">#{text}<a href="../pic/#{link}">?</a></span>} end else %{<span class="newWikiWord">#{text}<a href="#{base_url}/pic/#{link}">?</a></span>} end
end end
end end

View file

@ -31,7 +31,7 @@ require 'chunks/nowiki'
# engine. By default these are: # engine. By default these are:
# Literal::Pre, Literal::Tags # Literal::Pre, Literal::Tags
# * :mode # * :mode
# => How should the content be rendered? For normal display (:display), # => How should the content be rendered? For normal display (show),
# publishing (:publish) or export (:export)? # publishing (:publish) or export (:export)?
# #
# AUTHOR: Mark Reid <mark @ threewordslong . com> # AUTHOR: Mark Reid <mark @ threewordslong . com>
@ -47,7 +47,7 @@ class WikiContent < String
:post_engine_actions => POST_ENGINE_ACTIONS, :post_engine_actions => POST_ENGINE_ACTIONS,
:engine => Engines::Textile, :engine => Engines::Textile,
:engine_opts => [], :engine_opts => [],
:mode => [:display] :mode => :show
} }
attr_reader :web, :options, :rendered, :chunks attr_reader :web, :options, :rendered, :chunks

View file

@ -3,9 +3,9 @@
<ul id="authorList"> <ul id="authorList">
<% for author in @authors %> <% for author in @authors %>
<li> <li>
<%= @web.make_link(author) %> <%= link_to_page author %>
co- or authored: co- or authored:
<%= @web.select.pages_authored_by(author).collect { |page| page.link }.join ', ' %> <%= @web.select.pages_authored_by(author).collect { |page| link_to_page(page.name) }.join ', ' %>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View file

@ -1,10 +1,13 @@
<% @title = "#{@page.plain_name} is locked" %> <% @title = "#{@page.plain_name} is locked" %>
<% if @page.lock_duration(Time.now) == 0 %> <p>
<p><%= @page.locked_by_link %> just started editing this page.</p> <%= link_to_page(@page.locked_by) %>
<% else %> <% if @page.lock_duration(Time.now) == 0 %>
<p><%= @page.locked_by_link %> has been editing this page for <%= @page.lock_duration(Time.now) %> minutes.</p> just started editing this page.
<% end %> <% else %>
has been editing this page for <%= @page.lock_duration(Time.now) %> minutes.
<% end %>
</p>
<p> <p>
<%= link_to 'Edit the page anyway', <%= link_to 'Edit the page anyway',

View file

@ -18,7 +18,7 @@
<div class="byline"> <div class="byline">
<%= "Revision from #{@revision.pretty_created_at} by" %> <%= "Revision from #{@revision.pretty_created_at} by" %>
<%= @page.web.make_link(@revision.author) %> <%= link_to_page @revision.author %>
</div> </div>
<div class="navigation"> <div class="navigation">

View file

@ -4,15 +4,15 @@
<% for web in @webs %> <% for web in @webs %>
<li> <li>
<% if web.published %> <% if web.published %>
<%= web.make_link 'HomePage', web.name, :mode => :publish %> (read-only) / <%= link_to_page 'HomePage', web, web.name, :mode => 'publish' %>
<%= web.make_link 'HomePage', 'editable version', :mode => :edit %> (requires login) (read-only) /
<%= link_to_page 'HomePage', web, 'editable version', :mode => 'show' %> (requires login)
<% else %> <% else %>
<%= web.make_link 'HomePage', nil, :mode => :edit %> <%= link_to_page 'HomePage', web, web.name, :mode => 'show' %>
<% end %> <% end %>
<div class="byline" style="margin-bottom: 0px"> <div class="byline" style="margin-bottom: 0px">
<%= web.pages.length %> pages by <%= web.authors.length %> authors <%= web.pages.length %> pages by <%= web.authors.length %> authors
</div> </div>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View file

@ -44,26 +44,26 @@ class WikiTest < Test::Unit::TestCase
def test_file_types def test_file_types
# only link # only link
assert_link_parsed_as 'only text', 'only text', 'show', '[[only text]]' assert_link_parsed_as 'only text', 'only text', :show, '[[only text]]'
# link and text # link and text
assert_link_parsed_as 'page name', 'link text', 'show', '[[page name|link text]]' assert_link_parsed_as 'page name', 'link text', :show, '[[page name|link text]]'
# link and type (file) # link and type (file)
assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', 'file', '[[foo.tar.gz:file]]' assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', :file, '[[foo.tar.gz:file]]'
# link and type (pic) # link and type (pic)
assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', 'pic', '[[foo.tar.gz:pic]]' assert_link_parsed_as 'foo.tar.gz', 'foo.tar.gz', :pic, '[[foo.tar.gz:pic]]'
# link, text and type # link, text and type
assert_link_parsed_as 'foo.tar.gz', 'FooTar', 'file', '[[foo.tar.gz|FooTar:file]]' assert_link_parsed_as 'foo.tar.gz', 'FooTar', :file, '[[foo.tar.gz|FooTar:file]]'
# NEGATIVE TEST CASES # NEGATIVE TEST CASES
# empty page name # empty page name
assert_link_parsed_as '|link text?', '|link text?', 'file', '[[|link text?:file]]' assert_link_parsed_as '|link text?', '|link text?', :file, '[[|link text?:file]]'
# empty link text # empty link text
assert_link_parsed_as 'page name?|', 'page name?|', 'file', '[[page name?|:file]]' assert_link_parsed_as 'page name?|', 'page name?|', :file, '[[page name?|:file]]'
# empty link type # empty link type
assert_link_parsed_as 'page name', 'link?:', 'show', '[[page name|link?:]]' assert_link_parsed_as 'page name', 'link?:', :show, '[[page name|link?:]]'
# unknown link type # unknown link type
assert_link_parsed_as 'page name:create_system', 'page name:create_system', 'show', assert_link_parsed_as 'page name:create_system', 'page name:create_system', :show,
'[[page name:create_system]]' '[[page name:create_system]]'
end end
@ -78,4 +78,3 @@ class WikiTest < Test::Unit::TestCase
end end
end end