Add a very rough and dirty search mechanism that uses Git’s grep.

This commit is contained in:
Tobias Adam
2010-08-22 23:17:54 +02:00
parent 48b143c6ed
commit 4905ce99da
4 changed files with 60 additions and 1 deletions
+7
View File
@@ -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
+3 -1
View File
@@ -1,7 +1,9 @@
<div class="guide">
<div class="main">
<div class="actions">
<a href="/">Home</a> | <a href="/edit/{{name}}">Edit</a>
<form action="/search" method="get">
<a href="/">Home</a> | <a href="/edit/{{name}}">Edit</a> | <input type="text" name="q" size="10" /> <input type="submit" value="search" />
</form>
</div>
<h1>{{title}}</h1>
<div class="content wikistyle gollum {{format}}">
@@ -0,0 +1,20 @@
<div class="guide">
<div class="main">
<div class="actions">
<form action="/search" method="get">
<a href="/">Home</a> | <input type="text" name="q" size="10" /> <input type="submit" value="search" />
</form>
</div>
<h1>Search for “{{search_string}}”</h1>
<div class="content wikistyle gollum">
<p>Searched with <code>{{search_command}}</code></p>
{{#has_results}}
<ul>
{{#results}}
<li><a href="/{{name}}">{{name}} ({{count}})</a></li>
{{/results}}
</ul>
{{/has_results}}
</div>
</div>
</div>
+30
View File
@@ -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