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.1KB

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