aboutsummaryrefslogtreecommitdiff
path: root/src/man.c
blob: b86c7e634e54c4c00580bef565eb6b81b78edab3 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "config.h"
  6. #include "cmark.h"
  7. #include "node.h"
  8. #include "buffer.h"
  9. // Functions to convert cmark_nodes to groff man strings.
  10. static void escape_man(strbuf *dest, const unsigned char *source, int length)
  11. {
  12. int i;
  13. unsigned char c;
  14. for (i = 0; i < length; i++) {
  15. c = source[i];
  16. if (c == '.' && i == 0) {
  17. strbuf_puts(dest, "\\&.");
  18. } else if (c == '\'' && i == 0) {
  19. strbuf_puts(dest, "\\&'");
  20. } else if (c == '-') {
  21. strbuf_puts(dest, "\\-");
  22. } else if (c == '\\') {
  23. strbuf_puts(dest, "\\e");
  24. } else {
  25. strbuf_putc(dest, source[i]);
  26. }
  27. }
  28. }
  29. static inline void cr(strbuf *man)
  30. {
  31. if (man->size && man->ptr[man->size - 1] != '\n')
  32. strbuf_putc(man, '\n');
  33. }
  34. struct render_state {
  35. strbuf* man;
  36. cmark_node *plain;
  37. };
  38. static int
  39. S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate)
  40. {
  41. struct render_state *state = vstate;
  42. cmark_node *tmp;
  43. strbuf *man = state->man;
  44. int list_number;
  45. bool entering = (ev_type == CMARK_EVENT_ENTER);
  46. if (state->plain == node) { // back at original node
  47. state->plain = NULL;
  48. }
  49. if (state->plain != NULL) {
  50. switch(node->type) {
  51. case CMARK_NODE_TEXT:
  52. case CMARK_NODE_INLINE_CODE:
  53. case CMARK_NODE_INLINE_HTML:
  54. escape_man(man, node->as.literal.data,
  55. node->as.literal.len);
  56. break;
  57. case CMARK_NODE_LINEBREAK:
  58. case CMARK_NODE_SOFTBREAK:
  59. strbuf_putc(man, ' ');
  60. break;
  61. default:
  62. break;
  63. }
  64. return 1;
  65. }
  66. switch (node->type) {
  67. case CMARK_NODE_BLOCK_QUOTE:
  68. if (entering) {
  69. cr(man);
  70. strbuf_puts(man, ".RS");
  71. cr(man);
  72. } else {
  73. cr(man);
  74. strbuf_puts(man, ".RE");
  75. cr(man);
  76. }
  77. break;
  78. case CMARK_NODE_LIST:
  79. break;
  80. case CMARK_NODE_LIST_ITEM:
  81. if (entering) {
  82. cr(man);
  83. strbuf_puts(man, ".IP ");
  84. if (cmark_node_get_list_type(node->parent) ==
  85. CMARK_BULLET_LIST) {
  86. strbuf_puts(man, "\\[bu] 2");
  87. } else {
  88. list_number = cmark_node_get_list_start(node->parent);
  89. tmp = node;
  90. while (tmp->prev) {
  91. tmp = tmp->prev;
  92. list_number += 1;
  93. }
  94. strbuf_printf(man, "\"%d.\" 4", list_number);
  95. }
  96. cr(man);
  97. } else {
  98. cr(man);
  99. }
  100. break;
  101. case CMARK_NODE_HEADER:
  102. if (entering) {
  103. cr(man);
  104. strbuf_puts(man,
  105. cmark_node_get_header_level(node) == 1 ?
  106. ".SH" : ".SS");
  107. cr(man);
  108. } else {
  109. cr(man);
  110. }
  111. break;
  112. case CMARK_NODE_CODE_BLOCK:
  113. cr(man);
  114. strbuf_puts(man, ".IP\n.nf\n\\f[C]\n");
  115. escape_man(man, node->string_content.ptr,
  116. node->string_content.size);
  117. cr(man);
  118. strbuf_puts(man, "\\f[]\n.fi");
  119. cr(man);
  120. break;
  121. case CMARK_NODE_HTML:
  122. break;
  123. case CMARK_NODE_HRULE:
  124. cr(man);
  125. strbuf_puts(man, ".PP\n * * * * *");
  126. cr(man);
  127. break;
  128. case CMARK_NODE_PARAGRAPH:
  129. if (entering) {
  130. // no blank line if first paragraph in list:
  131. if (node->parent &&
  132. node->parent->type == CMARK_NODE_LIST_ITEM &&
  133. node->prev == NULL) {
  134. // no blank line or .PP
  135. } else {
  136. cr(man);
  137. strbuf_puts(man, ".PP\n");
  138. }
  139. } else {
  140. cr(man);
  141. }
  142. break;
  143. case CMARK_NODE_TEXT:
  144. escape_man(man, node->as.literal.data,
  145. node->as.literal.len);
  146. break;
  147. case CMARK_NODE_LINEBREAK:
  148. strbuf_puts(man, ".PD 0\n.P\n.PD");
  149. cr(man);
  150. break;
  151. case CMARK_NODE_SOFTBREAK:
  152. strbuf_putc(man, '\n');
  153. break;
  154. case CMARK_NODE_INLINE_CODE:
  155. strbuf_puts(man, "\\f[C]");
  156. escape_man(man, node->as.literal.data, node->as.literal.len);
  157. strbuf_puts(man, "\\f[]");
  158. break;
  159. case CMARK_NODE_INLINE_HTML:
  160. break;
  161. case CMARK_NODE_STRONG:
  162. if (entering) {
  163. strbuf_puts(man, "\\f[B]");
  164. } else {
  165. strbuf_puts(man, "\\f[]");
  166. }
  167. break;
  168. case CMARK_NODE_EMPH:
  169. if (entering) {
  170. strbuf_puts(man, "\\f[I]");
  171. } else {
  172. strbuf_puts(man, "\\f[]");
  173. }
  174. break;
  175. case CMARK_NODE_LINK:
  176. if (!entering) {
  177. strbuf_printf(man, " (%s)",
  178. cmark_node_get_url(node));
  179. }
  180. break;
  181. case CMARK_NODE_IMAGE:
  182. if (entering) {
  183. strbuf_puts(man, "[IMAGE: ");
  184. state->plain = node;
  185. } else {
  186. strbuf_puts(man, "]");
  187. }
  188. break;
  189. default:
  190. assert(false);
  191. break;
  192. }
  193. // strbuf_putc(man, 'x');
  194. return 1;
  195. }
  196. char *cmark_render_man(cmark_node *root)
  197. {
  198. char *result;
  199. strbuf man = GH_BUF_INIT;
  200. struct render_state state = { &man, NULL };
  201. cmark_node *cur;
  202. cmark_event_type ev_type;
  203. cmark_iter *iter = cmark_iter_new(root);
  204. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  205. cur = cmark_iter_get_node(iter);
  206. S_render_node(cur, ev_type, &state);
  207. }
  208. result = (char *)strbuf_detach(&man);
  209. cmark_iter_free(iter);
  210. strbuf_free(&man);
  211. return result;
  212. }