cmark.c 985 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include "node.h"
  5. #include "houdini.h"
  6. #include "cmark.h"
  7. #include "buffer.h"
  8. int cmark_version() { return CMARK_VERSION; }
  9. const char *cmark_version_string() { return CMARK_VERSION_STRING; }
  10. static void *xcalloc(size_t nmem, size_t size) {
  11. void *ptr = calloc(nmem, size);
  12. if (!ptr) {
  13. fprintf(stderr, "[cmark] calloc returned null pointer, aborting\n");
  14. abort();
  15. }
  16. return ptr;
  17. }
  18. static void *xrealloc(void *ptr, size_t size) {
  19. void *new_ptr = realloc(ptr, size);
  20. if (!new_ptr) {
  21. fprintf(stderr, "[cmark] realloc returned null pointer, aborting\n");
  22. abort();
  23. }
  24. return new_ptr;
  25. }
  26. cmark_mem DEFAULT_MEM_ALLOCATOR = {xcalloc, xrealloc, free};
  27. char *cmark_markdown_to_html(const char *text, size_t len, int options) {
  28. cmark_node *doc;
  29. char *result;
  30. doc = cmark_parse_document(text, len, options);
  31. result = cmark_render_html(doc, options);
  32. cmark_node_free(doc);
  33. return result;
  34. }