Document.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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() const
  13. {
  14. StringBuilder builder;
  15. builder.append("<!DOCTYPE html>\n");
  16. builder.append("<html>\n");
  17. builder.append("<head>\n");
  18. builder.append("<style>\n");
  19. builder.append("code { white-space: pre; }\n");
  20. builder.append("</style>\n");
  21. builder.append("</head>\n");
  22. builder.append("<body>\n");
  23. builder.append(render_to_inline_html());
  24. builder.append("</body>\n");
  25. builder.append("</html>\n");
  26. return builder.build();
  27. }
  28. String Document::render_to_inline_html() const
  29. {
  30. return m_container->render_to_html();
  31. }
  32. String Document::render_for_terminal(size_t view_width) const
  33. {
  34. return m_container->render_for_terminal(view_width);
  35. }
  36. RecursionDecision Document::walk(Visitor& visitor) const
  37. {
  38. RecursionDecision rd = visitor.visit(*this);
  39. if (rd != RecursionDecision::Recurse)
  40. return rd;
  41. return m_container->walk(visitor);
  42. }
  43. OwnPtr<Document> Document::parse(StringView str)
  44. {
  45. Vector<StringView> const lines_vec = str.lines();
  46. LineIterator lines(lines_vec.begin());
  47. return make<Document>(ContainerBlock::parse(lines));
  48. }
  49. }