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