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