2007-01-22 14:43:50 +01:00
|
|
|
# The methods added to this helper will be available to all templates in the application.
|
|
|
|
module ApplicationHelper
|
2010-06-09 18:47:39 +02:00
|
|
|
require 'instiki_stringsupport'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container
|
|
|
|
# where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and
|
|
|
|
# the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values
|
|
|
|
# become lasts. If +selected+ is specified, the matching "last" or element will get the selected option-tag.
|
|
|
|
#
|
|
|
|
# Examples (call, result):
|
|
|
|
# html_options([["Dollar", "$"], ["Kroner", "DKK"]])
|
|
|
|
# <option value="$">Dollar</option>\n<option value="DKK">Kroner</option>
|
|
|
|
#
|
|
|
|
# html_options([ "VISA", "Mastercard" ], "Mastercard")
|
|
|
|
# <option>VISA</option>\n<option selected>Mastercard</option>
|
|
|
|
#
|
|
|
|
# html_options({ "Basic" => "$20", "Plus" => "$40" }, "$40")
|
|
|
|
# <option value="$20">Basic</option>\n<option value="$40" selected>Plus</option>
|
|
|
|
def html_options(container, selected = nil)
|
|
|
|
container = container.to_a if Hash === container
|
|
|
|
|
|
|
|
html_options = container.inject([]) do |options, element|
|
|
|
|
if element.is_a? Array
|
|
|
|
if element.last != selected
|
|
|
|
options << "<option value=\"#{element.last}\">#{element.first}</option>"
|
|
|
|
else
|
2007-01-22 15:36:51 +01:00
|
|
|
options << "<option value=\"#{element.last}\" selected=\"selected\">#{element.first}</option>"
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
options << ((element != selected) ? "<option>#{element}</option>" : "<option selected>#{element}</option>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-26 07:27:49 +02:00
|
|
|
html_options.join("\n").html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a hyperlink to a Wiki page, without checking if the page exists or not
|
|
|
|
def link_to_existing_page(page, text = nil, html_options = {})
|
|
|
|
link_to(
|
|
|
|
text || page.plain_name,
|
|
|
|
{:web => @web.address, :action => 'show', :id => page.name, :only_path => true},
|
2010-05-26 07:27:49 +02:00
|
|
|
html_options).html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a hyperlink to a Wiki page, or to a "new page" form if the page doesn't exist yet
|
|
|
|
def link_to_page(page_name, web = @web, text = nil, options = {})
|
|
|
|
raise 'Web not defined' if web.nil?
|
2009-09-04 06:09:10 +02:00
|
|
|
UrlGenerator.new(@controller).make_link(@web, page_name, web, text,
|
2010-05-26 07:27:49 +02:00
|
|
|
options.merge(:base_url => "#{base_url}/#{web.address}")).html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_link(page, options = {})
|
2010-05-26 07:27:49 +02:00
|
|
|
UrlGenerator.new(@controller).make_link(@web, page.author.name, page.web, nil, options).purify.html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-03-31 06:50:06 +02:00
|
|
|
# Create a hyperlink to a particular revision of a Wiki page
|
|
|
|
def link_to_revision(page, revision_number, text = nil, mode = nil, html_options = {})
|
2011-04-25 17:54:44 +02:00
|
|
|
revision_number == page.rev_ids.size ?
|
2009-03-31 22:54:41 +02:00
|
|
|
link_to(
|
|
|
|
text || page.plain_name,
|
|
|
|
{:web => @web.address, :action => 'show', :id => page.name,
|
2010-05-26 07:27:49 +02:00
|
|
|
:mode => mode}, html_options).html_safe :
|
2009-03-31 22:54:41 +02:00
|
|
|
link_to(
|
2010-05-26 07:27:49 +02:00
|
|
|
text || page.plain_name + "(rev # #{revision_number})".html_safe,
|
2009-03-31 06:50:06 +02:00
|
|
|
{:web => @web.address, :action => 'revision', :id => page.name,
|
2010-05-26 07:27:49 +02:00
|
|
|
:rev => revision_number, :mode => mode}, html_options).html_safe
|
2009-03-31 06:50:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create a hyperlink to the history of a particular Wiki page
|
|
|
|
def link_to_history(page, text = nil, html_options = {})
|
|
|
|
link_to(
|
2010-05-26 07:27:49 +02:00
|
|
|
text || page.plain_name + "(history)".html_safe,
|
2009-03-31 06:50:06 +02:00
|
|
|
{:web => @web.address, :action => 'history', :id => page.name},
|
2010-05-26 07:27:49 +02:00
|
|
|
html_options).html_safe
|
2009-03-31 06:50:06 +02:00
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def base_url
|
|
|
|
home_page_url = url_for :controller => 'admin', :action => 'create_system', :only_path => true
|
|
|
|
home_page_url.sub(%r-/create_system/?$-, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a menu of categories
|
|
|
|
def categories_menu
|
|
|
|
if @categories.empty?
|
|
|
|
''
|
|
|
|
else
|
2010-05-26 07:27:49 +02:00
|
|
|
("<div id=\"categories\">\n" +
|
2007-01-22 14:43:50 +01:00
|
|
|
'<strong>Categories</strong>:' +
|
2008-05-23 18:04:00 +02:00
|
|
|
'[' + link_to_unless_current('Any', :web => @web.address, :action => self.action_name, :category => nil) + "]\n" +
|
2007-01-22 14:43:50 +01:00
|
|
|
@categories.map { |c|
|
2010-05-26 07:27:49 +02:00
|
|
|
link_to_unless_current(c.html_safe, :web => @web.address, :action => self.action_name, :category => c)
|
2007-01-22 14:43:50 +01:00
|
|
|
}.join(', ') + "\n" +
|
2010-05-26 07:27:49 +02:00
|
|
|
'</div>').html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs HTML escaping on text, but keeps linefeeds intact (by replacing them with <br/>)
|
|
|
|
def escape_preserving_linefeeds(text)
|
2011-09-03 18:33:51 +02:00
|
|
|
h(text).gsub(/\n/, '<br/>').as_utf8.html_safe
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_date(date, include_time = true)
|
|
|
|
# Must use DateTime because Time doesn't support %e on at least some platforms
|
|
|
|
if include_time
|
|
|
|
DateTime.new(date.year, date.mon, date.day, date.hour, date.min, date.sec).strftime("%B %e, %Y %H:%M:%S")
|
|
|
|
else
|
|
|
|
DateTime.new(date.year, date.mon, date.day).strftime("%B %e, %Y")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rendered_content(page)
|
2011-04-25 17:54:44 +02:00
|
|
|
PageRenderer.new(page.current_revision).display_content
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-12-15 00:41:28 +01:00
|
|
|
def truncate(text, *args)
|
|
|
|
options = args.extract_options!
|
|
|
|
options.reverse_merge!(:length => 30, :omission => "...")
|
2010-05-26 07:27:49 +02:00
|
|
|
return text.html_safe if text.num_chars <= options[:length]
|
2009-12-15 00:41:28 +01:00
|
|
|
len = options[:length] - options[:omission].as_utf8.num_chars
|
2009-12-14 09:01:50 +01:00
|
|
|
t = ''
|
|
|
|
text.split.collect do |word|
|
2009-12-15 00:41:28 +01:00
|
|
|
if t.num_chars + word.num_chars <= len
|
2009-12-14 09:01:50 +01:00
|
|
|
t << word + ' '
|
|
|
|
else
|
2010-05-26 07:27:49 +02:00
|
|
|
return (t.chop + options[:omission]).html_safe
|
2009-12-14 09:01:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|