From e44abaa777a038119e1d27aa7aa02883ee954450 Mon Sep 17 00:00:00 2001 From: Carwyn Nelson Date: Tue, 13 Jun 2023 16:15:13 +0100 Subject: [PATCH] LibMarkdown: Convert render_to_terminal to String This commit converts render_to_terminal from DeprecatedString to return an ErrorOr. This is to aid moving `man` away from DeprecatedString. I have opted not to convert render_to_html and render_to_inline_html for now to keep this commit as small as possible. --- Userland/Libraries/LibMarkdown/Document.cpp | 8 ++++---- Userland/Libraries/LibMarkdown/Document.h | 3 ++- Userland/Utilities/man.cpp | 3 ++- Userland/Utilities/md.cpp | 8 ++++++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibMarkdown/Document.cpp b/Userland/Libraries/LibMarkdown/Document.cpp index 6e6ff08041..0912744539 100644 --- a/Userland/Libraries/LibMarkdown/Document.cpp +++ b/Userland/Libraries/LibMarkdown/Document.cpp @@ -43,15 +43,15 @@ DeprecatedString Document::render_to_inline_html() const return m_container->render_to_html(); } -DeprecatedString Document::render_for_terminal(size_t view_width) const +ErrorOr Document::render_for_terminal(size_t view_width) const { StringBuilder builder; for (auto& line : m_container->render_lines_for_terminal(view_width)) { - builder.append(line); - builder.append("\n"sv); + TRY(builder.try_append(line)); + TRY(builder.try_append("\n"sv)); } - return builder.to_deprecated_string(); + return builder.to_string(); } RecursionDecision Document::walk(Visitor& visitor) const diff --git a/Userland/Libraries/LibMarkdown/Document.h b/Userland/Libraries/LibMarkdown/Document.h index 782ad6d317..26016e3bc8 100644 --- a/Userland/Libraries/LibMarkdown/Document.h +++ b/Userland/Libraries/LibMarkdown/Document.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -21,7 +22,7 @@ public: } DeprecatedString render_to_html(StringView extra_head_contents = ""sv) const; DeprecatedString render_to_inline_html() const; - DeprecatedString render_for_terminal(size_t view_width = 0) const; + ErrorOr render_for_terminal(size_t view_width = 0) const; /* * Walk recursively through the document tree. Returning `RecursionDecision::Recurse` from diff --git a/Userland/Utilities/man.cpp b/Userland/Utilities/man.cpp index cdd654bbcd..875dcd1fac 100644 --- a/Userland/Utilities/man.cpp +++ b/Userland/Utilities/man.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -100,7 +101,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto document = Markdown::Document::parse(source); VERIFY(document); - DeprecatedString rendered = document->render_for_terminal(view_width); + auto rendered = TRY(document->render_for_terminal(view_width)); outln("{}", rendered); // FIXME: Remove this wait, it shouldn't be necessary but Shell does not diff --git a/Userland/Utilities/md.cpp b/Userland/Utilities/md.cpp index c4fbd23b92..16a2c25d13 100644 --- a/Userland/Utilities/md.cpp +++ b/Userland/Utilities/md.cpp @@ -56,7 +56,11 @@ ErrorOr serenity_main(Main::Arguments arguments) return 1; } - DeprecatedString res = html ? document->render_to_html() : document->render_for_terminal(view_width); - out("{}", res); + if (html) { + out("{}", document->render_to_html()); + } else { + out("{}", TRY(document->render_for_terminal(view_width))); + } + return 0; }