##// END OF EJS Templates
Rails3: helper: html_safe for SearchHelper highlight_tokens method...
Toshi MARUYAMA -
r8335:c86cb730f19b
parent child
Show More
@@ -1,68 +1,68
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module SearchHelper
21 21 def highlight_tokens(text, tokens)
22 22 return text unless text && tokens && !tokens.empty?
23 23 re_tokens = tokens.collect {|t| Regexp.escape(t)}
24 24 regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
25 25 result = ''
26 26 text.split(regexp).each_with_index do |words, i|
27 27 if result.length > 1200
28 28 # maximum length of the preview reached
29 29 result << '...'
30 30 break
31 31 end
32 32 words = words.mb_chars
33 33 if i.even?
34 34 result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
35 35 else
36 36 t = (tokens.index(words.downcase) || 0) % 4
37 37 result << content_tag('span', h(words), :class => "highlight token-#{t}")
38 38 end
39 39 end
40 result
40 result.html_safe
41 41 end
42 42
43 43 def type_label(t)
44 44 l("label_#{t.singularize}_plural", :default => t.to_s.humanize)
45 45 end
46 46
47 47 def project_select_tag
48 48 options = [[l(:label_project_all), 'all']]
49 49 options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty?
50 50 options << [l(:label_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty?
51 51 options << [@project.name, ''] unless @project.nil?
52 52 label_tag("scope", l(:description_project_scope), :class => "hidden-for-sighted") +
53 53 select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1
54 54 end
55 55
56 56 def render_results_by_type(results_by_type)
57 57 links = []
58 58 # Sorts types by results count
59 59 results_by_type.keys.sort {|a, b| results_by_type[b] <=> results_by_type[a]}.each do |t|
60 60 c = results_by_type[t]
61 61 next if c == 0
62 62 text = "#{type_label(t)} (#{c})"
63 63 links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only],
64 64 :all_words => params[:all_words], :scope => params[:scope], t => 1)
65 65 end
66 66 ('<ul>' + links.map {|link| content_tag('li', link)}.join(' ') + '</ul>') unless links.empty?
67 67 end
68 68 end
General Comments 0
You need to be logged in to leave comments. Login now