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