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