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