Serenity Operating System
1/*
2 * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibMarkdown/LineIterator.h>
8
9namespace Markdown {
10
11void LineIterator::reset_ignore_prefix()
12{
13 for (auto& context : m_context_stack) {
14 context.ignore_prefix = false;
15 }
16}
17
18Optional<StringView> LineIterator::match_context(StringView line) const
19{
20 bool is_ws = line.is_whitespace();
21 size_t offset = 0;
22 for (auto& context : m_context_stack) {
23 switch (context.type) {
24 case Context::Type::ListItem:
25 if (is_ws)
26 break;
27
28 if (offset + context.indent > line.length())
29 return {};
30
31 if (!context.ignore_prefix && !line.substring_view(offset, context.indent).is_whitespace())
32 return {};
33
34 offset += context.indent;
35
36 break;
37 case Context::Type::BlockQuote:
38 for (; offset < line.length() && line[offset] == ' '; ++offset) { }
39 if (offset >= line.length() || line[offset] != '>') {
40 return {};
41 }
42 ++offset;
43 break;
44 }
45
46 if (offset > line.length())
47 return {};
48 }
49 return line.substring_view(offset);
50}
51
52bool LineIterator::is_end() const
53{
54 return m_iterator.is_end() || !match_context(*m_iterator).has_value();
55}
56
57StringView LineIterator::operator*() const
58{
59 auto line = match_context(*m_iterator);
60 VERIFY(line.has_value());
61 return line.value();
62}
63
64}