format.c 5.2 KB

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