CodeBlock.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/StringBuilder.h>
  27. #include <LibMarkdown/CodeBlock.h>
  28. namespace Markdown {
  29. Text::Style CodeBlock::style() const
  30. {
  31. if (m_style_spec.spans().is_empty())
  32. return {};
  33. return m_style_spec.spans()[0].style;
  34. }
  35. String CodeBlock::style_language() const
  36. {
  37. if (m_style_spec.spans().is_empty())
  38. return {};
  39. return m_style_spec.spans()[0].text;
  40. }
  41. String CodeBlock::render_to_html() const
  42. {
  43. StringBuilder builder;
  44. String style_language = this->style_language();
  45. Text::Style style = this->style();
  46. if (style.strong)
  47. builder.append("<b>");
  48. if (style.emph)
  49. builder.append("<i>");
  50. if (style_language.is_empty())
  51. builder.append("<code>");
  52. else
  53. builder.appendff("<code class=\"{}\">", style_language);
  54. builder.append(escape_html_entities(m_code));
  55. builder.append("</code>");
  56. if (style.emph)
  57. builder.append("</i>");
  58. if (style.strong)
  59. builder.append("</b>");
  60. builder.append('\n');
  61. return builder.build();
  62. }
  63. String CodeBlock::render_for_terminal(size_t) const
  64. {
  65. StringBuilder builder;
  66. Text::Style style = this->style();
  67. bool needs_styling = style.strong || style.emph;
  68. if (needs_styling) {
  69. builder.append("\033[");
  70. bool first = true;
  71. if (style.strong) {
  72. builder.append('1');
  73. first = false;
  74. }
  75. if (style.emph) {
  76. if (!first)
  77. builder.append(';');
  78. builder.append('4');
  79. }
  80. builder.append('m');
  81. }
  82. builder.append(m_code);
  83. if (needs_styling)
  84. builder.append("\033[0m");
  85. builder.append("\n\n");
  86. return builder.build();
  87. }
  88. OwnPtr<CodeBlock> CodeBlock::parse(Vector<StringView>::ConstIterator& lines)
  89. {
  90. if (lines.is_end())
  91. return nullptr;
  92. constexpr auto tick_tick_tick = "```";
  93. StringView line = *lines;
  94. if (!line.starts_with(tick_tick_tick))
  95. return nullptr;
  96. // Our Markdown extension: we allow
  97. // specifying a style and a language
  98. // for a code block, like so:
  99. //
  100. // ```**sh**
  101. // $ echo hello friends!
  102. // ````
  103. //
  104. // The code block will be made bold,
  105. // and if possible syntax-highlighted
  106. // as appropriate for a shell script.
  107. StringView style_spec = line.substring_view(3, line.length() - 3);
  108. auto spec = Text::parse(style_spec);
  109. if (!spec.has_value())
  110. return nullptr;
  111. ++lines;
  112. bool first = true;
  113. StringBuilder builder;
  114. while (true) {
  115. if (lines.is_end())
  116. break;
  117. line = *lines;
  118. ++lines;
  119. if (line == tick_tick_tick)
  120. break;
  121. if (!first)
  122. builder.append('\n');
  123. builder.append(line);
  124. first = false;
  125. }
  126. return make<CodeBlock>(move(spec.value()), builder.build());
  127. }
  128. }