From 49d88a037002c8091b257f7a245c8e34d9f0c948 Mon Sep 17 00:00:00 2001 From: Henrik Hansen Date: Sun, 29 Aug 2010 13:58:06 +0200 Subject: [PATCH] Add search API and update frontend The frontend has been updated to use the new search API. --- lib/gollum/frontend/app.rb | 4 ++-- lib/gollum/frontend/views/search.rb | 18 ++---------------- lib/gollum/wiki.rb | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index f022e34a..87771827 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -130,8 +130,8 @@ module Precious get '/search' do @query = params[:q] - @search_command = "cd #{$path} && git grep -c '#{@query}' master" - @results = `#{@search_command}` + wiki = Gollum::Wiki.new($path) + @results = wiki.search @query mustache :search end diff --git a/lib/gollum/frontend/views/search.rb b/lib/gollum/frontend/views/search.rb index b65191af..31789255 100644 --- a/lib/gollum/frontend/views/search.rb +++ b/lib/gollum/frontend/views/search.rb @@ -1,26 +1,12 @@ module Precious module Views class Search < Layout - attr_reader :content, :page, :footer, :query + attr_reader :content, :page, :footer, :results, :query def has_results - !results.empty? + !@results.empty? end - def results - ret = [] - @results.each_line do |line| - result = line.split(":") - file = result[1] - count = result[2].to_i - name = file.split(".")[0] - ret << { - :name => name, - :count => count - } - end - ret - end end end end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index a5f9a094..a98abf10 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -208,6 +208,28 @@ module Gollum end end + # Public: Search all pages for this wiki. + # + # query - The string to search for + # + # Returns an Array with Objects of page name and count of matches + def search(query) + search_command = "cd #{$path} && git grep -c '#{query}' master" + command_output = `#{search_command}` + results = [] + command_output.each_line do |line| + result = line.split(":") + file = result[1] + count = result[2].to_i + name = file.split(".")[0] + results << { + :name => name, + :count => count + } + end + results + end + # Public: All of the versions that have touched the Page. # # options - The options Hash: