Chris@909: # encoding: utf-8
Chris@909: #
Chris@909: # Redmine - project management software
Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909: #
Chris@909: # This program is free software; you can redistribute it and/or
Chris@909: # modify it under the terms of the GNU General Public License
Chris@909: # as published by the Free Software Foundation; either version 2
Chris@909: # of the License, or (at your option) any later version.
Chris@909: #
Chris@909: # This program is distributed in the hope that it will be useful,
Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909: # GNU General Public License for more details.
Chris@909: #
Chris@909: # You should have received a copy of the GNU General Public License
Chris@909: # along with this program; if not, write to the Free Software
Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909:
Chris@909: require 'forwardable'
Chris@909: require 'cgi'
Chris@909:
Chris@909: module ApplicationHelper
Chris@909: include Redmine::WikiFormatting::Macros::Definitions
Chris@909: include Redmine::I18n
Chris@909: include GravatarHelper::PublicMethods
Chris@909:
Chris@909: extend Forwardable
Chris@909: def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
Chris@909:
Chris@909: # Return true if user is authorized for controller/action, otherwise false
Chris@909: def authorize_for(controller, action)
Chris@909: User.current.allowed_to?({:controller => controller, :action => action}, @project)
Chris@909: end
Chris@909:
Chris@909: # Display a link if user is authorized
Chris@909: #
Chris@909: # @param [String] name Anchor text (passed to link_to)
Chris@909: # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
Chris@909: # @param [optional, Hash] html_options Options passed to link_to
Chris@909: # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
Chris@909: def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
Chris@909: link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
Chris@909: end
Chris@909:
Chris@909: # Display a link to remote if user is authorized
Chris@909: def link_to_remote_if_authorized(name, options = {}, html_options = nil)
Chris@909: url = options[:url] || {}
Chris@909: link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
Chris@909: end
Chris@909:
Chris@909: # Displays a link to user's account page if active
Chris@909: def link_to_user(user, options={})
Chris@909: if user.is_a?(User)
Chris@909: name = h(user.name(options[:format]))
Chris@909: if user.active?
Chris@909: link_to name, :controller => 'users', :action => 'show', :id => user
Chris@909: else
Chris@909: name
Chris@909: end
Chris@909: else
Chris@909: h(user.to_s)
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: # Displays a link to +issue+ with its subject.
Chris@909: # Examples:
Chris@909: #
Chris@909: # link_to_issue(issue) # => Defect #6: This is the subject
Chris@909: # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
Chris@909: # link_to_issue(issue, :subject => false) # => Defect #6
Chris@909: # link_to_issue(issue, :project => true) # => Foo - Defect #6
Chris@909: #
Chris@909: def link_to_issue(issue, options={})
Chris@909: title = nil
Chris@909: subject = nil
Chris@909: if options[:subject] == false
Chris@909: title = truncate(issue.subject, :length => 60)
Chris@909: else
Chris@909: subject = issue.subject
Chris@909: if options[:truncate]
Chris@909: subject = truncate(subject, :length => options[:truncate])
Chris@909: end
Chris@909: end
Chris@909: s = link_to "#{h(issue.tracker)} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue},
Chris@909: :class => issue.css_classes,
Chris@909: :title => title
Chris@909: s << ": #{h subject}" if subject
Chris@909: s = "#{h issue.project} - " + s if options[:project]
Chris@909: s
Chris@909: end
Chris@909:
Chris@909: # Generates a link to an attachment.
Chris@909: # Options:
Chris@909: # * :text - Link text (default to attachment filename)
Chris@909: # * :download - Force download (default: false)
Chris@909: def link_to_attachment(attachment, options={})
Chris@909: text = options.delete(:text) || attachment.filename
Chris@909: action = options.delete(:download) ? 'download' : 'show'
Chris@909: link_to(h(text),
Chris@909: {:controller => 'attachments', :action => action,
Chris@909: :id => attachment, :filename => attachment.filename },
Chris@909: options)
Chris@909: end
Chris@909:
Chris@909: # Generates a link to a SCM revision
Chris@909: # Options:
Chris@909: # * :text - Link text (default to the formatted revision)
Chris@909: def link_to_revision(revision, project, options={})
Chris@909: text = options.delete(:text) || format_revision(revision)
Chris@909: rev = revision.respond_to?(:identifier) ? revision.identifier : revision
Chris@909:
Chris@909: link_to(h(text), {:controller => 'repositories', :action => 'revision', :id => project, :rev => rev},
Chris@909: :title => l(:label_revision_id, format_revision(revision)))
Chris@909: end
Chris@909:
Chris@909: # Generates a link to a message
Chris@909: def link_to_message(message, options={}, html_options = nil)
Chris@909: link_to(
Chris@909: h(truncate(message.subject, :length => 60)),
Chris@909: { :controller => 'messages', :action => 'show',
Chris@909: :board_id => message.board_id,
Chris@909: :id => message.root,
Chris@909: :r => (message.parent_id && message.id),
Chris@909: :anchor => (message.parent_id ? "message-#{message.id}" : nil)
Chris@909: }.merge(options),
Chris@909: html_options
Chris@909: )
Chris@909: end
Chris@909:
Chris@909: # Generates a link to a project if active
Chris@909: # Examples:
Chris@909: #
Chris@909: # link_to_project(project) # => link to the specified project overview
Chris@909: # link_to_project(project, :action=>'settings') # => link to project settings
Chris@909: # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
Chris@909: # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
Chris@909: #
Chris@909: def link_to_project(project, options={}, html_options = nil)
Chris@909: if project.active?
Chris@909: url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
Chris@909: link_to(h(project), url, html_options)
Chris@909: else
Chris@909: h(project)
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: def toggle_link(name, id, options={})
Chris@909: onclick = "Element.toggle('#{id}'); "
Chris@909: onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
Chris@909: onclick << "return false;"
Chris@909: link_to(name, "#", :onclick => onclick)
Chris@909: end
Chris@909:
Chris@909: def image_to_function(name, function, html_options = {})
Chris@909: html_options.symbolize_keys!
Chris@909: tag(:input, html_options.merge({
Chris@909: :type => "image", :src => image_path(name),
Chris@909: :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
Chris@909: }))
Chris@909: end
Chris@909:
Chris@909: def prompt_to_remote(name, text, param, url, html_options = {})
Chris@909: html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
Chris@909: link_to name, {}, html_options
Chris@909: end
Chris@909:
Chris@909: def format_activity_title(text)
Chris@909: h(truncate_single_line(text, :length => 100))
Chris@909: end
Chris@909:
Chris@909: def format_activity_day(date)
Chris@909: date == Date.today ? l(:label_today).titleize : format_date(date)
Chris@909: end
Chris@909:
Chris@909: def format_activity_description(text)
Chris@909: h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "
")
Chris@909: end
Chris@909:
Chris@909: def format_version_name(version)
Chris@909: if version.project == @project
Chris@909: h(version)
Chris@909: else
Chris@909: h("#{version.project} - #{version}")
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: def due_date_distance_in_words(date)
Chris@909: if date
Chris@909: l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: def render_page_hierarchy(pages, node=nil, options={})
Chris@909: content = ''
Chris@909: if pages[node]
Chris@909: content << "
'.html_safe + l(:label_export_to)) Chris@909: yield Redmine::Views::OtherFormatsBuilder.new(self) Chris@909: concat('
'.html_safe) Chris@909: end Chris@909: Chris@909: def page_header_title Chris@909: if @project.nil? || @project.new_record? Chris@909: h(Setting.app_title) Chris@909: else Chris@909: b = [] Chris@909: ancestors = (@project.root? ? [] : @project.ancestors.visible.all) Chris@909: if ancestors.any? Chris@909: root = ancestors.shift Chris@909: b << link_to_project(root, {:jump => current_menu_item}, :class => 'root') Chris@909: if ancestors.size > 2 Chris@909: b << "\xe2\x80\xa6" Chris@909: ancestors = ancestors[-2, 2] Chris@909: end Chris@909: b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') } Chris@909: end Chris@909: b << h(@project) Chris@909: b.join(" \xc2\xbb ").html_safe Chris@909: end Chris@909: end Chris@909: Chris@909: def html_title(*args) Chris@909: if args.empty? Chris@909: title = @html_title || [] Chris@909: title << @project.name if @project Chris@909: title << Setting.app_title unless Setting.app_title == title.last Chris@909: title.select {|t| !t.blank? }.join(' - ') Chris@909: else Chris@909: @html_title ||= [] Chris@909: @html_title += args Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the theme, controller name, and action as css classes for the Chris@909: # HTML body. Chris@909: def body_css_classes Chris@909: css = [] Chris@909: if theme = Redmine::Themes.theme(Setting.ui_theme) Chris@909: css << 'theme-' + theme.name Chris@909: end Chris@909: Chris@909: css << 'controller-' + params[:controller] Chris@909: css << 'action-' + params[:action] Chris@909: css.join(' ') Chris@909: end Chris@909: Chris@909: def accesskey(s) Chris@909: Redmine::AccessKeys.key_for s Chris@909: end Chris@909: Chris@909: # Formats text according to system settings. Chris@909: # 2 ways to call this method: Chris@909: # * with a String: textilizable(text, options) Chris@909: # * with an object and one of its attribute: textilizable(issue, :description, options) Chris@909: def textilizable(*args) Chris@909: options = args.last.is_a?(Hash) ? args.pop : {} Chris@909: case args.size Chris@909: when 1 Chris@909: obj = options[:object] Chris@909: text = args.shift Chris@909: when 2 Chris@909: obj = args.shift Chris@909: attr = args.shift Chris@909: text = obj.send(attr).to_s Chris@909: else Chris@909: raise ArgumentError, 'invalid arguments to textilizable' Chris@909: end Chris@909: return '' if text.blank? Chris@909: project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) Chris@909: only_path = options.delete(:only_path) == false ? false : true Chris@909: Chris@909: text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) Chris@909: Chris@909: @parsed_headings = [] Chris@909: @current_section = 0 if options[:edit_section_links] Chris@909: text = parse_non_pre_blocks(text) do |text| Chris@909: [:parse_sections, :parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_macros, :parse_headings].each do |method_name| Chris@909: send method_name, text, project, obj, attr, only_path, options Chris@909: end Chris@909: end Chris@909: Chris@909: if @parsed_headings.any? Chris@909: replace_toc(text, @parsed_headings) Chris@909: end Chris@909: Chris@909: text Chris@909: end Chris@909: Chris@909: def parse_non_pre_blocks(text) Chris@909: s = StringScanner.new(text) Chris@909: tags = [] Chris@909: parsed = '' Chris@909: while !s.eos? Chris@909: s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im) Chris@909: text, full_tag, closing, tag = s[1], s[2], s[3], s[4] Chris@909: if tags.empty? Chris@909: yield text Chris@909: end Chris@909: parsed << text Chris@909: if tag Chris@909: if closing Chris@909: if tags.last == tag.downcase Chris@909: tags.pop Chris@909: end Chris@909: else Chris@909: tags << tag.downcase Chris@909: end Chris@909: parsed << full_tag Chris@909: end Chris@909: end Chris@909: # Close any non closing tags Chris@909: while tag = tags.pop Chris@909: parsed << "#{tag}>" Chris@909: end Chris@909: parsed.html_safe Chris@909: end Chris@909: Chris@909: def parse_inline_attachments(text, project, obj, attr, only_path, options) Chris@909: # when using an image link, try to use an attachment, if possible Chris@909: if options[:attachments] || (obj && obj.respond_to?(:attachments)) Chris@909: attachments = options[:attachments] || obj.attachments Chris@909: text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| Chris@909: filename, ext, alt, alttext = $1.downcase, $2, $3, $4 Chris@909: # search for the picture in attachments Chris@909: if found = Attachment.latest_attach(attachments, filename) Chris@909: image_url = url_for :only_path => only_path, :controller => 'attachments', Chris@909: :action => 'download', :id => found Chris@909: desc = found.description.to_s.gsub('"', '') Chris@909: if !desc.blank? && alttext.blank? Chris@909: alt = " title=\"#{desc}\" alt=\"#{desc}\"" Chris@909: end Chris@909: "src=\"#{image_url}\"#{alt}".html_safe Chris@909: else Chris@909: m.html_safe Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Wiki links Chris@909: # Chris@909: # Examples: Chris@909: # [[mypage]] Chris@909: # [[mypage|mytext]] Chris@909: # wiki links can refer other project wikis, using project name or identifier: Chris@909: # [[project:]] -> wiki starting page Chris@909: # [[project:|mytext]] Chris@909: # [[project:mypage]] Chris@909: # [[project:mypage|mytext]] Chris@909: def parse_wiki_links(text, project, obj, attr, only_path, options) Chris@909: text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| Chris@909: link_project = project Chris@909: esc, all, page, title = $1, $2, $3, $5 Chris@909: if esc.nil? Chris@909: if page =~ /^([^\:]+)\:(.*)$/ Chris@909: link_project = Project.find_by_identifier($1) || Project.find_by_name($1) Chris@909: page = $2 Chris@909: title ||= $1 if page.blank? Chris@909: end Chris@909: Chris@909: if link_project && link_project.wiki Chris@909: # extract anchor Chris@909: anchor = nil Chris@909: if page =~ /^(.+?)\#(.+)$/ Chris@909: page, anchor = $1, $2 Chris@909: end Chris@909: anchor = sanitize_anchor_name(anchor) if anchor.present? Chris@909: # check if page exists Chris@909: wiki_page = link_project.wiki.find_page(page) Chris@909: url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page Chris@909: "##{anchor}" Chris@909: else Chris@909: case options[:wiki_links] Chris@909: when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '') Chris@909: when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export Chris@909: else Chris@909: wiki_page_id = page.present? ? Wiki.titleize(page) : nil Chris@909: url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor) Chris@909: end Chris@909: end Chris@909: link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) Chris@909: else Chris@909: # project or wiki doesn't exist Chris@909: all.html_safe Chris@909: end Chris@909: else Chris@909: all.html_safe Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Redmine links Chris@909: # Chris@909: # Examples: Chris@909: # Issues: Chris@909: # #52 -> Link to issue #52 Chris@909: # Changesets: Chris@909: # r52 -> Link to revision 52 Chris@909: # commit:a85130f -> Link to scmid starting with a85130f Chris@909: # Documents: Chris@909: # document#17 -> Link to document with id 17 Chris@909: # document:Greetings -> Link to the document with title "Greetings" Chris@909: # document:"Some document" -> Link to the document with title "Some document" Chris@909: # Versions: Chris@909: # version#3 -> Link to version with id 3 Chris@909: # version:1.0.0 -> Link to version named "1.0.0" Chris@909: # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" Chris@909: # Attachments: Chris@909: # attachment:file.zip -> Link to the attachment of the current object named file.zip Chris@909: # Source files: Chris@909: # source:some/file -> Link to the file located at /some/file in the project's repository Chris@909: # source:some/file@52 -> Link to the file's revision 52 Chris@909: # source:some/file#L120 -> Link to line 120 of the file Chris@909: # source:some/file@52#L120 -> Link to line 120 of the file's revision 52 Chris@909: # export:some/file -> Force the download of the file Chris@909: # Forum messages: Chris@909: # message#1218 -> Link to message with id 1218 Chris@909: # Chris@909: # Links can refer other objects from other projects, using project identifier: Chris@909: # identifier:r52 Chris@909: # identifier:document:"Some document" Chris@909: # identifier:version:1.0.0 Chris@909: # identifier:source:some/file Chris@909: def parse_redmine_links(text, project, obj, attr, only_path, options) Chris@909: text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(([a-z0-9\-]+):)?(attachment|document|version|forum|news|commit|source|export|message|project)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|\]|<|$)}) do |m| Chris@909: leading, esc, project_prefix, project_identifier, prefix, sep, identifier = $1, $2, $3, $4, $5, $7 || $9, $8 || $10 Chris@909: link = nil Chris@909: if project_identifier Chris@909: project = Project.visible.find_by_identifier(project_identifier) Chris@909: end Chris@909: if esc.nil? Chris@909: if prefix.nil? && sep == 'r' Chris@909: # project.changesets.visible raises an SQL error because of a double join on repositories Chris@909: if project && project.repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(project.repository.id, identifier)) Chris@909: link = link_to(h("#{project_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, Chris@909: :class => 'changeset', Chris@909: :title => truncate_single_line(changeset.comments, :length => 100)) Chris@909: end Chris@909: elsif sep == '#' Chris@909: oid = identifier.to_i Chris@909: case prefix Chris@909: when nil Chris@909: if issue = Issue.visible.find_by_id(oid, :include => :status) Chris@909: link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid}, Chris@909: :class => issue.css_classes, Chris@909: :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") Chris@909: end Chris@909: when 'document' Chris@909: if document = Document.visible.find_by_id(oid) Chris@909: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@909: :class => 'document' Chris@909: end Chris@909: when 'version' Chris@909: if version = Version.visible.find_by_id(oid) Chris@909: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@909: :class => 'version' Chris@909: end Chris@909: when 'message' Chris@909: if message = Message.visible.find_by_id(oid, :include => :parent) Chris@909: link = link_to_message(message, {:only_path => only_path}, :class => 'message') Chris@909: end Chris@909: when 'forum' Chris@909: if board = Board.visible.find_by_id(oid) Chris@909: link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, Chris@909: :class => 'board' Chris@909: end Chris@909: when 'news' Chris@909: if news = News.visible.find_by_id(oid) Chris@909: link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, Chris@909: :class => 'news' Chris@909: end Chris@909: when 'project' Chris@909: if p = Project.visible.find_by_id(oid) Chris@909: link = link_to_project(p, {:only_path => only_path}, :class => 'project') Chris@909: end Chris@909: end Chris@909: elsif sep == ':' Chris@909: # removes the double quotes if any Chris@909: name = identifier.gsub(%r{^"(.*)"$}, "\\1") Chris@909: case prefix Chris@909: when 'document' Chris@909: if project && document = project.documents.visible.find_by_title(name) Chris@909: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@909: :class => 'document' Chris@909: end Chris@909: when 'version' Chris@909: if project && version = project.versions.visible.find_by_name(name) Chris@909: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@909: :class => 'version' Chris@909: end Chris@909: when 'forum' Chris@909: if project && board = project.boards.visible.find_by_name(name) Chris@909: link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, Chris@909: :class => 'board' Chris@909: end Chris@909: when 'news' Chris@909: if project && news = project.news.visible.find_by_title(name) Chris@909: link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, Chris@909: :class => 'news' Chris@909: end Chris@909: when 'commit' Chris@909: if project && project.repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", project.repository.id, "#{name}%"])) Chris@909: link = link_to h("#{project_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.identifier}, Chris@909: :class => 'changeset', Chris@909: :title => truncate_single_line(h(changeset.comments), :length => 100) Chris@909: end Chris@909: when 'source', 'export' Chris@909: if project && project.repository && User.current.allowed_to?(:browse_repository, project) Chris@909: name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$} Chris@909: path, rev, anchor = $1, $3, $5 Chris@909: link = link_to h("#{project_prefix}#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, Chris@909: :path => to_path_param(path), Chris@909: :rev => rev, Chris@909: :anchor => anchor, Chris@909: :format => (prefix == 'export' ? 'raw' : nil)}, Chris@909: :class => (prefix == 'export' ? 'source download' : 'source') Chris@909: end Chris@909: when 'attachment' Chris@909: attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) Chris@909: if attachments && attachment = attachments.detect {|a| a.filename == name } Chris@909: link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, Chris@909: :class => 'attachment' Chris@909: end Chris@909: when 'project' Chris@909: if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}]) Chris@909: link = link_to_project(p, {:only_path => only_path}, :class => 'project') Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: (leading + (link || "#{project_prefix}#{prefix}#{sep}#{identifier}")).html_safe Chris@909: end Chris@909: end Chris@909: Chris@909: HEADING_RE = /(\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) Chris@909: Chris@909: # Renders the TOC with given headings Chris@909: def replace_toc(text, headings) Chris@909: text.gsub!(TOC_RE) do Chris@909: if headings.empty? Chris@909: '' Chris@909: else Chris@909: div_class = 'toc' Chris@909: div_class << ' right' if $1 == '>' Chris@909: div_class << ' left' if $1 == '<' Chris@909: out = "