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