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 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 java.io.IOException;
  18. import java.io.Reader;
  19. import java.io.StringReader;
  20. import java.io.StringWriter;
  21. import org.tautua.markdownpapers.Markdown;
  22. import org.tautua.markdownpapers.parser.ParseException;
  23. public class MarkdownUtils {
  24. public static String transformMarkdown(String markdown) throws java.text.ParseException {
  25. // Read raw markdown content and transform it to html
  26. StringReader reader = new StringReader(markdown);
  27. StringWriter writer = new StringWriter();
  28. try {
  29. Markdown md = new Markdown();
  30. md.transform(reader, writer);
  31. return writer.toString();
  32. } catch (ParseException p) {
  33. throw new java.text.ParseException(p.getMessage(), 0);
  34. } finally {
  35. reader.close();
  36. try {
  37. writer.close();
  38. } catch (IOException e) {
  39. // IGNORE
  40. }
  41. }
  42. }
  43. public static String transformMarkdown(Reader markdownReader) throws java.text.ParseException {
  44. // Read raw markdown content and transform it to html
  45. StringWriter writer = new StringWriter();
  46. try {
  47. Markdown md = new Markdown();
  48. md.transform(markdownReader, writer);
  49. return writer.toString();
  50. } catch (ParseException p) {
  51. throw new java.text.ParseException(p.getMessage(), 0);
  52. } finally {
  53. try {
  54. markdownReader.close();
  55. } catch (IOException e) {
  56. // IGNORE
  57. }
  58. try {
  59. writer.close();
  60. } catch (IOException e) {
  61. // IGNORE
  62. }
  63. }
  64. }
  65. }