aboutsummaryrefslogtreecommitdiff
path: root/src/cmark.h
blob: 3d2ed15777f057f8be53601d876680e1f2c20e50 (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 starting number of 'node', if it is an ordered list, otherwise 0.
  201. */
  202. CMARK_EXPORT int
  203. cmark_node_get_list_start(cmark_node *node);
  204. /** Sets starting number of 'node', if it is an ordered list. Returns 1
  205. * on success, 0 on failure.
  206. */
  207. CMARK_EXPORT int
  208. cmark_node_set_list_start(cmark_node *node, int start);
  209. /** Returns 1 if 'node' is a tight list, 0 otherwise.
  210. */
  211. CMARK_EXPORT int
  212. cmark_node_get_list_tight(cmark_node *node);
  213. /** Sets the "tightness" of a list. Returns 1 on success, 0 on failure.
  214. */
  215. CMARK_EXPORT int
  216. cmark_node_set_list_tight(cmark_node *node, int tight);
  217. /** Returns the info string from a fenced code block, or NULL if none.
  218. */
  219. CMARK_EXPORT const char*
  220. cmark_node_get_fence_info(cmark_node *node);
  221. /** Sets the info string in a fenced code block, returning 1 on
  222. * success and 0 on failure.
  223. */
  224. CMARK_EXPORT int
  225. cmark_node_set_fence_info(cmark_node *node, const char *info);
  226. /** Gets the URL of a link or image 'node', or NULL if none.
  227. */
  228. CMARK_EXPORT const char*
  229. cmark_node_get_url(cmark_node *node);
  230. /** Sets the URL of a link or image 'node'. Returns 1 on success,
  231. * 0 on failure.
  232. */
  233. CMARK_EXPORT int
  234. cmark_node_set_url(cmark_node *node, const char *url);
  235. /** Gets the title of a link or image 'node', or NULL if none.
  236. */
  237. CMARK_EXPORT const char*
  238. cmark_node_get_title(cmark_node *node);
  239. /** Sets the title of a link or image 'node'. Returns 1 on success,
  240. * 0 on failure.
  241. */
  242. CMARK_EXPORT int
  243. cmark_node_set_title(cmark_node *node, const char *title);
  244. /** Returns the line on which 'node' begins.
  245. */
  246. CMARK_EXPORT int
  247. cmark_node_get_start_line(cmark_node *node);
  248. /** Returns the column at which 'node' begins.
  249. */
  250. CMARK_EXPORT int
  251. cmark_node_get_start_column(cmark_node *node);
  252. /** Returns the line on which 'node' ends.
  253. */
  254. CMARK_EXPORT int
  255. cmark_node_get_end_line(cmark_node *node);
  256. /** Returns the column at which 'node' ends.
  257. */
  258. CMARK_EXPORT int
  259. cmark_node_get_end_column(cmark_node *node);
  260. /**
  261. * ## Tree Manipulation
  262. */
  263. /** Unlinks a 'node', removing it from the tree, but not freeing its
  264. * memory. (Use 'cmark_node_free' for that.)
  265. */
  266. CMARK_EXPORT void
  267. cmark_node_unlink(cmark_node *node);
  268. /** Inserts 'sibling' before 'node'. Returns 1 on success, 0 on failure.
  269. */
  270. CMARK_EXPORT int
  271. cmark_node_insert_before(cmark_node *node, cmark_node *sibling);
  272. /** Inserts 'sibling' after 'node'. Returns 1 on success, 0 on failure.
  273. */
  274. CMARK_EXPORT int
  275. cmark_node_insert_after(cmark_node *node, cmark_node *sibling);
  276. /** Adds 'child' to the beginning of the children of 'node'.
  277. * Returns 1 on success, 0 on failure.
  278. */
  279. CMARK_EXPORT int
  280. cmark_node_prepend_child(cmark_node *node, cmark_node *child);
  281. /** Adds 'child' to the end of the children of 'node'.
  282. * Returns 1 on success, 0 on failure.
  283. */
  284. CMARK_EXPORT int
  285. cmark_node_append_child(cmark_node *node, cmark_node *child);
  286. /**
  287. * ## Parsing
  288. *
  289. * Simple interface:
  290. *
  291. * cmark_node *document = cmark_parse_document("Hello *world*", 12);
  292. *
  293. * Streaming interface:
  294. *
  295. * cmark_parser *parser = cmark_parser_new();
  296. * FILE *fp = fopen("myfile.md", "r");
  297. * while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  298. * cmark_parser_feed(parser, buffer, bytes);
  299. * if (bytes < sizeof(buffer)) {
  300. * break;
  301. * }
  302. * }
  303. * document = cmark_parser_finish(parser);
  304. * cmark_parser_free(parser);
  305. */
  306. /** Creates a new parser object.
  307. */
  308. CMARK_EXPORT
  309. cmark_parser *cmark_parser_new();
  310. /** Frees memory allocated for a parser object.
  311. */
  312. CMARK_EXPORT
  313. void cmark_parser_free(cmark_parser *parser);
  314. /** Feeds a string of length 'len' to 'parser'.
  315. */
  316. CMARK_EXPORT
  317. void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len);
  318. /** Finish parsing and return a pointer to a tree of nodes.
  319. */
  320. CMARK_EXPORT
  321. cmark_node *cmark_parser_finish(cmark_parser *parser);
  322. /** Parse a CommonMark document in 'buffer' of length 'len'.
  323. * Returns a pointer to a tree of nodes.
  324. */
  325. CMARK_EXPORT
  326. cmark_node *cmark_parse_document(const char *buffer, size_t len);
  327. /** Parse a CommonMark document in file 'f', returning a pointer to
  328. * a tree of nodes.
  329. */
  330. CMARK_EXPORT
  331. cmark_node *cmark_parse_file(FILE *f);
  332. /**
  333. * ## Rendering
  334. */
  335. /** Render a 'node' tree as XML.
  336. */
  337. CMARK_EXPORT
  338. char *cmark_render_xml(cmark_node *root);
  339. /** Render a 'node' tree as an HTML fragment. It is up to the user
  340. * to add an appropriate header and footer.
  341. */
  342. CMARK_EXPORT
  343. char *cmark_render_html(cmark_node *root);
  344. /** Render a 'node' tree as a groff man page, without the header.
  345. */
  346. CMARK_EXPORT
  347. char *cmark_render_man(cmark_node *root);
  348. /** # AUTHORS
  349. *
  350. * John MacFarlane, Vicent Marti, Kārlis Gaņģis, Nick Wellnhofer.
  351. */
  352. #ifndef CMARK_NO_SHORT_NAMES
  353. #define NODE_DOCUMENT CMARK_NODE_DOCUMENT
  354. #define NODE_BLOCK_QUOTE CMARK_NODE_BLOCK_QUOTE
  355. #define NODE_LIST CMARK_NODE_LIST
  356. #define NODE_ITEM CMARK_NODE_ITEM
  357. #define NODE_CODE_BLOCK CMARK_NODE_CODE_BLOCK
  358. #define NODE_HTML CMARK_NODE_HTML
  359. #define NODE_PARAGRAPH CMARK_NODE_PARAGRAPH
  360. #define NODE_HEADER CMARK_NODE_HEADER
  361. #define NODE_HRULE CMARK_NODE_HRULE
  362. #define NODE_TEXT CMARK_NODE_TEXT
  363. #define NODE_SOFTBREAK CMARK_NODE_SOFTBREAK
  364. #define NODE_LINEBREAK CMARK_NODE_LINEBREAK
  365. #define NODE_CODE CMARK_NODE_CODE
  366. #define NODE_INLINE_HTML CMARK_NODE_INLINE_HTML
  367. #define NODE_EMPH CMARK_NODE_EMPH
  368. #define NODE_STRONG CMARK_NODE_STRONG
  369. #define NODE_LINK CMARK_NODE_LINK
  370. #define NODE_IMAGE CMARK_NODE_IMAGE
  371. #define NODE_LINK_LABEL CMARK_NODE_LINK_LABEL
  372. #define BULLET_LIST CMARK_BULLET_LIST
  373. #define ORDERED_LIST CMARK_ORDERED_LIST
  374. #define PERIOD_DELIM CMARK_PERIOD_DELIM
  375. #define PAREN_DELIM CMARK_PAREN_DELIM
  376. #endif
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380. #endif