You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocPage.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  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. */
  16. package com.gitblit.wicket.pages;
  17. import java.util.List;
  18. import org.apache.wicket.PageParameters;
  19. import org.apache.wicket.markup.html.basic.Label;
  20. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  21. import org.apache.wicket.markup.html.link.ExternalLink;
  22. import org.apache.wicket.markup.html.panel.Fragment;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.revwalk.RevCommit;
  25. import com.gitblit.models.UserModel;
  26. import com.gitblit.servlet.RawServlet;
  27. import com.gitblit.utils.BugtraqProcessor;
  28. import com.gitblit.utils.JGitUtils;
  29. import com.gitblit.utils.StringUtils;
  30. import com.gitblit.wicket.CacheControl;
  31. import com.gitblit.wicket.GitBlitWebSession;
  32. import com.gitblit.wicket.CacheControl.LastModified;
  33. import com.gitblit.wicket.MarkupProcessor;
  34. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  35. import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
  36. import com.gitblit.wicket.WicketUtils;
  37. @CacheControl(LastModified.BOOT)
  38. public class DocPage extends RepositoryPage {
  39. public DocPage(PageParameters params) {
  40. super(params);
  41. final String path = WicketUtils.getPath(params).replace("%2f", "/").replace("%2F", "/");
  42. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  43. UserModel currentUser = (GitBlitWebSession.get().getUser() != null) ? GitBlitWebSession.get().getUser() : UserModel.ANONYMOUS;
  44. final boolean userCanEdit = currentUser.canEdit(getRepositoryModel());
  45. Repository r = getRepository();
  46. RevCommit commit = JGitUtils.getCommit(r, objectId);
  47. if (commit == null) {
  48. setResponsePage(NoDocsPage.class, params);
  49. return;
  50. }
  51. String [] encodings = getEncodings();
  52. // Read raw markup content and transform it to html
  53. String documentPath = path;
  54. String markupText = JGitUtils.getStringContent(r, commit.getTree(), path, encodings);
  55. // Hunt for document
  56. if (StringUtils.isEmpty(markupText)) {
  57. String name = StringUtils.stripFileExtension(path);
  58. List<String> docExtensions = processor.getAllExtensions();
  59. for (String ext : docExtensions) {
  60. String checkName = name + "." + ext;
  61. markupText = JGitUtils.getStringContent(r, commit.getTree(), checkName, encodings);
  62. if (!StringUtils.isEmpty(markupText)) {
  63. // found it
  64. documentPath = path;
  65. break;
  66. }
  67. }
  68. }
  69. if (markupText == null) {
  70. markupText = "";
  71. }
  72. BugtraqProcessor bugtraq = new BugtraqProcessor(app().settings());
  73. markupText = bugtraq.processText(getRepository(), repositoryName, markupText);
  74. Fragment fragment;
  75. MarkupDocument markupDoc = processor.parse(repositoryName, getBestCommitId(commit), documentPath, markupText);
  76. if (MarkupSyntax.PLAIN.equals(markupDoc.syntax)) {
  77. fragment = new Fragment("doc", "plainContent", this);
  78. } else {
  79. fragment = new Fragment("doc", "markupContent", this);
  80. }
  81. // document page links
  82. fragment.add(new BookmarkablePageLink<Void>("editLink", EditFilePage.class,
  83. WicketUtils.newPathParameter(repositoryName, objectId, documentPath))
  84. .setEnabled(userCanEdit));
  85. fragment.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  86. WicketUtils.newPathParameter(repositoryName, objectId, documentPath)));
  87. fragment.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  88. WicketUtils.newPathParameter(repositoryName, objectId, documentPath)));
  89. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, documentPath);
  90. fragment.add(new ExternalLink("rawLink", rawUrl));
  91. fragment.add(new Label("content", markupDoc.html).setEscapeModelStrings(false));
  92. add(fragment);
  93. }
  94. @Override
  95. protected String getPageName() {
  96. return getString("gb.docs");
  97. }
  98. @Override
  99. protected boolean isCommitPage() {
  100. return true;
  101. }
  102. @Override
  103. protected Class<? extends BasePage> getRepoNavPageClass() {
  104. return DocsPage.class;
  105. }
  106. }