format.c 5.3 KB

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