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