mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 04:13:11 +00:00
Userland: Add an md command
This command uses LibMarkdown to parse and render Markdown documents either for the terminal using escape sequences, or to HTML. For example, you can now do: $ md ReadMe.md to read the Serenity ReadMe file ^)
This commit is contained in:
parent
2e80b2b32f
commit
3089b539f0
44
Userland/md.cpp
Normal file
44
Userland/md.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <AK/String.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <LibMarkdown/MDDocument.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const char* file_name = nullptr;
|
||||
bool html = false;
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
if (strcmp(argv[i], "--html") == 0)
|
||||
html = true;
|
||||
else
|
||||
file_name = argv[i];
|
||||
|
||||
auto file = CFile::construct();;
|
||||
bool success;
|
||||
if (file_name == nullptr) {
|
||||
success = file->open(STDIN_FILENO, CIODevice::OpenMode::ReadOnly, CFile::ShouldCloseFileDescription::No);
|
||||
} else {
|
||||
file->set_filename(file_name);
|
||||
success = file->open(CIODevice::OpenMode::ReadOnly);
|
||||
}
|
||||
if (!success) {
|
||||
fprintf(stderr, "Error: %s\n", file->error_string());
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto buffer = file->read_all();
|
||||
dbg() << "Read size " << buffer.size();
|
||||
|
||||
String input { (char*)buffer.data(), buffer.size() };
|
||||
MDDocument document;
|
||||
success = document.parse(input);
|
||||
|
||||
if (!success) {
|
||||
fprintf(stderr, "Error parsing\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
String res = html ? document.render_to_html() : document.render_for_terminal();
|
||||
printf("%s", res.characters());
|
||||
}
|
Loading…
Reference in a new issue