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