Document.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/CodeBlock.h>
  28. #include <LibMarkdown/Document.h>
  29. #include <LibMarkdown/Heading.h>
  30. #include <LibMarkdown/List.h>
  31. #include <LibMarkdown/Paragraph.h>
  32. #include <LibMarkdown/Table.h>
  33. namespace Markdown {
  34. String Document::render_to_html() const
  35. {
  36. StringBuilder builder;
  37. builder.append("<!DOCTYPE html>\n");
  38. builder.append("<html>\n");
  39. builder.append("<head>\n");
  40. builder.append("<style>\n");
  41. builder.append("code { white-space: pre; }\n");
  42. builder.append("</style>\n");
  43. builder.append("</head>\n");
  44. builder.append("<body>\n");
  45. for (auto& block : m_blocks) {
  46. auto s = block.render_to_html();
  47. builder.append(s);
  48. }
  49. builder.append("</body>\n");
  50. builder.append("</html>\n");
  51. return builder.build();
  52. }
  53. String Document::render_for_terminal(size_t view_width) const
  54. {
  55. StringBuilder builder;
  56. for (auto& block : m_blocks) {
  57. auto s = block.render_for_terminal(view_width);
  58. builder.append(s);
  59. }
  60. return builder.build();
  61. }
  62. template<typename BlockType>
  63. static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector<Block>& blocks)
  64. {
  65. OwnPtr<BlockType> block = BlockType::parse(lines);
  66. if (!block)
  67. return false;
  68. blocks.append(block.release_nonnull());
  69. return true;
  70. }
  71. OwnPtr<Document> Document::parse(const StringView& str)
  72. {
  73. const Vector<StringView> lines_vec = str.lines();
  74. auto lines = lines_vec.begin();
  75. auto document = make<Document>();
  76. auto& blocks = document->m_blocks;
  77. NonnullOwnPtrVector<Paragraph::Line> paragraph_lines;
  78. auto flush_paragraph = [&] {
  79. if (paragraph_lines.is_empty())
  80. return;
  81. auto paragraph = make<Paragraph>(move(paragraph_lines));
  82. document->m_blocks.append(move(paragraph));
  83. paragraph_lines.clear();
  84. };
  85. while (true) {
  86. if (lines.is_end())
  87. break;
  88. if ((*lines).is_empty()) {
  89. ++lines;
  90. flush_paragraph();
  91. continue;
  92. }
  93. bool any = helper<Table>(lines, blocks) || helper<List>(lines, blocks) || helper<CodeBlock>(lines, blocks)
  94. || helper<Heading>(lines, blocks);
  95. if (any) {
  96. if (!paragraph_lines.is_empty()) {
  97. auto last_block = document->m_blocks.take_last();
  98. flush_paragraph();
  99. document->m_blocks.append(move(last_block));
  100. }
  101. continue;
  102. }
  103. auto line = Paragraph::Line::parse(lines);
  104. if (!line)
  105. return nullptr;
  106. paragraph_lines.append(line.release_nonnull());
  107. }
  108. if (!paragraph_lines.is_empty())
  109. flush_paragraph();
  110. return document;
  111. }
  112. }