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.

TableOfContentsGenerator.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.gitblit.markdown;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. public class TableOfContentsGenerator {
  6. public static final String DEFAULT_HEADER = "#";
  7. public static final char DEFAULT_HEADER_CHAR = '#';
  8. public static final String ALT_1_HEADER = "=";
  9. public static final String ALT_2_HEADER = "-";
  10. public static final String TOC_HEADER = "## Table of contents";
  11. private String markdown;
  12. private List<ModifyModel> rootModels = new ArrayList<ModifyModel>();
  13. public TableOfContentsGenerator(String markdown) {
  14. this.markdown = markdown;
  15. }
  16. public String start() {
  17. if (!this.markdown.contains("__TOC__")) {
  18. return this.markdown;
  19. }
  20. int patternLineNumber = -1;
  21. int currentLine = 0;
  22. String[] items = markdown.split("\n");
  23. List<String> fileContent = new ArrayList<String>(Arrays.asList(items));
  24. String previousLine = null;
  25. for (String line : fileContent) {
  26. ++currentLine;
  27. if (line.startsWith(DEFAULT_HEADER)) {
  28. String trim = line.trim();
  29. int count = getCount(trim);
  30. if (count < 1 || count > 6) {
  31. previousLine = line;
  32. continue;
  33. }
  34. String headerName = line.substring(count);
  35. rootModels.add(new ModifyModel(count, headerName, Utils.normalize(headerName)));
  36. } else if (line.startsWith(ALT_1_HEADER) && !Utils.isEmpty(previousLine)) {
  37. if (line.replaceAll(ALT_1_HEADER, "").isEmpty()) {
  38. rootModels.add(new ModifyModel(1, previousLine, Utils.normalize(previousLine)));
  39. }
  40. } else if (line.startsWith(ALT_2_HEADER) && !Utils.isEmpty(previousLine)) {
  41. if (line.replaceAll(ALT_2_HEADER, "").isEmpty()) {
  42. rootModels.add(new ModifyModel(2, previousLine, Utils.normalize(previousLine)));
  43. }
  44. } else if (line.trim().equals("__TOC__")) {
  45. patternLineNumber = currentLine;
  46. }
  47. previousLine = line;
  48. }
  49. return getMarkdown(fileContent, patternLineNumber).replaceAll("__TOC__", "");
  50. }
  51. private String getMarkdown(List<String> fileContent, int patternLineNumber) {
  52. StringBuilder writer = new StringBuilder();
  53. if (patternLineNumber == -1) {
  54. System.out.println("Pattern for replace not found!");
  55. return "";
  56. }
  57. fileContent.add(patternLineNumber - 1, TOC_HEADER);
  58. for (ModifyModel modifyModel : rootModels) {
  59. fileContent.add(patternLineNumber, modifyModel.create());
  60. patternLineNumber++;
  61. }
  62. for (String line : fileContent) {
  63. writer.append(line).append("\n");
  64. }
  65. return writer.toString();
  66. }
  67. private int getCount(String string) {
  68. int count = 0;
  69. for (int i = 0; i < string.length(); i++) {
  70. if (string.charAt(i) == DEFAULT_HEADER_CHAR) {
  71. ++count;
  72. } else {
  73. break;
  74. }
  75. }
  76. return count;
  77. }
  78. }