Document.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibMarkdown/Document.h>
  9. #include <LibMarkdown/LineIterator.h>
  10. #include <LibMarkdown/Visitor.h>
  11. namespace Markdown {
  12. String Document::render_to_html(StringView extra_head_contents) const
  13. {
  14. StringBuilder builder;
  15. builder.append(R"~~~(<!DOCTYPE html>
  16. <html>
  17. <head>
  18. <style>
  19. code { white-space: pre; }
  20. </style>
  21. )~~~"sv);
  22. if (!extra_head_contents.is_empty())
  23. builder.append(extra_head_contents);
  24. builder.append(R"~~~(
  25. </head>
  26. <body>
  27. )~~~"sv);
  28. builder.append(render_to_inline_html());
  29. builder.append(R"~~~(
  30. </body>
  31. </html>)~~~"sv);
  32. return builder.build();
  33. }
  34. String Document::render_to_inline_html() const
  35. {
  36. return m_container->render_to_html();
  37. }
  38. String Document::render_for_terminal(size_t view_width) const
  39. {
  40. return m_container->render_for_terminal(view_width);
  41. }
  42. RecursionDecision Document::walk(Visitor& visitor) const
  43. {
  44. RecursionDecision rd = visitor.visit(*this);
  45. if (rd != RecursionDecision::Recurse)
  46. return rd;
  47. return m_container->walk(visitor);
  48. }
  49. OwnPtr<Document> Document::parse(StringView str)
  50. {
  51. Vector<StringView> const lines_vec = str.lines();
  52. LineIterator lines(lines_vec.begin());
  53. return make<Document>(ContainerBlock::parse(lines));
  54. }
  55. }