diff --git a/src/main/scala/app/IndexController.scala b/src/main/scala/app/IndexController.scala index bc61690..7e92f71 100644 --- a/src/main/scala/app/IndexController.scala +++ b/src/main/scala/app/IndexController.scala @@ -62,21 +62,24 @@ 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)", "$1") - list.append((treeWalk.getPathString, highlightText)) + 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("|") + ")", + "$1") + list.append((treeWalk.getPathString, highlightText)) + } } } } diff --git a/src/main/scala/util/StringUtil.scala b/src/main/scala/util/StringUtil.scala index ae6d868..49b8de7 100644 --- a/src/main/scala/util/StringUtil.scala +++ b/src/main/scala/util/StringUtil.scala @@ -20,4 +20,7 @@ def urlDecode(value: String): String = URLDecoder.decode(value, "UTF-8") + def escapeHtml(value: String): String = + value.replace("&", "&").replace("<", ">").replace(">", ">").replace("\"", """) + } diff --git a/src/main/twirl/search/code.scala.html b/src/main/twirl/search/code.scala.html index 92b2746..55a7f14 100644 --- a/src/main/twirl/search/code.scala.html +++ b/src/main/twirl/search/code.scala.html @@ -2,16 +2,18 @@ @import context._ @import view.helpers._ @html.main("Search Results", Some(repository)){ - @if(files.isEmpty){ -

We couldn't find any code matching '@query'

- } else { -

We've found @files.size code @plural(files.size, "result")

- } - @files.map { file => -
-
@file.path
-
@datetime(file.lastModified)
-
@Html(file.highlightText)
-
+ @menu("code", query, repository){ + @if(files.isEmpty){ +

We couldn't find any code matching '@query'

+ } else { +

We've found @files.size code @plural(files.size, "result")

+ } + @files.map { file => +
+
@file.path
+
@datetime(file.lastModified)
+
@Html(file.highlightText)
+
+ } } } \ No newline at end of file diff --git a/src/main/twirl/search/menu.scala.html b/src/main/twirl/search/menu.scala.html new file mode 100644 index 0000000..500f874 --- /dev/null +++ b/src/main/twirl/search/menu.scala.html @@ -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) +
+
+
+ +
+
+
+ @body +
+