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