MDDocument.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibMarkdown/MDCodeBlock.h>
  28. #include <LibMarkdown/MDDocument.h>
  29. #include <LibMarkdown/MDHeading.h>
  30. #include <LibMarkdown/MDList.h>
  31. #include <LibMarkdown/MDParagraph.h>
  32. String MDDocument::render_to_html() const
  33. {
  34. StringBuilder builder;
  35. builder.append("<!DOCTYPE html>\n");
  36. builder.append("<html>\n");
  37. builder.append("<head></head>\n");
  38. builder.append("<body>\n");
  39. for (auto& block : m_blocks) {
  40. auto s = block.render_to_html();
  41. builder.append(s);
  42. }
  43. builder.append("</body>\n");
  44. builder.append("</html>\n");
  45. return builder.build();
  46. }
  47. String MDDocument::render_for_terminal() const
  48. {
  49. StringBuilder builder;
  50. for (auto& block : m_blocks) {
  51. auto s = block.render_for_terminal();
  52. builder.append(s);
  53. }
  54. return builder.build();
  55. }
  56. template<typename Block>
  57. static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector<MDBlock>& blocks)
  58. {
  59. NonnullOwnPtr<Block> block = make<Block>();
  60. bool success = block->parse(lines);
  61. if (!success)
  62. return false;
  63. blocks.append(move(block));
  64. return true;
  65. }
  66. bool MDDocument::parse(const StringView& str)
  67. {
  68. const Vector<StringView> lines_vec = str.lines();
  69. auto lines = lines_vec.begin();
  70. while (true) {
  71. if (lines.is_end())
  72. return true;
  73. if ((*lines).is_empty()) {
  74. ++lines;
  75. continue;
  76. }
  77. bool any = helper<MDList>(lines, m_blocks) || helper<MDParagraph>(lines, m_blocks) || helper<MDCodeBlock>(lines, m_blocks) || helper<MDHeading>(lines, m_blocks);
  78. if (!any)
  79. return false;
  80. }
  81. }