mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
(refs #3)Search by AND if query words are separated by whitespace.
This commit is contained in:
@@ -62,19 +62,22 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||
treeWalk.setRecursive(true)
|
||||
treeWalk.addTree(revCommit.getTree)
|
||||
|
||||
val lowerQuery = query.toLowerCase
|
||||
val lowerQueries = query.toLowerCase.split("[ \\t ]+")
|
||||
val list = new ListBuffer[(String, String)]
|
||||
while (treeWalk.next()) {
|
||||
if(treeWalk.getFileMode(0) != FileMode.TREE){
|
||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
|
||||
if(FileUtil.isText(bytes)){
|
||||
val text = new String(bytes, "UTF-8")
|
||||
val index = text.toLowerCase.indexOf(lowerQuery)
|
||||
if(index >= 0){
|
||||
val lineNumber = text.substring(0, index).split("\n").size - 1
|
||||
val highlightText = text.split("\n").drop(lineNumber).take(5).mkString("\n")
|
||||
.replace("&", "&").replace("<", ">").replace(">", ">").replace("\"", """)
|
||||
.replaceAll("(?i)(\\Q" + query + "\\E)", "<span style=\"background-color: yellow;\">$1</span>")
|
||||
val lowerText = text.toLowerCase
|
||||
val indices = lowerQueries.map { lowerQuery =>
|
||||
lowerText.indexOf(lowerQuery)
|
||||
}
|
||||
if(!indices.exists(_ < 0)){
|
||||
val lineNumber = text.substring(0, indices.min).split("\n").size - 1
|
||||
val highlightText = StringUtil.escapeHtml(text.split("\n").drop(lineNumber).take(5).mkString("\n"))
|
||||
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")",
|
||||
"<span style=\"background-color: yellow;\">$1</span>")
|
||||
list.append((treeWalk.getPathString, highlightText))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,7 @@ object StringUtil {
|
||||
|
||||
def urlDecode(value: String): String = URLDecoder.decode(value, "UTF-8")
|
||||
|
||||
def escapeHtml(value: String): String =
|
||||
value.replace("&", "&").replace("<", ">").replace(">", ">").replace("\"", """)
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
@html.main("Search Results", Some(repository)){
|
||||
@menu("code", query, repository){
|
||||
@if(files.isEmpty){
|
||||
<h4>We couldn't find any code matching '@query'</h4>
|
||||
} else {
|
||||
@@ -14,4 +15,5 @@
|
||||
<pre>@Html(file.highlightText)</pre>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/main/twirl/search/menu.scala.html
Normal file
21
src/main/twirl/search/menu.scala.html
Normal file
@@ -0,0 +1,21 @@
|
||||
@(active: String, query: String, repository: service.RepositoryService.RepositoryInfo)(body: Html)(implicit context: app.Context)
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
@html.header("", repository)
|
||||
<div class="row-fluid">
|
||||
<div class="span3">
|
||||
<div class="box">
|
||||
<ul class="nav nav-tabs nav-stacked side-menu">
|
||||
<li@if(active=="code"){ class="active"}>
|
||||
<a href="@url(repository)/search?q=@urlEncode(query)&type=Code">Code</a>
|
||||
</li>
|
||||
<li@if(active=="issue"){ class="active"}>
|
||||
<a href="@url(repository)/search?q=@urlEncode(query)&type=Issue">Issue</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="span9">
|
||||
@body
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user