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