2021-01-02 04:39:18 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-02 04:39:18 +03:00
|
|
|
*/
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-01-02 04:39:18 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <LibMarkdown/HorizontalRule.h>
|
2021-09-10 22:36:29 +03:00
|
|
|
#include <LibMarkdown/Visitor.h>
|
2022-04-25 07:44:53 +03:00
|
|
|
#include <LibRegex/Regex.h>
|
2021-01-02 04:39:18 +03:00
|
|
|
|
|
|
|
namespace Markdown {
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString HorizontalRule::render_to_html(bool) const
|
2021-01-02 04:39:18 +03:00
|
|
|
{
|
2021-08-30 01:59:41 +03:00
|
|
|
return "<hr />\n";
|
2021-01-02 04:39:18 +03:00
|
|
|
}
|
|
|
|
|
2022-12-23 12:25:00 +03:00
|
|
|
Vector<DeprecatedString> HorizontalRule::render_lines_for_terminal(size_t view_width) const
|
2021-01-02 04:39:18 +03:00
|
|
|
{
|
|
|
|
StringBuilder builder(view_width + 1);
|
|
|
|
for (size_t i = 0; i < view_width; ++i)
|
|
|
|
builder.append('-');
|
2022-07-11 20:32:29 +03:00
|
|
|
builder.append("\n\n"sv);
|
2022-12-23 12:25:00 +03:00
|
|
|
return Vector<DeprecatedString> { builder.to_deprecated_string() };
|
2021-01-02 04:39:18 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 22:36:29 +03:00
|
|
|
RecursionDecision HorizontalRule::walk(Visitor& visitor) const
|
|
|
|
{
|
|
|
|
RecursionDecision rd = visitor.visit(*this);
|
|
|
|
if (rd != RecursionDecision::Recurse)
|
|
|
|
return rd;
|
|
|
|
// Normalize return value.
|
|
|
|
return RecursionDecision::Continue;
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:52:12 +03:00
|
|
|
static Regex<ECMA262> thematic_break_re("^ {0,3}([\\*\\-_])\\s*(\\1\\s*){2,}$");
|
2022-04-25 07:44:53 +03:00
|
|
|
|
2021-09-19 20:14:18 +03:00
|
|
|
OwnPtr<HorizontalRule> HorizontalRule::parse(LineIterator& lines)
|
2021-01-02 04:39:18 +03:00
|
|
|
{
|
|
|
|
if (lines.is_end())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-01-02 04:39:18 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
StringView line = *lines;
|
2021-01-02 04:39:18 +03:00
|
|
|
|
2022-04-25 07:44:53 +03:00
|
|
|
auto match = thematic_break_re.match(line);
|
|
|
|
if (!match.success)
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-01-02 04:39:18 +03:00
|
|
|
|
|
|
|
++lines;
|
|
|
|
return make<HorizontalRule>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|