List.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/List.h>
  28. namespace Markdown {
  29. String List::render_to_html() const
  30. {
  31. StringBuilder builder;
  32. const char* tag = m_is_ordered ? "ol" : "ul";
  33. builder.appendf("<%s>", tag);
  34. for (auto& item : m_items) {
  35. builder.append("<li>");
  36. builder.append(item.render_to_html());
  37. builder.append("</li>\n");
  38. }
  39. builder.appendf("</%s>\n", tag);
  40. return builder.build();
  41. }
  42. String List::render_for_terminal(size_t) const
  43. {
  44. StringBuilder builder;
  45. int i = 0;
  46. for (auto& item : m_items) {
  47. builder.append(" ");
  48. if (m_is_ordered)
  49. builder.appendf("%d. ", ++i);
  50. else
  51. builder.append("* ");
  52. builder.append(item.render_for_terminal());
  53. builder.append("\n");
  54. }
  55. builder.append("\n");
  56. return builder.build();
  57. }
  58. OwnPtr<List> List::parse(Vector<StringView>::ConstIterator& lines)
  59. {
  60. Vector<Text> items;
  61. bool is_ordered = false;
  62. bool first = true;
  63. size_t offset = 0;
  64. StringBuilder item_builder;
  65. auto flush_item_if_needed = [&] {
  66. if (first)
  67. return true;
  68. auto text = Text::parse(item_builder.string_view());
  69. if (!text.has_value())
  70. return false;
  71. items.append(move(text.value()));
  72. item_builder.clear();
  73. return true;
  74. };
  75. while (true) {
  76. if (lines.is_end())
  77. break;
  78. const StringView& line = *lines;
  79. if (line.is_empty())
  80. break;
  81. bool appears_unordered = false;
  82. if (line.length() > 2) {
  83. if (line[1] == ' ' && (line[0] == '*' || line[0] == '-')) {
  84. appears_unordered = true;
  85. offset = 2;
  86. }
  87. }
  88. bool appears_ordered = false;
  89. for (size_t i = 0; i < 10 && i < line.length(); i++) {
  90. char ch = line[i];
  91. if ('0' <= ch && ch <= '9')
  92. continue;
  93. if (ch == '.' || ch == ')')
  94. if (i + 1 < line.length() && line[i + 1] == ' ') {
  95. appears_ordered = true;
  96. offset = i + 1;
  97. }
  98. break;
  99. }
  100. ASSERT(!(appears_unordered && appears_ordered));
  101. if (appears_unordered || appears_ordered) {
  102. if (first)
  103. is_ordered = appears_ordered;
  104. else if (is_ordered != appears_ordered)
  105. return nullptr;
  106. if (!flush_item_if_needed())
  107. return nullptr;
  108. while (offset + 1 < line.length() && line[offset + 1] == ' ')
  109. offset++;
  110. } else {
  111. if (first)
  112. return nullptr;
  113. for (size_t i = 0; i < offset; i++) {
  114. if (line[i] != ' ')
  115. return nullptr;
  116. }
  117. }
  118. first = false;
  119. if (!item_builder.is_empty())
  120. item_builder.append(' ');
  121. ASSERT(offset <= line.length());
  122. item_builder.append(line.substring_view(offset, line.length() - offset));
  123. ++lines;
  124. offset = 0;
  125. }
  126. if (!flush_item_if_needed() || first)
  127. return nullptr;
  128. return make<List>(move(items), is_ordered);
  129. }
  130. }