2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-15 00:29:06 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-05-18 23:58:00 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-09-21 00:46:34 +03:00
|
|
|
#include <AK/String.h>
|
2020-08-05 22:09:35 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/File.h>
|
2020-04-28 22:04:25 +03:00
|
|
|
#include <LibMarkdown/Document.h>
|
2019-09-21 00:46:34 +03:00
|
|
|
#include <stdio.h>
|
2020-03-08 14:05:14 +03:00
|
|
|
#include <string.h>
|
2020-09-20 15:11:04 +03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2019-09-21 00:46:34 +03:00
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2020-09-20 15:11:04 +03:00
|
|
|
if (pledge("stdio rpath tty", nullptr) < 0) {
|
2020-01-13 03:12:18 +03:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-21 00:46:34 +03:00
|
|
|
const char* file_name = nullptr;
|
|
|
|
bool html = false;
|
2020-09-20 15:11:04 +03:00
|
|
|
int view_width = 0;
|
2019-09-21 00:46:34 +03:00
|
|
|
|
2020-08-05 22:09:35 +03:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 18:22:58 +03:00
|
|
|
args_parser.set_general_help("Render Markdown to some other format.");
|
2020-08-05 22:09:35 +03:00
|
|
|
args_parser.add_option(html, "Render to HTML rather than for the terminal", "html", 'H');
|
2020-09-20 15:11:04 +03:00
|
|
|
args_parser.add_option(view_width, "Viewport width for the terminal (defaults to current terminal width)", "view-width", 0, "width");
|
2020-08-05 22:09:35 +03:00
|
|
|
args_parser.add_positional_argument(file_name, "Path to Markdown file", "path", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
2019-09-21 00:46:34 +03:00
|
|
|
|
2020-09-20 15:11:04 +03: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;
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 00:29:06 +03:00
|
|
|
auto file = Core::File::construct();
|
2019-09-21 00:46:34 +03:00
|
|
|
bool success;
|
|
|
|
if (file_name == nullptr) {
|
2020-10-25 14:31:27 +03:00
|
|
|
success = file->open(STDIN_FILENO, Core::IODevice::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::No);
|
2019-09-21 00:46:34 +03:00
|
|
|
} else {
|
|
|
|
file->set_filename(file_name);
|
2020-02-02 14:34:39 +03:00
|
|
|
success = file->open(Core::IODevice::OpenMode::ReadOnly);
|
2019-09-21 00:46:34 +03:00
|
|
|
}
|
|
|
|
if (!success) {
|
|
|
|
fprintf(stderr, "Error: %s\n", file->error_string());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-13 03:12:18 +03:00
|
|
|
if (pledge("stdio", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-21 00:46:34 +03:00
|
|
|
auto buffer = file->read_all();
|
2021-01-09 17:09:40 +03:00
|
|
|
dbgln("Read size {}", buffer.size());
|
2019-09-21 00:46:34 +03:00
|
|
|
|
2020-03-01 14:35:09 +03:00
|
|
|
auto input = String::copy(buffer);
|
2020-05-11 20:55:31 +03:00
|
|
|
auto document = Markdown::Document::parse(input);
|
2019-09-21 00:46:34 +03:00
|
|
|
|
2020-05-11 20:55:31 +03:00
|
|
|
if (!document) {
|
2019-09-21 00:46:34 +03:00
|
|
|
fprintf(stderr, "Error parsing\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-20 15:11:04 +03:00
|
|
|
String res = html ? document->render_to_html() : document->render_for_terminal(view_width);
|
2019-09-21 00:46:34 +03:00
|
|
|
printf("%s", res.characters());
|
|
|
|
}
|