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