James Moger
2012-09-14 16e4747d3cb2c2a53a6bef554bca306d8594a080
Mostly complete blob view line links feature, DOM offset bug remains (issue-130)
3 files modified
144 ■■■■ changed files
docs/04_releases.mkd 1 ●●●● patch | view | raw | blame | history
resources/gitblit.css 23 ●●●●● patch | view | raw | blame | history
src/com/gitblit/wicket/pages/BlobPage.java 120 ●●●● patch | view | raw | blame | history
docs/04_releases.mkd
@@ -16,6 +16,7 @@
#### additions
- added line links to blob view at the expense of zebra striping (issue 130)
- added RedmineUserService (github/mallowlabs)
#### changes
resources/gitblit.css
@@ -145,6 +145,7 @@
    border:0px;
    padding: 0;
    line-height: 1.35em;
    vertical-align:top;
}
table {
@@ -161,10 +162,32 @@
    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;
}
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 @@
                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("<!-- start blob table -->");
        sb.append("<table width=\"100%\"><tbody><tr>");
        // nums column
        sb.append("<!-- start nums column -->");
        sb.append("<td id=\"nums\">");
        sb.append("<pre>");
        String numPattern = "<a id=\"L{0}\" href=\"#L{0}\">{0}</a>\n";
        for (int i = 0; i < lines.length; i++) {
            sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
        }
        sb.append("</pre>");
        sb.append("<!-- end nums column -->");
        sb.append("</td>");
        sb.append("<!-- start lines column -->");
        sb.append("<td id=\"lines\">");
        sb.append("<div class=\"sourceview\">");
        if (prettyPrint) {
            sb.append("<pre class=\"prettyprint\">");
        } else {
            sb.append("<pre class=\"plainprint\">");
        }
        sb.append(StringUtils.escapeForHtml(source, true));
        sb.append("</pre>");
        sb.append("</div>");
        sb.append("</td>");
        sb.append("<!-- end lines column -->");
        sb.append("</tr></tbody></table>");
        sb.append("<!-- end blob table -->");
        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("<!-- start blob table -->");
        sb.append("<table width=\"100%\"><tbody><tr>");
        // nums column
        sb.append("<!-- start nums column -->");
        sb.append("<td id=\"nums\">");
        sb.append("<pre><table width=\"100%\"><tbody>");
        String numPattern = "<tr><td id=\"L{0}\"><a href=\"#L{0}\">{0}</a></td></tr>";
        for (int i = 0; i < lines.length; i++) {
            sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
        }
        sb.append("<!-- end nums column -->");
        sb.append("</tbody></table></pre>");
        sb.append("</td>");
        sb.append("<!-- start lines column -->");
        sb.append("<td id=\"lines\">");
        if (prettyPrint) {
            sb.append("<pre style=\"border: 0px;\" class=\"prettyprint\">");
        } else {
            sb.append("<pre class=\"plainprint\">");
        }
        sb.append("<table width=\"100%\"><tbody>");
        String linePattern = "<tr class=\"{0}\"><td>{1}</tr>";
        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("</tbody></table></pre>");
        sb.append("</td>");
        sb.append("<!-- end lines column -->");
        sb.append("</tr></tbody></table>");
        sb.append("<!-- end blob table -->");
        return sb.toString();
    }
    @Override
    protected String getPageName() {