aboutsummaryrefslogtreecommitdiff
path: root/src/cmark.h
blob: 72650c93208fc0d967593397718571476de0d1c8 (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. * Note that if you delete the current node, its first child, or its
  141. * next sibling, the iterator may point to a nonexistent note.
  142. * Use 'cmark_iter_reset' to set its pointer to the next node that
  143. * should be traversed.
  144. */
  145. /** Creates a new iterator starting at 'root'.
  146. */
  147. CMARK_EXPORT
  148. cmark_iter*
  149. cmark_iter_new(cmark_node *root);
  150. /** Frees the memory allocated for an iterator.
  151. */
  152. CMARK_EXPORT
  153. void
  154. cmark_iter_free(cmark_iter *iter);
  155. /** Resets the iterator so that the current node is 'current' and
  156. the event type is 'event_type'. Use this to resume after destructively
  157. modifying the tree structure.
  158. */
  159. CMARK_EXPORT
  160. void
  161. cmark_iter_reset(cmark_iter *iter, cmark_node *current,
  162. cmark_event_type event_type);
  163. /** Returns the event type (`CMARK_EVENT_ENTER`, `CMARK_EVENT_EXIT`,
  164. * or `CMARK_EVENT_DONE`) for the next node.
  165. */
  166. CMARK_EXPORT
  167. cmark_event_type
  168. cmark_iter_next(cmark_iter *iter);
  169. /** Returns the next node in the sequence described above.
  170. */
  171. CMARK_EXPORT
  172. cmark_node*
  173. cmark_iter_get_node(cmark_iter *iter);
  174. /**
  175. * ## Accessors
  176. */
  177. /** Returns the type of 'node', or `CMARK_NODE_NONE` on error.
  178. */
  179. CMARK_EXPORT cmark_node_type
  180. cmark_node_get_type(cmark_node *node);
  181. /** Like 'cmark_node_get_type', but returns a string representation
  182. of the type, or `"<unknown>"`.
  183. */
  184. CMARK_EXPORT
  185. const char*
  186. cmark_node_get_type_string(cmark_node *node);
  187. /** Returns the string contents of 'node', or NULL if none.
  188. */
  189. CMARK_EXPORT const char*
  190. cmark_node_get_literal(cmark_node *node);
  191. /** Sets the string contents of 'node'. Returns 1 on success,
  192. * 0 on failure.
  193. */
  194. CMARK_EXPORT int
  195. cmark_node_set_literal(cmark_node *node, const char *content);
  196. /** Returns the header level of 'node', or 0 if 'node' is not a header.
  197. */
  198. CMARK_EXPORT int
  199. cmark_node_get_header_level(cmark_node *node);
  200. /** Sets the header level of 'node', returning 1 on success and 0 on error.
  201. */
  202. CMARK_EXPORT int
  203. cmark_node_set_header_level(cmark_node *node, int level);
  204. /** Returns the list type of 'node', or `CMARK_NO_LIST` if 'node'
  205. * is not a list.
  206. */
  207. CMARK_EXPORT cmark_list_type
  208. cmark_node_get_list_type(cmark_node *node);
  209. /** Sets the list type of 'node', returning 1 on success and 0 on error.
  210. */
  211. CMARK_EXPORT int
  212. cmark_node_set_list_type(cmark_node *node, cmark_list_type type);
  213. /** Returns the list delimiter type of 'node', or `CMARK_NO_DELIM` if 'node'
  214. * is not a list.
  215. */
  216. CMARK_EXPORT cmark_delim_type
  217. cmark_node_get_list_delim(cmark_node *node);
  218. /** Sets the list delimiter type of 'node', returning 1 on success and 0
  219. * on error.
  220. */
  221. CMARK_EXPORT int
  222. cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim);
  223. /** Returns starting number of 'node', if it is an ordered list, otherwise 0.
  224. */
  225. CMARK_EXPORT int
  226. cmark_node_get_list_start(cmark_node *node);
  227. /** Sets starting number of 'node', if it is an ordered list. Returns 1
  228. * on success, 0 on failure.
  229. */
  230. CMARK_EXPORT int
  231. cmark_node_set_list_start(cmark_node *node, int start);
  232. /** Returns 1 if 'node' is a tight list, 0 otherwise.
  233. */
  234. CMARK_EXPORT int
  235. cmark_node_get_list_tight(cmark_node *node);
  236. /** Sets the "tightness" of a list. Returns 1 on success, 0 on failure.
  237. */
  238. CMARK_EXPORT int
  239. cmark_node_set_list_tight(cmark_node *node, int tight);
  240. /** Returns the info string from a fenced code block, or NULL if none.
  241. */
  242. CMARK_EXPORT const char*
  243. cmark_node_get_fence_info(cmark_node *node);
  244. /** Sets the info string in a fenced code block, returning 1 on
  245. * success and 0 on failure.
  246. */
  247. CMARK_EXPORT int
  248. cmark_node_set_fence_info(cmark_node *node, const char *info);
  249. /** Gets the URL of a link or image 'node', or NULL if none.
  250. */
  251. CMARK_EXPORT const char*
  252. cmark_node_get_url(cmark_node *node);
  253. /** Sets the URL of a link or image 'node'. Returns 1 on success,
  254. * 0 on failure.
  255. */
  256. CMARK_EXPORT int
  257. cmark_node_set_url(cmark_node *node, const char *url);
  258. /** Gets the title of a link or image 'node', or NULL if none.
  259. */
  260. CMARK_EXPORT const char*
  261. cmark_node_get_title(cmark_node *node);
  262. /** Sets the title of a link or image 'node'. Returns 1 on success,
  263. * 0 on failure.
  264. */
  265. CMARK_EXPORT int
  266. cmark_node_set_title(cmark_node *node, const char *title);
  267. /** Returns the line on which 'node' begins.
  268. */
  269. CMARK_EXPORT int
  270. cmark_node_get_start_line(cmark_node *node);
  271. /** Returns the column at which 'node' begins.
  272. */
  273. CMARK_EXPORT int
  274. cmark_node_get_start_column(cmark_node *node);
  275. /** Returns the line on which 'node' ends.
  276. */
  277. CMARK_EXPORT int
  278. cmark_node_get_end_line(cmark_node *node);
  279. /** Returns the column at which 'node' ends.
  280. */
  281. CMARK_EXPORT int
  282. cmark_node_get_end_column(cmark_node *node);
  283. /**
  284. * ## Tree Manipulation
  285. */
  286. /** Unlinks a 'node', removing it from the tree, but not freeing its
  287. * memory. (Use 'cmark_node_free' for that.)
  288. */
  289. CMARK_EXPORT void
  290. cmark_node_unlink(cmark_node *node);
  291. /** Inserts 'sibling' before 'node'. Returns 1 on success, 0 on failure.
  292. */
  293. CMARK_EXPORT int
  294. cmark_node_insert_before(cmark_node *node, cmark_node *sibling);
  295. /** Inserts 'sibling' after 'node'. Returns 1 on success, 0 on failure.
  296. */
  297. CMARK_EXPORT int
  298. cmark_node_insert_after(cmark_node *node, cmark_node *sibling);
  299. /** Adds 'child' to the beginning of the children of 'node'.
  300. * Returns 1 on success, 0 on failure.
  301. */
  302. CMARK_EXPORT int
  303. cmark_node_prepend_child(cmark_node *node, cmark_node *child);
  304. /** Adds 'child' to the end of the children of 'node'.
  305. * Returns 1 on success, 0 on failure.
  306. */
  307. CMARK_EXPORT int
  308. cmark_node_append_child(cmark_node *node, cmark_node *child);
  309. /** Consolidates adjacent text nodes.
  310. */
  311. CMARK_EXPORT void
  312. cmark_consolidate_text_nodes(cmark_node *root);
  313. /**
  314. * ## Parsing
  315. *
  316. * Simple interface:
  317. *
  318. * cmark_node *document = cmark_parse_document("Hello *world*", 12);
  319. *
  320. * Streaming interface:
  321. *
  322. * cmark_parser *parser = cmark_parser_new();
  323. * FILE *fp = fopen("myfile.md", "r");
  324. * while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  325. * cmark_parser_feed(parser, buffer, bytes);
  326. * if (bytes < sizeof(buffer)) {
  327. * break;
  328. * }
  329. * }
  330. * document = cmark_parser_finish(parser);
  331. * cmark_parser_free(parser);
  332. */
  333. /** Creates a new parser object.
  334. */
  335. CMARK_EXPORT
  336. cmark_parser *cmark_parser_new();
  337. /** Frees memory allocated for a parser object.
  338. */
  339. CMARK_EXPORT
  340. void cmark_parser_free(cmark_parser *parser);
  341. /** Feeds a string of length 'len' to 'parser'.
  342. */
  343. CMARK_EXPORT
  344. void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len);
  345. /** Finish parsing and return a pointer to a tree of nodes.
  346. */
  347. CMARK_EXPORT
  348. cmark_node *cmark_parser_finish(cmark_parser *parser);
  349. /** Parse a CommonMark document in 'buffer' of length 'len'.
  350. * Returns a pointer to a tree of nodes.
  351. */
  352. CMARK_EXPORT
  353. cmark_node *cmark_parse_document(const char *buffer, size_t len);
  354. /** Parse a CommonMark document in file 'f', returning a pointer to
  355. * a tree of nodes.
  356. */
  357. CMARK_EXPORT
  358. cmark_node *cmark_parse_file(FILE *f);
  359. /**
  360. * ## Rendering
  361. */
  362. /** Render a 'node' tree as XML.
  363. */
  364. CMARK_EXPORT
  365. char *cmark_render_xml(cmark_node *root, long options);
  366. /** Render a 'node' tree as an HTML fragment. It is up to the user
  367. * to add an appropriate header and footer.
  368. */
  369. CMARK_EXPORT
  370. char *cmark_render_html(cmark_node *root, long options);
  371. /** Render a 'node' tree as a groff man page, without the header.
  372. */
  373. CMARK_EXPORT
  374. char *cmark_render_man(cmark_node *root, long options);
  375. /** Default writer options.
  376. */
  377. #define CMARK_OPT_DEFAULT 0
  378. /** Include a `data-sourcepos` attribute on all block elements.
  379. */
  380. #define CMARK_OPT_SOURCEPOS 1
  381. /** Render `softbreak` elements as hard line breaks.
  382. */
  383. #define CMARK_OPT_HARDBREAKS 2
  384. /** Normalize tree by consolidating adjacent text nodes.
  385. */
  386. #define CMARK_OPT_NORMALIZE 4
  387. /** # AUTHORS
  388. *
  389. * John MacFarlane, Vicent Marti, Kārlis Gaņģis, Nick Wellnhofer.
  390. */
  391. #ifndef CMARK_NO_SHORT_NAMES
  392. #define NODE_DOCUMENT CMARK_NODE_DOCUMENT
  393. #define NODE_BLOCK_QUOTE CMARK_NODE_BLOCK_QUOTE
  394. #define NODE_LIST CMARK_NODE_LIST
  395. #define NODE_ITEM CMARK_NODE_ITEM
  396. #define NODE_CODE_BLOCK CMARK_NODE_CODE_BLOCK
  397. #define NODE_HTML CMARK_NODE_HTML
  398. #define NODE_PARAGRAPH CMARK_NODE_PARAGRAPH
  399. #define NODE_HEADER CMARK_NODE_HEADER
  400. #define NODE_HRULE CMARK_NODE_HRULE
  401. #define NODE_TEXT CMARK_NODE_TEXT
  402. #define NODE_SOFTBREAK CMARK_NODE_SOFTBREAK
  403. #define NODE_LINEBREAK CMARK_NODE_LINEBREAK
  404. #define NODE_CODE CMARK_NODE_CODE
  405. #define NODE_INLINE_HTML CMARK_NODE_INLINE_HTML
  406. #define NODE_EMPH CMARK_NODE_EMPH
  407. #define NODE_STRONG CMARK_NODE_STRONG
  408. #define NODE_LINK CMARK_NODE_LINK
  409. #define NODE_IMAGE CMARK_NODE_IMAGE
  410. #define NODE_LINK_LABEL CMARK_NODE_LINK_LABEL
  411. #define BULLET_LIST CMARK_BULLET_LIST
  412. #define ORDERED_LIST CMARK_ORDERED_LIST
  413. #define PERIOD_DELIM CMARK_PERIOD_DELIM
  414. #define PAREN_DELIM CMARK_PAREN_DELIM
  415. #endif
  416. #ifdef __cplusplus
  417. }
  418. #endif
  419. #endif