MDText.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/MDText.h>
  28. static String unescape(const StringView& text)
  29. {
  30. StringBuilder builder;
  31. for (size_t i = 0; i < text.length(); ++i) {
  32. if (text[i] == '\\' && i != text.length() - 1) {
  33. builder.append(text[i + 1]);
  34. i++;
  35. continue;
  36. }
  37. builder.append(text[i]);
  38. }
  39. return builder.build();
  40. }
  41. String MDText::render_to_html() const
  42. {
  43. StringBuilder builder;
  44. Vector<String> open_tags;
  45. Style current_style;
  46. for (auto& span : m_spans) {
  47. struct TagAndFlag {
  48. String tag;
  49. bool Style::*flag;
  50. };
  51. TagAndFlag tags_and_flags[] = {
  52. { "i", &Style::emph },
  53. { "b", &Style::strong },
  54. { "code", &Style::code }
  55. };
  56. auto it = open_tags.find([&](const String& open_tag) {
  57. if (open_tag == "a" && current_style.href != span.style.href)
  58. return true;
  59. for (auto& tag_and_flag : tags_and_flags) {
  60. if (open_tag == tag_and_flag.tag && !(span.style.*tag_and_flag.flag))
  61. return true;
  62. }
  63. return false;
  64. });
  65. if (!it.is_end()) {
  66. // We found an open tag that should
  67. // not be open for the new span. Close
  68. // it and all the open tags that follow
  69. // it.
  70. for (auto it2 = --open_tags.end(); it2 >= it; --it2) {
  71. const String& tag = *it2;
  72. builder.appendf("</%s>", tag.characters());
  73. if (tag == "a") {
  74. current_style.href = {};
  75. continue;
  76. }
  77. for (auto& tag_and_flag : tags_and_flags)
  78. if (tag == tag_and_flag.tag)
  79. current_style.*tag_and_flag.flag = false;
  80. }
  81. open_tags.shrink(it.index());
  82. }
  83. if (current_style.href.is_null() && !span.style.href.is_null()) {
  84. open_tags.append("a");
  85. builder.appendf("<a href=\"%s\">", span.style.href.characters());
  86. }
  87. for (auto& tag_and_flag : tags_and_flags) {
  88. if (current_style.*tag_and_flag.flag != span.style.*tag_and_flag.flag) {
  89. open_tags.append(tag_and_flag.tag);
  90. builder.appendf("<%s>", tag_and_flag.tag.characters());
  91. }
  92. }
  93. current_style = span.style;
  94. builder.append(span.text);
  95. }
  96. for (auto it = --open_tags.end(); it >= open_tags.begin(); --it) {
  97. const String& tag = *it;
  98. builder.appendf("</%s>", tag.characters());
  99. }
  100. return builder.build();
  101. }
  102. String MDText::render_for_terminal() const
  103. {
  104. StringBuilder builder;
  105. for (auto& span : m_spans) {
  106. bool needs_styling = span.style.strong || span.style.emph || span.style.code;
  107. if (needs_styling) {
  108. builder.append("\033[");
  109. bool first = true;
  110. if (span.style.strong || span.style.code) {
  111. builder.append('1');
  112. first = false;
  113. }
  114. if (span.style.emph) {
  115. if (!first)
  116. builder.append(';');
  117. builder.append('4');
  118. }
  119. builder.append('m');
  120. }
  121. builder.append(span.text.characters());
  122. if (needs_styling)
  123. builder.append("\033[0m");
  124. if (!span.style.href.is_null()) {
  125. // When rendering for the terminal, ignore any
  126. // non-absolute links, because the user has no
  127. // chance to follow them anyway.
  128. if (strstr(span.style.href.characters(), "://") != nullptr) {
  129. builder.appendf(" <%s>", span.style.href.characters());
  130. }
  131. }
  132. }
  133. return builder.build();
  134. }
  135. bool MDText::parse(const StringView& str)
  136. {
  137. Style current_style;
  138. size_t current_span_start = 0;
  139. int first_span_in_the_current_link = -1;
  140. auto append_span_if_needed = [&](size_t offset) {
  141. if (current_span_start != offset) {
  142. Span span {
  143. unescape(str.substring_view(current_span_start, offset - current_span_start)),
  144. current_style
  145. };
  146. m_spans.append(move(span));
  147. }
  148. };
  149. for (size_t offset = 0; offset < str.length(); offset++) {
  150. char ch = str[offset];
  151. bool is_escape = ch == '\\';
  152. if (is_escape && offset != str.length() - 1) {
  153. offset++;
  154. continue;
  155. }
  156. bool is_special_character = false;
  157. is_special_character |= ch == '`';
  158. if (!current_style.code)
  159. is_special_character |= ch == '*' || ch == '_' || ch == '[' || ch == ']';
  160. if (!is_special_character)
  161. continue;
  162. append_span_if_needed(offset);
  163. switch (ch) {
  164. case '`':
  165. current_style.code = !current_style.code;
  166. break;
  167. case '*':
  168. case '_':
  169. if (offset + 1 < str.length() && str[offset + 1] == ch) {
  170. offset++;
  171. current_style.strong = !current_style.strong;
  172. } else {
  173. current_style.emph = !current_style.emph;
  174. }
  175. break;
  176. case '[':
  177. ASSERT(first_span_in_the_current_link == -1);
  178. first_span_in_the_current_link = m_spans.size();
  179. break;
  180. case ']': {
  181. ASSERT(first_span_in_the_current_link != -1);
  182. ASSERT(offset + 2 < str.length());
  183. offset++;
  184. ASSERT(str[offset] == '(');
  185. offset++;
  186. size_t start_of_href = offset;
  187. do
  188. offset++;
  189. while (offset < str.length() && str[offset] != ')');
  190. const StringView href = str.substring_view(start_of_href, offset - start_of_href);
  191. for (size_t i = first_span_in_the_current_link; i < m_spans.size(); i++)
  192. m_spans[i].style.href = href;
  193. first_span_in_the_current_link = -1;
  194. break;
  195. }
  196. default:
  197. ASSERT_NOT_REACHED();
  198. }
  199. current_span_start = offset + 1;
  200. }
  201. append_span_if_needed(str.length());
  202. return true;
  203. }