aboutsummaryrefslogtreecommitdiff
path: root/src/cmark.h
blob: faa515072dd4a5d7f0138e0243f65cb35b5a6ce3 (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. /** Convert 'text' (assumed to be a UTF-8 encoded string with length
  17. * 'len' from CommonMark Markdown to HTML, returning a null-terminated,
  18. * UTF-8-encoded string.
  19. */
  20. CMARK_EXPORT
  21. char *cmark_markdown_to_html(const char *text, int len);
  22. /** ## Node Structure
  23. */
  24. typedef enum {
  25. /* Error status */
  26. CMARK_NODE_NONE,
  27. /* Block */
  28. CMARK_NODE_DOCUMENT,
  29. CMARK_NODE_BLOCK_QUOTE,
  30. CMARK_NODE_LIST,
  31. CMARK_NODE_ITEM,
  32. CMARK_NODE_CODE_BLOCK,
  33. CMARK_NODE_HTML,
  34. CMARK_NODE_PARAGRAPH,
  35. CMARK_NODE_HEADER,
  36. CMARK_NODE_HRULE,
  37. CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT,
  38. CMARK_NODE_LAST_BLOCK = CMARK_NODE_HRULE,
  39. /* Inline */
  40. CMARK_NODE_TEXT,
  41. CMARK_NODE_SOFTBREAK,
  42. CMARK_NODE_LINEBREAK,
  43. CMARK_NODE_CODE,
  44. CMARK_NODE_INLINE_HTML,
  45. CMARK_NODE_EMPH,
  46. CMARK_NODE_STRONG,
  47. CMARK_NODE_LINK,
  48. CMARK_NODE_IMAGE,
  49. CMARK_NODE_FIRST_INLINE = CMARK_NODE_TEXT,
  50. CMARK_NODE_LAST_INLINE = CMARK_NODE_IMAGE,
  51. } cmark_node_type;
  52. typedef enum {
  53. CMARK_NO_LIST,
  54. CMARK_BULLET_LIST,
  55. CMARK_ORDERED_LIST
  56. } cmark_list_type;
  57. typedef enum {
  58. CMARK_NO_DELIM,
  59. CMARK_PERIOD_DELIM,
  60. CMARK_PAREN_DELIM
  61. } cmark_delim_type;
  62. typedef struct cmark_node cmark_node;
  63. typedef struct cmark_parser cmark_parser;
  64. typedef struct cmark_iter cmark_iter;
  65. typedef enum {
  66. CMARK_EVENT_NONE,
  67. CMARK_EVENT_DONE,
  68. CMARK_EVENT_ENTER,
  69. CMARK_EVENT_EXIT
  70. } cmark_event_type;
  71. /**
  72. * ## Creating and Destroying Nodes
  73. */
  74. /** Creates a new node of type 'type'. Note that the node may have
  75. * other required properties, which it is the caller's responsibility
  76. * to assign.
  77. */
  78. CMARK_EXPORT cmark_node*
  79. cmark_node_new(cmark_node_type type);
  80. /** Frees the memory allocated for a node.
  81. */
  82. CMARK_EXPORT void
  83. cmark_node_free(cmark_node *node);
  84. /**
  85. * ## Tree Traversal
  86. */
  87. /** Returns the next node in the sequence after 'node', or NULL if
  88. * there is none.
  89. */
  90. CMARK_EXPORT cmark_node*
  91. cmark_node_next(cmark_node *node);
  92. /** Returns the previous node in the sequence after 'node', or NULL if
  93. * there is none.
  94. */
  95. CMARK_EXPORT cmark_node*
  96. cmark_node_previous(cmark_node *node);
  97. /** Returns the parent of 'node', or NULL if there is none.
  98. */
  99. CMARK_EXPORT cmark_node*
  100. cmark_node_parent(cmark_node *node);
  101. /** Returns the first child of 'node', or NULL if 'node' has no children.
  102. */
  103. CMARK_EXPORT cmark_node*
  104. cmark_node_first_child(cmark_node *node);
  105. /** Returns the last child of 'node', or NULL if 'node' has no children.
  106. */
  107. CMARK_EXPORT cmark_node*
  108. cmark_node_last_child(cmark_node *node);
  109. /**
  110. * ## Iterator
  111. *
  112. * An iterator will walk through a tree of nodes, starting from a root
  113. * node, returning one node at a time, together with information about
  114. * whether the node is being entered or exited. The iterator will
  115. * first descend to a child node, if there is one. When there is no
  116. * child, the iterator will go to the next sibling. When there is no
  117. * next sibling, the iterator will return to the parent (but with
  118. * a 'cmark_event_type' of `CMARK_EVENT_EXIT`). The iterator will
  119. * return `CMARK_EVENT_DONE` when it reaches the root node again.
  120. * One natural application is an HTML renderer, where an `ENTER` event
  121. * outputs an open tag and an `EXIT` event outputs a close tag.
  122. * An iterator might also be used to transform an AST in some systematic
  123. * way, for example, turning all level-3 headers into regular paragraphs.
  124. *
  125. * void
  126. * usage_example(cmark_node *root) {
  127. * cmark_event_type ev_type;
  128. * cmark_iter *iter = cmark_iter_new(root);
  129. *
  130. * while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  131. * cmark_node *cur = cmark_iter_get_node(iter);
  132. * // Do something with `cur` and `ev_type`
  133. * }
  134. *
  135. * cmark_iter_free(iter);
  136. * }
  137. *
  138. * Iterators will never return `EXIT` events for leaf nodes, which are nodes
  139. * of type:
  140. *
  141. * * CMARK_NODE_HTML
  142. * * CMARK_NODE_HRULE
  143. * * CMARK_NODE_CODE_BLOCK
  144. * * CMARK_NODE_TEXT
  145. * * CMARK_NODE_SOFTBREAK
  146. * * CMARK_NODE_LINEBREAK
  147. * * CMARK_NODE_CODE
  148. * * CMARK_NODE_INLINE_HTML
  149. *
  150. * Nodes must only be modified after an `EXIT` event, or an `ENTER` event for
  151. * leaf nodes.
  152. */
  153. /** Creates a new iterator starting at 'root'. The current node and event
  154. * type are undefined until `cmark_iter_next` is called for the first time.
  155. */
  156. CMARK_EXPORT
  157. cmark_iter*
  158. cmark_iter_new(cmark_node *root);
  159. /** Frees the memory allocated for an iterator.
  160. */
  161. CMARK_EXPORT
  162. void
  163. cmark_iter_free(cmark_iter *iter);
  164. /** Advances to the next node and returns the event type (`CMARK_EVENT_ENTER`,
  165. * `CMARK_EVENT_EXIT` or `CMARK_EVENT_DONE`).
  166. */
  167. CMARK_EXPORT
  168. cmark_event_type
  169. cmark_iter_next(cmark_iter *iter);
  170. /** Returns the current node.
  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. /** Returns the root node.
  181. */
  182. CMARK_EXPORT
  183. cmark_node*
  184. cmark_iter_get_root(cmark_iter *iter);
  185. /** Resets the iterator so that the current node is 'current' and
  186. * the event type is 'event_type'. The new current node must be a
  187. * descendant of the root node or the root node itself.
  188. */
  189. CMARK_EXPORT
  190. void
  191. cmark_iter_reset(cmark_iter *iter, cmark_node *current,
  192. cmark_event_type event_type);
  193. /**
  194. * ## Accessors
  195. */
  196. /** Returns the user data of 'node'.
  197. */
  198. CMARK_EXPORT void*
  199. cmark_node_get_user_data(cmark_node *node);
  200. /** Sets arbitrary user data for 'node'. Returns 1 on success,
  201. * 0 on failure.
  202. */
  203. CMARK_EXPORT int
  204. cmark_node_set_user_data(cmark_node *node, void *user_data);
  205. /** Returns the type of 'node', or `CMARK_NODE_NONE` on error.
  206. */
  207. CMARK_EXPORT cmark_node_type
  208. cmark_node_get_type(cmark_node *node);
  209. /** Like 'cmark_node_get_type', but returns a string representation
  210. of the type, or `"<unknown>"`.
  211. */
  212. CMARK_EXPORT
  213. const char*
  214. cmark_node_get_type_string(cmark_node *node);
  215. /** Returns the string contents of 'node', or NULL if none.
  216. */
  217. CMARK_EXPORT const char*
  218. cmark_node_get_literal(cmark_node *node);
  219. /** Sets the string contents of 'node'. Returns 1 on success,
  220. * 0 on failure.
  221. */
  222. CMARK_EXPORT int
  223. cmark_node_set_literal(cmark_node *node, const char *content);
  224. /** Returns the header level of 'node', or 0 if 'node' is not a header.
  225. */
  226. CMARK_EXPORT int
  227. cmark_node_get_header_level(cmark_node *node);
  228. /** Sets the header level of 'node', returning 1 on success and 0 on error.
  229. */
  230. CMARK_EXPORT int
  231. cmark_node_set_header_level(cmark_node *node, int level);
  232. /** Returns the list type of 'node', or `CMARK_NO_LIST` if 'node'
  233. * is not a list.
  234. */
  235. CMARK_EXPORT cmark_list_type
  236. cmark_node_get_list_type(cmark_node *node);
  237. /** Sets the list type of 'node', returning 1 on success and 0 on error.
  238. */
  239. CMARK_EXPORT int
  240. cmark_node_set_list_type(cmark_node *node, cmark_list_type type);
  241. /** Returns the list delimiter type of 'node', or `CMARK_NO_DELIM` if 'node'
  242. * is not a list.
  243. */
  244. CMARK_EXPORT cmark_delim_type
  245. cmark_node_get_list_delim(cmark_node *node);
  246. /** Sets the list delimiter type of 'node', returning 1 on success and 0
  247. * on error.
  248. */
  249. CMARK_EXPORT int
  250. cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim);
  251. /** Returns starting number of 'node', if it is an ordered list, otherwise 0.
  252. */
  253. CMARK_EXPORT int
  254. cmark_node_get_list_start(cmark_node *node);
  255. /** Sets starting number of 'node', if it is an ordered list. Returns 1
  256. * on success, 0 on failure.
  257. */
  258. CMARK_EXPORT int
  259. cmark_node_set_list_start(cmark_node *node, int start);
  260. /** Returns 1 if 'node' is a tight list, 0 otherwise.
  261. */
  262. CMARK_EXPORT int
  263. cmark_node_get_list_tight(cmark_node *node);
  264. /** Sets the "tightness" of a list. Returns 1 on success, 0 on failure.
  265. */
  266. CMARK_EXPORT int
  267. cmark_node_set_list_tight(cmark_node *node, int tight);
  268. /** Returns the info string from a fenced code block, or NULL if none.
  269. */
  270. CMARK_EXPORT const char*
  271. cmark_node_get_fence_info(cmark_node *node);
  272. /** Sets the info string in a fenced code block, returning 1 on
  273. * success and 0 on failure.
  274. */
  275. CMARK_EXPORT int
  276. cmark_node_set_fence_info(cmark_node *node, const char *info);
  277. /** Gets the URL of a link or image 'node', or NULL if none.
  278. */
  279. CMARK_EXPORT const char*
  280. cmark_node_get_url(cmark_node *node);
  281. /** Sets the URL of a link or image 'node'. Returns 1 on success,
  282. * 0 on failure.
  283. */
  284. CMARK_EXPORT int
  285. cmark_node_set_url(cmark_node *node, const char *url);
  286. /** Gets the title of a link or image 'node', or NULL if none.
  287. */
  288. CMARK_EXPORT const char*
  289. cmark_node_get_title(cmark_node *node);
  290. /** Sets the title of a link or image 'node'. Returns 1 on success,
  291. * 0 on failure.
  292. */
  293. CMARK_EXPORT int
  294. cmark_node_set_title(cmark_node *node, const char *title);
  295. /** Returns the line on which 'node' begins.
  296. */
  297. CMARK_EXPORT int
  298. cmark_node_get_start_line(cmark_node *node);
  299. /** Returns the column at which 'node' begins.
  300. */
  301. CMARK_EXPORT int
  302. cmark_node_get_start_column(cmark_node *node);
  303. /** Returns the line on which 'node' ends.
  304. */
  305. CMARK_EXPORT int
  306. cmark_node_get_end_line(cmark_node *node);
  307. /** Returns the column at which 'node' ends.
  308. */
  309. CMARK_EXPORT int
  310. cmark_node_get_end_column(cmark_node *node);
  311. /**
  312. * ## Tree Manipulation
  313. */
  314. /** Unlinks a 'node', removing it from the tree, but not freeing its
  315. * memory. (Use 'cmark_node_free' for that.)
  316. */
  317. CMARK_EXPORT void
  318. cmark_node_unlink(cmark_node *node);
  319. /** Inserts 'sibling' before 'node'. Returns 1 on success, 0 on failure.
  320. */
  321. CMARK_EXPORT int
  322. cmark_node_insert_before(cmark_node *node, cmark_node *sibling);
  323. /** Inserts 'sibling' after 'node'. Returns 1 on success, 0 on failure.
  324. */
  325. CMARK_EXPORT int
  326. cmark_node_insert_after(cmark_node *node, cmark_node *sibling);
  327. /** Adds 'child' to the beginning of the children of 'node'.
  328. * Returns 1 on success, 0 on failure.
  329. */
  330. CMARK_EXPORT int
  331. cmark_node_prepend_child(cmark_node *node, cmark_node *child);
  332. /** Adds 'child' to the end of the children of 'node'.
  333. * Returns 1 on success, 0 on failure.
  334. */
  335. CMARK_EXPORT int
  336. cmark_node_append_child(cmark_node *node, cmark_node *child);
  337. /** Consolidates adjacent text nodes.
  338. */
  339. CMARK_EXPORT void
  340. cmark_consolidate_text_nodes(cmark_node *root);
  341. /**
  342. * ## Parsing
  343. *
  344. * Simple interface:
  345. *
  346. * cmark_node *document = cmark_parse_document("Hello *world*", 12);
  347. *
  348. * Streaming interface:
  349. *
  350. * cmark_parser *parser = cmark_parser_new();
  351. * FILE *fp = fopen("myfile.md", "r");
  352. * while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  353. * cmark_parser_feed(parser, buffer, bytes);
  354. * if (bytes < sizeof(buffer)) {
  355. * break;
  356. * }
  357. * }
  358. * document = cmark_parser_finish(parser);
  359. * cmark_parser_free(parser);
  360. */
  361. /** Creates a new parser object.
  362. */
  363. CMARK_EXPORT
  364. cmark_parser *cmark_parser_new();
  365. /** Frees memory allocated for a parser object.
  366. */
  367. CMARK_EXPORT
  368. void cmark_parser_free(cmark_parser *parser);
  369. /** Feeds a string of length 'len' to 'parser'.
  370. */
  371. CMARK_EXPORT
  372. void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len);
  373. /** Finish parsing and return a pointer to a tree of nodes.
  374. */
  375. CMARK_EXPORT
  376. cmark_node *cmark_parser_finish(cmark_parser *parser);
  377. /** Parse a CommonMark document in 'buffer' of length 'len'.
  378. * Returns a pointer to a tree of nodes.
  379. */
  380. CMARK_EXPORT
  381. cmark_node *cmark_parse_document(const char *buffer, size_t len);
  382. /** Parse a CommonMark document in file 'f', returning a pointer to
  383. * a tree of nodes.
  384. */
  385. CMARK_EXPORT
  386. cmark_node *cmark_parse_file(FILE *f);
  387. /**
  388. * ## Rendering
  389. */
  390. /** Render a 'node' tree as XML.
  391. */
  392. CMARK_EXPORT
  393. char *cmark_render_xml(cmark_node *root, long options);
  394. /** Render a 'node' tree as an HTML fragment. It is up to the user
  395. * to add an appropriate header and footer.
  396. */
  397. CMARK_EXPORT
  398. char *cmark_render_html(cmark_node *root, long options);
  399. /** Render a 'node' tree as a groff man page, without the header.
  400. */
  401. CMARK_EXPORT
  402. char *cmark_render_man(cmark_node *root, long options);
  403. /** Default writer options.
  404. */
  405. #define CMARK_OPT_DEFAULT 0
  406. /** Include a `data-sourcepos` attribute on all block elements.
  407. */
  408. #define CMARK_OPT_SOURCEPOS 1
  409. /** Render `softbreak` elements as hard line breaks.
  410. */
  411. #define CMARK_OPT_HARDBREAKS 2
  412. /** Normalize tree by consolidating adjacent text nodes.
  413. */
  414. #define CMARK_OPT_NORMALIZE 4
  415. /**
  416. * ## Version information
  417. */
  418. /** Macro containing the library version as integer for compile time
  419. * checks.
  420. *
  421. * * Bits 16-23 contain the major version.
  422. * * Bits 8-15 contain the minor version.
  423. * * Bits 0-7 contain the patchlevel.
  424. *
  425. * In hexadecimal format, the number 0x010203 represents version 1.2.3.
  426. */
  427. #define CMARK_VERSION 0x000100
  428. /** Macro containing the library version string for compile time checks.
  429. */
  430. #define CMARK_VERSION_STRING "0.1.0"
  431. /** The library version as integer for runtime checks.
  432. */
  433. CMARK_EXPORT
  434. extern const int cmark_version;
  435. /** The library version string for runtime checks.
  436. */
  437. CMARK_EXPORT
  438. extern const char cmark_version_string[];
  439. /** # AUTHORS
  440. *
  441. * John MacFarlane, Vicent Marti, Kārlis Gaņģis, Nick Wellnhofer.
  442. */
  443. #ifndef CMARK_NO_SHORT_NAMES
  444. #define NODE_DOCUMENT CMARK_NODE_DOCUMENT
  445. #define NODE_BLOCK_QUOTE CMARK_NODE_BLOCK_QUOTE
  446. #define NODE_LIST CMARK_NODE_LIST
  447. #define NODE_ITEM CMARK_NODE_ITEM
  448. #define NODE_CODE_BLOCK CMARK_NODE_CODE_BLOCK
  449. #define NODE_HTML CMARK_NODE_HTML
  450. #define NODE_PARAGRAPH CMARK_NODE_PARAGRAPH
  451. #define NODE_HEADER CMARK_NODE_HEADER
  452. #define NODE_HRULE CMARK_NODE_HRULE
  453. #define NODE_TEXT CMARK_NODE_TEXT
  454. #define NODE_SOFTBREAK CMARK_NODE_SOFTBREAK
  455. #define NODE_LINEBREAK CMARK_NODE_LINEBREAK
  456. #define NODE_CODE CMARK_NODE_CODE
  457. #define NODE_INLINE_HTML CMARK_NODE_INLINE_HTML
  458. #define NODE_EMPH CMARK_NODE_EMPH
  459. #define NODE_STRONG CMARK_NODE_STRONG
  460. #define NODE_LINK CMARK_NODE_LINK
  461. #define NODE_IMAGE CMARK_NODE_IMAGE
  462. #define NODE_LINK_LABEL CMARK_NODE_LINK_LABEL
  463. #define BULLET_LIST CMARK_BULLET_LIST
  464. #define ORDERED_LIST CMARK_ORDERED_LIST
  465. #define PERIOD_DELIM CMARK_PERIOD_DELIM
  466. #define PAREN_DELIM CMARK_PAREN_DELIM
  467. #endif
  468. #ifdef __cplusplus
  469. }
  470. #endif
  471. #endif