format.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_regex.h"
  5. #include "snac.h"
  6. /* emoticons, people laughing and such */
  7. struct {
  8. const char *key;
  9. const char *value;
  10. } smileys[] = {
  11. { ":-)", "🙂" },
  12. { ":-D", "😀" },
  13. { "X-D", "😆" },
  14. { ";-)", "😉" },
  15. { "B-)", "😎" },
  16. { ">:-(", "😡" },
  17. { ":-(", "😞" },
  18. { ":-*", "😘" },
  19. { ":-/", "😕" },
  20. { "8-o", "😲" },
  21. { "%-)", "🤪" },
  22. { ":_(", "😢" },
  23. { ":-|", "😐" },
  24. { "<3", "&#128147;" },
  25. { ":facepalm:", "&#129318;" },
  26. { ":shrug:", "&#129335;" },
  27. { ":shrug2:", "&#175;\\_(&#12484;)_/&#175;" },
  28. { ":eyeroll:", "&#128580;" },
  29. { ":beer:", "&#127866;" },
  30. { ":beers:", "&#127867;" },
  31. { ":munch:", "&#128561;" },
  32. { ":thumb:", "&#128077;" },
  33. { NULL, NULL }
  34. };
  35. static d_char *format_line(const char *line)
  36. /* formats a line */
  37. {
  38. d_char *s = xs_str_new(NULL);
  39. char *p, *v;
  40. /* split by markup */
  41. xs *sm = xs_regex_split(line,
  42. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
  43. int n = 0;
  44. p = sm;
  45. while (xs_list_iter(&p, &v)) {
  46. if ((n & 0x1)) {
  47. /* markup */
  48. if (xs_startswith(v, "`")) {
  49. xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
  50. xs *s2 = xs_fmt("<code>%s</code>", s1);
  51. s = xs_str_cat(s, s2);
  52. }
  53. else
  54. if (xs_startswith(v, "**")) {
  55. xs *s1 = xs_crop_i(xs_dup(v), 2, -2);
  56. xs *s2 = xs_fmt("<b>%s</b>", s1);
  57. s = xs_str_cat(s, s2);
  58. }
  59. else
  60. if (xs_startswith(v, "*")) {
  61. xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
  62. xs *s2 = xs_fmt("<i>%s</i>", s1);
  63. s = xs_str_cat(s, s2);
  64. }
  65. else
  66. if (xs_startswith(v, "http")) {
  67. xs *v2 = xs_strip_chars_i(xs_dup(v), ".");
  68. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, v);
  69. s = xs_str_cat(s, s1);
  70. }
  71. else
  72. s = xs_str_cat(s, v);
  73. }
  74. else
  75. /* surrounded text, copy directly */
  76. s = xs_str_cat(s, v);
  77. n++;
  78. }
  79. return s;
  80. }
  81. d_char *not_really_markdown(const char *content)
  82. /* formats a content using some Markdown rules */
  83. {
  84. d_char *s = xs_str_new(NULL);
  85. int in_pre = 0;
  86. int in_blq = 0;
  87. xs *list;
  88. char *p, *v;
  89. /* work by lines */
  90. list = xs_split(content, "\n");
  91. p = list;
  92. while (xs_list_iter(&p, &v)) {
  93. xs *ss = NULL;
  94. if (strcmp(v, "```") == 0) {
  95. if (!in_pre)
  96. s = xs_str_cat(s, "<pre>");
  97. else
  98. s = xs_str_cat(s, "</pre>");
  99. in_pre = !in_pre;
  100. continue;
  101. }
  102. if (in_pre)
  103. ss = xs_dup(v);
  104. else
  105. ss = xs_strip_i(format_line(v));
  106. if (xs_startswith(ss, ">")) {
  107. /* delete the > and subsequent spaces */
  108. ss = xs_strip_i(xs_crop_i(ss, 1, 0));
  109. if (!in_blq) {
  110. s = xs_str_cat(s, "<blockquote>");
  111. in_blq = 1;
  112. }
  113. s = xs_str_cat(s, ss);
  114. s = xs_str_cat(s, "<br>");
  115. continue;
  116. }
  117. if (in_blq) {
  118. s = xs_str_cat(s, "</blockquote>");
  119. in_blq = 0;
  120. }
  121. s = xs_str_cat(s, ss);
  122. s = xs_str_cat(s, "<br>");
  123. }
  124. if (in_blq)
  125. s = xs_str_cat(s, "</blockquote>");
  126. if (in_pre)
  127. s = xs_str_cat(s, "</pre>");
  128. /* some beauty fixes */
  129. s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
  130. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  131. s = xs_replace_i(s, "</pre><br>", "</pre>");
  132. {
  133. /* traditional emoticons */
  134. int n;
  135. for (n = 0; smileys[n].key; n++)
  136. s = xs_replace_i(s, smileys[n].key, smileys[n].value);
  137. }
  138. return s;
  139. }
  140. const char *valid_tags[] = {
  141. "a", "p", "br", "br/", "blockquote", "ul", "li",
  142. "span", "i", "b", "u", "pre", "code", "em", "strong", NULL
  143. };
  144. d_char *sanitize(const char *content)
  145. /* cleans dangerous HTML output */
  146. {
  147. d_char *s = xs_str_new(NULL);
  148. xs *sl;
  149. int n = 0;
  150. char *p, *v;
  151. sl = xs_regex_split(content, "</?[^>]+>");
  152. p = sl;
  153. n = 0;
  154. while (xs_list_iter(&p, &v)) {
  155. if (n & 0x1) {
  156. xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
  157. xs *l1 = xs_split_n(s1, " ", 1);
  158. xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
  159. xs *s2 = NULL;
  160. int i;
  161. /* check if it's one of the valid tags */
  162. for (i = 0; valid_tags[i]; i++) {
  163. if (strcmp(tag, valid_tags[i]) == 0)
  164. break;
  165. }
  166. if (valid_tags[i]) {
  167. /* accepted tag: rebuild it with only the accepted elements */
  168. xs *el = xs_regex_match(v, "(href|rel|class|target)=\"[^\"]*\"");
  169. xs *s3 = xs_join(el, " ");
  170. s2 = xs_fmt("<%s%s%s%s>",
  171. v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
  172. }
  173. else {
  174. /* bad tag: escape it */
  175. s2 = xs_replace(v, "<", "&lt;");
  176. }
  177. s = xs_str_cat(s, s2);
  178. }
  179. else {
  180. /* non-tag */
  181. s = xs_str_cat(s, v);
  182. }
  183. n++;
  184. }
  185. return s;
  186. }