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.

DocsPage.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Component;
  19. import org.apache.wicket.PageParameters;
  20. import org.apache.wicket.behavior.SimpleAttributeModifier;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  23. import org.apache.wicket.markup.html.link.ExternalLink;
  24. import org.apache.wicket.markup.html.panel.Fragment;
  25. import org.apache.wicket.markup.repeater.Item;
  26. import org.apache.wicket.markup.repeater.data.DataView;
  27. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  28. import org.eclipse.jgit.lib.Repository;
  29. import org.eclipse.jgit.revwalk.RevCommit;
  30. import com.gitblit.models.PathModel;
  31. import com.gitblit.servlet.RawServlet;
  32. import com.gitblit.utils.ByteFormat;
  33. import com.gitblit.utils.JGitUtils;
  34. import com.gitblit.utils.StringUtils;
  35. import com.gitblit.wicket.CacheControl;
  36. import com.gitblit.wicket.CacheControl.LastModified;
  37. import com.gitblit.wicket.MarkupProcessor;
  38. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  39. import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
  40. import com.gitblit.wicket.WicketUtils;
  41. import com.gitblit.wicket.panels.LinkPanel;
  42. @CacheControl(LastModified.REPOSITORY)
  43. public class DocsPage extends RepositoryPage {
  44. public DocsPage(PageParameters params) {
  45. super(params);
  46. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  47. Repository r = getRepository();
  48. RevCommit head = JGitUtils.getCommit(r, null);
  49. final String commitId = getBestCommitId(head);
  50. List<String> extensions = processor.getAllExtensions();
  51. List<PathModel> paths = JGitUtils.getDocuments(r, extensions);
  52. List<MarkupDocument> roots = processor.getRootDocs(r, repositoryName, commitId);
  53. Fragment fragment = null;
  54. if (roots.isEmpty()) {
  55. // no identified root documents
  56. fragment = new Fragment("docs", "noIndexFragment", this);
  57. setResponsePage(NoDocsPage.class, params);
  58. } else {
  59. // root documents, use tabbed ui of index/root and document list
  60. fragment = new Fragment("docs", "tabsFragment", this);
  61. ListDataProvider<MarkupDocument> docDp = new ListDataProvider<MarkupDocument>(roots);
  62. // tab titles
  63. DataView<MarkupDocument> tabTitles = new DataView<MarkupDocument>("tabTitle", docDp) {
  64. private static final long serialVersionUID = 1L;
  65. int counter;
  66. @Override
  67. public void populateItem(final Item<MarkupDocument> item) {
  68. MarkupDocument doc = item.getModelObject();
  69. String file = StringUtils.getLastPathElement(doc.documentPath);
  70. file = StringUtils.stripFileExtension(file);
  71. String name = file.replace('_', ' ').replace('-', ' ');
  72. ExternalLink link = new ExternalLink("link", "#" + file);
  73. link.add(new Label("label", name.toUpperCase()).setRenderBodyOnly(true));
  74. item.add(link);
  75. if (counter == 0) {
  76. counter++;
  77. item.add(new SimpleAttributeModifier("class", "active"));
  78. }
  79. }
  80. };
  81. fragment.add(tabTitles);
  82. // tab content
  83. DataView<MarkupDocument> tabsView = new DataView<MarkupDocument>("tabContent", docDp) {
  84. private static final long serialVersionUID = 1L;
  85. int counter;
  86. @Override
  87. public void populateItem(final Item<MarkupDocument> item) {
  88. MarkupDocument doc = item.getModelObject();
  89. // document page links
  90. item.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  91. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  92. item.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  93. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  94. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, doc.documentPath);
  95. item.add(new ExternalLink("rawLink", rawUrl));
  96. // document content
  97. String file = StringUtils.getLastPathElement(doc.documentPath);
  98. file = StringUtils.stripFileExtension(file);
  99. Component content = new Label("content", doc.html)
  100. .setEscapeModelStrings(false);
  101. if (!MarkupSyntax.PLAIN.equals(doc.syntax)) {
  102. content.add(new SimpleAttributeModifier("class", "markdown"));
  103. }
  104. item.add(content);
  105. item.add(new SimpleAttributeModifier("id", file));
  106. if (counter == 0) {
  107. counter++;
  108. item.add(new SimpleAttributeModifier("class", "tab-pane active"));
  109. }
  110. }
  111. };
  112. fragment.add(tabsView);
  113. }
  114. // document list
  115. final String id = getBestCommitId(head);
  116. final ByteFormat byteFormat = new ByteFormat();
  117. Fragment docs = new Fragment("documents", "documentsFragment", this);
  118. ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
  119. DataView<PathModel> pathsView = new DataView<PathModel>("document", pathsDp) {
  120. private static final long serialVersionUID = 1L;
  121. int counter;
  122. @Override
  123. public void populateItem(final Item<PathModel> item) {
  124. PathModel entry = item.getModelObject();
  125. item.add(WicketUtils.newImage("docIcon", "file_world_16x16.png"));
  126. item.add(new Label("docSize", byteFormat.format(entry.size)));
  127. item.add(new LinkPanel("docName", "list", StringUtils.stripFileExtension(entry.name),
  128. DocPage.class, WicketUtils.newPathParameter(repositoryName, id, entry.path)));
  129. // links
  130. item.add(new BookmarkablePageLink<Void>("view", DocPage.class, WicketUtils
  131. .newPathParameter(repositoryName, id, entry.path)));
  132. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, id, entry.path);
  133. item.add(new ExternalLink("raw", rawUrl));
  134. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  135. .newPathParameter(repositoryName, id, entry.path)));
  136. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  137. .newPathParameter(repositoryName, id, entry.path)));
  138. WicketUtils.setAlternatingBackground(item, counter);
  139. counter++;
  140. }
  141. };
  142. docs.add(pathsView);
  143. fragment.add(docs);
  144. add(fragment);
  145. }
  146. @Override
  147. protected String getPageName() {
  148. return getString("gb.docs");
  149. }
  150. }