LineIterator.cpp 1.5 KB

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