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