html.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "snac.h"
  7. d_char *not_really_markdown(char *content, d_char **f_content)
  8. /* formats a content using some Markdown rules */
  9. {
  10. d_char *s = NULL;
  11. int in_pre = 0;
  12. int in_blq = 0;
  13. xs *list;
  14. char *p, *v;
  15. s = xs_str_new(NULL);
  16. p = list = xs_split(content, "\n");
  17. while (xs_list_iter(&p, &v)) {
  18. xs *ss = xs_strip(xs_dup(v));
  19. if (xs_startswith(ss, "```")) {
  20. if (!in_pre)
  21. s = xs_str_cat(s, "<pre>");
  22. else
  23. s = xs_str_cat(s, "</pre>");
  24. in_pre = !in_pre;
  25. continue;
  26. }
  27. if (xs_startswith(ss, ">")) {
  28. /* delete the > and subsequent spaces */
  29. ss = xs_strip(xs_crop(ss, 1, 0));
  30. if (!in_blq) {
  31. s = xs_str_cat(s, "<blockquote>");
  32. in_blq = 1;
  33. }
  34. s = xs_str_cat(s, ss);
  35. s = xs_str_cat(s, "<br>");
  36. continue;
  37. }
  38. if (in_blq) {
  39. s = xs_str_cat(s, "</blockquote>");
  40. in_blq = 0;
  41. }
  42. s = xs_str_cat(s, ss);
  43. s = xs_str_cat(s, "<br>");
  44. }
  45. if (in_blq)
  46. s = xs_str_cat(s, "</blockquote>");
  47. if (in_pre)
  48. s = xs_str_cat(s, "</pre>");
  49. /* some beauty fixes */
  50. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  51. *f_content = s;
  52. return *f_content;
  53. }