Mercurial > hg > soundsoftware-site
comparison app/helpers/application_helper.rb @ 51:371eac10df0b live
* Merge from "cannam" branch. This includes update to Redmine trunk, plus welcome/tip-of-the-day from luisf branch.
author | Chris Cannam |
---|---|
date | Wed, 24 Nov 2010 12:20:08 +0000 |
parents | 33d69fee1d99 |
children | 3e75f003034a b859cc0c4fa1 6a2f8e88344e |
comparison
equal
deleted
inserted
replaced
34:09b1d4349da3 | 51:371eac10df0b |
---|---|
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] || {} |
121 rev = revision.respond_to?(:identifier) ? revision.identifier : revision | 107 rev = revision.respond_to?(:identifier) ? revision.identifier : revision |
122 | 108 |
123 link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => rev}, | 109 link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => rev}, |
124 :title => l(:label_revision_id, format_revision(revision))) | 110 :title => l(:label_revision_id, format_revision(revision))) |
125 end | 111 end |
126 | |
127 def link_to_project(project, options={}) | |
128 options[:class] ||= 'project' | |
129 link_to(h(project), {:controller => 'projects', :action => 'show', :id => project}, :class => options[:class]) | |
130 end | |
131 | 112 |
132 # Generates a link to a project if active | 113 # Generates a link to a project if active |
133 # Examples: | 114 # Examples: |
134 # | 115 # |
135 # link_to_project(project) # => link to the specified project overview | 116 # link_to_project(project) # => link to the specified project overview |
196 content = '' | 177 content = '' |
197 if pages[node] | 178 if pages[node] |
198 content << "<ul class=\"pages-hierarchy\">\n" | 179 content << "<ul class=\"pages-hierarchy\">\n" |
199 pages[node].each do |page| | 180 pages[node].each do |page| |
200 content << "<li>" | 181 content << "<li>" |
201 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title}, | 182 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}, |
202 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil)) | 183 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil)) |
203 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id] | 184 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id] |
204 content << "</li>\n" | 185 content << "</li>\n" |
205 end | 186 end |
206 content << "</ul>\n" | 187 content << "</ul>\n" |
257 end | 238 end |
258 s | 239 s |
259 end | 240 end |
260 | 241 |
261 # Yields the given block for each project with its level in the tree | 242 # Yields the given block for each project with its level in the tree |
243 # | |
244 # Wrapper for Project#project_tree | |
262 def project_tree(projects, &block) | 245 def project_tree(projects, &block) |
263 ancestors = [] | 246 Project.project_tree(projects, &block) |
264 projects.sort_by(&:lft).each do |project| | |
265 while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) | |
266 ancestors.pop | |
267 end | |
268 yield project, ancestors.size | |
269 ancestors << project | |
270 end | |
271 end | 247 end |
272 | 248 |
273 def project_nested_ul(projects, &block) | 249 def project_nested_ul(projects, &block) |
274 s = '' | 250 s = '' |
275 if projects.any? | 251 if projects.any? |
475 only_path = options.delete(:only_path) == false ? false : true | 451 only_path = options.delete(:only_path) == false ? false : true |
476 | 452 |
477 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } | 453 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } |
478 | 454 |
479 parse_non_pre_blocks(text) do |text| | 455 parse_non_pre_blocks(text) do |text| |
480 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| | 456 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings].each do |method_name| |
481 send method_name, text, project, obj, attr, only_path, options | 457 send method_name, text, project, obj, attr, only_path, options |
482 end | 458 end |
483 end | 459 end |
484 end | 460 end |
485 | 461 |
548 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| | 524 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| |
549 link_project = project | 525 link_project = project |
550 esc, all, page, title = $1, $2, $3, $5 | 526 esc, all, page, title = $1, $2, $3, $5 |
551 if esc.nil? | 527 if esc.nil? |
552 if page =~ /^([^\:]+)\:(.*)$/ | 528 if page =~ /^([^\:]+)\:(.*)$/ |
553 link_project = Project.find_by_name($1) || Project.find_by_identifier($1) | 529 link_project = Project.find_by_identifier($1) || Project.find_by_name($1) |
554 page = $2 | 530 page = $2 |
555 title ||= $1 if page.blank? | 531 title ||= $1 if page.blank? |
556 end | 532 end |
557 | 533 |
558 if link_project && link_project.wiki | 534 if link_project && link_project.wiki |
565 wiki_page = link_project.wiki.find_page(page) | 541 wiki_page = link_project.wiki.find_page(page) |
566 url = case options[:wiki_links] | 542 url = case options[:wiki_links] |
567 when :local; "#{title}.html" | 543 when :local; "#{title}.html" |
568 when :anchor; "##{title}" # used for single-file wiki export | 544 when :anchor; "##{title}" # used for single-file wiki export |
569 else | 545 else |
570 url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => link_project, :page => Wiki.titleize(page), :anchor => anchor) | 546 wiki_page_id = page.present? ? Wiki.titleize(page) : nil |
547 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor) | |
571 end | 548 end |
572 link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) | 549 link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) |
573 else | 550 else |
574 # project or wiki doesn't exist | 551 # project or wiki doesn't exist |
575 all | 552 all |
696 end | 673 end |
697 end | 674 end |
698 leading + (link || "#{prefix}#{sep}#{identifier}") | 675 leading + (link || "#{prefix}#{sep}#{identifier}") |
699 end | 676 end |
700 end | 677 end |
678 | |
679 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) | |
680 HEADING_RE = /<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>/i unless const_defined?(:HEADING_RE) | |
681 | |
682 # Headings and TOC | |
683 # Adds ids and links to headings and renders the TOC if needed unless options[:headings] is set to false | |
684 def parse_headings(text, project, obj, attr, only_path, options) | |
685 headings = [] | |
686 text.gsub!(HEADING_RE) do | |
687 level, attrs, content = $1.to_i, $2, $3 | |
688 item = strip_tags(content).strip | |
689 anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') | |
690 headings << [level, anchor, item] | |
691 "<h#{level} #{attrs} id=\"#{anchor}\">#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">¶</a></h#{level}>" | |
692 end unless options[:headings] == false | |
693 | |
694 text.gsub!(TOC_RE) do | |
695 if headings.empty? | |
696 '' | |
697 else | |
698 div_class = 'toc' | |
699 div_class << ' right' if $1 == '>' | |
700 div_class << ' left' if $1 == '<' | |
701 out = "<ul class=\"#{div_class}\"><li>" | |
702 root = headings.map(&:first).min | |
703 current = root | |
704 started = false | |
705 headings.each do |level, anchor, item| | |
706 if level > current | |
707 out << '<ul><li>' * (level - current) | |
708 elsif level < current | |
709 out << "</li></ul>\n" * (current - level) + "</li><li>" | |
710 elsif started | |
711 out << '</li><li>' | |
712 end | |
713 out << "<a href=\"##{anchor}\">#{item}</a>" | |
714 current = level | |
715 started = true | |
716 end | |
717 out << '</li></ul>' * (current - root) | |
718 out << '</li></ul>' | |
719 end | |
720 end | |
721 end | |
701 | 722 |
702 # Same as Rails' simple_format helper without using paragraphs | 723 # Same as Rails' simple_format helper without using paragraphs |
703 def simple_format_without_paragraph(text) | 724 def simple_format_without_paragraph(text) |
704 text.to_s. | 725 text.to_s. |
705 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n | 726 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n |