md.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/OwnPtr.h>
  28. #include <AK/String.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/File.h>
  31. #include <LibMarkdown/Document.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/ioctl.h>
  35. #include <unistd.h>
  36. int main(int argc, char* argv[])
  37. {
  38. if (pledge("stdio rpath tty", nullptr) < 0) {
  39. perror("pledge");
  40. return 1;
  41. }
  42. const char* file_name = nullptr;
  43. bool html = false;
  44. int view_width = 0;
  45. Core::ArgsParser args_parser;
  46. args_parser.add_option(html, "Render to HTML rather than for the terminal", "html", 'H');
  47. args_parser.add_option(view_width, "Viewport width for the terminal (defaults to current terminal width)", "view-width", 0, "width");
  48. args_parser.add_positional_argument(file_name, "Path to Markdown file", "path", Core::ArgsParser::Required::No);
  49. args_parser.parse(argc, argv);
  50. if (!html && view_width == 0) {
  51. if (isatty(STDOUT_FILENO)) {
  52. struct winsize ws;
  53. if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) < 0)
  54. view_width = 80;
  55. else
  56. view_width = ws.ws_col;
  57. } else {
  58. view_width = 80;
  59. }
  60. }
  61. auto file = Core::File::construct();
  62. bool success;
  63. if (file_name == nullptr) {
  64. success = file->open(STDIN_FILENO, Core::IODevice::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescription::No);
  65. } else {
  66. file->set_filename(file_name);
  67. success = file->open(Core::IODevice::OpenMode::ReadOnly);
  68. }
  69. if (!success) {
  70. fprintf(stderr, "Error: %s\n", file->error_string());
  71. return 1;
  72. }
  73. if (pledge("stdio", nullptr) < 0) {
  74. perror("pledge");
  75. return 1;
  76. }
  77. auto buffer = file->read_all();
  78. dbg() << "Read size " << buffer.size();
  79. auto input = String::copy(buffer);
  80. auto document = Markdown::Document::parse(input);
  81. if (!document) {
  82. fprintf(stderr, "Error parsing\n");
  83. return 1;
  84. }
  85. String res = html ? document->render_to_html() : document->render_for_terminal(view_width);
  86. printf("%s", res.characters());
  87. }