aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
blob: 168c92674c5bbc919b481678b7fdb955c29b3673 (plain)
  1. #ifndef CMARK_AST_H
  2. #define CMARK_AST_H
  3. #include <stdio.h>
  4. #include "config.h"
  5. #include "node.h"
  6. #include "buffer.h"
  7. #include "chunk.h"
  8. #include "cmark.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #define REFMAP_SIZE 16
  13. #define MAX_LINK_LABEL_LENGTH 1000
  14. struct cmark_reference {
  15. struct cmark_reference *next;
  16. unsigned char *label;
  17. unsigned char *url;
  18. unsigned char *title;
  19. unsigned int hash;
  20. };
  21. typedef struct cmark_reference cmark_reference;
  22. struct cmark_reference_map {
  23. cmark_reference *table[REFMAP_SIZE];
  24. };
  25. typedef struct cmark_reference_map cmark_reference_map;
  26. struct cmark_doc_parser {
  27. struct cmark_reference_map *refmap;
  28. struct cmark_node* root;
  29. struct cmark_node* current;
  30. int line_number;
  31. cmark_strbuf *curline;
  32. };
  33. unsigned char *cmark_clean_autolink(chunk *url, int is_email);
  34. static inline cmark_node *cmark_make_link(cmark_node *label, unsigned char *url, unsigned char *title)
  35. {
  36. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  37. if(e != NULL) {
  38. e->type = CMARK_NODE_LINK;
  39. e->as.link.label = label;
  40. e->as.link.url = url;
  41. e->as.link.title = title;
  42. e->next = NULL;
  43. }
  44. return e;
  45. }
  46. static inline cmark_node* cmark_make_autolink(cmark_node* label, cmark_chunk url, int is_email)
  47. {
  48. return cmark_make_link(label, cmark_clean_autolink(&url, is_email), NULL);
  49. }
  50. static inline cmark_node* cmark_make_inlines(cmark_node_type t, cmark_node* contents)
  51. {
  52. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  53. if(e != NULL) {
  54. e->type = t;
  55. e->first_child = contents;
  56. e->next = NULL;
  57. }
  58. return e;
  59. }
  60. // Create an inline with a literal string value.
  61. static inline cmark_node* cmark_make_literal(cmark_node_type t, cmark_chunk s)
  62. {
  63. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  64. if(e != NULL) {
  65. e->type = t;
  66. e->as.literal = s;
  67. e->next = NULL;
  68. }
  69. return e;
  70. }
  71. // Create an inline with no value.
  72. static inline cmark_node* cmark_make_simple(cmark_node_type t)
  73. {
  74. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  75. if(e != NULL) {
  76. e->type = t;
  77. e->next = NULL;
  78. }
  79. return e;
  80. }
  81. // Macros for creating various kinds of simple.
  82. #define cmark_make_str(s) cmark_make_literal(CMARK_NODE_STRING, s)
  83. #define cmark_make_code(s) cmark_make_literal(CMARK_NODE_INLINE_CODE, s)
  84. #define cmark_make_raw_html(s) cmark_make_literal(CMARK_NODE_INLINE_HTML, s)
  85. #define cmark_make_linebreak() cmark_make_simple(CMARK_NODE_LINEBREAK)
  86. #define cmark_make_softbreak() cmark_make_simple(CMARK_NODE_SOFTBREAK)
  87. #define cmark_make_emph(contents) cmark_make_inlines(CMARK_NODE_EMPH, contents)
  88. #define cmark_make_strong(contents) cmark_make_inlines(CMARK_NODE_STRONG, contents)
  89. #ifndef CMARK_NO_SHORT_NAMES
  90. #define make_link cmark_make_link
  91. #define make_autolink cmark_make_autolink
  92. #define make_str cmark_make_str
  93. #define make_code cmark_make_code
  94. #define make_raw_html cmark_make_raw_html
  95. #define make_linebreak cmark_make_linebreak
  96. #define make_softbreak cmark_make_softbreak
  97. #define make_emph cmark_make_emph
  98. #define make_strong cmark_make_strong
  99. #define make_simple cmark_make_simple
  100. #define make_literal cmark_make_literal
  101. #define make_inlines cmark_make_inlines
  102. #endif
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #endif