CodeBlock.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Forward.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibJS/MarkupGenerator.h>
  10. #include <LibMarkdown/CodeBlock.h>
  11. #include <LibMarkdown/Visitor.h>
  12. #include <LibRegex/Regex.h>
  13. namespace Markdown {
  14. ByteString CodeBlock::render_to_html(bool) const
  15. {
  16. StringBuilder builder;
  17. builder.append("<pre>"sv);
  18. if (m_style.length() >= 2)
  19. builder.append("<strong>"sv);
  20. else if (m_style.length() >= 2)
  21. builder.append("<em>"sv);
  22. if (m_language.is_empty())
  23. builder.append("<code>"sv);
  24. else
  25. builder.appendff("<code class=\"language-{}\">", escape_html_entities(m_language));
  26. if (m_language == "js") {
  27. auto html_or_error = JS::MarkupGenerator::html_from_source(m_code);
  28. if (html_or_error.is_error()) {
  29. warnln("Could not render js code to html: {}", html_or_error.error());
  30. builder.append(escape_html_entities(m_code));
  31. } else {
  32. builder.append(html_or_error.release_value());
  33. }
  34. } else {
  35. builder.append(escape_html_entities(m_code));
  36. }
  37. builder.append("</code>"sv);
  38. if (m_style.length() >= 2)
  39. builder.append("</strong>"sv);
  40. else if (m_style.length() >= 2)
  41. builder.append("</em>"sv);
  42. builder.append("</pre>\n"sv);
  43. return builder.to_byte_string();
  44. }
  45. Vector<ByteString> CodeBlock::render_lines_for_terminal(size_t) const
  46. {
  47. Vector<ByteString> lines;
  48. // Do not indent too much if we are in the synopsis
  49. auto indentation = " "sv;
  50. if (m_current_section != nullptr) {
  51. auto current_section_name = m_current_section->render_lines_for_terminal()[0];
  52. if (current_section_name.contains("SYNOPSIS"sv))
  53. indentation = " "sv;
  54. }
  55. for (auto const& line : m_code.split('\n', SplitBehavior::KeepEmpty))
  56. lines.append(ByteString::formatted("{}{}", indentation, line));
  57. return lines;
  58. }
  59. RecursionDecision CodeBlock::walk(Visitor& visitor) const
  60. {
  61. RecursionDecision rd = visitor.visit(*this);
  62. if (rd != RecursionDecision::Recurse)
  63. return rd;
  64. rd = visitor.visit(m_code);
  65. if (rd != RecursionDecision::Recurse)
  66. return rd;
  67. // Don't recurse on m_language and m_style.
  68. // Normalize return value.
  69. return RecursionDecision::Continue;
  70. }
  71. static Regex<ECMA262> open_fence_re("^ {0,3}(([\\`\\~])\\2{2,})\\s*([\\*_]*)\\s*([^\\*_\\s]*).*$");
  72. static Regex<ECMA262> close_fence_re("^ {0,3}(([\\`\\~])\\2{2,})\\s*$");
  73. static Optional<int> line_block_prefix(StringView const& line)
  74. {
  75. int characters = 0;
  76. int indents = 0;
  77. for (char ch : line) {
  78. if (indents == 4)
  79. break;
  80. if (ch == ' ') {
  81. ++characters;
  82. ++indents;
  83. } else if (ch == '\t') {
  84. ++characters;
  85. indents = 4;
  86. } else {
  87. break;
  88. }
  89. }
  90. if (indents == 4)
  91. return characters;
  92. return {};
  93. }
  94. OwnPtr<CodeBlock> CodeBlock::parse(LineIterator& lines, Heading* current_section)
  95. {
  96. if (lines.is_end())
  97. return {};
  98. StringView line = *lines;
  99. if (open_fence_re.match(line).success)
  100. return parse_backticks(lines, current_section);
  101. if (line_block_prefix(line).has_value())
  102. return parse_indent(lines);
  103. return {};
  104. }
  105. OwnPtr<CodeBlock> CodeBlock::parse_backticks(LineIterator& lines, Heading* current_section)
  106. {
  107. StringView line = *lines;
  108. // Our Markdown extension: we allow
  109. // specifying a style and a language
  110. // for a code block, like so:
  111. //
  112. // ```**sh**
  113. // $ echo hello friends!
  114. // ````
  115. //
  116. // The code block will be made bold,
  117. // and if possible syntax-highlighted
  118. // as appropriate for a shell script.
  119. auto matches = open_fence_re.match(line).capture_group_matches[0];
  120. auto fence = matches[0].view.string_view();
  121. auto style = matches[2].view.string_view();
  122. auto language = matches[3].view.string_view();
  123. ++lines;
  124. StringBuilder builder;
  125. while (true) {
  126. if (lines.is_end())
  127. break;
  128. line = *lines;
  129. ++lines;
  130. auto close_match = close_fence_re.match(line);
  131. if (close_match.success) {
  132. auto close_fence = close_match.capture_group_matches[0][0].view.string_view();
  133. if (close_fence[0] == fence[0] && close_fence.length() >= fence.length())
  134. break;
  135. }
  136. builder.append(line);
  137. builder.append('\n');
  138. }
  139. return make<CodeBlock>(language, style, builder.to_byte_string(), current_section);
  140. }
  141. OwnPtr<CodeBlock> CodeBlock::parse_indent(LineIterator& lines)
  142. {
  143. StringBuilder builder;
  144. while (true) {
  145. if (lines.is_end())
  146. break;
  147. StringView line = *lines;
  148. auto prefix_length = line_block_prefix(line);
  149. if (!prefix_length.has_value())
  150. break;
  151. line = line.substring_view(prefix_length.value());
  152. ++lines;
  153. builder.append(line);
  154. builder.append('\n');
  155. }
  156. return make<CodeBlock>("", "", builder.to_byte_string(), nullptr);
  157. }
  158. }