format.c 5.5 KB

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