commit | author | age
|
f13c4c
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
5fe7df
|
16 |
package com.gitblit.wicket.pages;
|
JM |
17 |
|
16e474
|
18 |
import java.text.MessageFormat;
|
5fe7df
|
19 |
import java.util.HashMap;
|
JM |
20 |
import java.util.Map;
|
|
21 |
|
|
22 |
import org.apache.wicket.Component;
|
|
23 |
import org.apache.wicket.PageParameters;
|
adfbad
|
24 |
import org.apache.wicket.RedirectException;
|
5fe7df
|
25 |
import org.apache.wicket.markup.html.basic.Label;
|
e4f49a
|
26 |
import org.apache.wicket.markup.html.image.Image;
|
608ece
|
27 |
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
|
ff17f7
|
28 |
import org.apache.wicket.markup.html.link.ExternalLink;
|
5fe7df
|
29 |
import org.eclipse.jgit.lib.Repository;
|
JM |
30 |
import org.eclipse.jgit.revwalk.RevCommit;
|
|
31 |
|
155bf7
|
32 |
import com.gitblit.Keys;
|
ff17f7
|
33 |
import com.gitblit.servlet.RawServlet;
|
5fe7df
|
34 |
import com.gitblit.utils.JGitUtils;
|
4ab184
|
35 |
import com.gitblit.utils.StringUtils;
|
a7db57
|
36 |
import com.gitblit.wicket.CacheControl;
|
699e71
|
37 |
import com.gitblit.wicket.CacheControl.LastModified;
|
e4f49a
|
38 |
import com.gitblit.wicket.ExternalImage;
|
dc7c2f
|
39 |
import com.gitblit.wicket.MarkupProcessor;
|
5fe7df
|
40 |
import com.gitblit.wicket.WicketUtils;
|
c1c3c6
|
41 |
import com.gitblit.wicket.panels.CommitHeaderPanel;
|
5fe7df
|
42 |
import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
|
JM |
43 |
|
a7db57
|
44 |
@CacheControl(LastModified.BOOT)
|
5fe7df
|
45 |
public class BlobPage extends RepositoryPage {
|
JM |
46 |
|
ed566a
|
47 |
protected String fileExtension;
|
KM |
48 |
|
5fe7df
|
49 |
public BlobPage(PageParameters params) {
|
cebf45
|
50 |
super(params);
|
5fe7df
|
51 |
|
4ab184
|
52 |
Repository r = getRepository();
|
7d35e2
|
53 |
final String blobPath = WicketUtils.getPath(params);
|
99d0d4
|
54 |
String [] encodings = getEncodings();
|
699e71
|
55 |
|
adfbad
|
56 |
if (StringUtils.isEmpty(objectId) && StringUtils.isEmpty(blobPath)) {
|
JM |
57 |
throw new RedirectException(TreePage.class, WicketUtils.newRepositoryParameter(repositoryName));
|
|
58 |
}
|
|
59 |
|
4ab184
|
60 |
if (StringUtils.isEmpty(blobPath)) {
|
JM |
61 |
// blob by objectid
|
2a7306
|
62 |
|
716745
|
63 |
add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
|
008322
|
64 |
WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
|
JM |
65 |
.setEnabled(false));
|
4ab184
|
66 |
add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
|
ff17f7
|
67 |
String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, blobPath);
|
JM |
68 |
add(new ExternalLink("rawLink", rawUrl));
|
4ab184
|
69 |
add(new CommitHeaderPanel("commitHeader", objectId));
|
JM |
70 |
add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
|
ae9e15
|
71 |
Component c = new Label("blobText", JGitUtils.getStringContent(r, objectId, encodings));
|
4ab184
|
72 |
WicketUtils.setCssClass(c, "plainprint");
|
5fe7df
|
73 |
add(c);
|
JM |
74 |
} else {
|
4ab184
|
75 |
// standard blob view
|
JM |
76 |
String extension = null;
|
|
77 |
if (blobPath.lastIndexOf('.') > -1) {
|
|
78 |
extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
|
|
79 |
}
|
|
80 |
|
dc7c2f
|
81 |
// see if we should redirect to the doc page
|
11a173
|
82 |
MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
|
dc7c2f
|
83 |
for (String ext : processor.getMarkupExtensions()) {
|
4ab184
|
84 |
if (ext.equals(extension)) {
|
dc7c2f
|
85 |
setResponsePage(DocPage.class, params);
|
4ab184
|
86 |
return;
|
JM |
87 |
}
|
|
88 |
}
|
|
89 |
|
0fec67
|
90 |
RevCommit commit = getCommit();
|
4ab184
|
91 |
|
JM |
92 |
// blob page links
|
716745
|
93 |
add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
|
JM |
94 |
WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
|
4ab184
|
95 |
add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
|
JM |
96 |
WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
|
ff17f7
|
97 |
String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, blobPath);
|
JM |
98 |
add(new ExternalLink("rawLink", rawUrl));
|
4ab184
|
99 |
|
JM |
100 |
add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
|
|
101 |
|
|
102 |
add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
|
|
103 |
|
|
104 |
// Map the extensions to types
|
|
105 |
Map<String, Integer> map = new HashMap<String, Integer>();
|
99d0d4
|
106 |
for (String ext : app().settings().getStrings(Keys.web.prettyPrintExtensions)) {
|
4ab184
|
107 |
map.put(ext.toLowerCase(), 1);
|
JM |
108 |
}
|
99d0d4
|
109 |
for (String ext : app().settings().getStrings(Keys.web.imageExtensions)) {
|
4ab184
|
110 |
map.put(ext.toLowerCase(), 2);
|
JM |
111 |
}
|
99d0d4
|
112 |
for (String ext : app().settings().getStrings(Keys.web.binaryExtensions)) {
|
4ab184
|
113 |
map.put(ext.toLowerCase(), 3);
|
JM |
114 |
}
|
|
115 |
|
|
116 |
if (extension != null) {
|
|
117 |
int type = 0;
|
|
118 |
if (map.containsKey(extension)) {
|
|
119 |
type = map.get(extension);
|
|
120 |
}
|
|
121 |
switch (type) {
|
|
122 |
case 2:
|
e4f49a
|
123 |
// image blobs
|
16e474
|
124 |
add(new Label("blobText").setVisible(false));
|
ff17f7
|
125 |
add(new ExternalImage("blobImage", rawUrl));
|
4ab184
|
126 |
break;
|
JM |
127 |
case 3:
|
e4f49a
|
128 |
// binary blobs
|
16e474
|
129 |
add(new Label("blobText", "Binary File"));
|
JM |
130 |
add(new Image("blobImage").setVisible(false));
|
4ab184
|
131 |
break;
|
JM |
132 |
default:
|
|
133 |
// plain text
|
16e474
|
134 |
String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
|
f56547
|
135 |
String table;
|
JM |
136 |
if (source == null) {
|
|
137 |
table = missingBlob(blobPath, commit);
|
|
138 |
} else {
|
ed566a
|
139 |
table = generateSourceView(source, extension, type == 1);
|
4dee2c
|
140 |
addBottomScriptInline("jQuery(prettyPrint);");
|
f56547
|
141 |
}
|
16e474
|
142 |
add(new Label("blobText", table).setEscapeModelStrings(false));
|
JM |
143 |
add(new Image("blobImage").setVisible(false));
|
ed566a
|
144 |
fileExtension = extension;
|
4ab184
|
145 |
}
|
JM |
146 |
} else {
|
|
147 |
// plain text
|
16e474
|
148 |
String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
|
f56547
|
149 |
String table;
|
JM |
150 |
if (source == null) {
|
|
151 |
table = missingBlob(blobPath, commit);
|
|
152 |
} else {
|
ed566a
|
153 |
table = generateSourceView(source, null, false);
|
4dee2c
|
154 |
addBottomScriptInline("jQuery(prettyPrint);");
|
f56547
|
155 |
}
|
16e474
|
156 |
add(new Label("blobText", table).setEscapeModelStrings(false));
|
e4f49a
|
157 |
add(new Image("blobImage").setVisible(false));
|
4ab184
|
158 |
}
|
5fe7df
|
159 |
}
|
JM |
160 |
}
|
699e71
|
161 |
|
f56547
|
162 |
protected String missingBlob(String blobPath, RevCommit commit) {
|
JM |
163 |
StringBuilder sb = new StringBuilder();
|
|
164 |
sb.append("<div class=\"alert alert-error\">");
|
|
165 |
String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>");
|
|
166 |
sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName()));
|
|
167 |
sb.append("</div>");
|
|
168 |
return sb.toString();
|
|
169 |
}
|
ed566a
|
170 |
|
KM |
171 |
protected String generateSourceView(String source, String extension, boolean prettyPrint) {
|
16e474
|
172 |
String [] lines = source.split("\n");
|
699e71
|
173 |
|
16e474
|
174 |
StringBuilder sb = new StringBuilder();
|
JM |
175 |
sb.append("<!-- start blob table -->");
|
|
176 |
sb.append("<table width=\"100%\"><tbody><tr>");
|
699e71
|
177 |
|
16e474
|
178 |
// nums column
|
JM |
179 |
sb.append("<!-- start nums column -->");
|
|
180 |
sb.append("<td id=\"nums\">");
|
|
181 |
sb.append("<pre>");
|
4f0f65
|
182 |
String numPattern = "<span id=\"L{0}\" class=\"jump\"></span><a href=\"#L{0}\">{0}</a>\n";
|
16e474
|
183 |
for (int i = 0; i < lines.length; i++) {
|
JM |
184 |
sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
|
|
185 |
}
|
|
186 |
sb.append("</pre>");
|
|
187 |
sb.append("<!-- end nums column -->");
|
|
188 |
sb.append("</td>");
|
699e71
|
189 |
|
16e474
|
190 |
sb.append("<!-- start lines column -->");
|
JM |
191 |
sb.append("<td id=\"lines\">");
|
|
192 |
sb.append("<div class=\"sourceview\">");
|
|
193 |
if (prettyPrint) {
|
ed566a
|
194 |
sb.append("<pre class=\"prettyprint lang-" + extension + "\">");
|
16e474
|
195 |
} else {
|
JM |
196 |
sb.append("<pre class=\"plainprint\">");
|
|
197 |
}
|
310a80
|
198 |
final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);
|
JM |
199 |
lines = StringUtils.escapeForHtml(source, true, tabLength).split("\n");
|
699e71
|
200 |
|
16e474
|
201 |
sb.append("<table width=\"100%\"><tbody>");
|
699e71
|
202 |
|
4f0f65
|
203 |
String linePattern = "<tr class=\"{0}\"><td><div><span class=\"line\">{1}</span></div>\r</tr>";
|
16e474
|
204 |
for (int i = 0; i < lines.length; i++) {
|
5c6afb
|
205 |
String line = lines[i].replace('\r', ' ');
|
16e474
|
206 |
String cssClass = (i % 2 == 0) ? "even" : "odd";
|
4f0f65
|
207 |
if (StringUtils.isEmpty(line.trim())) {
|
JM |
208 |
line = " ";
|
|
209 |
}
|
5c6afb
|
210 |
sb.append(MessageFormat.format(linePattern, cssClass, line, "" + (i + 1)));
|
16e474
|
211 |
}
|
JM |
212 |
sb.append("</tbody></table></pre>");
|
9a73d1
|
213 |
sb.append("</pre>");
|
JM |
214 |
sb.append("</div>");
|
16e474
|
215 |
sb.append("</td>");
|
JM |
216 |
sb.append("<!-- end lines column -->");
|
699e71
|
217 |
|
16e474
|
218 |
sb.append("</tr></tbody></table>");
|
JM |
219 |
sb.append("<!-- end blob table -->");
|
699e71
|
220 |
|
16e474
|
221 |
return sb.toString();
|
JM |
222 |
}
|
155bf7
|
223 |
|
cebf45
|
224 |
@Override
|
JM |
225 |
protected String getPageName() {
|
1e47ab
|
226 |
return getString("gb.view");
|
cebf45
|
227 |
}
|
699e71
|
228 |
|
6ef8d7
|
229 |
@Override
|
5d5e55
|
230 |
protected boolean isCommitPage() {
|
JM |
231 |
return true;
|
|
232 |
}
|
|
233 |
|
|
234 |
@Override
|
6ef8d7
|
235 |
protected Class<? extends BasePage> getRepoNavPageClass() {
|
JM |
236 |
return TreePage.class;
|
|
237 |
}
|
5fe7df
|
238 |
}
|