aboutsummaryrefslogtreecommitdiff
path: root/src/stmd.h
blob: 21a86b041829f22d7a37c6fa6ac4f2890f970bfc (plain)
  1. #ifndef _STDMD_H_
  2. #define _STDMD_H_
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include "buffer.h"
  6. #include "chunk.h"
  7. #include "uthash.h"
  8. #define VERSION "0.1"
  9. #define CODE_INDENT 4
  10. struct node_inl {
  11. enum {
  12. INL_STRING,
  13. INL_SOFTBREAK,
  14. INL_LINEBREAK,
  15. INL_CODE,
  16. INL_RAW_HTML,
  17. INL_EMPH,
  18. INL_STRONG,
  19. INL_LINK,
  20. INL_IMAGE
  21. } tag;
  22. union {
  23. chunk literal;
  24. struct node_inl *inlines;
  25. struct {
  26. struct node_inl *label;
  27. unsigned char *url;
  28. unsigned char *title;
  29. } linkable;
  30. } content;
  31. struct node_inl *next;
  32. };
  33. typedef struct node_inl node_inl;
  34. struct reference {
  35. unsigned char *label;
  36. unsigned char *url;
  37. unsigned char *title;
  38. UT_hash_handle hh; // used by uthash
  39. };
  40. typedef struct reference reference;
  41. // Types for blocks
  42. struct ListData {
  43. enum {
  44. bullet,
  45. ordered
  46. } list_type;
  47. int marker_offset;
  48. int padding;
  49. int start;
  50. enum {
  51. period,
  52. parens
  53. } delimiter;
  54. unsigned char bullet_char;
  55. bool tight;
  56. };
  57. struct FencedCodeData {
  58. int fence_length;
  59. int fence_offset;
  60. char fence_char;
  61. strbuf info;
  62. };
  63. struct node_block {
  64. enum {
  65. BLOCK_DOCUMENT,
  66. BLOCK_BQUOTE,
  67. BLOCK_LIST,
  68. BLOCK_LIST_ITEM,
  69. BLOCK_FENCED_CODE,
  70. BLOCK_INDENTED_CODE,
  71. BLOCK_HTML,
  72. BLOCK_PARAGRAPH,
  73. BLOCK_ATX_HEADER,
  74. BLOCK_SETEXT_HEADER,
  75. BLOCK_HRULE,
  76. BLOCK_REFERENCE_DEF
  77. } tag;
  78. int start_line;
  79. int start_column;
  80. int end_line;
  81. bool open;
  82. bool last_line_blank;
  83. struct node_block* children;
  84. struct node_block* last_child;
  85. struct node_block* parent;
  86. struct node_block* top;
  87. strbuf string_content;
  88. node_inl* inline_content;
  89. union {
  90. struct ListData list;
  91. struct FencedCodeData code;
  92. struct {
  93. int level;
  94. } header;
  95. struct {
  96. reference** refmap;
  97. } document;
  98. } as;
  99. struct node_block *next;
  100. struct node_block *prev;
  101. };
  102. typedef struct node_block node_block;
  103. node_inl* parse_inlines(strbuf *input, reference** refmap);
  104. void free_inlines(node_inl* e);
  105. int parse_reference(strbuf *input, reference** refmap);
  106. void free_reference(reference *ref);
  107. void free_reference_map(reference **refmap);
  108. void add_reference(reference** refmap, reference* ref);
  109. void unescape_buffer(strbuf *buf);
  110. extern node_block* make_document();
  111. extern node_block* add_child(node_block* parent,
  112. int block_type, int start_line, int start_column);
  113. void free_blocks(node_block* e);
  114. extern node_block *stmd_parse_document(const unsigned char *buffer, size_t len);
  115. extern node_block *stmd_parse_file(FILE *f);
  116. void print_inlines(node_inl* ils, int indent);
  117. void print_blocks(node_block* blk, int indent);
  118. void blocks_to_html(strbuf *html, node_block *b, bool tight);
  119. void inlines_to_html(strbuf *html, node_inl *b);
  120. #endif