diff options
author | francis <francis> | 2008-03-06 21:49:33 +0000 |
---|---|---|
committer | francis <francis> | 2008-03-06 21:49:33 +0000 |
commit | da6ab0fbcec800b0e8548048ff7e19e4cbd7f7f7 (patch) | |
tree | 04b10f2fc75034923eb7204e6c2b47135157f544 | |
parent | 092d674f2c1825be04b65c4e9b9de8fc10fe44f6 (diff) |
Highlighting
-rw-r--r-- | app/controllers/general_controller.rb | 7 | ||||
-rw-r--r-- | app/helpers/application_helper.rb | 27 | ||||
-rw-r--r-- | app/views/request/_request_listing.rhtml | 2 | ||||
-rw-r--r-- | app/views/request/_request_listing_single.rhtml | 4 | ||||
-rw-r--r-- | app/views/request/_request_listing_via_incoming.rhtml | 4 | ||||
-rw-r--r-- | app/views/request/_request_listing_via_outgoing.rhtml | 6 | ||||
-rw-r--r-- | public/stylesheets/main.css | 7 |
7 files changed, 47 insertions, 10 deletions
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index 1f8c46648..342355c75 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -5,7 +5,7 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: general_controller.rb,v 1.2 2008-03-06 20:10:29 francis Exp $ +# $Id: general_controller.rb,v 1.3 2008-03-06 21:49:33 francis Exp $ class GeneralController < ApplicationController @@ -47,6 +47,11 @@ class GeneralController < ApplicationController def search @per_page = 20 query = params[:query] + + query_nopunc = query.gsub(/[^a-z0-9]/i, " ") + query_nopunc = query_nopunc.gsub(/\s+/, " ") + @highlight_words = query_nopunc.split(" ") + @solr_object = InfoRequest.multi_solr_search(query, :models => [ OutgoingMessage, IncomingMessage ], :limit => @per_page, :offset => ((params[:page]||"1").to_i-1) * @per_page) @search_results = @solr_object.results diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e40313d1f..9f7bbabdf 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -5,7 +5,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: application_helper.rb,v 1.16 2008-01-29 03:05:47 francis Exp $ +# $Id: application_helper.rb,v 1.17 2008-03-06 21:49:33 francis Exp $ module ApplicationHelper # URL generating functions are needed by all controllers (for redirects) @@ -41,5 +41,30 @@ module ApplicationHelper '' end end + + # Used for search results + def excerpt_and_highlight(text, words, count = 150) + # Find at least one word if we can + t = nil + for word in words + t = excerpt(text, word, count) + if not t.nil? + break + end + end + if t.nil? + t = excerpt(text, "", count * 2) + end + + # Highlight all the words, escaping HTML also + t = highlight_words(t, words) + return t + end + # Highlight words, also escapes HTML (other than spans that we add) + def highlight_words(t, words) + t = h(t) + t = highlight(t, words, '<span class="highlight">\1</span>') + return t + end end diff --git a/app/views/request/_request_listing.rhtml b/app/views/request/_request_listing.rhtml index ebfbc60a4..d779d0f74 100644 --- a/app/views/request/_request_listing.rhtml +++ b/app/views/request/_request_listing.rhtml @@ -2,7 +2,7 @@ None found. <% else %> <% for info_request in info_requests %> - <%= render :partial => 'request/request_listing_single', :locals => { :info_request => info_request } %> + <%= render :partial => 'request/request_listing_single', :locals => { :info_request => info_request, :highlight_words => [] } %> <% end %> <% end %> diff --git a/app/views/request/_request_listing_single.rhtml b/app/views/request/_request_listing_single.rhtml index b91d9c640..6a9a31100 100644 --- a/app/views/request/_request_listing_single.rhtml +++ b/app/views/request/_request_listing_single.rhtml @@ -1,8 +1,8 @@ <p class="request_listing"> - <%= request_link(info_request) %> + <%= link_to highlight_words(info_request.title, @highlight_words), request_url(info_request) %> <br> - <%=h excerpt(info_request.initial_request_text, "", 300) %> + <%= excerpt_and_highlight(info_request.initial_request_text, @highlight_words) %> <br> <span class="request_listing_bottomline"> diff --git a/app/views/request/_request_listing_via_incoming.rhtml b/app/views/request/_request_listing_via_incoming.rhtml index 8fd57559a..b2b3d7091 100644 --- a/app/views/request/_request_listing_via_incoming.rhtml +++ b/app/views/request/_request_listing_via_incoming.rhtml @@ -1,8 +1,8 @@ <p class="request_listing"> - <%= link_to "Response to '" + h(info_request.title) + "'", request_url(info_request)+"#incoming-"+incoming_message.id.to_s %> + <%= link_to "Response to '" + highlight_words(info_request.title, @highlight_words) + "'", request_url(info_request)+"#incoming-"+incoming_message.id.to_s %> <br> - <%=h excerpt(incoming_message.get_text_for_indexing, "", 300) %> + <%= excerpt_and_highlight(incoming_message.get_text_for_indexing, @highlight_words) %> <br> <span class="request_listing_bottomline"> diff --git a/app/views/request/_request_listing_via_outgoing.rhtml b/app/views/request/_request_listing_via_outgoing.rhtml index 7c079eeec..ce20231b6 100644 --- a/app/views/request/_request_listing_via_outgoing.rhtml +++ b/app/views/request/_request_listing_via_outgoing.rhtml @@ -1,14 +1,14 @@ <p class="request_listing"> <% if outgoing_message.message_type == 'initial_request' %> - <%= link_to "Initial request for '" + h(info_request.title) + "'", request_url(info_request)+"#outgoing-"+outgoing_message.id.to_s %> + <%= link_to "Initial request for '" + highlight_words(info_request.title, @highlight_words) + "'", request_url(info_request)+"#outgoing-"+outgoing_message.id.to_s %> <% elsif outgoing_message.message_type == 'followup' %> - <%= link_to "Initial request for '" + h(info_request.title) + "'", request_url(info_request)+"#outgoing-"+outgoing_message.id.to_s %> + <%= link_to "Initial request for '" + highlight_words(info_request.title, @highlight_words) + "'", request_url(info_request)+"#outgoing-"+outgoing_message.id.to_s %> <% else %> <% raise "unknown outgoing message type" %> <% end %> <br> - <%=h excerpt(outgoing_message.body, "", 300) %> + <%= excerpt_and_highlight(outgoing_message.body, @highlight_words) %> <br> <span class="request_listing_bottomline"> diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css index f49aa54c9..1d27faafa 100644 --- a/public/stylesheets/main.css +++ b/public/stylesheets/main.css @@ -357,6 +357,13 @@ table#list_requests .odd { color: #ff0000; } +/* /search - searching */ + +.highlight { + background-color: #eeee00; + color: #000000; +} + /* /body - listing bodies */ .body_listing { |