HorizontalRule.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibMarkdown/HorizontalRule.h>
  9. namespace Markdown {
  10. String HorizontalRule::render_to_html() const
  11. {
  12. return "<hr>\n";
  13. }
  14. String HorizontalRule::render_for_terminal(size_t view_width) const
  15. {
  16. StringBuilder builder(view_width + 1);
  17. for (size_t i = 0; i < view_width; ++i)
  18. builder.append('-');
  19. builder.append("\n\n");
  20. return builder.to_string();
  21. }
  22. OwnPtr<HorizontalRule> HorizontalRule::parse(Vector<StringView>::ConstIterator& lines)
  23. {
  24. if (lines.is_end())
  25. return {};
  26. const StringView& line = *lines;
  27. if (line.length() < 3)
  28. return {};
  29. if (!line.starts_with('-') && !line.starts_with('_') && !line.starts_with('*'))
  30. return {};
  31. auto first_character = line.characters_without_null_termination()[0];
  32. for (auto ch : line) {
  33. if (ch != first_character)
  34. return {};
  35. }
  36. ++lines;
  37. return make<HorizontalRule>();
  38. }
  39. }