annotate .svn/pristine/04/048c8c7c3fc4fac08662cbdaac59cd6348bb3d14.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # encoding: utf-8
Chris@1296 2 #
Chris@1296 3 # Redmine - project management software
Chris@1296 4 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 5 #
Chris@1296 6 # This program is free software; you can redistribute it and/or
Chris@1296 7 # modify it under the terms of the GNU General Public License
Chris@1296 8 # as published by the Free Software Foundation; either version 2
Chris@1296 9 # of the License, or (at your option) any later version.
Chris@1296 10 #
Chris@1296 11 # This program is distributed in the hope that it will be useful,
Chris@1296 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 14 # GNU General Public License for more details.
Chris@1296 15 #
Chris@1296 16 # You should have received a copy of the GNU General Public License
Chris@1296 17 # along with this program; if not, write to the Free Software
Chris@1296 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 19
Chris@1296 20 require 'forwardable'
Chris@1296 21 require 'cgi'
Chris@1296 22
Chris@1296 23 module ApplicationHelper
Chris@1296 24 include Redmine::WikiFormatting::Macros::Definitions
Chris@1296 25 include Redmine::I18n
Chris@1296 26 include GravatarHelper::PublicMethods
Chris@1296 27
Chris@1296 28 extend Forwardable
Chris@1296 29 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
Chris@1296 30
Chris@1296 31 # Return true if user is authorized for controller/action, otherwise false
Chris@1296 32 def authorize_for(controller, action)
Chris@1296 33 User.current.allowed_to?({:controller => controller, :action => action}, @project)
Chris@1296 34 end
Chris@1296 35
Chris@1296 36 # Display a link if user is authorized
Chris@1296 37 #
Chris@1296 38 # @param [String] name Anchor text (passed to link_to)
Chris@1296 39 # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
Chris@1296 40 # @param [optional, Hash] html_options Options passed to link_to
Chris@1296 41 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
Chris@1296 42 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
Chris@1296 43 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
Chris@1296 44 end
Chris@1296 45
Chris@1296 46 # Displays a link to user's account page if active
Chris@1296 47 def link_to_user(user, options={})
Chris@1296 48 if user.is_a?(User)
Chris@1296 49 name = h(user.name(options[:format]))
Chris@1296 50 if user.active? || (User.current.admin? && user.logged?)
Chris@1296 51 link_to name, user_path(user), :class => user.css_classes
Chris@1296 52 else
Chris@1296 53 name
Chris@1296 54 end
Chris@1296 55 else
Chris@1296 56 h(user.to_s)
Chris@1296 57 end
Chris@1296 58 end
Chris@1296 59
Chris@1296 60 # Displays a link to +issue+ with its subject.
Chris@1296 61 # Examples:
Chris@1296 62 #
Chris@1296 63 # link_to_issue(issue) # => Defect #6: This is the subject
Chris@1296 64 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
Chris@1296 65 # link_to_issue(issue, :subject => false) # => Defect #6
Chris@1296 66 # link_to_issue(issue, :project => true) # => Foo - Defect #6
Chris@1296 67 # link_to_issue(issue, :subject => false, :tracker => false) # => #6
Chris@1296 68 #
Chris@1296 69 def link_to_issue(issue, options={})
Chris@1296 70 title = nil
Chris@1296 71 subject = nil
Chris@1296 72 text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}"
Chris@1296 73 if options[:subject] == false
Chris@1296 74 title = truncate(issue.subject, :length => 60)
Chris@1296 75 else
Chris@1296 76 subject = issue.subject
Chris@1296 77 if options[:truncate]
Chris@1296 78 subject = truncate(subject, :length => options[:truncate])
Chris@1296 79 end
Chris@1296 80 end
Chris@1296 81 s = link_to text, issue_path(issue), :class => issue.css_classes, :title => title
Chris@1296 82 s << h(": #{subject}") if subject
Chris@1296 83 s = h("#{issue.project} - ") + s if options[:project]
Chris@1296 84 s
Chris@1296 85 end
Chris@1296 86
Chris@1296 87 # Generates a link to an attachment.
Chris@1296 88 # Options:
Chris@1296 89 # * :text - Link text (default to attachment filename)
Chris@1296 90 # * :download - Force download (default: false)
Chris@1296 91 def link_to_attachment(attachment, options={})
Chris@1296 92 text = options.delete(:text) || attachment.filename
Chris@1296 93 action = options.delete(:download) ? 'download' : 'show'
Chris@1296 94 opt_only_path = {}
Chris@1296 95 opt_only_path[:only_path] = (options[:only_path] == false ? false : true)
Chris@1296 96 options.delete(:only_path)
Chris@1296 97 link_to(h(text),
Chris@1296 98 {:controller => 'attachments', :action => action,
Chris@1296 99 :id => attachment, :filename => attachment.filename}.merge(opt_only_path),
Chris@1296 100 options)
Chris@1296 101 end
Chris@1296 102
Chris@1296 103 # Generates a link to a SCM revision
Chris@1296 104 # Options:
Chris@1296 105 # * :text - Link text (default to the formatted revision)
Chris@1296 106 def link_to_revision(revision, repository, options={})
Chris@1296 107 if repository.is_a?(Project)
Chris@1296 108 repository = repository.repository
Chris@1296 109 end
Chris@1296 110 text = options.delete(:text) || format_revision(revision)
Chris@1296 111 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
Chris@1296 112 link_to(
Chris@1296 113 h(text),
Chris@1296 114 {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
Chris@1296 115 :title => l(:label_revision_id, format_revision(revision))
Chris@1296 116 )
Chris@1296 117 end
Chris@1296 118
Chris@1296 119 # Generates a link to a message
Chris@1296 120 def link_to_message(message, options={}, html_options = nil)
Chris@1296 121 link_to(
Chris@1296 122 h(truncate(message.subject, :length => 60)),
Chris@1296 123 { :controller => 'messages', :action => 'show',
Chris@1296 124 :board_id => message.board_id,
Chris@1296 125 :id => (message.parent_id || message.id),
Chris@1296 126 :r => (message.parent_id && message.id),
Chris@1296 127 :anchor => (message.parent_id ? "message-#{message.id}" : nil)
Chris@1296 128 }.merge(options),
Chris@1296 129 html_options
Chris@1296 130 )
Chris@1296 131 end
Chris@1296 132
Chris@1296 133 # Generates a link to a project if active
Chris@1296 134 # Examples:
Chris@1296 135 #
Chris@1296 136 # link_to_project(project) # => link to the specified project overview
Chris@1296 137 # link_to_project(project, :action=>'settings') # => link to project settings
Chris@1296 138 # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
Chris@1296 139 # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
Chris@1296 140 #
Chris@1296 141 def link_to_project(project, options={}, html_options = nil)
Chris@1296 142 if project.archived?
Chris@1296 143 h(project)
Chris@1296 144 else
Chris@1296 145 url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
Chris@1296 146 link_to(h(project), url, html_options)
Chris@1296 147 end
Chris@1296 148 end
Chris@1296 149
Chris@1296 150 def wiki_page_path(page, options={})
Chris@1296 151 url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options))
Chris@1296 152 end
Chris@1296 153
Chris@1296 154 def thumbnail_tag(attachment)
Chris@1296 155 link_to image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)),
Chris@1296 156 {:controller => 'attachments', :action => 'show', :id => attachment, :filename => attachment.filename},
Chris@1296 157 :title => attachment.filename
Chris@1296 158 end
Chris@1296 159
Chris@1296 160 def toggle_link(name, id, options={})
Chris@1296 161 onclick = "$('##{id}').toggle(); "
Chris@1296 162 onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ")
Chris@1296 163 onclick << "return false;"
Chris@1296 164 link_to(name, "#", :onclick => onclick)
Chris@1296 165 end
Chris@1296 166
Chris@1296 167 def image_to_function(name, function, html_options = {})
Chris@1296 168 html_options.symbolize_keys!
Chris@1296 169 tag(:input, html_options.merge({
Chris@1296 170 :type => "image", :src => image_path(name),
Chris@1296 171 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
Chris@1296 172 }))
Chris@1296 173 end
Chris@1296 174
Chris@1296 175 def format_activity_title(text)
Chris@1296 176 h(truncate_single_line(text, :length => 100))
Chris@1296 177 end
Chris@1296 178
Chris@1296 179 def format_activity_day(date)
Chris@1296 180 date == User.current.today ? l(:label_today).titleize : format_date(date)
Chris@1296 181 end
Chris@1296 182
Chris@1296 183 def format_activity_description(text)
Chris@1296 184 h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
Chris@1296 185 ).gsub(/[\r\n]+/, "<br />").html_safe
Chris@1296 186 end
Chris@1296 187
Chris@1296 188 def format_version_name(version)
Chris@1296 189 if version.project == @project
Chris@1296 190 h(version)
Chris@1296 191 else
Chris@1296 192 h("#{version.project} - #{version}")
Chris@1296 193 end
Chris@1296 194 end
Chris@1296 195
Chris@1296 196 def due_date_distance_in_words(date)
Chris@1296 197 if date
Chris@1296 198 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
Chris@1296 199 end
Chris@1296 200 end
Chris@1296 201
Chris@1296 202 # Renders a tree of projects as a nested set of unordered lists
Chris@1296 203 # The given collection may be a subset of the whole project tree
Chris@1296 204 # (eg. some intermediate nodes are private and can not be seen)
Chris@1296 205 def render_project_nested_lists(projects)
Chris@1296 206 s = ''
Chris@1296 207 if projects.any?
Chris@1296 208 ancestors = []
Chris@1296 209 original_project = @project
Chris@1296 210 projects.sort_by(&:lft).each do |project|
Chris@1296 211 # set the project environment to please macros.
Chris@1296 212 @project = project
Chris@1296 213 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
Chris@1296 214 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
Chris@1296 215 else
Chris@1296 216 ancestors.pop
Chris@1296 217 s << "</li>"
Chris@1296 218 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
Chris@1296 219 ancestors.pop
Chris@1296 220 s << "</ul></li>\n"
Chris@1296 221 end
Chris@1296 222 end
Chris@1296 223 classes = (ancestors.empty? ? 'root' : 'child')
Chris@1296 224 s << "<li class='#{classes}'><div class='#{classes}'>"
Chris@1296 225 s << h(block_given? ? yield(project) : project.name)
Chris@1296 226 s << "</div>\n"
Chris@1296 227 ancestors << project
Chris@1296 228 end
Chris@1296 229 s << ("</li></ul>\n" * ancestors.size)
Chris@1296 230 @project = original_project
Chris@1296 231 end
Chris@1296 232 s.html_safe
Chris@1296 233 end
Chris@1296 234
Chris@1296 235 def render_page_hierarchy(pages, node=nil, options={})
Chris@1296 236 content = ''
Chris@1296 237 if pages[node]
Chris@1296 238 content << "<ul class=\"pages-hierarchy\">\n"
Chris@1296 239 pages[node].each do |page|
Chris@1296 240 content << "<li>"
Chris@1296 241 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title, :version => nil},
Chris@1296 242 :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
Chris@1296 243 content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
Chris@1296 244 content << "</li>\n"
Chris@1296 245 end
Chris@1296 246 content << "</ul>\n"
Chris@1296 247 end
Chris@1296 248 content.html_safe
Chris@1296 249 end
Chris@1296 250
Chris@1296 251 # Renders flash messages
Chris@1296 252 def render_flash_messages
Chris@1296 253 s = ''
Chris@1296 254 flash.each do |k,v|
Chris@1296 255 s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
Chris@1296 256 end
Chris@1296 257 s.html_safe
Chris@1296 258 end
Chris@1296 259
Chris@1296 260 # Renders tabs and their content
Chris@1296 261 def render_tabs(tabs)
Chris@1296 262 if tabs.any?
Chris@1296 263 render :partial => 'common/tabs', :locals => {:tabs => tabs}
Chris@1296 264 else
Chris@1296 265 content_tag 'p', l(:label_no_data), :class => "nodata"
Chris@1296 266 end
Chris@1296 267 end
Chris@1296 268
Chris@1296 269 # Renders the project quick-jump box
Chris@1296 270 def render_project_jump_box
Chris@1296 271 return unless User.current.logged?
Chris@1296 272 projects = User.current.memberships.collect(&:project).compact.select(&:active?).uniq
Chris@1296 273 if projects.any?
Chris@1296 274 options =
Chris@1296 275 ("<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
Chris@1296 276 '<option value="" disabled="disabled">---</option>').html_safe
Chris@1296 277
Chris@1296 278 options << project_tree_options_for_select(projects, :selected => @project) do |p|
Chris@1296 279 { :value => project_path(:id => p, :jump => current_menu_item) }
Chris@1296 280 end
Chris@1296 281
Chris@1296 282 select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }')
Chris@1296 283 end
Chris@1296 284 end
Chris@1296 285
Chris@1296 286 def project_tree_options_for_select(projects, options = {})
Chris@1296 287 s = ''
Chris@1296 288 project_tree(projects) do |project, level|
Chris@1296 289 name_prefix = (level > 0 ? '&nbsp;' * 2 * level + '&#187; ' : '').html_safe
Chris@1296 290 tag_options = {:value => project.id}
Chris@1296 291 if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
Chris@1296 292 tag_options[:selected] = 'selected'
Chris@1296 293 else
Chris@1296 294 tag_options[:selected] = nil
Chris@1296 295 end
Chris@1296 296 tag_options.merge!(yield(project)) if block_given?
Chris@1296 297 s << content_tag('option', name_prefix + h(project), tag_options)
Chris@1296 298 end
Chris@1296 299 s.html_safe
Chris@1296 300 end
Chris@1296 301
Chris@1296 302 # Yields the given block for each project with its level in the tree
Chris@1296 303 #
Chris@1296 304 # Wrapper for Project#project_tree
Chris@1296 305 def project_tree(projects, &block)
Chris@1296 306 Project.project_tree(projects, &block)
Chris@1296 307 end
Chris@1296 308
Chris@1296 309 def principals_check_box_tags(name, principals)
Chris@1296 310 s = ''
Chris@1296 311 principals.sort.each do |principal|
Chris@1296 312 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
Chris@1296 313 end
Chris@1296 314 s.html_safe
Chris@1296 315 end
Chris@1296 316
Chris@1296 317 # Returns a string for users/groups option tags
Chris@1296 318 def principals_options_for_select(collection, selected=nil)
Chris@1296 319 s = ''
Chris@1296 320 if collection.include?(User.current)
Chris@1296 321 s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id)
Chris@1296 322 end
Chris@1296 323 groups = ''
Chris@1296 324 collection.sort.each do |element|
Chris@1296 325 selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
Chris@1296 326 (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
Chris@1296 327 end
Chris@1296 328 unless groups.empty?
Chris@1296 329 s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
Chris@1296 330 end
Chris@1296 331 s.html_safe
Chris@1296 332 end
Chris@1296 333
Chris@1296 334 # Options for the new membership projects combo-box
Chris@1296 335 def options_for_membership_project_select(principal, projects)
Chris@1296 336 options = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---")
Chris@1296 337 options << project_tree_options_for_select(projects) do |p|
Chris@1296 338 {:disabled => principal.projects.include?(p)}
Chris@1296 339 end
Chris@1296 340 options
Chris@1296 341 end
Chris@1296 342
Chris@1296 343 # Truncates and returns the string as a single line
Chris@1296 344 def truncate_single_line(string, *args)
Chris@1296 345 truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
Chris@1296 346 end
Chris@1296 347
Chris@1296 348 # Truncates at line break after 250 characters or options[:length]
Chris@1296 349 def truncate_lines(string, options={})
Chris@1296 350 length = options[:length] || 250
Chris@1296 351 if string.to_s =~ /\A(.{#{length}}.*?)$/m
Chris@1296 352 "#{$1}..."
Chris@1296 353 else
Chris@1296 354 string
Chris@1296 355 end
Chris@1296 356 end
Chris@1296 357
Chris@1296 358 def anchor(text)
Chris@1296 359 text.to_s.gsub(' ', '_')
Chris@1296 360 end
Chris@1296 361
Chris@1296 362 def html_hours(text)
Chris@1296 363 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
Chris@1296 364 end
Chris@1296 365
Chris@1296 366 def authoring(created, author, options={})
Chris@1296 367 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
Chris@1296 368 end
Chris@1296 369
Chris@1296 370 def time_tag(time)
Chris@1296 371 text = distance_of_time_in_words(Time.now, time)
Chris@1296 372 if @project
Chris@1296 373 link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => User.current.time_to_date(time)}, :title => format_time(time))
Chris@1296 374 else
Chris@1296 375 content_tag('acronym', text, :title => format_time(time))
Chris@1296 376 end
Chris@1296 377 end
Chris@1296 378
Chris@1296 379 def syntax_highlight_lines(name, content)
Chris@1296 380 lines = []
Chris@1296 381 syntax_highlight(name, content).each_line { |line| lines << line }
Chris@1296 382 lines
Chris@1296 383 end
Chris@1296 384
Chris@1296 385 def syntax_highlight(name, content)
Chris@1296 386 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
Chris@1296 387 end
Chris@1296 388
Chris@1296 389 def to_path_param(path)
Chris@1296 390 str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/")
Chris@1296 391 str.blank? ? nil : str
Chris@1296 392 end
Chris@1296 393
Chris@1296 394 def pagination_links_full(paginator, count=nil, options={})
Chris@1296 395 page_param = options.delete(:page_param) || :page
Chris@1296 396 per_page_links = options.delete(:per_page_links)
Chris@1296 397 url_param = params.dup
Chris@1296 398
Chris@1296 399 html = ''
Chris@1296 400 if paginator.current.previous
Chris@1296 401 # \xc2\xab(utf-8) = &#171;
Chris@1296 402 html << link_to_content_update(
Chris@1296 403 "\xc2\xab " + l(:label_previous),
Chris@1296 404 url_param.merge(page_param => paginator.current.previous)) + ' '
Chris@1296 405 end
Chris@1296 406
Chris@1296 407 html << (pagination_links_each(paginator, options) do |n|
Chris@1296 408 link_to_content_update(n.to_s, url_param.merge(page_param => n))
Chris@1296 409 end || '')
Chris@1296 410
Chris@1296 411 if paginator.current.next
Chris@1296 412 # \xc2\xbb(utf-8) = &#187;
Chris@1296 413 html << ' ' + link_to_content_update(
Chris@1296 414 (l(:label_next) + " \xc2\xbb"),
Chris@1296 415 url_param.merge(page_param => paginator.current.next))
Chris@1296 416 end
Chris@1296 417
Chris@1296 418 unless count.nil?
Chris@1296 419 html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
Chris@1296 420 if per_page_links != false && links = per_page_links(paginator.items_per_page, count)
Chris@1296 421 html << " | #{links}"
Chris@1296 422 end
Chris@1296 423 end
Chris@1296 424
Chris@1296 425 html.html_safe
Chris@1296 426 end
Chris@1296 427
Chris@1296 428 def per_page_links(selected=nil, item_count=nil)
Chris@1296 429 values = Setting.per_page_options_array
Chris@1296 430 if item_count && values.any?
Chris@1296 431 if item_count > values.first
Chris@1296 432 max = values.detect {|value| value >= item_count} || item_count
Chris@1296 433 else
Chris@1296 434 max = item_count
Chris@1296 435 end
Chris@1296 436 values = values.select {|value| value <= max || value == selected}
Chris@1296 437 end
Chris@1296 438 if values.empty? || (values.size == 1 && values.first == selected)
Chris@1296 439 return nil
Chris@1296 440 end
Chris@1296 441 links = values.collect do |n|
Chris@1296 442 n == selected ? n : link_to_content_update(n, params.merge(:per_page => n))
Chris@1296 443 end
Chris@1296 444 l(:label_display_per_page, links.join(', '))
Chris@1296 445 end
Chris@1296 446
Chris@1296 447 def reorder_links(name, url, method = :post)
Chris@1296 448 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
Chris@1296 449 url.merge({"#{name}[move_to]" => 'highest'}),
Chris@1296 450 :method => method, :title => l(:label_sort_highest)) +
Chris@1296 451 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
Chris@1296 452 url.merge({"#{name}[move_to]" => 'higher'}),
Chris@1296 453 :method => method, :title => l(:label_sort_higher)) +
Chris@1296 454 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
Chris@1296 455 url.merge({"#{name}[move_to]" => 'lower'}),
Chris@1296 456 :method => method, :title => l(:label_sort_lower)) +
Chris@1296 457 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
Chris@1296 458 url.merge({"#{name}[move_to]" => 'lowest'}),
Chris@1296 459 :method => method, :title => l(:label_sort_lowest))
Chris@1296 460 end
Chris@1296 461
Chris@1296 462 def breadcrumb(*args)
Chris@1296 463 elements = args.flatten
Chris@1296 464 elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
Chris@1296 465 end
Chris@1296 466
Chris@1296 467 def other_formats_links(&block)
Chris@1296 468 concat('<p class="other-formats">'.html_safe + l(:label_export_to))
Chris@1296 469 yield Redmine::Views::OtherFormatsBuilder.new(self)
Chris@1296 470 concat('</p>'.html_safe)
Chris@1296 471 end
Chris@1296 472
Chris@1296 473 def page_header_title
Chris@1296 474 if @project.nil? || @project.new_record?
Chris@1296 475 h(Setting.app_title)
Chris@1296 476 else
Chris@1296 477 b = []
Chris@1296 478 ancestors = (@project.root? ? [] : @project.ancestors.visible.all)
Chris@1296 479 if ancestors.any?
Chris@1296 480 root = ancestors.shift
Chris@1296 481 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
Chris@1296 482 if ancestors.size > 2
Chris@1296 483 b << "\xe2\x80\xa6"
Chris@1296 484 ancestors = ancestors[-2, 2]
Chris@1296 485 end
Chris@1296 486 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
Chris@1296 487 end
Chris@1296 488 b << h(@project)
Chris@1296 489 b.join(" \xc2\xbb ").html_safe
Chris@1296 490 end
Chris@1296 491 end
Chris@1296 492
Chris@1296 493 def html_title(*args)
Chris@1296 494 if args.empty?
Chris@1296 495 title = @html_title || []
Chris@1296 496 title << @project.name if @project
Chris@1296 497 title << Setting.app_title unless Setting.app_title == title.last
Chris@1296 498 title.select {|t| !t.blank? }.join(' - ')
Chris@1296 499 else
Chris@1296 500 @html_title ||= []
Chris@1296 501 @html_title += args
Chris@1296 502 end
Chris@1296 503 end
Chris@1296 504
Chris@1296 505 # Returns the theme, controller name, and action as css classes for the
Chris@1296 506 # HTML body.
Chris@1296 507 def body_css_classes
Chris@1296 508 css = []
Chris@1296 509 if theme = Redmine::Themes.theme(Setting.ui_theme)
Chris@1296 510 css << 'theme-' + theme.name
Chris@1296 511 end
Chris@1296 512
Chris@1296 513 css << 'controller-' + controller_name
Chris@1296 514 css << 'action-' + action_name
Chris@1296 515 css.join(' ')
Chris@1296 516 end
Chris@1296 517
Chris@1296 518 def accesskey(s)
Chris@1296 519 Redmine::AccessKeys.key_for s
Chris@1296 520 end
Chris@1296 521
Chris@1296 522 # Formats text according to system settings.
Chris@1296 523 # 2 ways to call this method:
Chris@1296 524 # * with a String: textilizable(text, options)
Chris@1296 525 # * with an object and one of its attribute: textilizable(issue, :description, options)
Chris@1296 526 def textilizable(*args)
Chris@1296 527 options = args.last.is_a?(Hash) ? args.pop : {}
Chris@1296 528 case args.size
Chris@1296 529 when 1
Chris@1296 530 obj = options[:object]
Chris@1296 531 text = args.shift
Chris@1296 532 when 2
Chris@1296 533 obj = args.shift
Chris@1296 534 attr = args.shift
Chris@1296 535 text = obj.send(attr).to_s
Chris@1296 536 else
Chris@1296 537 raise ArgumentError, 'invalid arguments to textilizable'
Chris@1296 538 end
Chris@1296 539 return '' if text.blank?
Chris@1296 540 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
Chris@1296 541 only_path = options.delete(:only_path) == false ? false : true
Chris@1296 542
Chris@1296 543 text = text.dup
Chris@1296 544 macros = catch_macros(text)
Chris@1296 545 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
Chris@1296 546
Chris@1296 547 @parsed_headings = []
Chris@1296 548 @heading_anchors = {}
Chris@1296 549 @current_section = 0 if options[:edit_section_links]
Chris@1296 550
Chris@1296 551 parse_sections(text, project, obj, attr, only_path, options)
Chris@1296 552 text = parse_non_pre_blocks(text, obj, macros) do |text|
Chris@1296 553 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
Chris@1296 554 send method_name, text, project, obj, attr, only_path, options
Chris@1296 555 end
Chris@1296 556 end
Chris@1296 557 parse_headings(text, project, obj, attr, only_path, options)
Chris@1296 558
Chris@1296 559 if @parsed_headings.any?
Chris@1296 560 replace_toc(text, @parsed_headings)
Chris@1296 561 end
Chris@1296 562
Chris@1296 563 text.html_safe
Chris@1296 564 end
Chris@1296 565
Chris@1296 566 def parse_non_pre_blocks(text, obj, macros)
Chris@1296 567 s = StringScanner.new(text)
Chris@1296 568 tags = []
Chris@1296 569 parsed = ''
Chris@1296 570 while !s.eos?
Chris@1296 571 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
Chris@1296 572 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
Chris@1296 573 if tags.empty?
Chris@1296 574 yield text
Chris@1296 575 inject_macros(text, obj, macros) if macros.any?
Chris@1296 576 else
Chris@1296 577 inject_macros(text, obj, macros, false) if macros.any?
Chris@1296 578 end
Chris@1296 579 parsed << text
Chris@1296 580 if tag
Chris@1296 581 if closing
Chris@1296 582 if tags.last == tag.downcase
Chris@1296 583 tags.pop
Chris@1296 584 end
Chris@1296 585 else
Chris@1296 586 tags << tag.downcase
Chris@1296 587 end
Chris@1296 588 parsed << full_tag
Chris@1296 589 end
Chris@1296 590 end
Chris@1296 591 # Close any non closing tags
Chris@1296 592 while tag = tags.pop
Chris@1296 593 parsed << "</#{tag}>"
Chris@1296 594 end
Chris@1296 595 parsed
Chris@1296 596 end
Chris@1296 597
Chris@1296 598 def parse_inline_attachments(text, project, obj, attr, only_path, options)
Chris@1296 599 # when using an image link, try to use an attachment, if possible
Chris@1296 600 attachments = options[:attachments] || []
Chris@1296 601 attachments += obj.attachments if obj.respond_to?(:attachments)
Chris@1296 602 if attachments.present?
Chris@1296 603 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
Chris@1296 604 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
Chris@1296 605 # search for the picture in attachments
Chris@1296 606 if found = Attachment.latest_attach(attachments, filename)
Chris@1296 607 image_url = url_for :only_path => only_path, :controller => 'attachments',
Chris@1296 608 :action => 'download', :id => found
Chris@1296 609 desc = found.description.to_s.gsub('"', '')
Chris@1296 610 if !desc.blank? && alttext.blank?
Chris@1296 611 alt = " title=\"#{desc}\" alt=\"#{desc}\""
Chris@1296 612 end
Chris@1296 613 "src=\"#{image_url}\"#{alt}"
Chris@1296 614 else
Chris@1296 615 m
Chris@1296 616 end
Chris@1296 617 end
Chris@1296 618 end
Chris@1296 619 end
Chris@1296 620
Chris@1296 621 # Wiki links
Chris@1296 622 #
Chris@1296 623 # Examples:
Chris@1296 624 # [[mypage]]
Chris@1296 625 # [[mypage|mytext]]
Chris@1296 626 # wiki links can refer other project wikis, using project name or identifier:
Chris@1296 627 # [[project:]] -> wiki starting page
Chris@1296 628 # [[project:|mytext]]
Chris@1296 629 # [[project:mypage]]
Chris@1296 630 # [[project:mypage|mytext]]
Chris@1296 631 def parse_wiki_links(text, project, obj, attr, only_path, options)
Chris@1296 632 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
Chris@1296 633 link_project = project
Chris@1296 634 esc, all, page, title = $1, $2, $3, $5
Chris@1296 635 if esc.nil?
Chris@1296 636 if page =~ /^([^\:]+)\:(.*)$/
Chris@1296 637 link_project = Project.find_by_identifier($1) || Project.find_by_name($1)
Chris@1296 638 page = $2
Chris@1296 639 title ||= $1 if page.blank?
Chris@1296 640 end
Chris@1296 641
Chris@1296 642 if link_project && link_project.wiki
Chris@1296 643 # extract anchor
Chris@1296 644 anchor = nil
Chris@1296 645 if page =~ /^(.+?)\#(.+)$/
Chris@1296 646 page, anchor = $1, $2
Chris@1296 647 end
Chris@1296 648 anchor = sanitize_anchor_name(anchor) if anchor.present?
Chris@1296 649 # check if page exists
Chris@1296 650 wiki_page = link_project.wiki.find_page(page)
Chris@1296 651 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
Chris@1296 652 "##{anchor}"
Chris@1296 653 else
Chris@1296 654 case options[:wiki_links]
Chris@1296 655 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
Chris@1296 656 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
Chris@1296 657 else
Chris@1296 658 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
Chris@1296 659 parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil
Chris@1296 660 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project,
Chris@1296 661 :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent)
Chris@1296 662 end
Chris@1296 663 end
Chris@1296 664 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
Chris@1296 665 else
Chris@1296 666 # project or wiki doesn't exist
Chris@1296 667 all
Chris@1296 668 end
Chris@1296 669 else
Chris@1296 670 all
Chris@1296 671 end
Chris@1296 672 end
Chris@1296 673 end
Chris@1296 674
Chris@1296 675 # Redmine links
Chris@1296 676 #
Chris@1296 677 # Examples:
Chris@1296 678 # Issues:
Chris@1296 679 # #52 -> Link to issue #52
Chris@1296 680 # Changesets:
Chris@1296 681 # r52 -> Link to revision 52
Chris@1296 682 # commit:a85130f -> Link to scmid starting with a85130f
Chris@1296 683 # Documents:
Chris@1296 684 # document#17 -> Link to document with id 17
Chris@1296 685 # document:Greetings -> Link to the document with title "Greetings"
Chris@1296 686 # document:"Some document" -> Link to the document with title "Some document"
Chris@1296 687 # Versions:
Chris@1296 688 # version#3 -> Link to version with id 3
Chris@1296 689 # version:1.0.0 -> Link to version named "1.0.0"
Chris@1296 690 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
Chris@1296 691 # Attachments:
Chris@1296 692 # attachment:file.zip -> Link to the attachment of the current object named file.zip
Chris@1296 693 # Source files:
Chris@1296 694 # source:some/file -> Link to the file located at /some/file in the project's repository
Chris@1296 695 # source:some/file@52 -> Link to the file's revision 52
Chris@1296 696 # source:some/file#L120 -> Link to line 120 of the file
Chris@1296 697 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
Chris@1296 698 # export:some/file -> Force the download of the file
Chris@1296 699 # Forum messages:
Chris@1296 700 # message#1218 -> Link to message with id 1218
Chris@1296 701 #
Chris@1296 702 # Links can refer other objects from other projects, using project identifier:
Chris@1296 703 # identifier:r52
Chris@1296 704 # identifier:document:"Some document"
Chris@1296 705 # identifier:version:1.0.0
Chris@1296 706 # identifier:source:some/file
Chris@1296 707 def parse_redmine_links(text, default_project, obj, attr, only_path, options)
Chris@1296 708 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 709 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 710 link = nil
Chris@1296 711 project = default_project
Chris@1296 712 if project_identifier
Chris@1296 713 project = Project.visible.find_by_identifier(project_identifier)
Chris@1296 714 end
Chris@1296 715 if esc.nil?
Chris@1296 716 if prefix.nil? && sep == 'r'
Chris@1296 717 if project
Chris@1296 718 repository = nil
Chris@1296 719 if repo_identifier
Chris@1296 720 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
Chris@1296 721 else
Chris@1296 722 repository = project.repository
Chris@1296 723 end
Chris@1296 724 # project.changesets.visible raises an SQL error because of a double join on repositories
Chris@1296 725 if repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(repository.id, identifier))
Chris@1296 726 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 727 :class => 'changeset',
Chris@1296 728 :title => truncate_single_line(changeset.comments, :length => 100))
Chris@1296 729 end
Chris@1296 730 end
Chris@1296 731 elsif sep == '#'
Chris@1296 732 oid = identifier.to_i
Chris@1296 733 case prefix
Chris@1296 734 when nil
Chris@1296 735 if oid.to_s == identifier && issue = Issue.visible.find_by_id(oid, :include => :status)
Chris@1296 736 anchor = comment_id ? "note-#{comment_id}" : nil
Chris@1296 737 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor},
Chris@1296 738 :class => issue.css_classes,
Chris@1296 739 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
Chris@1296 740 end
Chris@1296 741 when 'document'
Chris@1296 742 if document = Document.visible.find_by_id(oid)
Chris@1296 743 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@1296 744 :class => 'document'
Chris@1296 745 end
Chris@1296 746 when 'version'
Chris@1296 747 if version = Version.visible.find_by_id(oid)
Chris@1296 748 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@1296 749 :class => 'version'
Chris@1296 750 end
Chris@1296 751 when 'message'
Chris@1296 752 if message = Message.visible.find_by_id(oid, :include => :parent)
Chris@1296 753 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
Chris@1296 754 end
Chris@1296 755 when 'forum'
Chris@1296 756 if board = Board.visible.find_by_id(oid)
Chris@1296 757 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@1296 758 :class => 'board'
Chris@1296 759 end
Chris@1296 760 when 'news'
Chris@1296 761 if news = News.visible.find_by_id(oid)
Chris@1296 762 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@1296 763 :class => 'news'
Chris@1296 764 end
Chris@1296 765 when 'project'
Chris@1296 766 if p = Project.visible.find_by_id(oid)
Chris@1296 767 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@1296 768 end
Chris@1296 769 end
Chris@1296 770 elsif sep == ':'
Chris@1296 771 # removes the double quotes if any
Chris@1296 772 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
Chris@1296 773 case prefix
Chris@1296 774 when 'document'
Chris@1296 775 if project && document = project.documents.visible.find_by_title(name)
Chris@1296 776 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@1296 777 :class => 'document'
Chris@1296 778 end
Chris@1296 779 when 'version'
Chris@1296 780 if project && version = project.versions.visible.find_by_name(name)
Chris@1296 781 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@1296 782 :class => 'version'
Chris@1296 783 end
Chris@1296 784 when 'forum'
Chris@1296 785 if project && board = project.boards.visible.find_by_name(name)
Chris@1296 786 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@1296 787 :class => 'board'
Chris@1296 788 end
Chris@1296 789 when 'news'
Chris@1296 790 if project && news = project.news.visible.find_by_title(name)
Chris@1296 791 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@1296 792 :class => 'news'
Chris@1296 793 end
Chris@1296 794 when 'commit', 'source', 'export'
Chris@1296 795 if project
Chris@1296 796 repository = nil
Chris@1296 797 if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$}
Chris@1296 798 repo_prefix, repo_identifier, name = $1, $2, $3
Chris@1296 799 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
Chris@1296 800 else
Chris@1296 801 repository = project.repository
Chris@1296 802 end
Chris@1296 803 if prefix == 'commit'
Chris@1296 804 if repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%"]))
Chris@1296 805 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 806 :class => 'changeset',
Chris@1296 807 :title => truncate_single_line(h(changeset.comments), :length => 100)
Chris@1296 808 end
Chris@1296 809 else
Chris@1296 810 if repository && User.current.allowed_to?(:browse_repository, project)
Chris@1296 811 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
Chris@1296 812 path, rev, anchor = $1, $3, $5
Chris@1296 813 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 814 :path => to_path_param(path),
Chris@1296 815 :rev => rev,
Chris@1296 816 :anchor => anchor},
Chris@1296 817 :class => (prefix == 'export' ? 'source download' : 'source')
Chris@1296 818 end
Chris@1296 819 end
Chris@1296 820 repo_prefix = nil
Chris@1296 821 end
Chris@1296 822 when 'attachment'
Chris@1296 823 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
Chris@1296 824 if attachments && attachment = Attachment.latest_attach(attachments, name)
Chris@1296 825 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
Chris@1296 826 :class => 'attachment'
Chris@1296 827 end
Chris@1296 828 when 'project'
Chris@1296 829 if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
Chris@1296 830 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@1296 831 end
Chris@1296 832 end
Chris@1296 833 end
Chris@1296 834 end
Chris@1296 835 (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
Chris@1296 836 end
Chris@1296 837 end
Chris@1296 838
Chris@1296 839 HEADING_RE = /(<h(\d)( [^>]+)?>(.+?)<\/h(\d)>)/i unless const_defined?(:HEADING_RE)
Chris@1296 840
Chris@1296 841 def parse_sections(text, project, obj, attr, only_path, options)
Chris@1296 842 return unless options[:edit_section_links]
Chris@1296 843 text.gsub!(HEADING_RE) do
Chris@1296 844 heading = $1
Chris@1296 845 @current_section += 1
Chris@1296 846 if @current_section > 1
Chris@1296 847 content_tag('div',
Chris@1296 848 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
Chris@1296 849 :class => 'contextual',
Chris@1296 850 :title => l(:button_edit_section)) + heading.html_safe
Chris@1296 851 else
Chris@1296 852 heading
Chris@1296 853 end
Chris@1296 854 end
Chris@1296 855 end
Chris@1296 856
Chris@1296 857 # Headings and TOC
Chris@1296 858 # Adds ids and links to headings unless options[:headings] is set to false
Chris@1296 859 def parse_headings(text, project, obj, attr, only_path, options)
Chris@1296 860 return if options[:headings] == false
Chris@1296 861
Chris@1296 862 text.gsub!(HEADING_RE) do
Chris@1296 863 level, attrs, content = $2.to_i, $3, $4
Chris@1296 864 item = strip_tags(content).strip
Chris@1296 865 anchor = sanitize_anchor_name(item)
Chris@1296 866 # used for single-file wiki export
Chris@1296 867 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
Chris@1296 868 @heading_anchors[anchor] ||= 0
Chris@1296 869 idx = (@heading_anchors[anchor] += 1)
Chris@1296 870 if idx > 1
Chris@1296 871 anchor = "#{anchor}-#{idx}"
Chris@1296 872 end
Chris@1296 873 @parsed_headings << [level, anchor, item]
Chris@1296 874 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
Chris@1296 875 end
Chris@1296 876 end
Chris@1296 877
Chris@1296 878 MACROS_RE = /(
Chris@1296 879 (!)? # escaping
Chris@1296 880 (
Chris@1296 881 \{\{ # opening tag
Chris@1296 882 ([\w]+) # macro name
Chris@1296 883 (\(([^\n\r]*?)\))? # optional arguments
Chris@1296 884 ([\n\r].*?[\n\r])? # optional block of text
Chris@1296 885 \}\} # closing tag
Chris@1296 886 )
Chris@1296 887 )/mx unless const_defined?(:MACROS_RE)
Chris@1296 888
Chris@1296 889 MACRO_SUB_RE = /(
Chris@1296 890 \{\{
Chris@1296 891 macro\((\d+)\)
Chris@1296 892 \}\}
Chris@1296 893 )/x unless const_defined?(:MACRO_SUB_RE)
Chris@1296 894
Chris@1296 895 # Extracts macros from text
Chris@1296 896 def catch_macros(text)
Chris@1296 897 macros = {}
Chris@1296 898 text.gsub!(MACROS_RE) do
Chris@1296 899 all, macro = $1, $4.downcase
Chris@1296 900 if macro_exists?(macro) || all =~ MACRO_SUB_RE
Chris@1296 901 index = macros.size
Chris@1296 902 macros[index] = all
Chris@1296 903 "{{macro(#{index})}}"
Chris@1296 904 else
Chris@1296 905 all
Chris@1296 906 end
Chris@1296 907 end
Chris@1296 908 macros
Chris@1296 909 end
Chris@1296 910
Chris@1296 911 # Executes and replaces macros in text
Chris@1296 912 def inject_macros(text, obj, macros, execute=true)
Chris@1296 913 text.gsub!(MACRO_SUB_RE) do
Chris@1296 914 all, index = $1, $2.to_i
Chris@1296 915 orig = macros.delete(index)
Chris@1296 916 if execute && orig && orig =~ MACROS_RE
Chris@1296 917 esc, all, macro, args, block = $2, $3, $4.downcase, $6.to_s, $7.try(:strip)
Chris@1296 918 if esc.nil?
Chris@1296 919 h(exec_macro(macro, obj, args, block) || all)
Chris@1296 920 else
Chris@1296 921 h(all)
Chris@1296 922 end
Chris@1296 923 elsif orig
Chris@1296 924 h(orig)
Chris@1296 925 else
Chris@1296 926 h(all)
Chris@1296 927 end
Chris@1296 928 end
Chris@1296 929 end
Chris@1296 930
Chris@1296 931 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
Chris@1296 932
Chris@1296 933 # Renders the TOC with given headings
Chris@1296 934 def replace_toc(text, headings)
Chris@1296 935 text.gsub!(TOC_RE) do
Chris@1296 936 # Keep only the 4 first levels
Chris@1296 937 headings = headings.select{|level, anchor, item| level <= 4}
Chris@1296 938 if headings.empty?
Chris@1296 939 ''
Chris@1296 940 else
Chris@1296 941 div_class = 'toc'
Chris@1296 942 div_class << ' right' if $1 == '>'
Chris@1296 943 div_class << ' left' if $1 == '<'
Chris@1296 944 out = "<ul class=\"#{div_class}\"><li>"
Chris@1296 945 root = headings.map(&:first).min
Chris@1296 946 current = root
Chris@1296 947 started = false
Chris@1296 948 headings.each do |level, anchor, item|
Chris@1296 949 if level > current
Chris@1296 950 out << '<ul><li>' * (level - current)
Chris@1296 951 elsif level < current
Chris@1296 952 out << "</li></ul>\n" * (current - level) + "</li><li>"
Chris@1296 953 elsif started
Chris@1296 954 out << '</li><li>'
Chris@1296 955 end
Chris@1296 956 out << "<a href=\"##{anchor}\">#{item}</a>"
Chris@1296 957 current = level
Chris@1296 958 started = true
Chris@1296 959 end
Chris@1296 960 out << '</li></ul>' * (current - root)
Chris@1296 961 out << '</li></ul>'
Chris@1296 962 end
Chris@1296 963 end
Chris@1296 964 end
Chris@1296 965
Chris@1296 966 # Same as Rails' simple_format helper without using paragraphs
Chris@1296 967 def simple_format_without_paragraph(text)
Chris@1296 968 text.to_s.
Chris@1296 969 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
Chris@1296 970 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
Chris@1296 971 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
Chris@1296 972 html_safe
Chris@1296 973 end
Chris@1296 974
Chris@1296 975 def lang_options_for_select(blank=true)
Chris@1296 976 (blank ? [["(auto)", ""]] : []) + languages_options
Chris@1296 977 end
Chris@1296 978
Chris@1296 979 def label_tag_for(name, option_tags = nil, options = {})
Chris@1296 980 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
Chris@1296 981 content_tag("label", label_text)
Chris@1296 982 end
Chris@1296 983
Chris@1296 984 def labelled_form_for(*args, &proc)
Chris@1296 985 args << {} unless args.last.is_a?(Hash)
Chris@1296 986 options = args.last
Chris@1296 987 if args.first.is_a?(Symbol)
Chris@1296 988 options.merge!(:as => args.shift)
Chris@1296 989 end
Chris@1296 990 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
Chris@1296 991 form_for(*args, &proc)
Chris@1296 992 end
Chris@1296 993
Chris@1296 994 def labelled_fields_for(*args, &proc)
Chris@1296 995 args << {} unless args.last.is_a?(Hash)
Chris@1296 996 options = args.last
Chris@1296 997 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
Chris@1296 998 fields_for(*args, &proc)
Chris@1296 999 end
Chris@1296 1000
Chris@1296 1001 def labelled_remote_form_for(*args, &proc)
Chris@1296 1002 ActiveSupport::Deprecation.warn "ApplicationHelper#labelled_remote_form_for is deprecated and will be removed in Redmine 2.2."
Chris@1296 1003 args << {} unless args.last.is_a?(Hash)
Chris@1296 1004 options = args.last
Chris@1296 1005 options.merge!({:builder => Redmine::Views::LabelledFormBuilder, :remote => true})
Chris@1296 1006 form_for(*args, &proc)
Chris@1296 1007 end
Chris@1296 1008
Chris@1296 1009 def error_messages_for(*objects)
Chris@1296 1010 html = ""
Chris@1296 1011 objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
Chris@1296 1012 errors = objects.map {|o| o.errors.full_messages}.flatten
Chris@1296 1013 if errors.any?
Chris@1296 1014 html << "<div id='errorExplanation'><ul>\n"
Chris@1296 1015 errors.each do |error|
Chris@1296 1016 html << "<li>#{h error}</li>\n"
Chris@1296 1017 end
Chris@1296 1018 html << "</ul></div>\n"
Chris@1296 1019 end
Chris@1296 1020 html.html_safe
Chris@1296 1021 end
Chris@1296 1022
Chris@1296 1023 def delete_link(url, options={})
Chris@1296 1024 options = {
Chris@1296 1025 :method => :delete,
Chris@1296 1026 :data => {:confirm => l(:text_are_you_sure)},
Chris@1296 1027 :class => 'icon icon-del'
Chris@1296 1028 }.merge(options)
Chris@1296 1029
Chris@1296 1030 link_to l(:button_delete), url, options
Chris@1296 1031 end
Chris@1296 1032
Chris@1296 1033 def preview_link(url, form, target='preview', options={})
Chris@1296 1034 content_tag 'a', l(:label_preview), {
Chris@1296 1035 :href => "#",
Chris@1296 1036 :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|,
Chris@1296 1037 :accesskey => accesskey(:preview)
Chris@1296 1038 }.merge(options)
Chris@1296 1039 end
Chris@1296 1040
Chris@1296 1041 def link_to_function(name, function, html_options={})
Chris@1296 1042 content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options))
Chris@1296 1043 end
Chris@1296 1044
Chris@1296 1045 # Helper to render JSON in views
Chris@1296 1046 def raw_json(arg)
Chris@1296 1047 arg.to_json.to_s.gsub('/', '\/').html_safe
Chris@1296 1048 end
Chris@1296 1049
Chris@1296 1050 def back_url
Chris@1296 1051 url = params[:back_url]
Chris@1296 1052 if url.nil? && referer = request.env['HTTP_REFERER']
Chris@1296 1053 url = CGI.unescape(referer.to_s)
Chris@1296 1054 end
Chris@1296 1055 url
Chris@1296 1056 end
Chris@1296 1057
Chris@1296 1058 def back_url_hidden_field_tag
Chris@1296 1059 url = back_url
Chris@1296 1060 hidden_field_tag('back_url', url, :id => nil) unless url.blank?
Chris@1296 1061 end
Chris@1296 1062
Chris@1296 1063 def check_all_links(form_name)
Chris@1296 1064 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
Chris@1296 1065 " | ".html_safe +
Chris@1296 1066 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
Chris@1296 1067 end
Chris@1296 1068
Chris@1296 1069 def progress_bar(pcts, options={})
Chris@1296 1070 pcts = [pcts, pcts] unless pcts.is_a?(Array)
Chris@1296 1071 pcts = pcts.collect(&:round)
Chris@1296 1072 pcts[1] = pcts[1] - pcts[0]
Chris@1296 1073 pcts << (100 - pcts[1] - pcts[0])
Chris@1296 1074 width = options[:width] || '100px;'
Chris@1296 1075 legend = options[:legend] || ''
Chris@1296 1076 content_tag('table',
Chris@1296 1077 content_tag('tr',
Chris@1296 1078 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
Chris@1296 1079 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
Chris@1296 1080 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
Chris@1296 1081 ), :class => 'progress', :style => "width: #{width};").html_safe +
Chris@1296 1082 content_tag('p', legend, :class => 'pourcent').html_safe
Chris@1296 1083 end
Chris@1296 1084
Chris@1296 1085 def checked_image(checked=true)
Chris@1296 1086 if checked
Chris@1296 1087 image_tag 'toggle_check.png'
Chris@1296 1088 end
Chris@1296 1089 end
Chris@1296 1090
Chris@1296 1091 def context_menu(url)
Chris@1296 1092 unless @context_menu_included
Chris@1296 1093 content_for :header_tags do
Chris@1296 1094 javascript_include_tag('context_menu') +
Chris@1296 1095 stylesheet_link_tag('context_menu')
Chris@1296 1096 end
Chris@1296 1097 if l(:direction) == 'rtl'
Chris@1296 1098 content_for :header_tags do
Chris@1296 1099 stylesheet_link_tag('context_menu_rtl')
Chris@1296 1100 end
Chris@1296 1101 end
Chris@1296 1102 @context_menu_included = true
Chris@1296 1103 end
Chris@1296 1104 javascript_tag "contextMenuInit('#{ url_for(url) }')"
Chris@1296 1105 end
Chris@1296 1106
Chris@1296 1107 def calendar_for(field_id)
Chris@1296 1108 include_calendar_headers_tags
Chris@1296 1109 javascript_tag("$(function() { $('##{field_id}').datepicker(datepickerOptions); });")
Chris@1296 1110 end
Chris@1296 1111
Chris@1296 1112 def include_calendar_headers_tags
Chris@1296 1113 unless @calendar_headers_tags_included
Chris@1296 1114 @calendar_headers_tags_included = true
Chris@1296 1115 content_for :header_tags do
Chris@1296 1116 start_of_week = Setting.start_of_week
Chris@1296 1117 start_of_week = l(:general_first_day_of_week, :default => '1') if start_of_week.blank?
Chris@1296 1118 # Redmine uses 1..7 (monday..sunday) in settings and locales
Chris@1296 1119 # JQuery uses 0..6 (sunday..saturday), 7 needs to be changed to 0
Chris@1296 1120 start_of_week = start_of_week.to_i % 7
Chris@1296 1121
Chris@1296 1122 tags = javascript_tag(
Chris@1296 1123 "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " +
Chris@1296 1124 "showOn: 'button', buttonImageOnly: true, buttonImage: '" +
Chris@1296 1125 path_to_image('/images/calendar.png') +
Chris@1296 1126 "', showButtonPanel: true};")
Chris@1296 1127 jquery_locale = l('jquery.locale', :default => current_language.to_s)
Chris@1296 1128 unless jquery_locale == 'en'
Chris@1296 1129 tags << javascript_include_tag("i18n/jquery.ui.datepicker-#{jquery_locale}.js")
Chris@1296 1130 end
Chris@1296 1131 tags
Chris@1296 1132 end
Chris@1296 1133 end
Chris@1296 1134 end
Chris@1296 1135
Chris@1296 1136 # Overrides Rails' stylesheet_link_tag with themes and plugins support.
Chris@1296 1137 # Examples:
Chris@1296 1138 # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults
Chris@1296 1139 # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets
Chris@1296 1140 #
Chris@1296 1141 def stylesheet_link_tag(*sources)
Chris@1296 1142 options = sources.last.is_a?(Hash) ? sources.pop : {}
Chris@1296 1143 plugin = options.delete(:plugin)
Chris@1296 1144 sources = sources.map do |source|
Chris@1296 1145 if plugin
Chris@1296 1146 "/plugin_assets/#{plugin}/stylesheets/#{source}"
Chris@1296 1147 elsif current_theme && current_theme.stylesheets.include?(source)
Chris@1296 1148 current_theme.stylesheet_path(source)
Chris@1296 1149 else
Chris@1296 1150 source
Chris@1296 1151 end
Chris@1296 1152 end
Chris@1296 1153 super sources, options
Chris@1296 1154 end
Chris@1296 1155
Chris@1296 1156 # Overrides Rails' image_tag with themes and plugins support.
Chris@1296 1157 # Examples:
Chris@1296 1158 # image_tag('image.png') # => picks image.png from the current theme or defaults
Chris@1296 1159 # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets
Chris@1296 1160 #
Chris@1296 1161 def image_tag(source, options={})
Chris@1296 1162 if plugin = options.delete(:plugin)
Chris@1296 1163 source = "/plugin_assets/#{plugin}/images/#{source}"
Chris@1296 1164 elsif current_theme && current_theme.images.include?(source)
Chris@1296 1165 source = current_theme.image_path(source)
Chris@1296 1166 end
Chris@1296 1167 super source, options
Chris@1296 1168 end
Chris@1296 1169
Chris@1296 1170 # Overrides Rails' javascript_include_tag with plugins support
Chris@1296 1171 # Examples:
Chris@1296 1172 # javascript_include_tag('scripts') # => picks scripts.js from defaults
Chris@1296 1173 # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets
Chris@1296 1174 #
Chris@1296 1175 def javascript_include_tag(*sources)
Chris@1296 1176 options = sources.last.is_a?(Hash) ? sources.pop : {}
Chris@1296 1177 if plugin = options.delete(:plugin)
Chris@1296 1178 sources = sources.map do |source|
Chris@1296 1179 if plugin
Chris@1296 1180 "/plugin_assets/#{plugin}/javascripts/#{source}"
Chris@1296 1181 else
Chris@1296 1182 source
Chris@1296 1183 end
Chris@1296 1184 end
Chris@1296 1185 end
Chris@1296 1186 super sources, options
Chris@1296 1187 end
Chris@1296 1188
Chris@1296 1189 def content_for(name, content = nil, &block)
Chris@1296 1190 @has_content ||= {}
Chris@1296 1191 @has_content[name] = true
Chris@1296 1192 super(name, content, &block)
Chris@1296 1193 end
Chris@1296 1194
Chris@1296 1195 def has_content?(name)
Chris@1296 1196 (@has_content && @has_content[name]) || false
Chris@1296 1197 end
Chris@1296 1198
Chris@1296 1199 def sidebar_content?
Chris@1296 1200 has_content?(:sidebar) || view_layouts_base_sidebar_hook_response.present?
Chris@1296 1201 end
Chris@1296 1202
Chris@1296 1203 def view_layouts_base_sidebar_hook_response
Chris@1296 1204 @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar)
Chris@1296 1205 end
Chris@1296 1206
Chris@1296 1207 def email_delivery_enabled?
Chris@1296 1208 !!ActionMailer::Base.perform_deliveries
Chris@1296 1209 end
Chris@1296 1210
Chris@1296 1211 # Returns the avatar image tag for the given +user+ if avatars are enabled
Chris@1296 1212 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
Chris@1296 1213 def avatar(user, options = { })
Chris@1296 1214 if Setting.gravatar_enabled?
Chris@1296 1215 options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default})
Chris@1296 1216 email = nil
Chris@1296 1217 if user.respond_to?(:mail)
Chris@1296 1218 email = user.mail
Chris@1296 1219 elsif user.to_s =~ %r{<(.+?)>}
Chris@1296 1220 email = $1
Chris@1296 1221 end
Chris@1296 1222 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
Chris@1296 1223 else
Chris@1296 1224 ''
Chris@1296 1225 end
Chris@1296 1226 end
Chris@1296 1227
Chris@1296 1228 def sanitize_anchor_name(anchor)
Chris@1296 1229 if ''.respond_to?(:encoding) || RUBY_PLATFORM == 'java'
Chris@1296 1230 anchor.gsub(%r{[^\p{Word}\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@1296 1231 else
Chris@1296 1232 # TODO: remove when ruby1.8 is no longer supported
Chris@1296 1233 anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@1296 1234 end
Chris@1296 1235 end
Chris@1296 1236
Chris@1296 1237 # Returns the javascript tags that are included in the html layout head
Chris@1296 1238 def javascript_heads
Chris@1296 1239 tags = javascript_include_tag('jquery-1.7.2-ui-1.8.21-ujs-2.0.3', 'application')
Chris@1296 1240 unless User.current.pref.warn_on_leaving_unsaved == '0'
Chris@1296 1241 tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });")
Chris@1296 1242 end
Chris@1296 1243 tags
Chris@1296 1244 end
Chris@1296 1245
Chris@1296 1246 def favicon
Chris@1296 1247 "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />".html_safe
Chris@1296 1248 end
Chris@1296 1249
Chris@1296 1250 def robot_exclusion_tag
Chris@1296 1251 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
Chris@1296 1252 end
Chris@1296 1253
Chris@1296 1254 # Returns true if arg is expected in the API response
Chris@1296 1255 def include_in_api_response?(arg)
Chris@1296 1256 unless @included_in_api_response
Chris@1296 1257 param = params[:include]
Chris@1296 1258 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
Chris@1296 1259 @included_in_api_response.collect!(&:strip)
Chris@1296 1260 end
Chris@1296 1261 @included_in_api_response.include?(arg.to_s)
Chris@1296 1262 end
Chris@1296 1263
Chris@1296 1264 # Returns options or nil if nometa param or X-Redmine-Nometa header
Chris@1296 1265 # was set in the request
Chris@1296 1266 def api_meta(options)
Chris@1296 1267 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
Chris@1296 1268 # compatibility mode for activeresource clients that raise
Chris@1296 1269 # an error when unserializing an array with attributes
Chris@1296 1270 nil
Chris@1296 1271 else
Chris@1296 1272 options
Chris@1296 1273 end
Chris@1296 1274 end
Chris@1296 1275
Chris@1296 1276 private
Chris@1296 1277
Chris@1296 1278 def wiki_helper
Chris@1296 1279 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@1296 1280 extend helper
Chris@1296 1281 return self
Chris@1296 1282 end
Chris@1296 1283
Chris@1296 1284 def link_to_content_update(text, url_params = {}, html_options = {})
Chris@1296 1285 link_to(text, url_params, html_options)
Chris@1296 1286 end
Chris@1296 1287 end