format.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. { NULL, NULL }
  27. };
  28. d_char *not_really_markdown(char *content, d_char **f_content)
  29. /* formats a content using some Markdown rules */
  30. {
  31. d_char *s = NULL;
  32. int in_pre = 0;
  33. int in_blq = 0;
  34. xs *list;
  35. char *p, *v;
  36. xs *wrk = xs_str_new(NULL);
  37. {
  38. /* split by special markup */
  39. xs *sm = xs_regex_split(content,
  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. wrk = xs_str_cat(wrk, 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. wrk = xs_str_cat(wrk, 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. wrk = xs_str_cat(wrk, s2);
  62. }
  63. else
  64. if (xs_startswith(v, "http")) {
  65. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  66. wrk = xs_str_cat(wrk, s1);
  67. }
  68. else
  69. /* what the hell is this */
  70. wrk = xs_str_cat(wrk, v);
  71. }
  72. else
  73. /* surrounded text, copy directly */
  74. wrk = xs_str_cat(wrk, v);
  75. n++;
  76. }
  77. }
  78. /* now work by lines */
  79. p = list = xs_split(wrk, "\n");
  80. s = xs_str_new(NULL);
  81. while (xs_list_iter(&p, &v)) {
  82. xs *ss = xs_strip(xs_dup(v));
  83. if (xs_startswith(ss, "```")) {
  84. if (!in_pre)
  85. s = xs_str_cat(s, "<pre>");
  86. else
  87. s = xs_str_cat(s, "</pre>");
  88. in_pre = !in_pre;
  89. continue;
  90. }
  91. if (xs_startswith(ss, ">")) {
  92. /* delete the > and subsequent spaces */
  93. ss = xs_strip(xs_crop(ss, 1, 0));
  94. if (!in_blq) {
  95. s = xs_str_cat(s, "<blockquote>");
  96. in_blq = 1;
  97. }
  98. s = xs_str_cat(s, ss);
  99. s = xs_str_cat(s, "<br>");
  100. continue;
  101. }
  102. if (in_blq) {
  103. s = xs_str_cat(s, "</blockquote>");
  104. in_blq = 0;
  105. }
  106. s = xs_str_cat(s, ss);
  107. s = xs_str_cat(s, "<br>");
  108. }
  109. if (in_blq)
  110. s = xs_str_cat(s, "</blockquote>");
  111. if (in_pre)
  112. s = xs_str_cat(s, "</pre>");
  113. /* some beauty fixes */
  114. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  115. {
  116. /* traditional emoticons */
  117. int n;
  118. for (n = 0; smileys[n].key; n++)
  119. s = xs_replace_i(s, smileys[n].key, smileys[n].value);
  120. }
  121. *f_content = s;
  122. return *f_content;
  123. }