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.

MarkdownUtils.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.utils;
  17. import static org.pegdown.Extensions.ALL;
  18. import static org.pegdown.Extensions.SMARTYPANTS;
  19. import java.io.IOException;
  20. import java.io.Reader;
  21. import java.io.StringWriter;
  22. import java.text.MessageFormat;
  23. import org.apache.commons.io.IOUtils;
  24. import org.pegdown.LinkRenderer;
  25. import org.pegdown.PegDownProcessor;
  26. import com.gitblit.IStoredSettings;
  27. import com.gitblit.Keys;
  28. /**
  29. * Utility methods for transforming raw markdown text to html.
  30. *
  31. * @author James Moger
  32. *
  33. */
  34. public class MarkdownUtils {
  35. /**
  36. * Returns the html version of the plain source text.
  37. *
  38. * @param text
  39. * @return html version of plain text
  40. * @throws java.text.ParseException
  41. */
  42. public static String transformPlainText(String text) {
  43. // url auto-linking
  44. text = text.replaceAll("((http|https)://[0-9A-Za-z-_=\\?\\.\\$#&/]*)", "<a href=\"$1\">$1</a>");
  45. String html = "<pre>" + text + "</pre>";
  46. return html;
  47. }
  48. /**
  49. * Returns the html version of the markdown source text.
  50. *
  51. * @param markdown
  52. * @return html version of markdown text
  53. * @throws java.text.ParseException
  54. */
  55. public static String transformMarkdown(String markdown) {
  56. return transformMarkdown(markdown, null);
  57. }
  58. /**
  59. * Returns the html version of the markdown source text.
  60. *
  61. * @param markdown
  62. * @return html version of markdown text
  63. * @throws java.text.ParseException
  64. */
  65. public static String transformMarkdown(String markdown, LinkRenderer linkRenderer) {
  66. PegDownProcessor pd = new PegDownProcessor(ALL & ~SMARTYPANTS);
  67. String html = pd.markdownToHtml(markdown, linkRenderer == null ? new LinkRenderer() : linkRenderer);
  68. return html;
  69. }
  70. /**
  71. * Returns the html version of the markdown source reader. The reader is
  72. * closed regardless of success or failure.
  73. *
  74. * @param markdownReader
  75. * @return html version of the markdown text
  76. * @throws java.text.ParseException
  77. */
  78. public static String transformMarkdown(Reader markdownReader) throws IOException {
  79. // Read raw markdown content and transform it to html
  80. StringWriter writer = new StringWriter();
  81. try {
  82. IOUtils.copy(markdownReader, writer);
  83. String markdown = writer.toString();
  84. return transformMarkdown(markdown);
  85. } finally {
  86. try {
  87. writer.close();
  88. } catch (IOException e) {
  89. // IGNORE
  90. }
  91. }
  92. }
  93. /**
  94. * Transforms GFM (Github Flavored Markdown) to html.
  95. * Gitblit does not support the complete GFM specification.
  96. *
  97. * @param input
  98. * @param repositoryName
  99. * @return html
  100. */
  101. public static String transformGFM(IStoredSettings settings, String input, String repositoryName) {
  102. String text = input;
  103. // strikethrough
  104. text = text.replaceAll("~~(.*)~~", "<s>$1</s>");
  105. text = text.replaceAll("\\{(?:-){2}(.*)(?:-){2}}", "<s>$1</s>");
  106. // underline
  107. text = text.replaceAll("\\{(?:\\+){2}(.*)(?:\\+){2}}", "<u>$1</u>");
  108. // strikethrough, replacement
  109. text = text.replaceAll("\\{~~(.*)~>(.*)~~}", "<s>$1</s><u>$2</u>");
  110. // highlight
  111. text = text.replaceAll("\\{==(.*)==}", "<span class='highlight'>$1</span>");
  112. String canonicalUrl = settings.getString(Keys.web.canonicalUrl, "https://localhost:8443");
  113. // emphasize and link mentions
  114. String mentionReplacement = String.format(" **<a href=\"%1s/user/$1\">@$1</a>**", canonicalUrl);
  115. text = text.replaceAll("\\s@([A-Za-z0-9-_]+)", mentionReplacement);
  116. // link ticket refs
  117. String ticketReplacement = MessageFormat.format("$1[#$2]({0}/tickets?r={1}&h=$2)$3", canonicalUrl, repositoryName);
  118. text = text.replaceAll("([\\s,]+)#(\\d+)([\\s,:\\.\\n])", ticketReplacement);
  119. // link commit shas
  120. int shaLen = settings.getInteger(Keys.web.shortCommitIdLength, 6);
  121. String commitPattern = MessageFormat.format("\\s([A-Fa-f0-9]'{'{0}'}')([A-Fa-f0-9]'{'{1}'}')", shaLen, 40 - shaLen);
  122. String commitReplacement = String.format(" <a class='commit' href='%1$s/commit?r=%2$s&h=$1$2'>$1</a>", canonicalUrl, repositoryName);
  123. text = text.replaceAll(commitPattern, commitReplacement);
  124. String html = transformMarkdown(text);
  125. return html;
  126. }
  127. }