aboutsummaryrefslogtreecommitdiff
path: root/src/cmark.h
blob: 3b60d679477718b5d7984f01ecec7b1085d9b3cc (plain)
  1. #ifndef CMARK_H
  2. #define CMARK_H
  3. #include <stdio.h>
  4. #include "cmark_export.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /** # NAME
  9. *
  10. * **cmark** - CommonMark parsing, manipulating, and rendering
  11. */
  12. /** # DESCRIPTION
  13. *
  14. * ## Simple Interface
  15. */
  16. /** Current version of library.
  17. */
  18. #define CMARK_VERSION "0.1"
  19. /** Convert 'text' (assumed to be a UTF-8 encoded string with length
  20. * 'len' from CommonMark Markdown to HTML, returning a null-terminated,
  21. * UTF-8-encoded string.
  22. */
  23. CMARK_EXPORT
  24. char *cmark_markdown_to_html(const char *text, int len);
  25. /** ## Node Structure
  26. */
  27. typedef enum {
  28. /* Error status */
  29. CMARK_NODE_NONE,
  30. /* Block */
  31. CMARK_NODE_DOCUMENT,
  32. CMARK_NODE_BLOCK_QUOTE,
  33. CMARK_NODE_LIST,
  34. CMARK_NODE_LIST_ITEM,
  35. CMARK_NODE_CODE_BLOCK,
  36. CMARK_NODE_HTML,
  37. CMARK_NODE_PARAGRAPH,
  38. CMARK_NODE_HEADER,
  39. CMARK_NODE_HRULE,
  40. CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT,
  41. CMARK_NODE_LAST_BLOCK = CMARK_NODE_HRULE,
  42. /* Inline */
  43. CMARK_NODE_TEXT,
  44. CMARK_NODE_SOFTBREAK,
  45. CMARK_NODE_LINEBREAK,
  46. CMARK_NODE_CODE,
  47. CMARK_NODE_INLINE_HTML,
  48. CMARK_NODE_EMPH,
  49. CMARK_NODE_STRONG,
  50. CMARK_NODE_LINK,
  51. CMARK_NODE_IMAGE,
  52. CMARK_NODE_FIRST_INLINE = CMARK_NODE_TEXT,
  53. CMARK_NODE_LAST_INLINE = CMARK_NODE_IMAGE,
  54. } cmark_node_type;
  55. typedef enum {
  56. CMARK_NO_LIST,
  57. CMARK_BULLET_LIST,
  58. CMARK_ORDERED_LIST
  59. } cmark_list_type;
  60. typedef enum {
  61. CMARK_PERIOD_DELIM,
  62. CMARK_PAREN_DELIM
  63. } cmark_delim_type;
  64. typedef struct cmark_node cmark_node;
  65. typedef struct cmark_parser cmark_parser;
  66. typedef struct cmark_iter cmark_iter;
  67. typedef enum {
  68. CMARK_EVENT_DONE,
  69. CMARK_EVENT_ENTER,
  70. CMARK_EVENT_EXIT
  71. } cmark_event_type;
  72. /**
  73. * ## Creating and Destroying Nodes
  74. */
  75. /** Creates a new node of type 'type'. Note that the node may have
  76. * other required properties, which it is the caller's responsibility
  77. * to assign.
  78. */
  79. CMARK_EXPORT cmark_node*
  80. cmark_node_new(cmark_node_type type);
  81. /** Frees the memory allocated for a node.
  82. */
  83. CMARK_EXPORT void
  84. cmark_node_free(cmark_node *node);
  85. /**
  86. * ## Tree Traversal
  87. */
  88. /** Returns the next node in the sequence after 'node', or NULL if
  89. * there is none.
  90. */
  91. CMARK_EXPORT cmark_node*
  92. cmark_node_next(cmark_node *node);
  93. /** Returns the previous node in the sequence after 'node', or NULL if
  94. * there is none.
  95. */
  96. CMARK_EXPORT cmark_node*
  97. cmark_node_previous(cmark_node *node);
  98. /** Returns the parent of 'node', or NULL if there is none.
  99. */
  100. CMARK_EXPORT cmark_node*
  101. cmark_node_parent(cmark_node *node);
  102. /** Returns the first child of 'node', or NULL if 'node' has no children.
  103. */
  104. CMARK_EXPORT cmark_node*
  105. cmark_node_first_child(cmark_node *node);
  106. /** Returns the last child of 'node', or NULL if 'node' has no children.
  107. */
  108. CMARK_EXPORT cmark_node*
  109. cmark_node_last_child(cmark_node *node);
  110. /**
  111. * ## Iterator
  112. *
  113. * An iterator will walk through a tree of nodes, starting from a root
  114. * node, returning one node at a time, together with information about
  115. * whether the node is being entered or exited. The iterator will
  116. * first descend to a child node, if there is one. When there is no
  117. * child, the iterator will go to the next sibling. When there is no
  118. * next sibling, the iterator will return to the parent (but with
  119. * a 'cmark_event_type' of `CMARK_EVENT_EXIT`). The iterator will
  120. * return `CMARK_EVENT_DONE` when it reaches the root node again.
  121. * One natural application is an HTML renderer, where an `ENTER` event
  122. * outputs an open tag and an `EXIT` event outputs a close tag.
  123. * An iterator might also be used to transform an AST in some systematic
  124. * way, for example, turning all level-3 headers into regular paragraphs.
  125. *
  126. * void
  127. * usage_example(cmark_node *root) {
  128. * cmark_event_type ev_type;
  129. * cmark_iter *iter = cmark_iter_new(root);
  130. *
  131. * while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  132. * cmark_node *cur = cmark_iter_get_node(iter);
  133. * // Do something with `cur` and `ev_type`
  134. * }
  135. *
  136. * cmark_iter_free(iter);
  137. * }
  138. */
  139. /** Creates a new iterator starting at 'root'.
  140. */
  141. CMARK_EXPORT
  142. cmark_iter*
  143. cmark_iter_new(cmark_node *root);
  144. /** Frees the memory allocated for an iterator.
  145. */
  146. CMARK_EXPORT
  147. void
  148. cmark_iter_free(cmark_iter *iter);
  149. /** Returns the event type (`CMARK_EVENT_ENTER`, `CMARK_EVENT_EXIT`,
  150. * or `CMARK_EVENT_DONE`) for the next node.
  151. */
  152. CMARK_EXPORT
  153. cmark_event_type
  154. cmark_iter_next(cmark_iter *iter);
  155. /** Returns the next node in the sequence described above.
  156. */
  157. CMARK_EXPORT
  158. cmark_node*
  159. cmark_iter_get_node(cmark_iter *iter);
  160. /**
  161. * ## Accessors
  162. */
  163. /** Returns the type of 'node', or `CMARK_NODE_NONE` on error.
  164. */
  165. CMARK_EXPORT cmark_node_type
  166. cmark_node_get_type(cmark_node *node);
  167. /** Returns the string contents of 'node', or NULL if none.
  168. */
  169. CMARK_EXPORT const char*
  170. cmark_node_get_literal(cmark_node *node);
  171. /** Sets the string contents of 'node'. Returns 1 on success,
  172. * 0 on failure.
  173. */
  174. CMARK_EXPORT int
  175. cmark_node_set_literal(cmark_node *node, const char *content);
  176. /** Returns the header level of 'node', or 0 if 'node' is not a header.
  177. */
  178. CMARK_EXPORT int
  179. cmark_node_get_header_level(cmark_node *node);
  180. /** Sets the header level of 'node', returning 1 on success and 0 on error.
  181. */
  182. CMARK_EXPORT int
  183. cmark_node_set_header_level(cmark_node *node, int level);
  184. /** Returns the list type of 'node', or `CMARK_NO_LIST` if 'node'
  185. * is not a list.
  186. */
  187. CMARK_EXPORT cmark_list_type
  188. cmark_node_get_list_type(cmark_node *node);
  189. /** Sets the list type of 'node', returning 1 on success and 0 on error.
  190. */
  191. CMARK_EXPORT int
  192. cmark_node_set_list_type(cmark_node *node, cmark_list_type type);
  193. /** Returns starting number of 'node', if it is an ordered list, otherwise 0.
  194. */
  195. CMARK_EXPORT int
  196. cmark_node_get_list_start(cmark_node *node);
  197. /** Sets starting number of 'node', if it is an ordered list. Returns 1
  198. * on success, 0 on failure.
  199. */
  200. CMARK_EXPORT int
  201. cmark_node_set_list_start(cmark_node *node, int start);
  202. /** Returns 1 if 'node' is a tight list, 0 otherwise.
  203. */
  204. CMARK_EXPORT int
  205. cmark_node_get_list_tight(cmark_node *node);
  206. /** Sets the "tightness" of a list. Returns 1 on success, 0 on failure.
  207. */
  208. CMARK_EXPORT int
  209. cmark_node_set_list_tight(cmark_node *node, int tight);
  210. /** Returns the info string from a fenced code block, or NULL if none.
  211. */
  212. CMARK_EXPORT const char*
  213. cmark_node_get_fence_info(cmark_node *node);
  214. /** Sets the info string in a fenced code block, returning 1 on
  215. * success and 0 on failure.
  216. */
  217. CMARK_EXPORT int
  218. cmark_node_set_fence_info(cmark_node *node, const char *info);
  219. /** Gets the URL of a link or image 'node', or NULL if none.
  220. */
  221. CMARK_EXPORT const char*
  222. cmark_node_get_url(cmark_node *node);
  223. /** Sets the URL of a link or image 'node'. Returns 1 on success,
  224. * 0 on failure.
  225. */
  226. CMARK_EXPORT int
  227. cmark_node_set_url(cmark_node *node, const char *url);
  228. /** Gets the title of a link or image 'node', or NULL if none.
  229. */
  230. CMARK_EXPORT const char*
  231. cmark_node_get_title(cmark_node *node);
  232. /** Sets the title of a link or image 'node'. Returns 1 on success,
  233. * 0 on failure.
  234. */
  235. CMARK_EXPORT int
  236. cmark_node_set_title(cmark_node *node, const char *title);
  237. /** Returns the line on which 'node' begins.
  238. */
  239. CMARK_EXPORT int
  240. cmark_node_get_start_line(cmark_node *node);
  241. /** Returns the column at which 'node' begins.
  242. */
  243. CMARK_EXPORT int
  244. cmark_node_get_start_column(cmark_node *node);
  245. /** Returns the line on which 'node' ends.
  246. */
  247. CMARK_EXPORT int
  248. cmark_node_get_end_line(cmark_node *node);
  249. /**
  250. * ## Tree Manipulation
  251. */
  252. /** Unlinks a 'node', removing it from the tree, but not freeing its
  253. * memory. (Use 'cmark_node_free' for that.)
  254. */
  255. CMARK_EXPORT void
  256. cmark_node_unlink(cmark_node *node);
  257. /** Inserts 'sibling' before 'node'. Returns 1 on success, 0 on failure.
  258. */
  259. CMARK_EXPORT int
  260. cmark_node_insert_before(cmark_node *node, cmark_node *sibling);
  261. /** Inserts 'sibling' after 'node'. Returns 1 on success, 0 on failure.
  262. */
  263. CMARK_EXPORT int
  264. cmark_node_insert_after(cmark_node *node, cmark_node *sibling);
  265. /** Adds 'child' to the beginning of the children of 'node'.
  266. * Returns 1 on success, 0 on failure.
  267. */
  268. CMARK_EXPORT int
  269. cmark_node_prepend_child(cmark_node *node, cmark_node *child);
  270. /** Adds 'child' to the end of the children of 'node'.
  271. * Returns 1 on success, 0 on failure.
  272. */
  273. CMARK_EXPORT int
  274. cmark_node_append_child(cmark_node *node, cmark_node *child);
  275. /**
  276. * ## Parsing
  277. *
  278. * Simple interface:
  279. *
  280. * cmark_node *document = cmark_parse_document("Hello *world*", 12);
  281. *
  282. * Streaming interface:
  283. *
  284. * cmark_parser *parser = cmark_parser_new();
  285. * FILE *fp = fopen("myfile.md", "r");
  286. * while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  287. * cmark_parser_feed(parser, buffer, bytes);
  288. * if (bytes < sizeof(buffer)) {
  289. * break;
  290. * }
  291. * }
  292. * document = cmark_parser_finish(parser);
  293. * cmark_parser_free(parser);
  294. */
  295. /** Creates a new parser object.
  296. */
  297. CMARK_EXPORT
  298. cmark_parser *cmark_parser_new();
  299. /** Frees memory allocated for a parser object.
  300. */
  301. CMARK_EXPORT
  302. void cmark_parser_free(cmark_parser *parser);
  303. /** Feeds a string of length 'len' to 'parser'.
  304. */
  305. CMARK_EXPORT
  306. void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len);
  307. /** Finish parsing and return a pointer to a tree of nodes.
  308. */
  309. CMARK_EXPORT
  310. cmark_node *cmark_parser_finish(cmark_parser *parser);
  311. /** Parse a CommonMark document in 'buffer' of length 'len'.
  312. * Returns a pointer to a tree of nodes.
  313. */
  314. CMARK_EXPORT
  315. cmark_node *cmark_parse_document(const char *buffer, size_t len);
  316. /** Parse a CommonMark document in file 'f', returning a pointer to
  317. * a tree of nodes.
  318. */
  319. CMARK_EXPORT
  320. cmark_node *cmark_parse_file(FILE *f);
  321. /**
  322. * ## Rendering
  323. */
  324. /** Render a 'node' tree for debugging purposes, showing
  325. * the hierachy of nodes and their types and contents.
  326. */
  327. CMARK_EXPORT
  328. char *cmark_render_ast(cmark_node *root);
  329. /** Render a 'node' tree as an HTML fragment. It is up to the user
  330. * to add an appropriate header and footer.
  331. */
  332. CMARK_EXPORT
  333. char *cmark_render_html(cmark_node *root);
  334. /** Render a 'node' tree as a groff man page, without the header.
  335. */
  336. CMARK_EXPORT
  337. char *cmark_render_man(cmark_node *root);
  338. /** # AUTHORS
  339. *
  340. * John MacFarlane, Vicent Marti, Kārlis Gaņģis, Nick Wellnhofer.
  341. */
  342. #ifndef CMARK_NO_SHORT_NAMES
  343. #define NODE_DOCUMENT CMARK_NODE_DOCUMENT
  344. #define NODE_BLOCK_QUOTE CMARK_NODE_BLOCK_QUOTE
  345. #define NODE_LIST CMARK_NODE_LIST
  346. #define NODE_LIST_ITEM CMARK_NODE_LIST_ITEM
  347. #define NODE_CODE_BLOCK CMARK_NODE_CODE_BLOCK
  348. #define NODE_HTML CMARK_NODE_HTML
  349. #define NODE_PARAGRAPH CMARK_NODE_PARAGRAPH
  350. #define NODE_HEADER CMARK_NODE_HEADER
  351. #define NODE_HRULE CMARK_NODE_HRULE
  352. #define NODE_TEXT CMARK_NODE_TEXT
  353. #define NODE_SOFTBREAK CMARK_NODE_SOFTBREAK
  354. #define NODE_LINEBREAK CMARK_NODE_LINEBREAK
  355. #define NODE_CODE CMARK_NODE_CODE
  356. #define NODE_INLINE_HTML CMARK_NODE_INLINE_HTML
  357. #define NODE_EMPH CMARK_NODE_EMPH
  358. #define NODE_STRONG CMARK_NODE_STRONG
  359. #define NODE_LINK CMARK_NODE_LINK
  360. #define NODE_IMAGE CMARK_NODE_IMAGE
  361. #define NODE_LINK_LABEL CMARK_NODE_LINK_LABEL
  362. #define BULLET_LIST CMARK_BULLET_LIST
  363. #define ORDERED_LIST CMARK_ORDERED_LIST
  364. #define PERIOD_DELIM CMARK_PERIOD_DELIM
  365. #define PAREN_DELIM CMARK_PAREN_DELIM
  366. #endif
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. #endif