From 16e4747d3cb2c2a53a6bef554bca306d8594a080 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 14 Sep 2012 17:51:04 -0400 Subject: [PATCH] Mostly complete blob view line links feature, DOM offset bug remains (issue 130) --- docs/04_releases.mkd | 1 + resources/gitblit.css | 23 ++++ src/com/gitblit/wicket/pages/BlobPage.java | 120 +++++++++++++++++---- 3 files changed, 121 insertions(+), 23 deletions(-) diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd index 2ea0e23d..07a1fb31 100644 --- a/docs/04_releases.mkd +++ b/docs/04_releases.mkd @@ -16,6 +16,7 @@ If you are updating from an earlier release AND you have indexed branches with t #### additions +- added line links to blob view at the expense of zebra striping (issue 130) - added RedmineUserService (github/mallowlabs) #### changes diff --git a/resources/gitblit.css b/resources/gitblit.css index 7a73a24c..861fc411 100644 --- a/resources/gitblit.css +++ b/resources/gitblit.css @@ -145,6 +145,7 @@ pre, code, pre.prettyprint, pre.plainprint { border:0px; padding: 0; line-height: 1.35em; + vertical-align:top; } table { @@ -161,10 +162,32 @@ th { text-align: left; } +div.sourceview { + overflow: auto; +} pre.prettyprint ol { padding-left:25px; } +#nums { + text-align: right; + padding-right:10px; + border-right:1px solid #ddd; + font-family: monospace; + line-height: 1.35em; + vertical-align:top; +} + +#nums pre { + white-space: pre; +} + +#lines pre { + padding: 0px 5px !important; + border: 0px !important; + white-space: nowrap; +} + h1 small, h2 small, h3 small, h4 small, h5 small, h6 small { color: #888; } diff --git a/src/com/gitblit/wicket/pages/BlobPage.java b/src/com/gitblit/wicket/pages/BlobPage.java index e2683ae1..f1d802d4 100644 --- a/src/com/gitblit/wicket/pages/BlobPage.java +++ b/src/com/gitblit/wicket/pages/BlobPage.java @@ -15,6 +15,7 @@ */ package com.gitblit.wicket.pages; +import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; @@ -109,45 +110,118 @@ public class BlobPage extends RepositoryPage { if (map.containsKey(extension)) { type = map.get(extension); } - Component c = null; - Component i = null; switch (type) { - case 1: - // PrettyPrint blob text - c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(), - blobPath, encodings)); - WicketUtils.setCssClass(c, "prettyprint linenums"); - i = new Image("blobImage").setVisible(false); - break; case 2: // image blobs - c = new Label("blobText").setVisible(false); - i = new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString()); + add(new Label("blobText").setVisible(false)); + add(new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString())); break; case 3: // binary blobs - c = new Label("blobText", "Binary File"); - i = new Image("blobImage").setVisible(false); + add(new Label("blobText", "Binary File")); + add(new Image("blobImage").setVisible(false)); break; default: // plain text - c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(), - blobPath, encodings)); - WicketUtils.setCssClass(c, "plainprint"); - i = new Image("blobImage").setVisible(false); + String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings); + String table = generateSourceView(source, type == 1); + add(new Label("blobText", table).setEscapeModelStrings(false)); + add(new Image("blobImage").setVisible(false)); } - add(c); - add(i); } else { // plain text - Label blobLabel = new Label("blobText", JGitUtils.getStringContent(r, - commit.getTree(), blobPath, encodings)); - WicketUtils.setCssClass(blobLabel, "plainprint"); - add(blobLabel); + String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings); + String table = generateSourceTable(source, false); + add(new Label("blobText", table).setEscapeModelStrings(false)); add(new Image("blobImage").setVisible(false)); } } } + + protected String generateSourceView(String source, boolean prettyPrint) { + String [] lines = source.split("\n"); + + StringBuilder sb = new StringBuilder(); + sb.append(""); + sb.append(""); + + // nums column + sb.append(""); + sb.append(""); + + sb.append(""); + sb.append(""); + sb.append(""); + + sb.append("
"); + sb.append("
");
+		String numPattern = "{0}\n";
+		for (int i = 0; i < lines.length; i++) {
+			sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
+		}
+		sb.append("
"); + sb.append(""); + sb.append("
"); + sb.append("
"); + if (prettyPrint) { + sb.append("
");
+		} else {
+			sb.append("
");
+		}
+		sb.append(StringUtils.escapeForHtml(source, true));
+		sb.append("
"); + sb.append("
"); + sb.append("
"); + sb.append(""); + + return sb.toString(); + } + + protected String generateSourceTable(String source, boolean prettyPrint) { + String [] lines = source.split("\n"); + + // be careful adding line breaks to this method + // GoogleCode Prettify is sensitive + StringBuilder sb = new StringBuilder(); + sb.append(""); + sb.append(""); + + // nums column + sb.append(""); + sb.append(""); + + sb.append(""); + sb.append(""); + sb.append(""); + + sb.append("
"); + sb.append("
");
+		String numPattern = "";
+		for (int i = 0; i < lines.length; i++) {
+			sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
+		}
+		sb.append("");
+		sb.append("
{0}
"); + sb.append("
"); + if (prettyPrint) { + sb.append("
");
+		} else {
+			sb.append("
");
+		}
+		sb.append("");
+		
+		String linePattern = "";
+		for (int i = 0; i < lines.length; i++) {
+			String l = StringUtils.escapeForHtml(lines[i], true);
+			String cssClass = (i % 2 == 0) ? "even" : "odd";
+			sb.append(MessageFormat.format(linePattern, cssClass, l));
+		}
+		sb.append("
{1}
"); + sb.append("
"); + sb.append(""); + + return sb.toString(); + } @Override protected String getPageName() { -- 2.39.5