format.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {
  41. /* split by special markup */
  42. xs *sm = xs_regex_split(content,
  43. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
  44. int n = 0;
  45. p = sm;
  46. while (xs_list_iter(&p, &v)) {
  47. if ((n & 0x1)) {
  48. /* markup */
  49. if (xs_startswith(v, "`")) {
  50. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  51. xs *s2 = xs_fmt("<code>%s</code>", s1);
  52. wrk = xs_str_cat(wrk, s2);
  53. }
  54. else
  55. if (xs_startswith(v, "**")) {
  56. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  57. xs *s2 = xs_fmt("<b>%s</b>", s1);
  58. wrk = xs_str_cat(wrk, s2);
  59. }
  60. else
  61. if (xs_startswith(v, "*")) {
  62. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  63. xs *s2 = xs_fmt("<i>%s</i>", s1);
  64. wrk = xs_str_cat(wrk, s2);
  65. }
  66. else
  67. if (xs_startswith(v, "http")) {
  68. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v, v);
  69. wrk = xs_str_cat(wrk, s1);
  70. }
  71. else
  72. /* what the hell is this */
  73. wrk = xs_str_cat(wrk, v);
  74. }
  75. else
  76. /* surrounded text, copy directly */
  77. wrk = xs_str_cat(wrk, v);
  78. n++;
  79. }
  80. }
  81. /* now work by lines */
  82. p = list = xs_split(wrk, "\n");
  83. s = xs_str_new(NULL);
  84. while (xs_list_iter(&p, &v)) {
  85. xs *ss = xs_strip(xs_dup(v));
  86. if (xs_startswith(ss, "```")) {
  87. if (!in_pre)
  88. s = xs_str_cat(s, "<pre>");
  89. else
  90. s = xs_str_cat(s, "</pre>");
  91. in_pre = !in_pre;
  92. continue;
  93. }
  94. if (xs_startswith(ss, ">")) {
  95. /* delete the > and subsequent spaces */
  96. ss = xs_strip(xs_crop(ss, 1, 0));
  97. if (!in_blq) {
  98. s = xs_str_cat(s, "<blockquote>");
  99. in_blq = 1;
  100. }
  101. s = xs_str_cat(s, ss);
  102. s = xs_str_cat(s, "<br>");
  103. continue;
  104. }
  105. if (in_blq) {
  106. s = xs_str_cat(s, "</blockquote>");
  107. in_blq = 0;
  108. }
  109. s = xs_str_cat(s, ss);
  110. s = xs_str_cat(s, "<br>");
  111. }
  112. if (in_blq)
  113. s = xs_str_cat(s, "</blockquote>");
  114. if (in_pre)
  115. s = xs_str_cat(s, "</pre>");
  116. /* some beauty fixes */
  117. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  118. {
  119. /* traditional emoticons */
  120. int n;
  121. for (n = 0; smileys[n].key; n++)
  122. s = xs_replace_i(s, smileys[n].key, smileys[n].value);
  123. }
  124. *f_content = s;
  125. return *f_content;
  126. }