diff options
-rw-r--r-- | app/controllers/general_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/track_controller.rb | 2 | ||||
-rw-r--r-- | app/helpers/highlight_helper.rb | 29 | ||||
-rw-r--r-- | app/views/track_mailer/event_digest.text.erb | 2 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 1 | ||||
-rw-r--r-- | spec/helpers/highlight_helper_spec.rb | 14 |
7 files changed, 33 insertions, 19 deletions
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index 28055ddbf..759e80af9 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -159,7 +159,7 @@ class GeneralController < ApplicationController end # Spelling and highight words are same for all three queries - @highlight_words = @request_for_spelling.words_to_highlight + @highlight_words = @request_for_spelling.words_to_highlight(:regex => true) if !(@request_for_spelling.spelling_correction =~ /[a-z]+:/) @spelling_correction = @request_for_spelling.spelling_correction end diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index c15fb573d..551d9e72e 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -154,7 +154,7 @@ class TrackController < ApplicationController request.format = 'xml' unless params[:format] respond_to do |format| format.json { render :json => @xapian_object.results.map { |r| r[:model].json_for_api(true, - lambda { |t| view_context.highlight_and_excerpt(t, @xapian_object.words_to_highlight, 150) } + lambda { |t| view_context.highlight_and_excerpt(t, @xapian_object.words_to_highlight(:regex => true), 150) } ) } } format.any { render :template => 'track/atom_feed', :formats => ['atom'], diff --git a/app/helpers/highlight_helper.rb b/app/helpers/highlight_helper.rb index 63809aff5..06ade48ee 100644 --- a/app/helpers/highlight_helper.rb +++ b/app/helpers/highlight_helper.rb @@ -6,14 +6,19 @@ module HighlightHelper def highlight_matches(text, phrases, options = {}) text = ActionController::Base.helpers.sanitize(text).try(:html_safe) if options.fetch(:sanitize, true) - if text.blank? || phrases.blank? - text - else - highlighter = options.fetch(:highlighter, '<mark>\1</mark>') - match = Array(phrases).map do |p| - Regexp === p ? p.to_s : Regexp.escape(p) - end.join('|') - text.gsub(/(#{match})(?![^<]*?>)/i, highlighter) + if text.blank? || phrases.blank? + text + else + match = Array(phrases).map do |p| + Regexp === p ? p.to_s : Regexp.escape(p) + end.join('|') + + if block_given? + text.gsub(/(#{match})(?![^<]*?>)/i) { |found| yield found } + else + highlighter = options.fetch(:highlighter, '<mark>\1</mark>') + text.gsub(/(#{match})(?![^<]*?>)/i, highlighter) + end end.html_safe end @@ -40,11 +45,11 @@ module HighlightHelper return unless text && phrase separator = options.fetch(:separator, nil) || "" - if Regexp === phrase + case phrase + when Regexp regex = phrase else - phrase = Regexp.escape(phrase) - regex = /#{phrase}/iu + regex = /#{Regexp.escape(phrase)}/i end return unless matches = text.match(regex) @@ -59,7 +64,7 @@ module HighlightHelper end end - first_part, second_part = text.split(regex, 2) + first_part, second_part = text.split(phrase, 2) prefix, first_part = cut_excerpt_part(:first, first_part, separator, options) postfix, second_part = cut_excerpt_part(:second, second_part, separator, options) diff --git a/app/views/track_mailer/event_digest.text.erb b/app/views/track_mailer/event_digest.text.erb index a154f430f..f6e699e41 100644 --- a/app/views/track_mailer/event_digest.text.erb +++ b/app/views/track_mailer/event_digest.text.erb @@ -4,7 +4,7 @@ for track_thing, alert_results, xapian_object in @email_about_things main_text += track_thing.params[:title_in_email] + "\n" main_text += ("=" * track_thing.params[:title_in_email].size) + "\n\n" - @highlight_words = xapian_object.words_to_highlight + @highlight_words = xapian_object.words_to_highlight(:regex => true) for result in alert_results.reverse if result[:model].class.to_s == "InfoRequestEvent" event = result[:model] diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 7590a5b42..f9c307913 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -188,7 +188,7 @@ describe GeneralController, 'when using xapian search' do it 'should highlight words for a user-only request' do get :search, :combined => "bob/users" - assigns[:highlight_words].should == ['bob'] + assigns[:highlight_words].should == [/\b(bob)\w*\b/iu] end it 'should show spelling corrections for a user-only request' do diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 48f37a45c..f7c935af3 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -923,7 +923,6 @@ describe RequestController, "when searching for an authority" do end it "should return matching bodies" do - session[:user_id] = @user.id get :select_authority, :query => "Quango" diff --git a/spec/helpers/highlight_helper_spec.rb b/spec/helpers/highlight_helper_spec.rb index bd0c62226..675100f00 100644 --- a/spec/helpers/highlight_helper_spec.rb +++ b/spec/helpers/highlight_helper_spec.rb @@ -105,6 +105,13 @@ describe HighlightHelper do assert_equal options, passed_options end + it 'highlights with a block' do + assert_equal( + "<b>one</b> <b>two</b> <b>three</b>", + highlight_matches("one two three", ["one", "two", "three"]) { |word| "<b>#{word}</b>" } + ) + end + end describe :excerpt do @@ -113,8 +120,6 @@ describe HighlightHelper do assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5)) assert_equal("This is a...", excerpt("This is a beautiful morning", "this", :radius => 5)) assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", :radius => 5)) - assert_equal("...udge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 5)) - assert_equal("...judge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 1, :separator => ' ')) assert_nil excerpt("This is a beautiful morning", "day") end @@ -142,6 +147,11 @@ describe HighlightHelper do it 'excerpts with regex' do assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', :radius => 5)) assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', :radius => 5)) + assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', /\bbeau\w*\b/i, :radius => 5)) + assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', /\b(beau\w*)\b/i, :radius => 5)) + assert_equal("...udge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 5)) + assert_equal("...judge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 1, :separator => ' ')) + assert_equal("...was challenging for...", excerpt("This day was challenging for judge Allen and his colleagues.", /\b(\w*allen\w*)\b/i, :radius => 5)) end it 'excerpts with omission' do |