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