aboutsummaryrefslogtreecommitdiff
path: root/src/print.c
blob: c8bf5f5cd2ac94b71bd920a56b0e9abd18b86aa5 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "cmark.h"
  5. #include "node.h"
  6. #include "debug.h"
  7. static void print_str(const unsigned char *s, int len)
  8. {
  9. int i;
  10. if (len < 0)
  11. len = strlen((char *)s);
  12. putchar('"');
  13. for (i = 0; i < len; ++i) {
  14. unsigned char c = s[i];
  15. switch (c) {
  16. case '\n':
  17. printf("\\n");
  18. break;
  19. case '"':
  20. printf("\\\"");
  21. break;
  22. case '\\':
  23. printf("\\\\");
  24. break;
  25. default:
  26. putchar((int)c);
  27. }
  28. }
  29. putchar('"');
  30. }
  31. // Prettyprint an inline list, for debugging.
  32. static void print_inlines(cmark_node* ils, int indent)
  33. {
  34. int i;
  35. while(ils != NULL) {
  36. for (i=0; i < indent; i++) {
  37. putchar(' ');
  38. }
  39. switch(ils->type) {
  40. case NODE_STRING:
  41. printf("str ");
  42. print_str(ils->as.literal.data, ils->as.literal.len);
  43. putchar('\n');
  44. break;
  45. case NODE_LINEBREAK:
  46. printf("linebreak\n");
  47. break;
  48. case NODE_SOFTBREAK:
  49. printf("softbreak\n");
  50. break;
  51. case NODE_INLINE_CODE:
  52. printf("code ");
  53. print_str(ils->as.literal.data, ils->as.literal.len);
  54. putchar('\n');
  55. break;
  56. case NODE_INLINE_HTML:
  57. printf("html ");
  58. print_str(ils->as.literal.data, ils->as.literal.len);
  59. putchar('\n');
  60. break;
  61. case NODE_LINK:
  62. case NODE_IMAGE:
  63. printf("%s url=", ils->type == NODE_LINK ? "link" : "image");
  64. if (ils->as.link.url)
  65. print_str(ils->as.link.url, -1);
  66. if (ils->as.link.title) {
  67. printf(" title=");
  68. print_str(ils->as.link.title, -1);
  69. }
  70. putchar('\n');
  71. print_inlines(ils->first_child, indent + 2);
  72. break;
  73. case NODE_STRONG:
  74. printf("strong\n");
  75. print_inlines(ils->first_child, indent + 2);
  76. break;
  77. case NODE_EMPH:
  78. printf("emph\n");
  79. print_inlines(ils->first_child, indent + 2);
  80. break;
  81. default:
  82. break;
  83. }
  84. ils = ils->next;
  85. }
  86. }
  87. // Functions to pretty-print inline and cmark_node lists, for debugging.
  88. // Prettyprint an inline list, for debugging.
  89. static void print_blocks(cmark_node* b, int indent)
  90. {
  91. cmark_list *data;
  92. int i;
  93. while(b != NULL) {
  94. for (i=0; i < indent; i++) {
  95. putchar(' ');
  96. }
  97. switch(b->type) {
  98. case NODE_DOCUMENT:
  99. printf("document\n");
  100. print_blocks(b->first_child, indent + 2);
  101. break;
  102. case NODE_BQUOTE:
  103. printf("block_quote\n");
  104. print_blocks(b->first_child, indent + 2);
  105. break;
  106. case NODE_LIST_ITEM:
  107. printf("list_item\n");
  108. print_blocks(b->first_child, indent + 2);
  109. break;
  110. case NODE_LIST:
  111. data = &(b->as.list);
  112. if (data->list_type == CMARK_ORDERED_LIST) {
  113. printf("list (type=ordered tight=%s start=%d delim=%s)\n",
  114. (data->tight ? "true" : "false"),
  115. data->start,
  116. (data->delimiter == CMARK_PAREN_DELIM ? "parens" : "period"));
  117. } else {
  118. printf("list (type=bullet tight=%s bullet_char=%c)\n",
  119. (data->tight ? "true" : "false"),
  120. data->bullet_char);
  121. }
  122. print_blocks(b->first_child, indent + 2);
  123. break;
  124. case NODE_ATX_HEADER:
  125. printf("atx_header (level=%d)\n", b->as.header.level);
  126. print_inlines(b->first_child, indent + 2);
  127. break;
  128. case NODE_SETEXT_HEADER:
  129. printf("setext_header (level=%d)\n", b->as.header.level);
  130. print_inlines(b->first_child, indent + 2);
  131. break;
  132. case NODE_PARAGRAPH:
  133. printf("paragraph\n");
  134. print_inlines(b->first_child, indent + 2);
  135. break;
  136. case NODE_HRULE:
  137. printf("hrule\n");
  138. break;
  139. case NODE_INDENTED_CODE:
  140. printf("indented_code ");
  141. print_str(b->string_content.ptr, -1);
  142. putchar('\n');
  143. break;
  144. case NODE_FENCED_CODE:
  145. printf("fenced_code length=%d info=",
  146. b->as.code.fence_length);
  147. print_str(b->as.code.info.ptr, -1);
  148. putchar(' ');
  149. print_str(b->string_content.ptr, -1);
  150. putchar('\n');
  151. break;
  152. case NODE_HTML:
  153. printf("html_block ");
  154. print_str(b->string_content.ptr, -1);
  155. putchar('\n');
  156. break;
  157. case NODE_REFERENCE_DEF:
  158. printf("reference_def\n");
  159. break;
  160. default:
  161. printf("# NOT IMPLEMENTED (%d)\n", b->type);
  162. break;
  163. }
  164. b = b->next;
  165. }
  166. }
  167. void cmark_debug_print(cmark_node *root)
  168. {
  169. print_blocks(root, 0);
  170. }