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