Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MarkdownUtils.java 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 java.io.IOException;
  19. import java.io.Reader;
  20. import java.io.StringWriter;
  21. import org.apache.commons.io.IOUtils;
  22. import org.pegdown.PegDownProcessor;
  23. /**
  24. * Utility methods for transforming raw markdown text to html.
  25. *
  26. * @author James Moger
  27. *
  28. */
  29. public class MarkdownUtils {
  30. /**
  31. * Returns the html version of the markdown source text.
  32. *
  33. * @param markdown
  34. * @return html version of markdown text
  35. * @throws java.text.ParseException
  36. */
  37. public static String transformMarkdown(String markdown) {
  38. PegDownProcessor pd = new PegDownProcessor(ALL);
  39. String html = pd.markdownToHtml(markdown);
  40. return html;
  41. }
  42. /**
  43. * Returns the html version of the markdown source reader. The reader is
  44. * closed regardless of success or failure.
  45. *
  46. * @param markdownReader
  47. * @return html version of the markdown text
  48. * @throws java.text.ParseException
  49. */
  50. public static String transformMarkdown(Reader markdownReader) throws IOException {
  51. // Read raw markdown content and transform it to html
  52. StringWriter writer = new StringWriter();
  53. try {
  54. IOUtils.copy(markdownReader, writer);
  55. String markdown = writer.toString();
  56. return transformMarkdown(markdown);
  57. } finally {
  58. try {
  59. writer.close();
  60. } catch (IOException e) {
  61. // IGNORE
  62. }
  63. }
  64. }
  65. }