List.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibMarkdown/List.h>
  9. #include <LibMarkdown/Paragraph.h>
  10. namespace Markdown {
  11. String List::render_to_html(bool) const
  12. {
  13. StringBuilder builder;
  14. const char* tag = m_is_ordered ? "ol" : "ul";
  15. builder.appendff("<{}>\n", tag);
  16. for (auto& item : m_items) {
  17. builder.append("<li>");
  18. if (!m_is_tight || (item->blocks().size() != 0 && !dynamic_cast<Paragraph const*>(&(item->blocks()[0]))))
  19. builder.append("\n");
  20. builder.append(item->render_to_html(m_is_tight));
  21. builder.append("</li>\n");
  22. }
  23. builder.appendff("</{}>\n", tag);
  24. return builder.build();
  25. }
  26. String List::render_for_terminal(size_t) const
  27. {
  28. StringBuilder builder;
  29. int i = 0;
  30. for (auto& item : m_items) {
  31. builder.append(" ");
  32. if (m_is_ordered)
  33. builder.appendff("{}. ", ++i);
  34. else
  35. builder.append("* ");
  36. builder.append(item->render_for_terminal());
  37. builder.append("\n");
  38. }
  39. builder.append("\n");
  40. return builder.build();
  41. }
  42. OwnPtr<List> List::parse(LineIterator& lines)
  43. {
  44. Vector<OwnPtr<ContainerBlock>> items;
  45. bool first = true;
  46. bool is_ordered = false;
  47. bool is_tight = true;
  48. bool has_trailing_blank_lines = false;
  49. while (!lines.is_end()) {
  50. size_t offset = 0;
  51. const StringView& line = *lines;
  52. bool appears_unordered = false;
  53. while (offset < line.length() && line[offset] == ' ')
  54. ++offset;
  55. if (offset + 2 <= line.length()) {
  56. if (line[offset + 1] == ' ' && (line[offset] == '*' || line[offset] == '-' || line[offset] == '+')) {
  57. appears_unordered = true;
  58. offset++;
  59. }
  60. }
  61. bool appears_ordered = false;
  62. for (size_t i = offset; i < 10 && i < line.length(); i++) {
  63. char ch = line[i];
  64. if ('0' <= ch && ch <= '9')
  65. continue;
  66. if (ch == '.' || ch == ')')
  67. if (i + 1 < line.length() && line[i + 1] == ' ') {
  68. appears_ordered = true;
  69. offset = i + 1;
  70. }
  71. break;
  72. }
  73. VERIFY(!(appears_unordered && appears_ordered));
  74. if (!appears_unordered && !appears_ordered) {
  75. if (first)
  76. return {};
  77. break;
  78. }
  79. while (offset < line.length() && line[offset] == ' ')
  80. offset++;
  81. if (first) {
  82. is_ordered = appears_ordered;
  83. } else if (appears_ordered != is_ordered) {
  84. break;
  85. }
  86. is_tight = is_tight && !has_trailing_blank_lines;
  87. size_t saved_indent = lines.indent();
  88. lines.set_indent(saved_indent + offset);
  89. lines.ignore_next_prefix();
  90. auto list_item = ContainerBlock::parse(lines);
  91. is_tight = is_tight && !list_item->has_blank_lines();
  92. has_trailing_blank_lines = has_trailing_blank_lines || list_item->has_trailing_blank_lines();
  93. items.append(move(list_item));
  94. lines.set_indent(saved_indent);
  95. first = false;
  96. }
  97. return make<List>(move(items), is_ordered, is_tight);
  98. }
  99. }