*/\r
package com.gitblit.wicket.pages;\r
\r
+import java.text.MessageFormat;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
if (map.containsKey(extension)) {\r
type = map.get(extension);\r
}\r
- Component c = null;\r
- Component i = null;\r
switch (type) {\r
- case 1:\r
- // PrettyPrint blob text\r
- c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),\r
- blobPath, encodings));\r
- WicketUtils.setCssClass(c, "prettyprint linenums");\r
- i = new Image("blobImage").setVisible(false);\r
- break;\r
case 2:\r
// image blobs\r
- c = new Label("blobText").setVisible(false);\r
- i = new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString());\r
+ add(new Label("blobText").setVisible(false));\r
+ add(new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString()));\r
break;\r
case 3:\r
// binary blobs\r
- c = new Label("blobText", "Binary File");\r
- i = new Image("blobImage").setVisible(false);\r
+ add(new Label("blobText", "Binary File"));\r
+ add(new Image("blobImage").setVisible(false));\r
break;\r
default:\r
// plain text\r
- c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),\r
- blobPath, encodings));\r
- WicketUtils.setCssClass(c, "plainprint");\r
- i = new Image("blobImage").setVisible(false);\r
+ String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);\r
+ String table = generateSourceView(source, type == 1);\r
+ add(new Label("blobText", table).setEscapeModelStrings(false));\r
+ add(new Image("blobImage").setVisible(false));\r
}\r
- add(c);\r
- add(i);\r
} else {\r
// plain text\r
- Label blobLabel = new Label("blobText", JGitUtils.getStringContent(r,\r
- commit.getTree(), blobPath, encodings));\r
- WicketUtils.setCssClass(blobLabel, "plainprint");\r
- add(blobLabel);\r
+ String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);\r
+ String table = generateSourceTable(source, false);\r
+ add(new Label("blobText", table).setEscapeModelStrings(false));\r
add(new Image("blobImage").setVisible(false));\r
}\r
}\r
}\r
+ \r
+ protected String generateSourceView(String source, boolean prettyPrint) {\r
+ String [] lines = source.split("\n");\r
+ \r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("<!-- start blob table -->");\r
+ sb.append("<table width=\"100%\"><tbody><tr>");\r
+ \r
+ // nums column\r
+ sb.append("<!-- start nums column -->");\r
+ sb.append("<td id=\"nums\">");\r
+ sb.append("<pre>");\r
+ String numPattern = "<a id=\"L{0}\" href=\"#L{0}\">{0}</a>\n";\r
+ for (int i = 0; i < lines.length; i++) {\r
+ sb.append(MessageFormat.format(numPattern, "" + (i + 1)));\r
+ }\r
+ sb.append("</pre>");\r
+ sb.append("<!-- end nums column -->");\r
+ sb.append("</td>");\r
+ \r
+ sb.append("<!-- start lines column -->");\r
+ sb.append("<td id=\"lines\">");\r
+ sb.append("<div class=\"sourceview\">");\r
+ if (prettyPrint) {\r
+ sb.append("<pre class=\"prettyprint\">");\r
+ } else {\r
+ sb.append("<pre class=\"plainprint\">");\r
+ }\r
+ sb.append(StringUtils.escapeForHtml(source, true));\r
+ sb.append("</pre>");\r
+ sb.append("</div>");\r
+ sb.append("</td>");\r
+ sb.append("<!-- end lines column -->");\r
+ \r
+ sb.append("</tr></tbody></table>");\r
+ sb.append("<!-- end blob table -->");\r
+ \r
+ return sb.toString();\r
+ }\r
+ \r
+ protected String generateSourceTable(String source, boolean prettyPrint) {\r
+ String [] lines = source.split("\n");\r
+ \r
+ // be careful adding line breaks to this method\r
+ // GoogleCode Prettify is sensitive\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("<!-- start blob table -->");\r
+ sb.append("<table width=\"100%\"><tbody><tr>");\r
+ \r
+ // nums column\r
+ sb.append("<!-- start nums column -->");\r
+ sb.append("<td id=\"nums\">");\r
+ sb.append("<pre><table width=\"100%\"><tbody>");\r
+ String numPattern = "<tr><td id=\"L{0}\"><a href=\"#L{0}\">{0}</a></td></tr>";\r
+ for (int i = 0; i < lines.length; i++) {\r
+ sb.append(MessageFormat.format(numPattern, "" + (i + 1)));\r
+ }\r
+ sb.append("<!-- end nums column -->");\r
+ sb.append("</tbody></table></pre>");\r
+ sb.append("</td>");\r
+ \r
+ sb.append("<!-- start lines column -->");\r
+ sb.append("<td id=\"lines\">");\r
+ if (prettyPrint) {\r
+ sb.append("<pre style=\"border: 0px;\" class=\"prettyprint\">");\r
+ } else {\r
+ sb.append("<pre class=\"plainprint\">");\r
+ }\r
+ sb.append("<table width=\"100%\"><tbody>");\r
+ \r
+ String linePattern = "<tr class=\"{0}\"><td>{1}</tr>";\r
+ for (int i = 0; i < lines.length; i++) {\r
+ String l = StringUtils.escapeForHtml(lines[i], true);\r
+ String cssClass = (i % 2 == 0) ? "even" : "odd";\r
+ sb.append(MessageFormat.format(linePattern, cssClass, l));\r
+ }\r
+ sb.append("</tbody></table></pre>");\r
+ sb.append("</td>");\r
+ sb.append("<!-- end lines column -->");\r
+ \r
+ sb.append("</tr></tbody></table>");\r
+ sb.append("<!-- end blob table -->");\r
+ \r
+ return sb.toString();\r
+ }\r
\r
@Override\r
protected String getPageName() {\r