2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-01-24 13:45:29 +00:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-08-05 19:09:35 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-09-14 15:26:53 +00:00
|
|
|
#include <LibCore/Stream.h>
|
2022-01-23 17:10:52 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-04-28 19:04:25 +00:00
|
|
|
#include <LibMarkdown/Document.h>
|
2020-09-20 12:11:04 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2019-09-20 21:46:34 +00:00
|
|
|
|
2022-01-23 17:10:52 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-09-20 21:46:34 +00:00
|
|
|
{
|
2022-01-23 17:10:52 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty"));
|
2020-01-13 00:12:18 +00:00
|
|
|
|
2022-09-14 15:26:53 +00:00
|
|
|
StringView filename;
|
2019-09-20 21:46:34 +00:00
|
|
|
bool html = false;
|
2020-09-20 12:11:04 +00:00
|
|
|
int view_width = 0;
|
2019-09-20 21:46:34 +00:00
|
|
|
|
2020-08-05 19:09:35 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("Render Markdown to some other format.");
|
2020-08-05 19:09:35 +00:00
|
|
|
args_parser.add_option(html, "Render to HTML rather than for the terminal", "html", 'H');
|
2020-09-20 12:11:04 +00:00
|
|
|
args_parser.add_option(view_width, "Viewport width for the terminal (defaults to current terminal width)", "view-width", 0, "width");
|
2021-04-29 19:46:15 +00:00
|
|
|
args_parser.add_positional_argument(filename, "Path to Markdown file", "path", Core::ArgsParser::Required::No);
|
2022-01-23 17:10:52 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-09-20 21:46:34 +00:00
|
|
|
|
2020-09-20 12:11:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 15:26:53 +00:00
|
|
|
|
|
|
|
auto file = TRY(Core::Stream::File::open_file_or_standard_stream(filename, Core::Stream::OpenMode::Read));
|
2019-09-20 21:46:34 +00:00
|
|
|
|
2022-01-23 17:10:52 +00:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-01-13 00:12:18 +00:00
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto buffer = TRY(file->read_until_eof());
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("Read size {}", buffer.size());
|
2019-09-20 21:46:34 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
auto input = DeprecatedString::copy(buffer);
|
2020-05-11 17:55:31 +00:00
|
|
|
auto document = Markdown::Document::parse(input);
|
2019-09-20 21:46:34 +00:00
|
|
|
|
2020-05-11 17:55:31 +00:00
|
|
|
if (!document) {
|
2021-06-01 18:29:42 +00:00
|
|
|
warnln("Error parsing Markdown document");
|
2019-09-20 21:46:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString res = html ? document->render_to_html() : document->render_for_terminal(view_width);
|
2021-05-31 14:43:25 +00:00
|
|
|
out("{}", res);
|
2022-01-23 17:10:52 +00:00
|
|
|
return 0;
|
2019-09-20 21:46:34 +00:00
|
|
|
}
|