aboutsummaryrefslogtreecommitdiff
path: root/man/man3/cmark.3
blob: 4b24391fbf71144bf565992db0c13c2e9944a29e (plain)
  1. .TH cmark 3 "January 23, 2015" "LOCAL" "Library Functions Manual"
  2. .SH
  3. NAME
  4. .PP
  5. \f[B]cmark\f[] \- CommonMark parsing, manipulating, and rendering
  6. .SH
  7. DESCRIPTION
  8. .SS
  9. Simple Interface
  10. .PP
  11. \fIchar *\f[] \fBcmark_markdown_to_html\f[](\fIconst char *text\f[], \fIint len\f[])
  12. .PP
  13. Convert \f[I]text\f[] (assumed to be a UTF\-8 encoded string with length
  14. \f[I]len\f[] from CommonMark Markdown to HTML, returning a null\-terminated,
  15. UTF\-8\-encoded string.
  16. .SS
  17. Node Structure
  18. .SS
  19. Creating and Destroying Nodes
  20. .PP
  21. \fIcmark_node*\f[] \fBcmark_node_new\f[](\fIcmark_node_type type\f[])
  22. .PP
  23. Creates a new node of type \f[I]type\f[]\&. Note that the node may have
  24. other required properties, which it is the caller's responsibility
  25. to assign.
  26. .PP
  27. \fIvoid\f[] \fBcmark_node_free\f[](\fIcmark_node *node\f[])
  28. .PP
  29. Frees the memory allocated for a node.
  30. .SS
  31. Tree Traversal
  32. .PP
  33. \fIcmark_node*\f[] \fBcmark_node_next\f[](\fIcmark_node *node\f[])
  34. .PP
  35. Returns the next node in the sequence after \f[I]node\f[], or NULL if
  36. there is none.
  37. .PP
  38. \fIcmark_node*\f[] \fBcmark_node_previous\f[](\fIcmark_node *node\f[])
  39. .PP
  40. Returns the previous node in the sequence after \f[I]node\f[], or NULL if
  41. there is none.
  42. .PP
  43. \fIcmark_node*\f[] \fBcmark_node_parent\f[](\fIcmark_node *node\f[])
  44. .PP
  45. Returns the parent of \f[I]node\f[], or NULL if there is none.
  46. .PP
  47. \fIcmark_node*\f[] \fBcmark_node_first_child\f[](\fIcmark_node *node\f[])
  48. .PP
  49. Returns the first child of \f[I]node\f[], or NULL if \f[I]node\f[] has no children.
  50. .PP
  51. \fIcmark_node*\f[] \fBcmark_node_last_child\f[](\fIcmark_node *node\f[])
  52. .PP
  53. Returns the last child of \f[I]node\f[], or NULL if \f[I]node\f[] has no children.
  54. .SS
  55. Iterator
  56. .PP
  57. An iterator will walk through a tree of nodes, starting from a root
  58. node, returning one node at a time, together with information about
  59. whether the node is being entered or exited. The iterator will
  60. first descend to a child node, if there is one. When there is no
  61. child, the iterator will go to the next sibling. When there is no
  62. next sibling, the iterator will return to the parent (but with
  63. a \f[I]cmark_event_type\f[] of \f[C]CMARK_EVENT_EXIT\f[]). The iterator will
  64. return \f[C]CMARK_EVENT_DONE\f[] when it reaches the root node again.
  65. One natural application is an HTML renderer, where an \f[C]ENTER\f[] event
  66. outputs an open tag and an \f[C]EXIT\f[] event outputs a close tag.
  67. An iterator might also be used to transform an AST in some systematic
  68. way, for example, turning all level\-3 headers into regular paragraphs.
  69. .IP
  70. .nf
  71. \f[C]
  72. void
  73. usage_example(cmark_node *root) {
  74. cmark_event_type ev_type;
  75. cmark_iter *iter = cmark_iter_new(root);
  76. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  77. cmark_node *cur = cmark_iter_get_node(iter);
  78. // Do something with `cur` and `ev_type`
  79. }
  80. cmark_iter_free(iter);
  81. }
  82. \f[]
  83. .fi
  84. .PP
  85. Iterators will never return \f[C]EXIT\f[] events for leaf nodes, which are nodes
  86. of type:
  87. .IP \[bu] 2
  88. CMARK_NODE_HTML
  89. .IP \[bu] 2
  90. CMARK_NODE_HRULE
  91. .IP \[bu] 2
  92. CMARK_NODE_CODE_BLOCK
  93. .IP \[bu] 2
  94. CMARK_NODE_TEXT
  95. .IP \[bu] 2
  96. CMARK_NODE_SOFTBREAK
  97. .IP \[bu] 2
  98. CMARK_NODE_LINEBREAK
  99. .IP \[bu] 2
  100. CMARK_NODE_CODE
  101. .IP \[bu] 2
  102. CMARK_NODE_INLINE_HTML
  103. .PP
  104. Nodes must only be modified after an \f[C]EXIT\f[] event, or an \f[C]ENTER\f[] event for
  105. leaf nodes.
  106. .PP
  107. \fIcmark_iter*\f[] \fBcmark_iter_new\f[](\fIcmark_node *root\f[])
  108. .PP
  109. Creates a new iterator starting at \f[I]root\f[]\&. The current node and event
  110. type are undefined until \f[C]cmark_iter_next\f[] is called for the first time.
  111. .PP
  112. \fIvoid\f[] \fBcmark_iter_free\f[](\fIcmark_iter *iter\f[])
  113. .PP
  114. Frees the memory allocated for an iterator.
  115. .PP
  116. \fIcmark_event_type\f[] \fBcmark_iter_next\f[](\fIcmark_iter *iter\f[])
  117. .PP
  118. Advances to the next node and returns the event type (\f[C]CMARK_EVENT_ENTER\f[],
  119. \f[C]CMARK_EVENT_EXIT\f[] or \f[C]CMARK_EVENT_DONE\f[]).
  120. .PP
  121. \fIcmark_node*\f[] \fBcmark_iter_get_node\f[](\fIcmark_iter *iter\f[])
  122. .PP
  123. Returns the current node.
  124. .PP
  125. \fIcmark_event_type\f[] \fBcmark_iter_get_event_type\f[](\fIcmark_iter *iter\f[])
  126. .PP
  127. Returns the current event type.
  128. .PP
  129. \fIcmark_node*\f[] \fBcmark_iter_get_root\f[](\fIcmark_iter *iter\f[])
  130. .PP
  131. Returns the root node.
  132. .PP
  133. \fIvoid\f[] \fBcmark_iter_reset\f[](\fIcmark_iter *iter\f[], \fIcmark_node *current\f[], \fIcmark_event_type event_type\f[])
  134. .PP
  135. Resets the iterator so that the current node is \f[I]current\f[] and
  136. the event type is \f[I]event_type\f[]\&. The new current node must be a
  137. descendant of the root node or the root node itself.
  138. .SS
  139. Accessors
  140. .PP
  141. \fIvoid*\f[] \fBcmark_node_get_user_data\f[](\fIcmark_node *node\f[])
  142. .PP
  143. Returns the user data of \f[I]node\f[]\&.
  144. .PP
  145. \fIint\f[] \fBcmark_node_set_user_data\f[](\fIcmark_node *node\f[], \fIvoid *user_data\f[])
  146. .PP
  147. Sets arbitrary user data for \f[I]node\f[]\&. Returns 1 on success,
  148. 0 on failure.
  149. .PP
  150. \fIcmark_node_type\f[] \fBcmark_node_get_type\f[](\fIcmark_node *node\f[])
  151. .PP
  152. Returns the type of \f[I]node\f[], or \f[C]CMARK_NODE_NONE\f[] on error.
  153. .PP
  154. \fIconst char*\f[] \fBcmark_node_get_type_string\f[](\fIcmark_node *node\f[])
  155. .PP
  156. Like \f[I]cmark_node_get_type\f[], but returns a string representation
  157. of the type, or \f[C]"<unknown>"\f[]\&.
  158. .PP
  159. \fIconst char*\f[] \fBcmark_node_get_literal\f[](\fIcmark_node *node\f[])
  160. .PP
  161. Returns the string contents of \f[I]node\f[], or NULL if none.
  162. .PP
  163. \fIint\f[] \fBcmark_node_set_literal\f[](\fIcmark_node *node\f[], \fIconst char *content\f[])
  164. .PP
  165. Sets the string contents of \f[I]node\f[]\&. Returns 1 on success,
  166. 0 on failure.
  167. .PP
  168. \fIint\f[] \fBcmark_node_get_header_level\f[](\fIcmark_node *node\f[])
  169. .PP
  170. Returns the header level of \f[I]node\f[], or 0 if \f[I]node\f[] is not a header.
  171. .PP
  172. \fIint\f[] \fBcmark_node_set_header_level\f[](\fIcmark_node *node\f[], \fIint level\f[])
  173. .PP
  174. Sets the header level of \f[I]node\f[], returning 1 on success and 0 on error.
  175. .PP
  176. \fIcmark_list_type\f[] \fBcmark_node_get_list_type\f[](\fIcmark_node *node\f[])
  177. .PP
  178. Returns the list type of \f[I]node\f[], or \f[C]CMARK_NO_LIST\f[] if \f[I]node\f[]
  179. is not a list.
  180. .PP
  181. \fIint\f[] \fBcmark_node_set_list_type\f[](\fIcmark_node *node\f[], \fIcmark_list_type type\f[])
  182. .PP
  183. Sets the list type of \f[I]node\f[], returning 1 on success and 0 on error.
  184. .PP
  185. \fIcmark_delim_type\f[] \fBcmark_node_get_list_delim\f[](\fIcmark_node *node\f[])
  186. .PP
  187. Returns the list delimiter type of \f[I]node\f[], or \f[C]CMARK_NO_DELIM\f[] if \f[I]node\f[]
  188. is not a list.
  189. .PP
  190. \fIint\f[] \fBcmark_node_set_list_delim\f[](\fIcmark_node *node\f[], \fIcmark_delim_type delim\f[])
  191. .PP
  192. Sets the list delimiter type of \f[I]node\f[], returning 1 on success and 0
  193. on error.
  194. .PP
  195. \fIint\f[] \fBcmark_node_get_list_start\f[](\fIcmark_node *node\f[])
  196. .PP
  197. Returns starting number of \f[I]node\f[], if it is an ordered list, otherwise 0.
  198. .PP
  199. \fIint\f[] \fBcmark_node_set_list_start\f[](\fIcmark_node *node\f[], \fIint start\f[])
  200. .PP
  201. Sets starting number of \f[I]node\f[], if it is an ordered list. Returns 1
  202. on success, 0 on failure.
  203. .PP
  204. \fIint\f[] \fBcmark_node_get_list_tight\f[](\fIcmark_node *node\f[])
  205. .PP
  206. Returns 1 if \f[I]node\f[] is a tight list, 0 otherwise.
  207. .PP
  208. \fIint\f[] \fBcmark_node_set_list_tight\f[](\fIcmark_node *node\f[], \fIint tight\f[])
  209. .PP
  210. Sets the "tightness" of a list. Returns 1 on success, 0 on failure.
  211. .PP
  212. \fIconst char*\f[] \fBcmark_node_get_fence_info\f[](\fIcmark_node *node\f[])
  213. .PP
  214. Returns the info string from a fenced code block, or NULL if none.
  215. .PP
  216. \fIint\f[] \fBcmark_node_set_fence_info\f[](\fIcmark_node *node\f[], \fIconst char *info\f[])
  217. .PP
  218. Sets the info string in a fenced code block, returning 1 on
  219. success and 0 on failure.
  220. .PP
  221. \fIconst char*\f[] \fBcmark_node_get_url\f[](\fIcmark_node *node\f[])
  222. .PP
  223. Gets the URL of a link or image \f[I]node\f[], or NULL if none.
  224. .PP
  225. \fIint\f[] \fBcmark_node_set_url\f[](\fIcmark_node *node\f[], \fIconst char *url\f[])
  226. .PP
  227. Sets the URL of a link or image \f[I]node\f[]\&. Returns 1 on success,
  228. 0 on failure.
  229. .PP
  230. \fIconst char*\f[] \fBcmark_node_get_title\f[](\fIcmark_node *node\f[])
  231. .PP
  232. Gets the title of a link or image \f[I]node\f[], or NULL if none.
  233. .PP
  234. \fIint\f[] \fBcmark_node_set_title\f[](\fIcmark_node *node\f[], \fIconst char *title\f[])
  235. .PP
  236. Sets the title of a link or image \f[I]node\f[]\&. Returns 1 on success,
  237. 0 on failure.
  238. .PP
  239. \fIint\f[] \fBcmark_node_get_start_line\f[](\fIcmark_node *node\f[])
  240. .PP
  241. Returns the line on which \f[I]node\f[] begins.
  242. .PP
  243. \fIint\f[] \fBcmark_node_get_start_column\f[](\fIcmark_node *node\f[])
  244. .PP
  245. Returns the column at which \f[I]node\f[] begins.
  246. .PP
  247. \fIint\f[] \fBcmark_node_get_end_line\f[](\fIcmark_node *node\f[])
  248. .PP
  249. Returns the line on which \f[I]node\f[] ends.
  250. .PP
  251. \fIint\f[] \fBcmark_node_get_end_column\f[](\fIcmark_node *node\f[])
  252. .PP
  253. Returns the column at which \f[I]node\f[] ends.
  254. .SS
  255. Tree Manipulation
  256. .PP
  257. \fIvoid\f[] \fBcmark_node_unlink\f[](\fIcmark_node *node\f[])
  258. .PP
  259. Unlinks a \f[I]node\f[], removing it from the tree, but not freeing its
  260. memory. (Use \f[I]cmark_node_free\f[] for that.)
  261. .PP
  262. \fIint\f[] \fBcmark_node_insert_before\f[](\fIcmark_node *node\f[], \fIcmark_node *sibling\f[])
  263. .PP
  264. Inserts \f[I]sibling\f[] before \f[I]node\f[]\&. Returns 1 on success, 0 on failure.
  265. .PP
  266. \fIint\f[] \fBcmark_node_insert_after\f[](\fIcmark_node *node\f[], \fIcmark_node *sibling\f[])
  267. .PP
  268. Inserts \f[I]sibling\f[] after \f[I]node\f[]\&. Returns 1 on success, 0 on failure.
  269. .PP
  270. \fIint\f[] \fBcmark_node_prepend_child\f[](\fIcmark_node *node\f[], \fIcmark_node *child\f[])
  271. .PP
  272. Adds \f[I]child\f[] to the beginning of the children of \f[I]node\f[]\&.
  273. Returns 1 on success, 0 on failure.
  274. .PP
  275. \fIint\f[] \fBcmark_node_append_child\f[](\fIcmark_node *node\f[], \fIcmark_node *child\f[])
  276. .PP
  277. Adds \f[I]child\f[] to the end of the children of \f[I]node\f[]\&.
  278. Returns 1 on success, 0 on failure.
  279. .PP
  280. \fIvoid\f[] \fBcmark_consolidate_text_nodes\f[](\fIcmark_node *root\f[])
  281. .PP
  282. Consolidates adjacent text nodes.
  283. .SS
  284. Parsing
  285. .PP
  286. Simple interface:
  287. .IP
  288. .nf
  289. \f[C]
  290. cmark_node *document = cmark_parse_document("Hello *world*", 12);
  291. \f[]
  292. .fi
  293. .PP
  294. Streaming interface:
  295. .IP
  296. .nf
  297. \f[C]
  298. cmark_parser *parser = cmark_parser_new();
  299. FILE *fp = fopen("myfile.md", "r");
  300. while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  301. cmark_parser_feed(parser, buffer, bytes);
  302. if (bytes < sizeof(buffer)) {
  303. break;
  304. }
  305. }
  306. document = cmark_parser_finish(parser);
  307. cmark_parser_free(parser);
  308. \f[]
  309. .fi
  310. .PP
  311. \fIcmark_parser *\f[] \fBcmark_parser_new\f[](\fI\f[])
  312. .PP
  313. Creates a new parser object.
  314. .PP
  315. \fIvoid\f[] \fBcmark_parser_free\f[](\fIcmark_parser *parser\f[])
  316. .PP
  317. Frees memory allocated for a parser object.
  318. .PP
  319. \fIvoid\f[] \fBcmark_parser_feed\f[](\fIcmark_parser *parser\f[], \fIconst char *buffer\f[], \fIsize_t len\f[])
  320. .PP
  321. Feeds a string of length \f[I]len\f[] to \f[I]parser\f[]\&.
  322. .PP
  323. \fIcmark_node *\f[] \fBcmark_parser_finish\f[](\fIcmark_parser *parser\f[])
  324. .PP
  325. Finish parsing and return a pointer to a tree of nodes.
  326. .PP
  327. \fIcmark_node *\f[] \fBcmark_parse_document\f[](\fIconst char *buffer\f[], \fIsize_t len\f[])
  328. .PP
  329. Parse a CommonMark document in \f[I]buffer\f[] of length \f[I]len\f[]\&.
  330. Returns a pointer to a tree of nodes.
  331. .PP
  332. \fIcmark_node *\f[] \fBcmark_parse_file\f[](\fIFILE *f\f[])
  333. .PP
  334. Parse a CommonMark document in file \f[I]f\f[], returning a pointer to
  335. a tree of nodes.
  336. .SS
  337. Rendering
  338. .PP
  339. \fIchar *\f[] \fBcmark_render_xml\f[](\fIcmark_node *root\f[], \fIlong options\f[])
  340. .PP
  341. Render a \f[I]node\f[] tree as XML.
  342. .PP
  343. \fIchar *\f[] \fBcmark_render_html\f[](\fIcmark_node *root\f[], \fIlong options\f[])
  344. .PP
  345. Render a \f[I]node\f[] tree as an HTML fragment. It is up to the user
  346. to add an appropriate header and footer.
  347. .PP
  348. \fIchar *\f[] \fBcmark_render_man\f[](\fIcmark_node *root\f[], \fIlong options\f[])
  349. .PP
  350. Render a \f[I]node\f[] tree as a groff man page, without the header.
  351. .PP
  352. .nf
  353. \fC
  354. .RS 0n
  355. #define CMARK_OPT_DEFAULT 0
  356. .RE
  357. \f[]
  358. .fi
  359. .PP
  360. Default writer options.
  361. .PP
  362. .nf
  363. \fC
  364. .RS 0n
  365. #define CMARK_OPT_SOURCEPOS 1
  366. .RE
  367. \f[]
  368. .fi
  369. .PP
  370. Include a \f[C]data\-sourcepos\f[] attribute on all block elements.
  371. .PP
  372. .nf
  373. \fC
  374. .RS 0n
  375. #define CMARK_OPT_HARDBREAKS 2
  376. .RE
  377. \f[]
  378. .fi
  379. .PP
  380. Render \f[C]softbreak\f[] elements as hard line breaks.
  381. .PP
  382. .nf
  383. \fC
  384. .RS 0n
  385. #define CMARK_OPT_NORMALIZE 4
  386. .RE
  387. \f[]
  388. .fi
  389. .PP
  390. Normalize tree by consolidating adjacent text nodes.
  391. .SS
  392. Version information
  393. .PP
  394. .nf
  395. \fC
  396. .RS 0n
  397. extern const int cmark_version;
  398. .RE
  399. \f[]
  400. .fi
  401. .PP
  402. The library version as integer for runtime checks. Also available as
  403. macro CMARK_VERSION for compile time checks.
  404. .IP \[bu] 2
  405. Bits 16\-23 contain the major version.
  406. .IP \[bu] 2
  407. Bits 8\-15 contain the minor version.
  408. .IP \[bu] 2
  409. Bits 0\-7 contain the patchlevel.
  410. .PP
  411. In hexadecimal format, the number 0x010203 represents version 1.2.3.
  412. .PP
  413. .nf
  414. \fC
  415. .RS 0n
  416. extern const char cmark_version_string[];
  417. .RE
  418. \f[]
  419. .fi
  420. .PP
  421. The library version string for runtime checks. Also available as
  422. macro CMARK_VERSION_STRING for compile time checks.
  423. .SH
  424. AUTHORS
  425. .PP
  426. John MacFarlane, Vicent Marti, Kārlis Gaņģis, Nick Wellnhofer.