ladybird/Userland/Utilities/md.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

66 lines
1.9 KiB
C++

/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <LibMarkdown/Document.h>
#include <sys/ioctl.h>
#include <unistd.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath tty"));
StringView filename;
bool html = false;
int view_width = 0;
Core::ArgsParser args_parser;
args_parser.set_general_help("Render Markdown to some other format.");
args_parser.add_option(html, "Render to HTML rather than for the terminal", "html", 'H');
args_parser.add_option(view_width, "Viewport width for the terminal (defaults to current terminal width)", "view-width", 0, "width");
args_parser.add_positional_argument(filename, "Path to Markdown file", "path", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
if (!html && view_width == 0) {
if (isatty(STDOUT_FILENO)) {
struct winsize ws;
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) < 0)
view_width = 80;
else
view_width = ws.ws_col;
} else {
view_width = 80;
}
}
auto file = TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read));
TRY(Core::System::pledge("stdio"));
auto buffer = TRY(file->read_until_eof());
dbgln("Read size {}", buffer.size());
auto input = ByteString::copy(buffer);
auto document = Markdown::Document::parse(input);
if (!document) {
warnln("Error parsing Markdown document");
return 1;
}
if (html) {
out("{}", document->render_to_html());
} else {
out("{}", TRY(document->render_for_terminal(view_width)));
}
return 0;
}