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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Arrays;
  18. import java.util.List;
  19. import org.apache.wicket.Component;
  20. import org.apache.wicket.PageParameters;
  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.panel.Fragment;
  24. import org.apache.wicket.markup.repeater.Item;
  25. import org.apache.wicket.markup.repeater.data.DataView;
  26. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  27. import org.eclipse.jgit.lib.Repository;
  28. import org.eclipse.jgit.revwalk.RevCommit;
  29. import com.gitblit.GitBlit;
  30. import com.gitblit.models.PathModel;
  31. import com.gitblit.utils.ByteFormat;
  32. import com.gitblit.utils.JGitUtils;
  33. import com.gitblit.utils.StringUtils;
  34. import com.gitblit.wicket.CacheControl;
  35. import com.gitblit.wicket.CacheControl.LastModified;
  36. import com.gitblit.wicket.MarkupProcessor;
  37. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  38. import com.gitblit.wicket.WicketUtils;
  39. import com.gitblit.wicket.panels.LinkPanel;
  40. @CacheControl(LastModified.REPOSITORY)
  41. public class DocsPage extends RepositoryPage {
  42. public DocsPage(PageParameters params) {
  43. super(params);
  44. MarkupProcessor processor = new MarkupProcessor(GitBlit.getSettings());
  45. Repository r = getRepository();
  46. RevCommit head = JGitUtils.getCommit(r, null);
  47. List<String> extensions = processor.getMarkupExtensions();
  48. List<PathModel> paths = JGitUtils.getDocuments(r, extensions);
  49. String doc = null;
  50. String markup = null;
  51. String html = null;
  52. List<String> roots = Arrays.asList("home");
  53. // try to find a custom index/root page
  54. for (PathModel path : paths) {
  55. String name = path.name.toLowerCase();
  56. name = StringUtils.stripFileExtension(name);
  57. if (roots.contains(name)) {
  58. doc = path.name;
  59. break;
  60. }
  61. }
  62. if (!StringUtils.isEmpty(doc)) {
  63. // load the document
  64. String [] encodings = GitBlit.getEncodings();
  65. markup = JGitUtils.getStringContent(r, head.getTree(), doc, encodings);
  66. // parse document
  67. MarkupDocument markupDoc = processor.parse(repositoryName, getBestCommitId(head), doc, markup);
  68. html = markupDoc.html;
  69. }
  70. Fragment fragment = null;
  71. if (StringUtils.isEmpty(html)) {
  72. // no custom index/root, use the standard document list
  73. fragment = new Fragment("docs", "noIndexFragment", this);
  74. fragment.add(new Label("header", getString("gb.docs")));
  75. } else {
  76. // custom index/root, use tabbed ui of index/root and document list
  77. fragment = new Fragment("docs", "indexFragment", this);
  78. Component content = new Label("index", html).setEscapeModelStrings(false);
  79. fragment.add(content);
  80. }
  81. // document list
  82. final String id = getBestCommitId(head);
  83. final ByteFormat byteFormat = new ByteFormat();
  84. Fragment docs = new Fragment("documents", "documentsFragment", this);
  85. ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
  86. DataView<PathModel> pathsView = new DataView<PathModel>("document", pathsDp) {
  87. private static final long serialVersionUID = 1L;
  88. int counter;
  89. @Override
  90. public void populateItem(final Item<PathModel> item) {
  91. PathModel entry = item.getModelObject();
  92. item.add(WicketUtils.newImage("docIcon", "file_world_16x16.png"));
  93. item.add(new Label("docSize", byteFormat.format(entry.size)));
  94. item.add(new LinkPanel("docName", "list", entry.name, DocPage.class, WicketUtils
  95. .newPathParameter(repositoryName, id, entry.path)));
  96. // links
  97. item.add(new BookmarkablePageLink<Void>("view", DocPage.class, WicketUtils
  98. .newPathParameter(repositoryName, id, entry.path)));
  99. item.add(new BookmarkablePageLink<Void>("raw", RawPage.class, WicketUtils
  100. .newPathParameter(repositoryName, id, entry.path)));
  101. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  102. .newPathParameter(repositoryName, id, entry.path)));
  103. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  104. .newPathParameter(repositoryName, id, entry.path)));
  105. WicketUtils.setAlternatingBackground(item, counter);
  106. counter++;
  107. }
  108. };
  109. docs.add(pathsView);
  110. fragment.add(docs);
  111. add(fragment);
  112. }
  113. @Override
  114. protected String getPageName() {
  115. return getString("gb.docs");
  116. }
  117. }