Document.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. namespace Markdown {
  33. String Document::render_to_html() const
  34. {
  35. StringBuilder builder;
  36. builder.append("<!DOCTYPE html>\n");
  37. builder.append("<html>\n");
  38. builder.append("<head></head>\n");
  39. builder.append("<body>\n");
  40. for (auto& block : m_blocks) {
  41. auto s = block.render_to_html();
  42. builder.append(s);
  43. }
  44. builder.append("</body>\n");
  45. builder.append("</html>\n");
  46. return builder.build();
  47. }
  48. String Document::render_for_terminal() const
  49. {
  50. StringBuilder builder;
  51. for (auto& block : m_blocks) {
  52. auto s = block.render_for_terminal();
  53. builder.append(s);
  54. }
  55. return builder.build();
  56. }
  57. template<typename BlockType>
  58. static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector<Block>& blocks)
  59. {
  60. NonnullOwnPtr<BlockType> block = make<BlockType>();
  61. bool success = block->parse(lines);
  62. if (!success)
  63. return false;
  64. blocks.append(move(block));
  65. return true;
  66. }
  67. RefPtr<Document> Document::parse(const StringView& str)
  68. {
  69. const Vector<StringView> lines_vec = str.lines();
  70. auto lines = lines_vec.begin();
  71. auto document = adopt(*new Document);
  72. auto& blocks = document->m_blocks;
  73. while (true) {
  74. if (lines.is_end())
  75. break;
  76. if ((*lines).is_empty()) {
  77. ++lines;
  78. continue;
  79. }
  80. bool any = helper<List>(lines, blocks) || helper<Paragraph>(lines, blocks) || helper<CodeBlock>(lines, blocks) || helper<Heading>(lines, blocks);
  81. if (!any)
  82. return nullptr;
  83. }
  84. return document;
  85. }
  86. }