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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. }
  40. }
  41. }
  42. public static String transformMarkdown(Reader markdownReader) throws java.text.ParseException {
  43. // Read raw markdown content and transform it to html
  44. StringWriter writer = new StringWriter();
  45. try {
  46. Markdown md = new Markdown();
  47. md.transform(markdownReader, writer);
  48. return writer.toString();
  49. } catch (ParseException p) {
  50. throw new java.text.ParseException(p.getMessage(), 0);
  51. } finally {
  52. try {
  53. markdownReader.close();
  54. } catch (IOException e) {
  55. }
  56. try {
  57. writer.close();
  58. } catch (IOException e) {
  59. }
  60. }
  61. }
  62. }