]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
1 /* $Id: mdoc_markdown.c,v 1.32 2020/01/19 18:02:00 schwarze Exp $ */
3 * Copyright (c) 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
25 #include "mandoc_aux.h"
32 int (*cond
)(struct roff_node
*n
);
33 int (*pre
)(struct roff_node
*n
);
34 void (*post
)(struct roff_node
*n
);
35 const char *prefix
; /* pre-node string constant */
36 const char *suffix
; /* post-node string constant */
39 static void md_nodelist(struct roff_node
*);
40 static void md_node(struct roff_node
*);
41 static const char *md_stack(char c
);
42 static void md_preword(void);
43 static void md_rawword(const char *);
44 static void md_word(const char *);
45 static void md_named(const char *);
46 static void md_char(unsigned char);
47 static void md_uri(const char *);
49 static int md_cond_head(struct roff_node
*);
50 static int md_cond_body(struct roff_node
*);
52 static int md_pre_abort(struct roff_node
*);
53 static int md_pre_raw(struct roff_node
*);
54 static int md_pre_word(struct roff_node
*);
55 static int md_pre_skip(struct roff_node
*);
56 static void md_pre_syn(struct roff_node
*);
57 static int md_pre_An(struct roff_node
*);
58 static int md_pre_Ap(struct roff_node
*);
59 static int md_pre_Bd(struct roff_node
*);
60 static int md_pre_Bk(struct roff_node
*);
61 static int md_pre_Bl(struct roff_node
*);
62 static int md_pre_D1(struct roff_node
*);
63 static int md_pre_Dl(struct roff_node
*);
64 static int md_pre_En(struct roff_node
*);
65 static int md_pre_Eo(struct roff_node
*);
66 static int md_pre_Fa(struct roff_node
*);
67 static int md_pre_Fd(struct roff_node
*);
68 static int md_pre_Fn(struct roff_node
*);
69 static int md_pre_Fo(struct roff_node
*);
70 static int md_pre_In(struct roff_node
*);
71 static int md_pre_It(struct roff_node
*);
72 static int md_pre_Lk(struct roff_node
*);
73 static int md_pre_Mt(struct roff_node
*);
74 static int md_pre_Nd(struct roff_node
*);
75 static int md_pre_Nm(struct roff_node
*);
76 static int md_pre_No(struct roff_node
*);
77 static int md_pre_Ns(struct roff_node
*);
78 static int md_pre_Pp(struct roff_node
*);
79 static int md_pre_Rs(struct roff_node
*);
80 static int md_pre_Sh(struct roff_node
*);
81 static int md_pre_Sm(struct roff_node
*);
82 static int md_pre_Vt(struct roff_node
*);
83 static int md_pre_Xr(struct roff_node
*);
84 static int md_pre__T(struct roff_node
*);
85 static int md_pre_br(struct roff_node
*);
87 static void md_post_raw(struct roff_node
*);
88 static void md_post_word(struct roff_node
*);
89 static void md_post_pc(struct roff_node
*);
90 static void md_post_Bk(struct roff_node
*);
91 static void md_post_Bl(struct roff_node
*);
92 static void md_post_D1(struct roff_node
*);
93 static void md_post_En(struct roff_node
*);
94 static void md_post_Eo(struct roff_node
*);
95 static void md_post_Fa(struct roff_node
*);
96 static void md_post_Fd(struct roff_node
*);
97 static void md_post_Fl(struct roff_node
*);
98 static void md_post_Fn(struct roff_node
*);
99 static void md_post_Fo(struct roff_node
*);
100 static void md_post_In(struct roff_node
*);
101 static void md_post_It(struct roff_node
*);
102 static void md_post_Lb(struct roff_node
*);
103 static void md_post_Nm(struct roff_node
*);
104 static void md_post_Pf(struct roff_node
*);
105 static void md_post_Vt(struct roff_node
*);
106 static void md_post__T(struct roff_node
*);
108 static const struct md_act md_acts
[MDOC_MAX
- MDOC_Dd
] = {
109 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
110 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
111 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
112 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Sh */
113 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Ss */
114 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Pp */
115 { md_cond_body
, md_pre_D1
, md_post_D1
, NULL
, NULL
}, /* D1 */
116 { md_cond_body
, md_pre_Dl
, md_post_D1
, NULL
, NULL
}, /* Dl */
117 { md_cond_body
, md_pre_Bd
, md_post_D1
, NULL
, NULL
}, /* Bd */
118 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
119 { md_cond_body
, md_pre_Bl
, md_post_Bl
, NULL
, NULL
}, /* Bl */
120 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
121 { NULL
, md_pre_It
, md_post_It
, NULL
, NULL
}, /* It */
122 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ad */
123 { NULL
, md_pre_An
, NULL
, NULL
, NULL
}, /* An */
124 { NULL
, md_pre_Ap
, NULL
, NULL
, NULL
}, /* Ap */
125 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ar */
126 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cd */
127 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cm */
128 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Dv */
129 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Er */
130 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Ev */
131 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ex */
132 { NULL
, md_pre_Fa
, md_post_Fa
, NULL
, NULL
}, /* Fa */
133 { NULL
, md_pre_Fd
, md_post_Fd
, "**", "**" }, /* Fd */
134 { NULL
, md_pre_raw
, md_post_Fl
, "**-", "**" }, /* Fl */
135 { NULL
, md_pre_Fn
, md_post_Fn
, NULL
, NULL
}, /* Fn */
136 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ft */
137 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ic */
138 { NULL
, md_pre_In
, md_post_In
, NULL
, NULL
}, /* In */
139 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Li */
140 { md_cond_head
, md_pre_Nd
, NULL
, NULL
, NULL
}, /* Nd */
141 { NULL
, md_pre_Nm
, md_post_Nm
, "**", "**" }, /* Nm */
142 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Op */
143 { NULL
, md_pre_abort
, NULL
, NULL
, NULL
}, /* Ot */
144 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Pa */
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Rv */
146 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
147 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Va */
148 { NULL
, md_pre_Vt
, md_post_Vt
, "*", "*" }, /* Vt */
149 { NULL
, md_pre_Xr
, NULL
, NULL
, NULL
}, /* Xr */
150 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %A */
151 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %B */
152 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %D */
153 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %I */
154 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %J */
155 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %N */
156 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %O */
157 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %P */
158 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %R */
159 { NULL
, md_pre__T
, md_post__T
, NULL
, NULL
}, /* %T */
160 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %V */
161 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
162 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Ao */
163 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Aq */
164 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
165 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
166 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bf XXX not implemented */
167 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bo */
168 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bq */
169 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
170 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
171 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
172 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
173 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Do */
174 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Dq */
175 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
176 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
177 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Em */
178 { md_cond_body
, md_pre_Eo
, md_post_Eo
, NULL
, NULL
}, /* Eo */
179 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
180 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ms */
181 { NULL
, md_pre_No
, NULL
, NULL
, NULL
}, /* No */
182 { NULL
, md_pre_Ns
, NULL
, NULL
, NULL
}, /* Ns */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
184 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
186 { NULL
, NULL
, md_post_Pf
, NULL
, NULL
}, /* Pf */
187 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Po */
188 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Pq */
189 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
190 { md_cond_body
, md_pre_raw
, md_post_raw
, "'`", "`'" }, /* Ql */
191 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qo */
192 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qq */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
194 { md_cond_body
, md_pre_Rs
, NULL
, NULL
, NULL
}, /* Rs */
195 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
196 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* So */
197 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* Sq */
198 { NULL
, md_pre_Sm
, NULL
, NULL
, NULL
}, /* Sm */
199 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Sx */
200 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Sy */
201 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Tn */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
203 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
205 { NULL
, md_pre_Fo
, md_post_Fo
, "**", "**" }, /* Fo */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
207 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Oo */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
209 { NULL
, md_pre_Bk
, md_post_Bk
, NULL
, NULL
}, /* Bk */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
213 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Fr */
214 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
215 { NULL
, NULL
, md_post_Lb
, NULL
, NULL
}, /* Lb */
216 { NULL
, md_pre_abort
, NULL
, NULL
, NULL
}, /* Lp */
217 { NULL
, md_pre_Lk
, NULL
, NULL
, NULL
}, /* Lk */
218 { NULL
, md_pre_Mt
, NULL
, NULL
, NULL
}, /* Mt */
219 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Brq */
220 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Bro */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
222 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %C */
223 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Es */
224 { md_cond_body
, md_pre_En
, md_post_En
, NULL
, NULL
}, /* En */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
226 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %Q */
227 { NULL
, md_pre_Lk
, md_post_pc
, NULL
, NULL
}, /* %U */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
229 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Tg */
231 static const struct md_act
*md_act(enum roff_tok
);
234 #define MD_spc (1 << 0) /* Blank character before next word. */
235 #define MD_spc_force (1 << 1) /* Even before trailing punctuation. */
236 #define MD_nonl (1 << 2) /* Prevent linebreak in markdown code. */
237 #define MD_nl (1 << 3) /* Break markdown code line. */
238 #define MD_br (1 << 4) /* Insert an output line break. */
239 #define MD_sp (1 << 5) /* Insert a paragraph break. */
240 #define MD_Sm (1 << 6) /* Horizontal spacing mode. */
241 #define MD_Bk (1 << 7) /* Word keep mode. */
242 #define MD_An_split (1 << 8) /* Author mode is "split". */
243 #define MD_An_nosplit (1 << 9) /* Author mode is "nosplit". */
245 static int escflags
; /* Escape in generated markdown code: */
246 #define ESC_BOL (1 << 0) /* "#*+-" near the beginning of a line. */
247 #define ESC_NUM (1 << 1) /* "." after a leading number. */
248 #define ESC_HYP (1 << 2) /* "(" immediately after "]". */
249 #define ESC_SQU (1 << 4) /* "]" when "[" is open. */
250 #define ESC_FON (1 << 5) /* "*" immediately after unrelated "*". */
251 #define ESC_EOL (1 << 6) /* " " at the and of a line. */
253 static int code_blocks
, quote_blocks
, list_blocks
;
257 static const struct md_act
*
258 md_act(enum roff_tok tok
)
260 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
261 return md_acts
+ (tok
- MDOC_Dd
);
265 markdown_mdoc(void *arg
, const struct roff_meta
*mdoc
)
268 md_word(mdoc
->title
);
269 if (mdoc
->msec
!= NULL
) {
277 if (mdoc
->arch
!= NULL
) {
284 md_nodelist(mdoc
->first
->child
);
294 md_nodelist(struct roff_node
*n
)
303 md_node(struct roff_node
*n
)
305 const struct md_act
*act
;
306 int cond
, process_children
;
308 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
311 if (outflags
& MD_nonl
)
312 outflags
&= ~(MD_nl
| MD_sp
);
313 else if (outflags
& MD_spc
&& n
->flags
& NODE_LINE
)
318 process_children
= 1;
319 n
->flags
&= ~NODE_ENDED
;
321 if (n
->type
== ROFFT_TEXT
) {
322 if (n
->flags
& NODE_DELIMC
)
323 outflags
&= ~(MD_spc
| MD_spc_force
);
324 else if (outflags
& MD_Sm
)
325 outflags
|= MD_spc_force
;
327 if (n
->flags
& NODE_DELIMO
)
328 outflags
&= ~(MD_spc
| MD_spc_force
);
329 else if (outflags
& MD_Sm
)
331 } else if (n
->tok
< ROFF_MAX
) {
334 process_children
= md_pre_br(n
);
337 process_children
= md_pre_Pp(n
);
340 process_children
= 0;
344 act
= md_act(n
->tok
);
345 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
346 if (cond
&& act
->pre
!= NULL
&&
347 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
348 process_children
= (*act
->pre
)(n
);
351 if (process_children
&& n
->child
!= NULL
)
352 md_nodelist(n
->child
);
354 if (n
->flags
& NODE_ENDED
)
357 if (cond
&& act
->post
!= NULL
)
360 if (n
->end
!= ENDBODY_NOT
)
361 n
->body
->flags
|= NODE_ENDED
;
381 stack
= mandoc_realloc(stack
, sz
);
387 return stack
== NULL
? "" : stack
;
391 * Handle vertical and horizontal spacing.
399 * If a list block is nested inside a code block or a blockquote,
400 * blank lines for paragraph breaks no longer work; instead,
401 * they terminate the list. Work around this markdown issue
402 * by using mere line breaks instead.
405 if (list_blocks
&& outflags
& MD_sp
) {
411 * End the old line if requested.
412 * Escape whitespace at the end of the markdown line
413 * such that it won't look like an output line break.
416 if (outflags
& MD_sp
)
418 else if (outflags
& MD_br
) {
421 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
424 /* Start a new line if necessary. */
426 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
428 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
433 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
437 /* Handle horizontal spacing. */
439 } else if (outflags
& MD_spc
) {
440 if (outflags
& MD_Bk
)
441 fputs(" ", stdout
);
444 escflags
&= ~ESC_FON
;
448 outflags
&= ~(MD_spc_force
| MD_nonl
);
449 if (outflags
& MD_Sm
)
456 * Print markdown syntax elements.
457 * Can also be used for constant strings when neither escaping
458 * nor delimiter handling is required.
461 md_rawword(const char *s
)
468 if (escflags
& ESC_FON
) {
469 escflags
&= ~ESC_FON
;
470 if (*s
== '*' && !code_blocks
)
471 fputs("‌", stdout
);
485 escflags
&= ~ESC_SQU
;
495 escflags
&= ~ESC_EOL
;
499 * Print text and mdoc(7) syntax elements.
502 md_word(const char *s
)
504 const char *seq
, *prevfont
, *currfont
, *nextfont
;
506 int bs
, sz
, uc
, breakline
;
508 /* No spacing before closing delimiters. */
509 if (s
[0] != '\0' && s
[1] == '\0' &&
510 strchr("!),.:;?]", s
[0]) != NULL
&&
511 (outflags
& MD_spc_force
) == 0)
519 /* No spacing after opening delimiters. */
520 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
524 prevfont
= currfont
= "";
525 while ((c
= *s
++) != '\0') {
537 bs
= escflags
& ESC_BOL
&& !code_blocks
;
545 bs
= escflags
& ESC_BOL
&& !code_blocks
;
548 bs
= escflags
& ESC_HYP
&& !code_blocks
;
551 bs
= escflags
& ESC_NUM
&& !code_blocks
;
560 bs
= escflags
& ESC_NUM
&& !code_blocks
;
563 if (code_blocks
== 0) {
569 if (escflags
& ESC_BOL
&& !code_blocks
) {
575 if (code_blocks
== 0) {
583 switch (mandoc_escape(&s
, &seq
, &sz
)) {
585 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
587 case ESCAPE_NUMBERED
:
588 uc
= mchars_num2char(seq
, sz
);
591 uc
= mchars_spec2cp(seq
, sz
);
597 md_rawword("markdown");
599 case ESCAPE_FONTBOLD
:
602 case ESCAPE_FONTITALIC
:
610 case ESCAPE_FONTROMAN
:
613 case ESCAPE_FONTPREV
:
620 case ESCAPE_SKIPCHAR
:
621 case ESCAPE_OVERSTRIKE
:
622 /* XXX not implemented */
628 if (nextfont
!= NULL
&& !code_blocks
) {
629 if (*currfont
!= '\0') {
631 md_rawword(currfont
);
635 if (*currfont
!= '\0') {
637 md_rawword(currfont
);
641 if ((uc
< 0x20 && uc
!= 0x09) ||
642 (uc
> 0x7E && uc
< 0xA0))
645 seq
= mchars_uc2str(uc
);
647 outcount
+= strlen(seq
);
652 escflags
&= ~ESC_FON
;
657 bs
= escflags
& ESC_SQU
&& !code_blocks
;
667 (*s
== '\0' || *s
== ' ' || *s
== ASCII_NBRSP
)) {
670 while (*s
== ' ' || *s
== ASCII_NBRSP
)
674 if (*currfont
!= '\0') {
676 md_rawword(currfont
);
677 } else if (s
[-2] == ' ')
680 escflags
&= ~ESC_EOL
;
684 * Print a single HTML named character reference.
687 md_named(const char *s
)
690 escflags
&= ~(ESC_FON
| ESC_EOL
);
695 * Print a single raw character and maintain certain escape flags.
698 md_char(unsigned char c
)
705 escflags
&= ~ESC_FON
;
709 escflags
&= ~ESC_HYP
;
710 if (c
== ' ' || c
== '\t' || c
== '>')
713 escflags
&= ~ESC_NUM
;
714 else if (escflags
& ESC_BOL
)
716 escflags
&= ~ESC_BOL
;
720 md_cond_head(struct roff_node
*n
)
722 return n
->type
== ROFFT_HEAD
;
726 md_cond_body(struct roff_node
*n
)
728 return n
->type
== ROFFT_BODY
;
732 md_pre_abort(struct roff_node
*n
)
738 md_pre_raw(struct roff_node
*n
)
742 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
752 md_post_raw(struct roff_node
*n
)
756 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
757 outflags
&= ~(MD_spc
| MD_nl
);
765 md_pre_word(struct roff_node
*n
)
769 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
777 md_post_word(struct roff_node
*n
)
781 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
782 outflags
&= ~(MD_spc
| MD_nl
);
788 md_post_pc(struct roff_node
*n
)
791 if (n
->parent
->tok
!= MDOC_Rs
)
793 if (n
->next
!= NULL
) {
795 if (n
->prev
!= NULL
&&
796 n
->prev
->tok
== n
->tok
&&
797 n
->next
->tok
== n
->tok
)
806 md_pre_skip(struct roff_node
*n
)
812 md_pre_syn(struct roff_node
*n
)
814 if (n
->prev
== NULL
|| ! (n
->flags
& NODE_SYNPRETTY
))
817 if (n
->prev
->tok
== n
->tok
&&
825 switch (n
->prev
->tok
) {
834 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
846 md_pre_An(struct roff_node
*n
)
848 switch (n
->norm
->An
.auth
) {
850 outflags
&= ~MD_An_nosplit
;
851 outflags
|= MD_An_split
;
854 outflags
&= ~MD_An_split
;
855 outflags
|= MD_An_nosplit
;
858 if (outflags
& MD_An_split
)
860 else if (n
->sec
== SEC_AUTHORS
&&
861 ! (outflags
& MD_An_nosplit
))
862 outflags
|= MD_An_split
;
868 md_pre_Ap(struct roff_node
*n
)
877 md_pre_Bd(struct roff_node
*n
)
879 switch (n
->norm
->Bd
.type
) {
889 md_pre_Bk(struct roff_node
*n
)
903 md_post_Bk(struct roff_node
*n
)
905 if (n
->type
== ROFFT_BODY
)
910 md_pre_Bl(struct roff_node
*n
)
912 n
->norm
->Bl
.count
= 0;
913 if (n
->norm
->Bl
.type
== LIST_column
)
920 md_post_Bl(struct roff_node
*n
)
922 n
->norm
->Bl
.count
= 0;
923 if (n
->norm
->Bl
.type
== LIST_column
)
929 md_pre_D1(struct roff_node
*n
)
932 * Markdown blockquote syntax does not work inside code blocks.
933 * The best we can do is fall back to another nested code block.
947 md_post_D1(struct roff_node
*n
)
958 md_pre_Dl(struct roff_node
*n
)
961 * Markdown code block syntax does not work inside blockquotes.
962 * The best we can do is fall back to another nested blockquote.
976 md_pre_En(struct roff_node
*n
)
978 if (n
->norm
->Es
== NULL
||
979 n
->norm
->Es
->child
== NULL
)
982 md_word(n
->norm
->Es
->child
->string
);
988 md_post_En(struct roff_node
*n
)
990 if (n
->norm
->Es
== NULL
||
991 n
->norm
->Es
->child
== NULL
||
992 n
->norm
->Es
->child
->next
== NULL
)
996 md_word(n
->norm
->Es
->child
->next
->string
);
1000 md_pre_Eo(struct roff_node
*n
)
1002 if (n
->end
== ENDBODY_NOT
&&
1003 n
->parent
->head
->child
== NULL
&&
1005 n
->child
->end
!= ENDBODY_NOT
)
1007 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1008 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1009 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1010 outflags
&= ~(MD_spc
| MD_nl
);
1015 md_post_Eo(struct roff_node
*n
)
1017 if (n
->end
!= ENDBODY_NOT
) {
1022 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
1025 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
1026 outflags
&= ~MD_spc
;
1032 md_pre_Fa(struct roff_node
*n
)
1036 am_Fa
= n
->tok
== MDOC_Fa
;
1043 outflags
&= ~MD_spc
;
1045 outflags
&= ~MD_spc
;
1047 if ((n
= n
->next
) != NULL
)
1054 md_post_Fa(struct roff_node
*n
)
1056 if (n
->next
!= NULL
&& n
->next
->tok
== MDOC_Fa
)
1061 md_pre_Fd(struct roff_node
*n
)
1069 md_post_Fd(struct roff_node
*n
)
1076 md_post_Fl(struct roff_node
*n
)
1079 if (n
->child
== NULL
&& n
->next
!= NULL
&&
1080 n
->next
->type
!= ROFFT_TEXT
&& !(n
->next
->flags
& NODE_LINE
))
1081 outflags
&= ~MD_spc
;
1085 md_pre_Fn(struct roff_node
*n
)
1089 if ((n
= n
->child
) == NULL
)
1093 outflags
&= ~MD_spc
;
1095 outflags
&= ~MD_spc
;
1097 outflags
&= ~MD_spc
;
1100 if ((n
= n
->next
) != NULL
)
1106 md_post_Fn(struct roff_node
*n
)
1109 if (n
->flags
& NODE_SYNPRETTY
) {
1116 md_pre_Fo(struct roff_node
*n
)
1123 if (n
->child
== NULL
)
1128 outflags
&= ~(MD_spc
| MD_nl
);
1138 md_post_Fo(struct roff_node
*n
)
1142 if (n
->child
!= NULL
)
1154 md_pre_In(struct roff_node
*n
)
1156 if (n
->flags
& NODE_SYNPRETTY
) {
1159 outflags
&= ~MD_spc
;
1160 md_word("#include <");
1163 outflags
&= ~MD_spc
;
1166 outflags
&= ~MD_spc
;
1171 md_post_In(struct roff_node
*n
)
1173 if (n
->flags
& NODE_SYNPRETTY
) {
1174 outflags
&= ~MD_spc
;
1178 outflags
&= ~MD_spc
;
1184 md_pre_It(struct roff_node
*n
)
1186 struct roff_node
*bln
;
1193 bln
= n
->parent
->parent
;
1194 if (bln
->norm
->Bl
.comp
== 0 &&
1195 bln
->norm
->Bl
.type
!= LIST_column
)
1199 switch (bln
->norm
->Bl
.type
) {
1221 if (bln
->norm
->Bl
.count
< 99)
1222 bln
->norm
->Bl
.count
++;
1223 printf("%d.\t", bln
->norm
->Bl
.count
);
1224 escflags
&= ~ESC_FON
;
1232 outflags
&= ~MD_spc
;
1233 outflags
|= MD_nonl
;
1236 if (code_blocks
|| quote_blocks
)
1241 bln
= n
->parent
->parent
;
1242 switch (bln
->norm
->Bl
.type
) {
1261 md_post_It(struct roff_node
*n
)
1263 struct roff_node
*bln
;
1266 if (n
->type
!= ROFFT_BODY
)
1269 bln
= n
->parent
->parent
;
1270 switch (bln
->norm
->Bl
.type
) {
1276 if (code_blocks
|| quote_blocks
)
1285 if (n
->next
== NULL
)
1288 /* Calculate the array index of the current column. */
1291 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1295 * If a width was specified for this column,
1296 * subtract what printed, and
1297 * add the same spacing as in mdoc_term.c.
1300 nc
= bln
->norm
->Bl
.ncols
;
1301 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1302 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1308 outflags
&= ~MD_spc
;
1309 escflags
&= ~ESC_FON
;
1319 md_post_Lb(struct roff_node
*n
)
1321 if (n
->sec
== SEC_LIBRARY
)
1326 md_uri(const char *s
)
1328 while (*s
!= '\0') {
1329 if (strchr("%()<>", *s
) != NULL
) {
1330 printf("%%%2.2hhX", *s
);
1341 md_pre_Lk(struct roff_node
*n
)
1343 const struct roff_node
*link
, *descr
, *punct
;
1345 if ((link
= n
->child
) == NULL
)
1348 /* Find beginning of trailing punctuation. */
1350 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1351 punct
= punct
->prev
;
1352 punct
= punct
->next
;
1357 descr
= link
; /* no text */
1359 outflags
&= ~MD_spc
;
1361 md_word(descr
->string
);
1362 descr
= descr
->next
;
1363 } while (descr
!= punct
);
1364 outflags
&= ~MD_spc
;
1368 md_uri(link
->string
);
1369 outflags
&= ~MD_spc
;
1372 /* Trailing punctuation. */
1373 while (punct
!= NULL
) {
1374 md_word(punct
->string
);
1375 punct
= punct
->next
;
1381 md_pre_Mt(struct roff_node
*n
)
1383 const struct roff_node
*nch
;
1386 outflags
&= ~MD_spc
;
1387 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
1388 md_word(nch
->string
);
1389 outflags
&= ~MD_spc
;
1390 md_rawword("](mailto:");
1391 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
1392 md_uri(nch
->string
);
1393 if (nch
->next
!= NULL
) {
1398 outflags
&= ~MD_spc
;
1404 md_pre_Nd(struct roff_node
*n
)
1413 md_pre_Nm(struct roff_node
*n
)
1431 md_post_Nm(struct roff_node
*n
)
1447 md_pre_No(struct roff_node
*n
)
1449 outflags
|= MD_spc_force
;
1454 md_pre_Ns(struct roff_node
*n
)
1456 outflags
&= ~MD_spc
;
1461 md_post_Pf(struct roff_node
*n
)
1463 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1464 outflags
&= ~MD_spc
;
1468 md_pre_Pp(struct roff_node
*n
)
1475 md_pre_Rs(struct roff_node
*n
)
1477 if (n
->sec
== SEC_SEE_ALSO
)
1483 md_pre_Sh(struct roff_node
*n
)
1487 if (n
->sec
== SEC_AUTHORS
)
1488 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1492 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1504 md_pre_Sm(struct roff_node
*n
)
1506 if (n
->child
== NULL
)
1508 else if (strcmp("on", n
->child
->string
) == 0)
1513 if (outflags
& MD_Sm
)
1520 md_pre_Vt(struct roff_node
*n
)
1536 md_post_Vt(struct roff_node
*n
)
1549 md_pre_Xr(struct roff_node
*n
)
1558 outflags
&= ~MD_spc
;
1566 md_pre__T(struct roff_node
*n
)
1568 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1572 outflags
&= ~MD_spc
;
1577 md_post__T(struct roff_node
*n
)
1579 outflags
&= ~MD_spc
;
1580 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1588 md_pre_br(struct roff_node
*n
)