From 4905ce99daacd98d759333f1ca38b01535c35ad8 Mon Sep 17 00:00:00 2001 From: Tobias Adam Date: Sun, 22 Aug 2010 23:17:54 +0200 Subject: [PATCH] =?UTF-8?q?Add=20a=20very=20rough=20and=20dirty=20search?= =?UTF-8?q?=20mechanism=20that=20uses=20Git=E2=80=99s=20grep.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/gollum/frontend/app.rb | 7 +++++ lib/gollum/frontend/templates/page.mustache | 4 ++- lib/gollum/frontend/templates/search.mustache | 20 +++++++++++++ lib/gollum/frontend/views/search.rb | 30 +++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/gollum/frontend/templates/search.mustache create mode 100644 lib/gollum/frontend/views/search.rb diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index e4cb0991..6acb1006 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -128,6 +128,13 @@ module Precious end end + get '/search' do + @q = params[:q] + @search_command = "cd #{$path} && git grep -c '#{@q}' master" + @results = `#{@search_command}` + mustache :search + end + get '/*' do show_page_or_file(params[:splat].first) end diff --git a/lib/gollum/frontend/templates/page.mustache b/lib/gollum/frontend/templates/page.mustache index 24956273..87e70be4 100644 --- a/lib/gollum/frontend/templates/page.mustache +++ b/lib/gollum/frontend/templates/page.mustache @@ -1,7 +1,9 @@
- Home | Edit +
+ Home | Edit | +

{{title}}

diff --git a/lib/gollum/frontend/templates/search.mustache b/lib/gollum/frontend/templates/search.mustache new file mode 100644 index 00000000..964452dc --- /dev/null +++ b/lib/gollum/frontend/templates/search.mustache @@ -0,0 +1,20 @@ +
+
+
+
+ Home | +
+
+

Search for “{{search_string}}”

+
+

Searched with {{search_command}}

+ {{#has_results}} + + {{/has_results}} +
+
+
diff --git a/lib/gollum/frontend/views/search.rb b/lib/gollum/frontend/views/search.rb new file mode 100644 index 00000000..84938b5d --- /dev/null +++ b/lib/gollum/frontend/views/search.rb @@ -0,0 +1,30 @@ +module Precious + module Views + class Search < Layout + attr_reader :content, :page, :footer, :search_command + + def search_string + @q + end + + def has_results + !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