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