aboutsummaryrefslogtreecommitdiff
path: root/src/xml.c
blob: c0f9d4e499d9770c75c9ca2e92f530abdce285b1 (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, void *vstate,
  32. long options)
  33. {
  34. struct render_state *state = vstate;
  35. cmark_strbuf *xml = state->xml;
  36. bool literal = false;
  37. bool entering = (ev_type == CMARK_EVENT_ENTER);
  38. if (entering) {
  39. indent(state);
  40. cmark_strbuf_printf(xml, "<%s",
  41. cmark_node_get_type_string(node));
  42. if (options & CMARK_OPT_SOURCEPOS && node->start_line != 0) {
  43. cmark_strbuf_printf(xml, " sourcepos=\"%d:%d-%d:%d\"",
  44. node->start_line,
  45. node->start_column,
  46. node->end_line,
  47. node->end_column);
  48. }
  49. literal = false;
  50. switch (node->type) {
  51. case CMARK_NODE_TEXT:
  52. case CMARK_NODE_CODE:
  53. case CMARK_NODE_HTML:
  54. case CMARK_NODE_INLINE_HTML:
  55. cmark_strbuf_puts(xml, ">");
  56. escape_xml(xml, node->as.literal.data,
  57. node->as.literal.len);
  58. cmark_strbuf_puts(xml, "</");
  59. cmark_strbuf_puts(xml,
  60. cmark_node_get_type_string(node));
  61. literal = true;
  62. break;
  63. case CMARK_NODE_CODE_BLOCK:
  64. if (node->as.code.info.len > 0) {
  65. cmark_strbuf_puts(xml, " info=\"");
  66. escape_xml(xml, node->as.code.info.data,
  67. node->as.code.info.len);
  68. cmark_strbuf_putc(xml, '"');
  69. }
  70. cmark_strbuf_puts(xml, ">");
  71. escape_xml(xml, node->as.code.literal.data,
  72. node->as.code.literal.len);
  73. cmark_strbuf_puts(xml, "</");
  74. cmark_strbuf_puts(xml,
  75. cmark_node_get_type_string(node));
  76. literal = true;
  77. break;
  78. case CMARK_NODE_LINK:
  79. case CMARK_NODE_IMAGE:
  80. cmark_strbuf_puts(xml, " url=\"");
  81. escape_xml(xml, node->as.link.url, -1);
  82. cmark_strbuf_putc(xml, '"');
  83. cmark_strbuf_puts(xml, " title=\"");
  84. escape_xml(xml, node->as.link.title, -1);
  85. cmark_strbuf_putc(xml, '"');
  86. break;
  87. default:
  88. break;
  89. }
  90. if (node->first_child) {
  91. state->indent += 2;
  92. } else if (!literal) {
  93. cmark_strbuf_puts(xml, " /");
  94. }
  95. } else {
  96. if (node->first_child) {
  97. state->indent -= 2;
  98. }
  99. indent(state);
  100. cmark_strbuf_printf(xml, "</%s",
  101. cmark_node_get_type_string(node));
  102. }
  103. // TODO print attributes
  104. cmark_strbuf_puts(xml, ">\n");
  105. return 1;
  106. }
  107. char *cmark_render_xml(cmark_node *root, long options)
  108. {
  109. char *result;
  110. cmark_strbuf xml = GH_BUF_INIT;
  111. cmark_event_type ev_type;
  112. cmark_node *cur;
  113. struct render_state state = { &xml, 0 };
  114. cmark_iter *iter = cmark_iter_new(root);
  115. cmark_strbuf_puts(state.xml,
  116. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  117. cmark_strbuf_puts(state.xml,
  118. "<!DOCTYPE CommonMark SYSTEM \"CommonMark.dtd\">\n");
  119. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  120. cur = cmark_iter_get_node(iter);
  121. S_render_node(cur, ev_type, &state, options);
  122. }
  123. result = (char *)cmark_strbuf_detach(&xml);
  124. cmark_iter_free(iter);
  125. cmark_strbuf_free(&xml);
  126. return result;
  127. }