From: James Moger Date: Wed, 14 Mar 2012 20:31:28 +0000 (-0400) Subject: A bunch more Lucene work X-Git-Tag: v0.9.0~59 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6722961db100d58f8290d51768943fc6b416f7a9;p=gitblit.git A bunch more Lucene work * Only list user-accessible repositories * Syntax-highlight matched blob fragments * Improve look and layout of search results --- diff --git a/resources/gitblit.css b/resources/gitblit.css index 0e8e7532..0701a7cf 100644 --- a/resources/gitblit.css +++ b/resources/gitblit.css @@ -267,7 +267,7 @@ img.gravatar { } div.searchResult { - padding:5px; + padding: 10px 5px 10px 5px; } div.searchResult .summary { @@ -300,18 +300,24 @@ div.searchResult .highlight { padding: 0 2px; } -div.searchResult .ellipses { - font-family: sans-serif; - font-size: 9px; - font-weight: normal; - background-color: #eee; - border: 1px solid #ccc; - padding: 0 3px; - margin: 0px; +div.searchResult .ellipses { + padding-left:25px; + color: #aaa; } div.searchResult pre { margin: 1px 0px; + border: 0px; +} + +div.searchResult .text { + border-left: 5px solid #EEEEEE; + + padding: 0 0 0 15px; +} + +div.searchResult ol { + margin-bottom: 0px !important; } div.header, div.commitHeader, table.repositories th { diff --git a/src/com/gitblit/utils/LuceneUtils.java b/src/com/gitblit/utils/LuceneUtils.java index 1c24f287..ca1bbf1d 100644 --- a/src/com/gitblit/utils/LuceneUtils.java +++ b/src/com/gitblit/utils/LuceneUtils.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.text.MessageFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; @@ -903,7 +904,8 @@ public class LuceneUtils { Document doc = searcher.doc(docId); SearchResult result = createSearchResult(doc, hits[i].score); String content = doc.get(FIELD_CONTENT); - result.fragment = getHighlightedFragment(analyzer, query, content); + + result.fragment = getHighlightedFragment(analyzer, query, content, result); results.add(result); } } catch (Exception e) { @@ -913,28 +915,44 @@ public class LuceneUtils { } private static String getHighlightedFragment(Analyzer analyzer, Query query, - String content) throws IOException, InvalidTokenOffsetsException { - content = content == null ? "":StringUtils.escapeForHtml(content, false); + String content, SearchResult result) throws IOException, InvalidTokenOffsetsException { + content = content == null ? "":StringUtils.escapeForHtml(content, false); + TokenStream stream = TokenSources.getTokenStream("content", content, analyzer); QueryScorer scorer = new QueryScorer(query, "content"); - Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, 150); - - SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("", ""); - Highlighter highlighter = new Highlighter(formatter, scorer); + Fragmenter fragmenter; + if (ObjectType.commit == result.type) { + fragmenter = new SimpleSpanFragmenter(scorer, 1024); + } else { + fragmenter = new SimpleSpanFragmenter(scorer, 150); + } + + // use an artificial delimiter for the token + String termTag = ""; + SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(termTag, termTagEnd); + Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.setTextFragmenter(fragmenter); + String [] fragments = highlighter.getBestFragments(stream, content, 5); if (ArrayUtils.isEmpty(fragments)) { - return content; - } - if (fragments.length == 1) { - return "
" + fragments[0] + "
"; + if (ObjectType.blob == result.type) { + return ""; + } + return "
" + content + "
"; } StringBuilder sb = new StringBuilder(); for (int i = 0, len = fragments.length; i < len; i++) { - String fragment = fragments[i].trim(); - sb.append("
");
-			sb.append(fragment);
+			String fragment = fragments[i];
+			
+			// resurrect the raw fragment from removing the artificial delimiters
+			String raw = fragment.replace(termTag, "").replace(termTagEnd, "");			
+			sb.append(getPreTag(result, raw, content));
+			
+			// replace the artificial delimiter with html tags
+			String html = fragment.replace(termTag, "").replace(termTagEnd, "");
+			sb.append(html);
 			sb.append("
"); if (i < len - 1) { sb.append("...
"); @@ -942,6 +960,21 @@ public class LuceneUtils { } return sb.toString(); } + + private static String getPreTag(SearchResult result, String fragment, String content) { + String pre = "
";
+		if (ObjectType.blob  == result.type) {
+			int line = StringUtils.countLines(content.substring(0, content.indexOf(fragment)));			
+			int lastDot = result.path.lastIndexOf('.');
+			if (lastDot > -1) {
+				String ext = result.path.substring(lastDot + 1).toLowerCase();
+				pre = MessageFormat.format("
", line, ext);	
+			} else {
+				pre = MessageFormat.format("
", line);
+			}
+		}
+		return pre;
+	}
 
 	/**
 	 * Close all the index writers and searchers
diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java
index a465e0d7..adede1fa 100644
--- a/src/com/gitblit/utils/StringUtils.java
+++ b/src/com/gitblit/utils/StringUtils.java
@@ -485,4 +485,11 @@ public class StringUtils {
 		}
 		return value;
 	}
+	
+	public static int countLines(String value) {
+		if (isEmpty(value)) {
+			return 0;
+		}
+		return value.split("\n").length;
+	}
 }
\ No newline at end of file
diff --git a/src/com/gitblit/wicket/pages/LucenePage.html b/src/com/gitblit/wicket/pages/LucenePage.html
index 0abe3d15..e6a9b932 100644
--- a/src/com/gitblit/wicket/pages/LucenePage.html
+++ b/src/com/gitblit/wicket/pages/LucenePage.html
@@ -3,8 +3,17 @@
       xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd"  
       xml:lang="en"  
       lang="en"> 
-
+
+
+
+  
+   	
+	
+  
+
+      
 
+
 	

@@ -17,7 +26,7 @@

- +
@@ -45,12 +54,11 @@
- committed to :
- -
+
+ :
-
+ \ No newline at end of file diff --git a/src/com/gitblit/wicket/pages/LucenePage.java b/src/com/gitblit/wicket/pages/LucenePage.java index dc740125..39d33b34 100644 --- a/src/com/gitblit/wicket/pages/LucenePage.java +++ b/src/com/gitblit/wicket/pages/LucenePage.java @@ -32,10 +32,14 @@ import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants.SearchType; import com.gitblit.GitBlit; +import com.gitblit.models.RepositoryModel; import com.gitblit.models.SearchResult; +import com.gitblit.models.UserModel; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.LuceneUtils; import com.gitblit.utils.StringUtils; +import com.gitblit.wicket.GitBlitWebSession; +import com.gitblit.wicket.StringChoiceRenderer; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.panels.LinkPanel; @@ -110,7 +114,14 @@ public class LucenePage extends RootPage { setResponsePage(LucenePage.class, params); } }; - ListMultipleChoice selections = new ListMultipleChoice("repositories", repositoriesModel, GitBlit.self().getRepositoryList()); + + UserModel user = GitBlitWebSession.get().getUser(); + List availableRepositories = new ArrayList(); + for (RepositoryModel model : GitBlit.self().getRepositoryModels(user)) { + availableRepositories.add(model.name); + } + ListMultipleChoice selections = new ListMultipleChoice("repositories", + repositoriesModel, availableRepositories, new StringChoiceRenderer()); selections.setMaxRows(10); form.add(selections); form.add(new TextField("query", queryModel)); @@ -153,7 +164,7 @@ public class LucenePage extends RootPage { item.add(new LinkPanel("repository", null, sr.repository, SummaryPage.class, WicketUtils.newRepositoryParameter(sr.repository))); item.add(new LinkPanel("branch", "branch", StringUtils.getRelativePath(Constants.R_HEADS, sr.branch), LogPage.class, WicketUtils.newObjectParameter(sr.repository, sr.branch))); item.add(new Label("author", sr.author)); - item.add(WicketUtils.createTimestampLabel("date", sr.date, getTimeZone())); + item.add(WicketUtils.createDatestampLabel("date", sr.date, getTimeZone())); } }; add(resultsView.setVisible(results.size() > 0));