aboutsummaryrefslogtreecommitdiff
path: root/src/xml.c
blob: 93a6eda8ec9149c4afc6333e9ce9941d226d1c4e (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "config.h"
  6. #include "cmark.h"
  7. #include "node.h"
  8. #include "buffer.h"
  9. #include "houdini.h"
  10. // Functions to convert cmark_nodes to XML strings.
  11. static void escape_xml(cmark_strbuf *dest, const unsigned char *source, int length)
  12. {
  13. if (source != NULL) {
  14. if (length < 0)
  15. length = strlen((char *)source);
  16. houdini_escape_html0(dest, source, (size_t)length, 0);
  17. }
  18. }
  19. struct render_state {
  20. cmark_strbuf* xml;
  21. int indent;
  22. };
  23. static inline void indent(struct render_state *state)
  24. {
  25. int i;
  26. for (i = 0; i < state->indent; i++) {
  27. cmark_strbuf_putc(state->xml, ' ');
  28. }
  29. }
  30. static int
  31. S_render_node(cmark_node *node, cmark_event_type ev_type,
  32. struct render_state *state, long options)
  33. {
  34. cmark_strbuf *xml = state->xml;
  35. bool literal = false;
  36. bool entering = (ev_type == CMARK_EVENT_ENTER);
  37. if (entering) {
  38. indent(state);
  39. cmark_strbuf_printf(xml, "<%s",
  40. cmark_node_get_type_string(node));
  41. if (options & CMARK_OPT_SOURCEPOS && node->start_line != 0) {
  42. cmark_strbuf_printf(xml, " sourcepos=\"%d:%d-%d:%d\"",
  43. node->start_line,
  44. node->start_column,
  45. node->end_line,
  46. node->end_column);
  47. }
  48. literal = false;
  49. switch (node->type) {
  50. case CMARK_NODE_TEXT:
  51. case CMARK_NODE_CODE:
  52. case CMARK_NODE_HTML:
  53. case CMARK_NODE_INLINE_HTML:
  54. cmark_strbuf_puts(xml, ">");
  55. escape_xml(xml, node->as.literal.data,
  56. node->as.literal.len);
  57. cmark_strbuf_puts(xml, "</");
  58. cmark_strbuf_puts(xml,
  59. cmark_node_get_type_string(node));
  60. literal = true;
  61. break;
  62. case CMARK_NODE_CODE_BLOCK:
  63. if (node->as.code.info.len > 0) {
  64. cmark_strbuf_puts(xml, " info=\"");
  65. escape_xml(xml, node->as.code.info.data,
  66. node->as.code.info.len);
  67. cmark_strbuf_putc(xml, '"');
  68. }
  69. cmark_strbuf_puts(xml, ">");
  70. escape_xml(xml, node->as.code.literal.data,
  71. node->as.code.literal.len);
  72. cmark_strbuf_puts(xml, "</");
  73. cmark_strbuf_puts(xml,
  74. cmark_node_get_type_string(node));
  75. literal = true;
  76. break;
  77. case CMARK_NODE_LINK:
  78. case CMARK_NODE_IMAGE:
  79. cmark_strbuf_puts(xml, " url=\"");
  80. escape_xml(xml, node->as.link.url, -1);
  81. cmark_strbuf_putc(xml, '"');
  82. cmark_strbuf_puts(xml, " title=\"");
  83. escape_xml(xml, node->as.link.title, -1);
  84. cmark_strbuf_putc(xml, '"');
  85. break;
  86. default:
  87. break;
  88. }
  89. if (node->first_child) {
  90. state->indent += 2;
  91. } else if (!literal) {
  92. cmark_strbuf_puts(xml, " /");
  93. }
  94. cmark_strbuf_puts(xml, ">\n");
  95. } else if (node->first_child) {
  96. state->indent -= 2;
  97. indent(state);
  98. cmark_strbuf_printf(xml, "</%s>\n",
  99. cmark_node_get_type_string(node));
  100. }
  101. return 1;
  102. }
  103. char *cmark_render_xml(cmark_node *root, long options)
  104. {
  105. char *result;
  106. cmark_strbuf xml = GH_BUF_INIT;
  107. cmark_event_type ev_type;
  108. cmark_node *cur;
  109. struct render_state state = { &xml, 0 };
  110. if (options & CMARK_OPT_NORMALIZE) {
  111. cmark_consolidate_text_nodes(root);
  112. }
  113. cmark_iter *iter = cmark_iter_new(root);
  114. cmark_strbuf_puts(state.xml,
  115. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  116. cmark_strbuf_puts(state.xml,
  117. "<!DOCTYPE CommonMark SYSTEM \"CommonMark.dtd\">\n");
  118. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  119. cur = cmark_iter_get_node(iter);
  120. S_render_node(cur, ev_type, &state, options);
  121. }
  122. result = (char *)cmark_strbuf_detach(&xml);
  123. cmark_iter_free(iter);
  124. cmark_strbuf_free(&xml);
  125. return result;
  126. }