[Groonga-commit] groonga/heroku-sample-rroonga-blog at 20295e5 [master] Support highlihgt

Back to archive index

Kouhei Sutou null+****@clear*****
Sun May 25 16:51:10 JST 2014


Kouhei Sutou	2014-05-25 16:51:10 +0900 (Sun, 25 May 2014)

  New Revision: 20295e5e2fa18c86b6957411cd93a9ddcd6b9f3b
  https://github.com/groonga/heroku-sample-rroonga-blog/commit/20295e5e2fa18c86b6957411cd93a9ddcd6b9f3b

  Message:
    Support highlihgt

  Added files:
    lib/highlighter.rb
  Modified files:
    app/assets/stylesheets/scaffolds.css.scss
    app/controllers/posts_controller.rb
    app/helpers/posts_helper.rb
    app/views/posts/index.html.erb
    lib/post_searcher.rb

  Modified: app/assets/stylesheets/scaffolds.css.scss (+4 -0)
===================================================================
--- app/assets/stylesheets/scaffolds.css.scss    2014-05-25 16:39:33 +0900 (6ec6a8f)
+++ app/assets/stylesheets/scaffolds.css.scss    2014-05-25 16:51:10 +0900 (2b39bd8)
@@ -67,3 +67,7 @@ div {
     list-style: square;
   }
 }
+
+.keyword {
+  color: #f33;
+}

  Modified: app/controllers/posts_controller.rb (+2 -0)
===================================================================
--- app/controllers/posts_controller.rb    2014-05-25 16:39:33 +0900 (f13d25b)
+++ app/controllers/posts_controller.rb    2014-05-25 16:51:10 +0900 (ee6d21f)
@@ -8,8 +8,10 @@ class PostsController < ApplicationController
     if @search_condition.valid?
       searcher = PostSearcher.new(@search_condition.query)
       @posts = searcher.search
+      @highlighter = searcher.highlighter
     else
       @posts = Post.all
+      @highlighter = nil
     end
   end
 

  Modified: app/helpers/posts_helper.rb (+7 -0)
===================================================================
--- app/helpers/posts_helper.rb    2014-05-25 16:39:33 +0900 (a7b8cec)
+++ app/helpers/posts_helper.rb    2014-05-25 16:51:10 +0900 (886a156)
@@ -1,2 +1,9 @@
 module PostsHelper
+  def highlight(text)
+    if @highlighter
+      raw(@highlighter.highlight(text))
+    else
+      text
+    end
+  end
 end

  Modified: app/views/posts/index.html.erb (+2 -2)
===================================================================
--- app/views/posts/index.html.erb    2014-05-25 16:39:33 +0900 (c5a5b5f)
+++ app/views/posts/index.html.erb    2014-05-25 16:51:10 +0900 (684faad)
@@ -17,8 +17,8 @@
   <tbody>
     <% @posts.each do |post| %>
       <tr>
-        <td><%= post.title %></td>
-        <td><%= post.content %></td>
+        <td><%= highlight(post.title) %></td>
+        <td><%= highlight(post.content) %></td>
         <td><%= link_to 'Show', post %></td>
         <td><%= link_to 'Edit', edit_post_path(post) %></td>
         <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>

  Added: lib/highlighter.rb (+21 -0) 100644
===================================================================
--- /dev/null
+++ lib/highlighter.rb    2014-05-25 16:51:10 +0900 (a60941e)
@@ -0,0 +1,21 @@
+class Highlighter
+  def initialize(keywords)
+    @patricia_trie = Groonga::PatriciaTrie.create(key_type: "ShortText",
+                                                  normalizer: "NormalizerAuto")
+    keywords.each do |word|
+      @patricia_trie.add(word)
+    end
+  end
+
+  def highlight(text)
+    other_text_handler = lambda do |string|
+      ERB::Util.html_escape(string)
+    end
+    options = {
+      other_text_handler: other_text_handler,
+    }
+    @patricia_trie.tag_keys(text, options) do |record, word|
+      "<span class=\"keyword\">#{ERB::Util.html_escape(word)}</span>"
+    end
+  end
+end

  Modified: lib/post_searcher.rb (+4 -0)
===================================================================
--- lib/post_searcher.rb    2014-05-25 16:39:33 +0900 (b236017)
+++ lib/post_searcher.rb    2014-05-25 16:51:10 +0900 (e3353df)
@@ -19,6 +19,10 @@ class PostSearcher
     Post.where(id: post_ids)
   end
 
+  def highlighter
+    @highlighter ||= Highlighter.new(words)
+  end
+
   private
   def words
     @query.split(/\s+/)
-------------- next part --------------
HTML����������������������������...
下載 



More information about the Groonga-commit mailing list
Back to archive index