format.c 3.8 KB

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