comparison app/helpers/application_helper.rb @ 37:94944d00e43c

* Update to SVN trunk rev 4411
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 19 Nov 2010 13:24:41 +0000
parents 40f7cfd4df19
children 33d69fee1d99 af80e5618e9b 8661b858af72
comparison
equal deleted inserted replaced
22:40f7cfd4df19 37:94944d00e43c
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang 2 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
32 end 32 end
33 33
34 # Display a link if user is authorized 34 # Display a link if user is authorized
35 # 35 #
36 # @param [String] name Anchor text (passed to link_to) 36 # @param [String] name Anchor text (passed to link_to)
37 # @param [Hash, String] options Hash params or url for the link target (passed to link_to). 37 # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
38 # This will checked by authorize_for to see if the user is authorized
39 # @param [optional, Hash] html_options Options passed to link_to 38 # @param [optional, Hash] html_options Options passed to link_to
40 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to 39 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
41 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) 40 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
42 if options.is_a?(String) 41 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
43 begin
44 route = ActionController::Routing::Routes.recognize_path(options.gsub(/\?.*/,''), :method => options[:method] || :get)
45 link_controller = route[:controller]
46 link_action = route[:action]
47 rescue ActionController::RoutingError # Parse failed, not a route
48 link_controller, link_action = nil, nil
49 end
50 else
51 link_controller = options[:controller] || params[:controller]
52 link_action = options[:action]
53 end
54
55 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(link_controller, link_action)
56 end 42 end
57 43
58 # Display a link to remote if user is authorized 44 # Display a link to remote if user is authorized
59 def link_to_remote_if_authorized(name, options = {}, html_options = nil) 45 def link_to_remote_if_authorized(name, options = {}, html_options = nil)
60 url = options[:url] || {} 46 url = options[:url] || {}
119 def link_to_revision(revision, project, options={}) 105 def link_to_revision(revision, project, options={})
120 text = options.delete(:text) || format_revision(revision) 106 text = options.delete(:text) || format_revision(revision)
121 107
122 link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision)) 108 link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision))
123 end 109 end
124
125 def link_to_project(project, options={})
126 options[:class] ||= 'project'
127 link_to(h(project), {:controller => 'projects', :action => 'show', :id => project}, :class => options[:class])
128 end
129 110
130 # Generates a link to a project if active 111 # Generates a link to a project if active
131 # Examples: 112 # Examples:
132 # 113 #
133 # link_to_project(project) # => link to the specified project overview 114 # link_to_project(project) # => link to the specified project overview
194 content = '' 175 content = ''
195 if pages[node] 176 if pages[node]
196 content << "<ul class=\"pages-hierarchy\">\n" 177 content << "<ul class=\"pages-hierarchy\">\n"
197 pages[node].each do |page| 178 pages[node].each do |page|
198 content << "<li>" 179 content << "<li>"
199 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title}, 180 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title},
200 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil)) 181 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
201 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id] 182 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id]
202 content << "</li>\n" 183 content << "</li>\n"
203 end 184 end
204 content << "</ul>\n" 185 content << "</ul>\n"
255 end 236 end
256 s 237 s
257 end 238 end
258 239
259 # Yields the given block for each project with its level in the tree 240 # Yields the given block for each project with its level in the tree
241 #
242 # Wrapper for Project#project_tree
260 def project_tree(projects, &block) 243 def project_tree(projects, &block)
261 ancestors = [] 244 Project.project_tree(projects, &block)
262 projects.sort_by(&:lft).each do |project|
263 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
264 ancestors.pop
265 end
266 yield project, ancestors.size
267 ancestors << project
268 end
269 end 245 end
270 246
271 def project_nested_ul(projects, &block) 247 def project_nested_ul(projects, &block)
272 s = '' 248 s = ''
273 if projects.any? 249 if projects.any?
473 only_path = options.delete(:only_path) == false ? false : true 449 only_path = options.delete(:only_path) == false ? false : true
474 450
475 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } 451 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
476 452
477 parse_non_pre_blocks(text) do |text| 453 parse_non_pre_blocks(text) do |text|
478 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| 454 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings].each do |method_name|
479 send method_name, text, project, obj, attr, only_path, options 455 send method_name, text, project, obj, attr, only_path, options
480 end 456 end
481 end 457 end
482 end 458 end
483 459
546 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| 522 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
547 link_project = project 523 link_project = project
548 esc, all, page, title = $1, $2, $3, $5 524 esc, all, page, title = $1, $2, $3, $5
549 if esc.nil? 525 if esc.nil?
550 if page =~ /^([^\:]+)\:(.*)$/ 526 if page =~ /^([^\:]+)\:(.*)$/
551 link_project = Project.find_by_name($1) || Project.find_by_identifier($1) 527 link_project = Project.find_by_identifier($1) || Project.find_by_name($1)
552 page = $2 528 page = $2
553 title ||= $1 if page.blank? 529 title ||= $1 if page.blank?
554 end 530 end
555 531
556 if link_project && link_project.wiki 532 if link_project && link_project.wiki
563 wiki_page = link_project.wiki.find_page(page) 539 wiki_page = link_project.wiki.find_page(page)
564 url = case options[:wiki_links] 540 url = case options[:wiki_links]
565 when :local; "#{title}.html" 541 when :local; "#{title}.html"
566 when :anchor; "##{title}" # used for single-file wiki export 542 when :anchor; "##{title}" # used for single-file wiki export
567 else 543 else
568 url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => link_project, :page => Wiki.titleize(page), :anchor => anchor) 544 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
545 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor)
569 end 546 end
570 link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) 547 link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
571 else 548 else
572 # project or wiki doesn't exist 549 # project or wiki doesn't exist
573 all 550 all
694 end 671 end
695 end 672 end
696 leading + (link || "#{prefix}#{sep}#{identifier}") 673 leading + (link || "#{prefix}#{sep}#{identifier}")
697 end 674 end
698 end 675 end
676
677 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
678 HEADING_RE = /<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>/i unless const_defined?(:HEADING_RE)
679
680 # Headings and TOC
681 # Adds ids and links to headings and renders the TOC if needed unless options[:headings] is set to false
682 def parse_headings(text, project, obj, attr, only_path, options)
683 headings = []
684 text.gsub!(HEADING_RE) do
685 level, attrs, content = $1.to_i, $2, $3
686 item = strip_tags(content).strip
687 anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
688 headings << [level, anchor, item]
689 "<h#{level} #{attrs} id=\"#{anchor}\">#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
690 end unless options[:headings] == false
691
692 text.gsub!(TOC_RE) do
693 if headings.empty?
694 ''
695 else
696 div_class = 'toc'
697 div_class << ' right' if $1 == '>'
698 div_class << ' left' if $1 == '<'
699 out = "<ul class=\"#{div_class}\"><li>"
700 root = headings.map(&:first).min
701 current = root
702 started = false
703 headings.each do |level, anchor, item|
704 if level > current
705 out << '<ul><li>' * (level - current)
706 elsif level < current
707 out << "</li></ul>\n" * (current - level) + "</li><li>"
708 elsif started
709 out << '</li><li>'
710 end
711 out << "<a href=\"##{anchor}\">#{item}</a>"
712 current = level
713 started = true
714 end
715 out << '</li></ul>' * (current - root)
716 out << '</li></ul>'
717 end
718 end
719 end
699 720
700 # Same as Rails' simple_format helper without using paragraphs 721 # Same as Rails' simple_format helper without using paragraphs
701 def simple_format_without_paragraph(text) 722 def simple_format_without_paragraph(text)
702 text.to_s. 723 text.to_s.
703 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n 724 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n