aboutsummaryrefslogtreecommitdiff
path: root/src/print.c
blob: a9248706ad42b7e29b9cf2d69385f1d37f8c96aa (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "bstrlib.h"
  4. #include "stmd.h"
  5. #include "debug.h"
  6. static bstring format_str(bstring s)
  7. {
  8. int pos = 0;
  9. int len = blength(s);
  10. bstring result = bfromcstr("");
  11. char c;
  12. bformata(result, "\"");
  13. while (pos < len) {
  14. c = bchar(s, pos);
  15. switch (c) {
  16. case '\n':
  17. bformata(result, "\\n");
  18. break;
  19. case '"':
  20. bformata(result, "\\\"");
  21. break;
  22. case '\\':
  23. bformata(result, "\\\\");
  24. break;
  25. default:
  26. bformata(result, "%c", c);
  27. }
  28. pos++;
  29. }
  30. bformata(result, "\"");
  31. return result;
  32. }
  33. // Functions to pretty-print inline and block lists, for debugging.
  34. // Prettyprint an inline list, for debugging.
  35. extern void print_blocks(block* b, int indent)
  36. {
  37. struct ListData * data;
  38. while(b != NULL) {
  39. // printf("%3d %3d %3d| ", b->start_line, b->start_column, b->end_line);
  40. for (int i=0; i < indent; i++) {
  41. putchar(' ');
  42. }
  43. switch(b->tag) {
  44. case document:
  45. printf("document\n");
  46. print_blocks(b->children, indent + 2);
  47. break;
  48. case block_quote:
  49. printf("block_quote\n");
  50. print_blocks(b->children, indent + 2);
  51. break;
  52. case list_item:
  53. data = &(b->attributes.list_data);
  54. printf("list_item\n");
  55. print_blocks(b->children, indent + 2);
  56. break;
  57. case list:
  58. data = &(b->attributes.list_data);
  59. if (data->list_type == ordered) {
  60. printf("list (type=ordered tight=%s start=%d delim=%s)\n",
  61. (data->tight ? "true" : "false"),
  62. data->start,
  63. (data->delimiter == parens ? "parens" : "period"));
  64. } else {
  65. printf("list (type=bullet tight=%s bullet_char=%c)\n",
  66. (data->tight ? "true" : "false"),
  67. data->bullet_char);
  68. }
  69. print_blocks(b->children, indent + 2);
  70. break;
  71. case atx_header:
  72. printf("atx_header (level=%d)\n", b->attributes.header_level);
  73. print_inlines(b->inline_content, indent + 2);
  74. break;
  75. case setext_header:
  76. printf("setext_header (level=%d)\n", b->attributes.header_level);
  77. print_inlines(b->inline_content, indent + 2);
  78. break;
  79. case paragraph:
  80. printf("paragraph\n");
  81. print_inlines(b->inline_content, indent + 2);
  82. break;
  83. case hrule:
  84. printf("hrule\n");
  85. break;
  86. case indented_code:
  87. printf("indented_code %s\n", format_str(b->string_content)->data);
  88. break;
  89. case fenced_code:
  90. printf("fenced_code length=%d info=%s %s\n",
  91. b->attributes.fenced_code_data.fence_length,
  92. format_str(b->attributes.fenced_code_data.info)->data,
  93. format_str(b->string_content)->data);
  94. break;
  95. case html_block:
  96. printf("html_block %s\n", format_str(b->string_content)->data);
  97. break;
  98. case reference_def:
  99. printf("reference_def\n");
  100. break;
  101. default:
  102. log_warn("block type %d not implemented\n", b->tag);
  103. break;
  104. }
  105. b = b->next;
  106. }
  107. }
  108. // Prettyprint an inline list, for debugging.
  109. extern void print_inlines(inl* ils, int indent)
  110. {
  111. while(ils != NULL) {
  112. /*
  113. // we add 11 extra spaces for the line/column info
  114. for (int i=0; i < 11; i++) {
  115. putchar(' ');
  116. }
  117. putchar('|');
  118. putchar(' ');
  119. */
  120. for (int i=0; i < indent; i++) {
  121. putchar(' ');
  122. }
  123. switch(ils->tag) {
  124. case str:
  125. printf("str %s\n", format_str(ils->content.literal)->data);
  126. break;
  127. case linebreak:
  128. printf("linebreak\n");
  129. break;
  130. case softbreak:
  131. printf("softbreak\n");
  132. break;
  133. case code:
  134. printf("code %s\n", format_str(ils->content.literal)->data);
  135. break;
  136. case raw_html:
  137. printf("html %s\n", format_str(ils->content.literal)->data);
  138. break;
  139. case entity:
  140. printf("entity %s\n", format_str(ils->content.literal)->data);
  141. break;
  142. case link:
  143. printf("link url=%s title=%s\n",
  144. format_str(ils->content.linkable.url)->data,
  145. format_str(ils->content.linkable.title)->data);
  146. print_inlines(ils->content.linkable.label, indent + 2);
  147. break;
  148. case image:
  149. printf("image url=%s title=%s\n",
  150. format_str(ils->content.linkable.url)->data,
  151. format_str(ils->content.linkable.title)->data);
  152. print_inlines(ils->content.linkable.label, indent + 2);
  153. break;
  154. case strong:
  155. printf("strong\n");
  156. print_inlines(ils->content.linkable.label, indent + 2);
  157. break;
  158. case emph:
  159. printf("emph\n");
  160. print_inlines(ils->content.linkable.label, indent + 2);
  161. break;
  162. }
  163. ils = ils->next;
  164. }
  165. }