format.c 5.6 KB

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