CodeBlock.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. DeprecatedString 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_deprecated_string();
  44. }
  45. Vector<DeprecatedString> CodeBlock::render_lines_for_terminal(size_t) const
  46. {
  47. Vector<DeprecatedString> 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'))
  56. lines.append(DeprecatedString::formatted("{}{}", indentation, line));
  57. lines.append("");
  58. return lines;
  59. }
  60. RecursionDecision CodeBlock::walk(Visitor& visitor) const
  61. {
  62. RecursionDecision rd = visitor.visit(*this);
  63. if (rd != RecursionDecision::Recurse)
  64. return rd;
  65. rd = visitor.visit(m_code);
  66. if (rd != RecursionDecision::Recurse)
  67. return rd;
  68. // Don't recurse on m_language and m_style.
  69. // Normalize return value.
  70. return RecursionDecision::Continue;
  71. }
  72. static Regex<ECMA262> open_fence_re("^ {0,3}(([\\`\\~])\\2{2,})\\s*([\\*_]*)\\s*([^\\*_\\s]*).*$");
  73. static Regex<ECMA262> close_fence_re("^ {0,3}(([\\`\\~])\\2{2,})\\s*$");
  74. static Optional<int> line_block_prefix(StringView const& line)
  75. {
  76. int characters = 0;
  77. int indents = 0;
  78. for (char ch : line) {
  79. if (indents == 4)
  80. break;
  81. if (ch == ' ') {
  82. ++characters;
  83. ++indents;
  84. } else if (ch == '\t') {
  85. ++characters;
  86. indents = 4;
  87. } else {
  88. break;
  89. }
  90. }
  91. if (indents == 4)
  92. return characters;
  93. return {};
  94. }
  95. OwnPtr<CodeBlock> CodeBlock::parse(LineIterator& lines, Heading* current_section)
  96. {
  97. if (lines.is_end())
  98. return {};
  99. StringView line = *lines;
  100. if (open_fence_re.match(line).success)
  101. return parse_backticks(lines, current_section);
  102. if (line_block_prefix(line).has_value())
  103. return parse_indent(lines);
  104. return {};
  105. }
  106. OwnPtr<CodeBlock> CodeBlock::parse_backticks(LineIterator& lines, Heading* current_section)
  107. {
  108. StringView line = *lines;
  109. // Our Markdown extension: we allow
  110. // specifying a style and a language
  111. // for a code block, like so:
  112. //
  113. // ```**sh**
  114. // $ echo hello friends!
  115. // ````
  116. //
  117. // The code block will be made bold,
  118. // and if possible syntax-highlighted
  119. // as appropriate for a shell script.
  120. auto matches = open_fence_re.match(line).capture_group_matches[0];
  121. auto fence = matches[0].view.string_view();
  122. auto style = matches[2].view.string_view();
  123. auto language = matches[3].view.string_view();
  124. ++lines;
  125. StringBuilder builder;
  126. while (true) {
  127. if (lines.is_end())
  128. break;
  129. line = *lines;
  130. ++lines;
  131. auto close_match = close_fence_re.match(line);
  132. if (close_match.success) {
  133. auto close_fence = close_match.capture_group_matches[0][0].view.string_view();
  134. if (close_fence[0] == fence[0] && close_fence.length() >= fence.length())
  135. break;
  136. }
  137. builder.append(line);
  138. builder.append('\n');
  139. }
  140. return make<CodeBlock>(language, style, builder.to_deprecated_string(), current_section);
  141. }
  142. OwnPtr<CodeBlock> CodeBlock::parse_indent(LineIterator& lines)
  143. {
  144. StringBuilder builder;
  145. while (true) {
  146. if (lines.is_end())
  147. break;
  148. StringView line = *lines;
  149. auto prefix_length = line_block_prefix(line);
  150. if (!prefix_length.has_value())
  151. break;
  152. line = line.substring_view(prefix_length.value());
  153. ++lines;
  154. builder.append(line);
  155. builder.append('\n');
  156. }
  157. return make<CodeBlock>("", "", builder.to_deprecated_string(), nullptr);
  158. }
  159. }