MDCodeBlock.cpp 4.5 KB

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