aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
blob: d97d7c794847b9541928e78dc4390d0a6080b1ac (plain)
  1. #ifndef CMARK_AST_H
  2. #define CMARK_AST_H
  3. #include <stdio.h>
  4. #include "config.h"
  5. #include "buffer.h"
  6. #include "chunk.h"
  7. #include "cmark.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #define REFMAP_SIZE 16
  12. #define MAX_LINK_LABEL_LENGTH 1000
  13. struct cmark_node_inl {
  14. cmark_inl_tag tag;
  15. union {
  16. cmark_chunk literal;
  17. struct cmark_node_inl *inlines;
  18. struct {
  19. struct cmark_node_inl *label;
  20. unsigned char *url;
  21. unsigned char *title;
  22. } linkable;
  23. } content;
  24. struct cmark_node_inl *next;
  25. };
  26. struct cmark_reference {
  27. struct cmark_reference *next;
  28. unsigned char *label;
  29. unsigned char *url;
  30. unsigned char *title;
  31. unsigned int hash;
  32. };
  33. typedef struct cmark_reference cmark_reference;
  34. struct cmark_reference_map {
  35. cmark_reference *table[REFMAP_SIZE];
  36. };
  37. typedef struct cmark_reference_map cmark_reference_map;
  38. // Types for blocks
  39. struct cmark_ListData {
  40. cmark_list_type list_type;
  41. int marker_offset;
  42. int padding;
  43. int start;
  44. cmark_delim_type delimiter;
  45. unsigned char bullet_char;
  46. bool tight;
  47. };
  48. struct cmark_FencedCodeData {
  49. int fence_length;
  50. int fence_offset;
  51. unsigned char fence_char;
  52. cmark_strbuf info;
  53. };
  54. struct cmark_node_block {
  55. cmark_block_tag tag;
  56. int start_line;
  57. int start_column;
  58. int end_line;
  59. bool open;
  60. bool last_line_blank;
  61. struct cmark_node_block* children;
  62. struct cmark_node_block* last_child;
  63. struct cmark_node_block* parent;
  64. struct cmark_node_block* top;
  65. cmark_strbuf string_content;
  66. struct cmark_node_inl* inline_content;
  67. union {
  68. struct cmark_ListData list;
  69. struct cmark_FencedCodeData code;
  70. struct {
  71. int level;
  72. } header;
  73. struct {
  74. cmark_reference_map *refmap;
  75. } document;
  76. } as;
  77. struct cmark_node_block *next;
  78. struct cmark_node_block *prev;
  79. };
  80. struct cmark_doc_parser {
  81. struct cmark_node_block* head;
  82. struct cmark_node_block* current;
  83. int line_number;
  84. cmark_strbuf *curline;
  85. };
  86. unsigned char *cmark_clean_autolink(chunk *url, int is_email);
  87. static inline cmark_node_inl *cmark_make_link(cmark_node_inl *label, unsigned char *url, unsigned char *title)
  88. {
  89. cmark_node_inl* e = (cmark_node_inl *)calloc(1, sizeof(*e));
  90. if(e != NULL) {
  91. e->tag = CMARK_INL_LINK;
  92. e->content.linkable.label = label;
  93. e->content.linkable.url = url;
  94. e->content.linkable.title = title;
  95. e->next = NULL;
  96. }
  97. return e;
  98. }
  99. static inline cmark_node_inl* cmark_make_autolink(cmark_node_inl* label, cmark_chunk url, int is_email)
  100. {
  101. return cmark_make_link(label, cmark_clean_autolink(&url, is_email), NULL);
  102. }
  103. static inline cmark_node_inl* cmark_make_inlines(cmark_inl_tag t, cmark_node_inl* contents)
  104. {
  105. cmark_node_inl * e = (cmark_node_inl *)calloc(1, sizeof(*e));
  106. if(e != NULL) {
  107. e->tag = t;
  108. e->content.inlines = contents;
  109. e->next = NULL;
  110. }
  111. return e;
  112. }
  113. // Create an inline with a literal string value.
  114. static inline cmark_node_inl* cmark_make_literal(cmark_inl_tag t, cmark_chunk s)
  115. {
  116. cmark_node_inl * e = (cmark_node_inl *)calloc(1, sizeof(*e));
  117. if(e != NULL) {
  118. e->tag = t;
  119. e->content.literal = s;
  120. e->next = NULL;
  121. }
  122. return e;
  123. }
  124. // Create an inline with no value.
  125. static inline cmark_node_inl* cmark_make_simple(cmark_inl_tag t)
  126. {
  127. cmark_node_inl* e = (cmark_node_inl *)calloc(1, sizeof(*e));
  128. if(e != NULL) {
  129. e->tag = t;
  130. e->next = NULL;
  131. }
  132. return e;
  133. }
  134. // Macros for creating various kinds of simple.
  135. #define cmark_make_str(s) cmark_make_literal(INL_STRING, s)
  136. #define cmark_make_code(s) cmark_make_literal(INL_CODE, s)
  137. #define cmark_make_raw_html(s) cmark_make_literal(INL_RAW_HTML, s)
  138. #define cmark_make_linebreak() cmark_make_simple(INL_LINEBREAK)
  139. #define cmark_make_softbreak() cmark_make_simple(INL_SOFTBREAK)
  140. #define cmark_make_emph(contents) cmark_make_inlines(INL_EMPH, contents)
  141. #define cmark_make_strong(contents) cmark_make_inlines(INL_STRONG, contents)
  142. #ifndef CMARK_NO_SHORT_NAMES
  143. #define node_inl cmark_node_inl
  144. #define ListData cmark_ListData
  145. #define FencedCodeData cmark_FencedCodeData
  146. #define node_block cmark_node_block
  147. #define make_link cmark_make_link
  148. #define make_autolink cmark_make_autolink
  149. #define make_str cmark_make_str
  150. #define make_code cmark_make_code
  151. #define make_raw_html cmark_make_raw_html
  152. #define make_linebreak cmark_make_linebreak
  153. #define make_softbreak cmark_make_softbreak
  154. #define make_emph cmark_make_emph
  155. #define make_strong cmark_make_strong
  156. #define make_simple cmark_make_simple
  157. #define make_literal cmark_make_literal
  158. #define make_inlines cmark_make_inlines
  159. #endif
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif