aboutsummaryrefslogtreecommitdiff
path: root/src/iterator.c
blob: eb7b49cb77c4a533181534d7fa96ad0a42f66bb4 (plain)
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include "config.h"
  4. #include "node.h"
  5. #include "cmark.h"
  6. #include "iterator.h"
  7. static const int S_leaf_mask =
  8. (1 << CMARK_NODE_HTML) |
  9. (1 << CMARK_NODE_HRULE) |
  10. (1 << CMARK_NODE_CODE_BLOCK) |
  11. (1 << CMARK_NODE_TEXT) |
  12. (1 << CMARK_NODE_SOFTBREAK) |
  13. (1 << CMARK_NODE_LINEBREAK) |
  14. (1 << CMARK_NODE_CODE) |
  15. (1 << CMARK_NODE_INLINE_HTML);
  16. cmark_iter*
  17. cmark_iter_new(cmark_node *root)
  18. {
  19. if (root == NULL) {
  20. return NULL;
  21. }
  22. cmark_iter *iter = (cmark_iter*)malloc(sizeof(cmark_iter));
  23. if (iter == NULL) {
  24. return NULL;
  25. }
  26. iter->root = root;
  27. iter->cur.ev_type = CMARK_EVENT_NONE;
  28. iter->cur.node = NULL;
  29. iter->next.ev_type = CMARK_EVENT_ENTER;
  30. iter->next.node = root;
  31. return iter;
  32. }
  33. void
  34. cmark_iter_free(cmark_iter *iter)
  35. {
  36. free(iter);
  37. }
  38. static bool
  39. S_is_leaf(cmark_node *node)
  40. {
  41. return (1 << node->type) & S_leaf_mask;
  42. }
  43. cmark_event_type
  44. cmark_iter_next(cmark_iter *iter)
  45. {
  46. cmark_event_type ev_type = iter->next.ev_type;
  47. cmark_node *node = iter->next.node;
  48. iter->cur.ev_type = ev_type;
  49. iter->cur.node = node;
  50. if (ev_type == CMARK_EVENT_DONE) {
  51. return ev_type;
  52. }
  53. /* roll forward to next item, setting both fields */
  54. if (ev_type == CMARK_EVENT_ENTER && !S_is_leaf(node)) {
  55. if (node->first_child == NULL) {
  56. /* stay on this node but exit */
  57. iter->next.ev_type = CMARK_EVENT_EXIT;
  58. } else {
  59. iter->next.ev_type = CMARK_EVENT_ENTER;
  60. iter->next.node = node->first_child;
  61. }
  62. } else if (node == iter->root) {
  63. /* don't move past root */
  64. iter->next.ev_type = CMARK_EVENT_DONE;
  65. iter->next.node = NULL;
  66. } else if (node->next) {
  67. iter->next.ev_type = CMARK_EVENT_ENTER;
  68. iter->next.node = node->next;
  69. } else if (node->parent) {
  70. iter->next.ev_type = CMARK_EVENT_EXIT;
  71. iter->next.node = node->parent;
  72. } else {
  73. assert(false);
  74. iter->next.ev_type = CMARK_EVENT_DONE;
  75. iter->next.node = NULL;
  76. }
  77. return ev_type;
  78. }
  79. void
  80. cmark_iter_reset(cmark_iter *iter, cmark_node *current,
  81. cmark_event_type event_type)
  82. {
  83. iter->next.ev_type = event_type;
  84. iter->next.node = current;
  85. cmark_iter_next(iter);
  86. }
  87. cmark_node*
  88. cmark_iter_get_node(cmark_iter *iter)
  89. {
  90. return iter->cur.node;
  91. }
  92. cmark_event_type
  93. cmark_iter_get_event_type(cmark_iter *iter)
  94. {
  95. return iter->cur.ev_type;
  96. }
  97. cmark_node*
  98. cmark_iter_get_root(cmark_iter *iter)
  99. {
  100. return iter->root;
  101. }
  102. void cmark_consolidate_text_nodes(cmark_node *root)
  103. {
  104. cmark_iter *iter = cmark_iter_new(root);
  105. cmark_strbuf buf = GH_BUF_INIT;
  106. cmark_event_type ev_type;
  107. cmark_node *cur, *tmp, *next;
  108. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  109. cur = cmark_iter_get_node(iter);
  110. if (ev_type == CMARK_EVENT_ENTER &&
  111. cur->type == CMARK_NODE_TEXT &&
  112. cur->next &&
  113. cur->next->type == CMARK_NODE_TEXT) {
  114. cmark_strbuf_clear(&buf);
  115. cmark_strbuf_puts(&buf, cmark_node_get_literal(cur));
  116. tmp = cur->next;
  117. while (tmp && tmp->type == CMARK_NODE_TEXT) {
  118. cmark_iter_get_node(iter); // advance pointer
  119. cmark_strbuf_puts(&buf, cmark_node_get_literal(tmp));
  120. next = tmp->next;
  121. cmark_node_free(tmp);
  122. tmp = next;
  123. }
  124. cmark_node_set_literal(cur, (char *)cmark_strbuf_detach(&buf));
  125. }
  126. }
  127. cmark_iter_free(iter);
  128. }