aboutsummaryrefslogtreecommitdiff
path: root/src/print.c
blob: 1afa584760800831c44215d6b8ca260afc4566fb (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_BLOCK_QUOTE:
  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_HEADER:
  125. printf("setext_header (level=%d)\n", b->as.header.level);
  126. print_inlines(b->first_child, indent + 2);
  127. break;
  128. case NODE_PARAGRAPH:
  129. printf("paragraph\n");
  130. print_inlines(b->first_child, indent + 2);
  131. break;
  132. case NODE_HRULE:
  133. printf("hrule\n");
  134. break;
  135. case NODE_CODE_BLOCK:
  136. printf("code block info=");
  137. print_str(b->as.code.info.ptr, -1);
  138. putchar(' ');
  139. print_str(b->string_content.ptr, -1);
  140. putchar('\n');
  141. break;
  142. case NODE_HTML:
  143. printf("html_block ");
  144. print_str(b->string_content.ptr, -1);
  145. putchar('\n');
  146. break;
  147. case NODE_REFERENCE_DEF:
  148. printf("reference_def\n");
  149. break;
  150. default:
  151. printf("# NOT IMPLEMENTED (%d)\n", b->type);
  152. break;
  153. }
  154. b = b->next;
  155. }
  156. }
  157. void cmark_debug_print(cmark_node *root)
  158. {
  159. print_blocks(root, 0);
  160. }