]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
1 /* $Id: mdoc_markdown.c,v 1.15 2017/03/11 12:35:45 schwarze Exp $ */
3 * Copyright (c) 2017 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>
24 #include "mandoc_aux.h"
31 int (*cond
)(struct roff_node
*n
);
32 int (*pre
)(struct roff_node
*n
);
33 void (*post
)(struct roff_node
*n
);
34 const char *prefix
; /* pre-node string constant */
35 const char *suffix
; /* post-node string constant */
38 static void md_nodelist(struct roff_node
*);
39 static void md_node(struct roff_node
*);
40 static const char *md_stack(char c
);
41 static void md_preword(void);
42 static void md_rawword(const char *);
43 static void md_word(const char *);
44 static void md_named(const char *);
45 static void md_char(unsigned char);
46 static void md_uri(const char *);
48 static int md_cond_head(struct roff_node
*);
49 static int md_cond_body(struct roff_node
*);
51 static int md_pre_raw(struct roff_node
*);
52 static int md_pre_word(struct roff_node
*);
53 static int md_pre_skip(struct roff_node
*);
54 static void md_pre_syn(struct roff_node
*);
55 static int md_pre_An(struct roff_node
*);
56 static int md_pre_Ap(struct roff_node
*);
57 static int md_pre_Bd(struct roff_node
*);
58 static int md_pre_Bk(struct roff_node
*);
59 static int md_pre_Bl(struct roff_node
*);
60 static int md_pre_D1(struct roff_node
*);
61 static int md_pre_Dl(struct roff_node
*);
62 static int md_pre_En(struct roff_node
*);
63 static int md_pre_Eo(struct roff_node
*);
64 static int md_pre_Fa(struct roff_node
*);
65 static int md_pre_Fd(struct roff_node
*);
66 static int md_pre_Fn(struct roff_node
*);
67 static int md_pre_Fo(struct roff_node
*);
68 static int md_pre_In(struct roff_node
*);
69 static int md_pre_It(struct roff_node
*);
70 static int md_pre_Lk(struct roff_node
*);
71 static int md_pre_Mt(struct roff_node
*);
72 static int md_pre_Nd(struct roff_node
*);
73 static int md_pre_Nm(struct roff_node
*);
74 static int md_pre_No(struct roff_node
*);
75 static int md_pre_Ns(struct roff_node
*);
76 static int md_pre_Pp(struct roff_node
*);
77 static int md_pre_Rs(struct roff_node
*);
78 static int md_pre_Sh(struct roff_node
*);
79 static int md_pre_Sm(struct roff_node
*);
80 static int md_pre_Vt(struct roff_node
*);
81 static int md_pre_Xr(struct roff_node
*);
82 static int md_pre__T(struct roff_node
*);
83 static int md_pre_br(struct roff_node
*);
85 static void md_post_raw(struct roff_node
*);
86 static void md_post_word(struct roff_node
*);
87 static void md_post_pc(struct roff_node
*);
88 static void md_post_Bk(struct roff_node
*);
89 static void md_post_Bl(struct roff_node
*);
90 static void md_post_D1(struct roff_node
*);
91 static void md_post_En(struct roff_node
*);
92 static void md_post_Eo(struct roff_node
*);
93 static void md_post_Fa(struct roff_node
*);
94 static void md_post_Fd(struct roff_node
*);
95 static void md_post_Fl(struct roff_node
*);
96 static void md_post_Fn(struct roff_node
*);
97 static void md_post_Fo(struct roff_node
*);
98 static void md_post_In(struct roff_node
*);
99 static void md_post_It(struct roff_node
*);
100 static void md_post_Lb(struct roff_node
*);
101 static void md_post_Nm(struct roff_node
*);
102 static void md_post_Pf(struct roff_node
*);
103 static void md_post_Vt(struct roff_node
*);
104 static void md_post__T(struct roff_node
*);
106 static const struct md_act md_acts
[MDOC_MAX
+ 1] = {
107 { NULL
, md_pre_Ap
, NULL
, NULL
, NULL
}, /* Ap */
108 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
109 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
110 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
111 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Sh */
112 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Ss */
113 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Pp */
114 { md_cond_body
, md_pre_D1
, md_post_D1
, NULL
, NULL
}, /* D1 */
115 { md_cond_body
, md_pre_Dl
, md_post_D1
, NULL
, NULL
}, /* Dl */
116 { md_cond_body
, md_pre_Bd
, md_post_D1
, NULL
, NULL
}, /* Bd */
117 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
118 { md_cond_body
, md_pre_Bl
, md_post_Bl
, NULL
, NULL
}, /* Bl */
119 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
120 { NULL
, md_pre_It
, md_post_It
, NULL
, NULL
}, /* It */
121 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ad */
122 { NULL
, md_pre_An
, NULL
, NULL
, NULL
}, /* An */
123 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ar */
124 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cd */
125 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cm */
126 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Dv */
127 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Er */
128 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Ev */
129 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ex */
130 { NULL
, md_pre_Fa
, md_post_Fa
, NULL
, NULL
}, /* Fa */
131 { NULL
, md_pre_Fd
, md_post_Fd
, "**", "**" }, /* Fd */
132 { NULL
, md_pre_raw
, md_post_Fl
, "**-", "**" }, /* Fl */
133 { NULL
, md_pre_Fn
, md_post_Fn
, NULL
, NULL
}, /* Fn */
134 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ft */
135 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ic */
136 { NULL
, md_pre_In
, md_post_In
, NULL
, NULL
}, /* In */
137 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Li */
138 { md_cond_head
, md_pre_Nd
, NULL
, NULL
, NULL
}, /* Nd */
139 { NULL
, md_pre_Nm
, md_post_Nm
, "**", "**" }, /* Nm */
140 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Op */
141 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ot */
142 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Pa */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Rv */
144 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
145 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Va */
146 { NULL
, md_pre_Vt
, md_post_Vt
, "*", "*" }, /* Vt */
147 { NULL
, md_pre_Xr
, NULL
, NULL
, NULL
}, /* Xr */
148 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %A */
149 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %B */
150 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %D */
151 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %I */
152 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %J */
153 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %N */
154 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %O */
155 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %P */
156 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %R */
157 { NULL
, md_pre__T
, md_post__T
, NULL
, NULL
}, /* %T */
158 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %V */
159 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
160 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Ao */
161 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Aq */
162 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
163 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
164 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bf XXX not implemented */
165 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bo */
166 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bq */
167 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
168 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
169 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
170 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
171 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Do */
172 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Dq */
173 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
174 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
175 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Em */
176 { md_cond_body
, md_pre_Eo
, md_post_Eo
, NULL
, NULL
}, /* Eo */
177 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
178 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ms */
179 { NULL
, md_pre_No
, NULL
, NULL
, NULL
}, /* No */
180 { NULL
, md_pre_Ns
, NULL
, NULL
, NULL
}, /* Ns */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
182 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
184 { NULL
, NULL
, md_post_Pf
, NULL
, NULL
}, /* Pf */
185 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Po */
186 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Pq */
187 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
188 { md_cond_body
, md_pre_raw
, md_post_raw
, "'`", "`'" }, /* Ql */
189 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qo */
190 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qq */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
192 { md_cond_body
, md_pre_Rs
, NULL
, NULL
, NULL
}, /* Rs */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
194 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* So */
195 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* Sq */
196 { NULL
, md_pre_Sm
, NULL
, NULL
, NULL
}, /* Sm */
197 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Sx */
198 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Sy */
199 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Tn */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
203 { NULL
, md_pre_Fo
, md_post_Fo
, "**", "**" }, /* Fo */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
205 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Oo */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
207 { NULL
, md_pre_Bk
, md_post_Bk
, NULL
, NULL
}, /* Bk */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
211 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Fr */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
213 { NULL
, NULL
, md_post_Lb
, NULL
, NULL
}, /* Lb */
214 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Lp */
215 { NULL
, md_pre_Lk
, NULL
, NULL
, NULL
}, /* Lk */
216 { NULL
, md_pre_Mt
, NULL
, NULL
, NULL
}, /* Mt */
217 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Brq */
218 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Bro */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
220 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %C */
221 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Es */
222 { md_cond_body
, md_pre_En
, md_post_En
, NULL
, NULL
}, /* En */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
224 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %Q */
225 { NULL
, md_pre_br
, NULL
, NULL
, NULL
}, /* br */
226 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* sp */
227 { NULL
, md_pre_Lk
, md_post_pc
, NULL
, NULL
}, /* %U */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
229 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ll */
230 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
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 markdown_mdoc(void *arg
, const struct roff_man
*mdoc
)
260 md_word(mdoc
->meta
.title
);
261 if (mdoc
->meta
.msec
!= NULL
) {
264 md_word(mdoc
->meta
.msec
);
268 md_word(mdoc
->meta
.vol
);
269 if (mdoc
->meta
.arch
!= NULL
) {
271 md_word(mdoc
->meta
.arch
);
276 md_nodelist(mdoc
->first
->child
);
279 md_word(mdoc
->meta
.os
);
281 md_word(mdoc
->meta
.date
);
286 md_nodelist(struct roff_node
*n
)
295 md_node(struct roff_node
*n
)
297 const struct md_act
*act
;
298 int cond
, process_children
;
300 if (n
->flags
& NODE_NOPRT
)
303 if (outflags
& MD_nonl
)
304 outflags
&= ~(MD_nl
| MD_sp
);
305 else if (outflags
& MD_spc
&& n
->flags
& NODE_LINE
)
310 process_children
= 1;
311 n
->flags
&= ~NODE_ENDED
;
315 if (n
->flags
& NODE_DELIMC
)
316 outflags
&= ~(MD_spc
| MD_spc_force
);
317 else if (outflags
& MD_Sm
)
318 outflags
|= MD_spc_force
;
320 if (n
->flags
& NODE_DELIMO
)
321 outflags
&= ~(MD_spc
| MD_spc_force
);
322 else if (outflags
& MD_Sm
)
326 act
= md_acts
+ n
->tok
;
327 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
328 if (cond
&& act
->pre
!= NULL
&&
329 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
330 process_children
= (*act
->pre
)(n
);
334 if (process_children
&& n
->child
!= NULL
)
335 md_nodelist(n
->child
);
337 if (n
->flags
& NODE_ENDED
)
340 if (cond
&& act
->post
!= NULL
)
343 if (n
->end
!= ENDBODY_NOT
)
344 n
->body
->flags
|= NODE_ENDED
;
364 stack
= mandoc_realloc(stack
, sz
);
370 return stack
== NULL
? "" : stack
;
374 * Handle vertical and horizontal spacing.
382 * If a list block is nested inside a code block or a blockquote,
383 * blank lines for paragraph breaks no longer work; instead,
384 * they terminate the list. Work around this markdown issue
385 * by using mere line breaks instead.
388 if (list_blocks
&& outflags
& MD_sp
) {
394 * End the old line if requested.
395 * Escape whitespace at the end of the markdown line
396 * such that it won't look like an output line break.
399 if (outflags
& MD_sp
)
401 else if (outflags
& MD_br
) {
404 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
407 /* Start a new line if necessary. */
409 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
411 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
416 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
420 /* Handle horizontal spacing. */
422 } else if (outflags
& MD_spc
) {
423 if (outflags
& MD_Bk
)
424 fputs(" ", stdout
);
427 escflags
&= ~ESC_FON
;
431 outflags
&= ~(MD_spc_force
| MD_nonl
);
432 if (outflags
& MD_Sm
)
439 * Print markdown syntax elements.
440 * Can also be used for constant strings when neither escaping
441 * nor delimiter handling is required.
444 md_rawword(const char *s
)
451 if (escflags
& ESC_FON
) {
452 escflags
&= ~ESC_FON
;
453 if (*s
== '*' && !code_blocks
)
454 fputs("‌", stdout
);
468 escflags
&= ~ESC_SQU
;
478 escflags
&= ~ESC_EOL
;
482 * Print text and mdoc(7) syntax elements.
485 md_word(const char *s
)
487 const char *seq
, *prevfont
, *currfont
, *nextfont
;
491 /* No spacing before closing delimiters. */
492 if (s
[0] != '\0' && s
[1] == '\0' &&
493 strchr("!),.:;?]", s
[0]) != NULL
&&
494 (outflags
& MD_spc_force
) == 0)
502 /* No spacing after opening delimiters. */
503 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
506 prevfont
= currfont
= "";
507 while ((c
= *s
++) != '\0') {
519 bs
= escflags
& ESC_BOL
&& !code_blocks
;
527 bs
= escflags
& ESC_BOL
&& !code_blocks
;
530 bs
= escflags
& ESC_HYP
&& !code_blocks
;
533 bs
= escflags
& ESC_NUM
&& !code_blocks
;
542 bs
= escflags
& ESC_NUM
&& !code_blocks
;
545 if (code_blocks
== 0) {
551 if (escflags
& ESC_BOL
&& !code_blocks
) {
557 if (code_blocks
== 0) {
565 switch (mandoc_escape(&s
, &seq
, &sz
)) {
567 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
569 case ESCAPE_NUMBERED
:
570 uc
= mchars_num2char(seq
, sz
);
573 uc
= mchars_spec2cp(seq
, sz
);
575 case ESCAPE_FONTBOLD
:
578 case ESCAPE_FONTITALIC
:
585 case ESCAPE_FONTROMAN
:
588 case ESCAPE_FONTPREV
:
592 case ESCAPE_SKIPCHAR
:
593 case ESCAPE_OVERSTRIKE
:
594 /* XXX not implemented */
600 if (nextfont
!= NULL
&& !code_blocks
) {
601 if (*currfont
!= '\0') {
603 md_rawword(currfont
);
607 if (*currfont
!= '\0') {
609 md_rawword(currfont
);
613 if ((uc
< 0x20 && uc
!= 0x09) ||
614 (uc
> 0x7E && uc
< 0xA0))
617 seq
= mchars_uc2str(uc
);
619 outcount
+= strlen(seq
);
624 escflags
&= ~ESC_FON
;
629 bs
= escflags
& ESC_SQU
&& !code_blocks
;
639 if (*currfont
!= '\0') {
641 md_rawword(currfont
);
642 } else if (s
[-2] == ' ')
645 escflags
&= ~ESC_EOL
;
649 * Print a single HTML named character reference.
652 md_named(const char *s
)
655 escflags
&= ~(ESC_FON
| ESC_EOL
);
660 * Print a single raw character and maintain certain escape flags.
663 md_char(unsigned char c
)
670 escflags
&= ~ESC_FON
;
674 escflags
&= ~ESC_HYP
;
675 if (c
== ' ' || c
== '\t' || c
== '>')
678 escflags
&= ~ESC_NUM
;
679 else if (escflags
& ESC_BOL
)
681 escflags
&= ~ESC_BOL
;
685 md_cond_head(struct roff_node
*n
)
687 return n
->type
== ROFFT_HEAD
;
691 md_cond_body(struct roff_node
*n
)
693 return n
->type
== ROFFT_BODY
;
697 md_pre_raw(struct roff_node
*n
)
701 if ((prefix
= md_acts
[n
->tok
].prefix
) != NULL
) {
711 md_post_raw(struct roff_node
*n
)
715 if ((suffix
= md_acts
[n
->tok
].suffix
) != NULL
) {
716 outflags
&= ~(MD_spc
| MD_nl
);
724 md_pre_word(struct roff_node
*n
)
728 if ((prefix
= md_acts
[n
->tok
].prefix
) != NULL
) {
736 md_post_word(struct roff_node
*n
)
740 if ((suffix
= md_acts
[n
->tok
].suffix
) != NULL
) {
741 outflags
&= ~(MD_spc
| MD_nl
);
747 md_post_pc(struct roff_node
*n
)
750 if (n
->parent
->tok
!= MDOC_Rs
)
752 if (n
->next
!= NULL
) {
754 if (n
->prev
!= NULL
&&
755 n
->prev
->tok
== n
->tok
&&
756 n
->next
->tok
== n
->tok
)
765 md_pre_skip(struct roff_node
*n
)
771 md_pre_syn(struct roff_node
*n
)
773 if (n
->prev
== NULL
|| ! (n
->flags
& NODE_SYNPRETTY
))
776 if (n
->prev
->tok
== n
->tok
&&
784 switch (n
->prev
->tok
) {
793 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
805 md_pre_An(struct roff_node
*n
)
807 switch (n
->norm
->An
.auth
) {
809 outflags
&= ~MD_An_nosplit
;
810 outflags
|= MD_An_split
;
813 outflags
&= ~MD_An_split
;
814 outflags
|= MD_An_nosplit
;
817 if (outflags
& MD_An_split
)
819 else if (n
->sec
== SEC_AUTHORS
&&
820 ! (outflags
& MD_An_nosplit
))
821 outflags
|= MD_An_split
;
827 md_pre_Ap(struct roff_node
*n
)
836 md_pre_Bd(struct roff_node
*n
)
838 switch (n
->norm
->Bd
.type
) {
848 md_pre_Bk(struct roff_node
*n
)
862 md_post_Bk(struct roff_node
*n
)
864 if (n
->type
== ROFFT_BODY
)
869 md_pre_Bl(struct roff_node
*n
)
871 n
->norm
->Bl
.count
= 0;
872 if (n
->norm
->Bl
.type
== LIST_column
)
879 md_post_Bl(struct roff_node
*n
)
881 n
->norm
->Bl
.count
= 0;
882 if (n
->norm
->Bl
.type
== LIST_column
)
888 md_pre_D1(struct roff_node
*n
)
891 * Markdown blockquote syntax does not work inside code blocks.
892 * The best we can do is fall back to another nested code block.
906 md_post_D1(struct roff_node
*n
)
917 md_pre_Dl(struct roff_node
*n
)
920 * Markdown code block syntax does not work inside blockquotes.
921 * The best we can do is fall back to another nested blockquote.
935 md_pre_En(struct roff_node
*n
)
937 if (n
->norm
->Es
== NULL
||
938 n
->norm
->Es
->child
== NULL
)
941 md_word(n
->norm
->Es
->child
->string
);
947 md_post_En(struct roff_node
*n
)
949 if (n
->norm
->Es
== NULL
||
950 n
->norm
->Es
->child
== NULL
||
951 n
->norm
->Es
->child
->next
== NULL
)
955 md_word(n
->norm
->Es
->child
->next
->string
);
959 md_pre_Eo(struct roff_node
*n
)
961 if (n
->end
== ENDBODY_NOT
&&
962 n
->parent
->head
->child
== NULL
&&
964 n
->child
->end
!= ENDBODY_NOT
)
966 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
967 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
968 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
969 outflags
&= ~(MD_spc
| MD_nl
);
974 md_post_Eo(struct roff_node
*n
)
976 if (n
->end
!= ENDBODY_NOT
) {
981 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
984 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
991 md_pre_Fa(struct roff_node
*n
)
995 am_Fa
= n
->tok
== MDOC_Fa
;
1002 outflags
&= ~MD_spc
;
1004 outflags
&= ~MD_spc
;
1006 if ((n
= n
->next
) != NULL
)
1013 md_post_Fa(struct roff_node
*n
)
1015 if (n
->next
!= NULL
&& n
->next
->tok
== MDOC_Fa
)
1020 md_pre_Fd(struct roff_node
*n
)
1028 md_post_Fd(struct roff_node
*n
)
1035 md_post_Fl(struct roff_node
*n
)
1038 if (n
->child
== NULL
&& n
->next
!= NULL
&&
1039 n
->next
->type
!= ROFFT_TEXT
&& !(n
->next
->flags
& NODE_LINE
))
1040 outflags
&= ~MD_spc
;
1044 md_pre_Fn(struct roff_node
*n
)
1048 if ((n
= n
->child
) == NULL
)
1052 outflags
&= ~MD_spc
;
1054 outflags
&= ~MD_spc
;
1056 outflags
&= ~MD_spc
;
1059 if ((n
= n
->next
) != NULL
)
1065 md_post_Fn(struct roff_node
*n
)
1068 if (n
->flags
& NODE_SYNPRETTY
) {
1075 md_pre_Fo(struct roff_node
*n
)
1082 if (n
->child
== NULL
)
1087 outflags
&= ~(MD_spc
| MD_nl
);
1097 md_post_Fo(struct roff_node
*n
)
1101 if (n
->child
!= NULL
)
1113 md_pre_In(struct roff_node
*n
)
1115 if (n
->flags
& NODE_SYNPRETTY
) {
1118 outflags
&= ~MD_spc
;
1119 md_word("#include <");
1122 outflags
&= ~MD_spc
;
1125 outflags
&= ~MD_spc
;
1130 md_post_In(struct roff_node
*n
)
1132 if (n
->flags
& NODE_SYNPRETTY
) {
1133 outflags
&= ~MD_spc
;
1137 outflags
&= ~MD_spc
;
1143 md_pre_It(struct roff_node
*n
)
1145 struct roff_node
*bln
;
1152 bln
= n
->parent
->parent
;
1153 if (bln
->norm
->Bl
.comp
== 0 &&
1154 bln
->norm
->Bl
.type
!= LIST_column
)
1158 switch (bln
->norm
->Bl
.type
) {
1180 if (bln
->norm
->Bl
.count
< 99)
1181 bln
->norm
->Bl
.count
++;
1182 printf("%d.\t", bln
->norm
->Bl
.count
);
1183 escflags
&= ~ESC_FON
;
1191 outflags
&= ~MD_spc
;
1192 outflags
|= MD_nonl
;
1195 if (code_blocks
|| quote_blocks
)
1200 bln
= n
->parent
->parent
;
1201 switch (bln
->norm
->Bl
.type
) {
1220 md_post_It(struct roff_node
*n
)
1222 struct roff_node
*bln
;
1225 if (n
->type
!= ROFFT_BODY
)
1228 bln
= n
->parent
->parent
;
1229 switch (bln
->norm
->Bl
.type
) {
1235 if (code_blocks
|| quote_blocks
)
1244 if (n
->next
== NULL
)
1247 /* Calculate the array index of the current column. */
1250 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1254 * If a width was specified for this column,
1255 * subtract what printed, and
1256 * add the same spacing as in mdoc_term.c.
1259 nc
= bln
->norm
->Bl
.ncols
;
1260 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1261 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1267 outflags
&= ~MD_spc
;
1268 escflags
&= ~ESC_FON
;
1278 md_post_Lb(struct roff_node
*n
)
1280 if (n
->sec
== SEC_LIBRARY
)
1285 md_uri(const char *s
)
1287 while (*s
!= '\0') {
1288 if (strchr("%()<>", *s
) != NULL
) {
1289 printf("%%%2.2hhX", *s
);
1300 md_pre_Lk(struct roff_node
*n
)
1302 const struct roff_node
*link
, *descr
;
1304 if ((link
= n
->child
) == NULL
)
1307 descr
= link
->next
== NULL
? link
: link
->next
;
1309 outflags
&= ~MD_spc
;
1311 md_word(descr
->string
);
1312 descr
= link
->next
== NULL
? NULL
: descr
->next
;
1313 } while (descr
!= NULL
);
1314 outflags
&= ~MD_spc
;
1316 md_uri(link
->string
);
1317 outflags
&= ~MD_spc
;
1323 md_pre_Mt(struct roff_node
*n
)
1325 const struct roff_node
*nch
;
1328 outflags
&= ~MD_spc
;
1329 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
1330 md_word(nch
->string
);
1331 outflags
&= ~MD_spc
;
1332 md_rawword("](mailto:");
1333 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
1334 md_uri(nch
->string
);
1335 if (nch
->next
!= NULL
) {
1340 outflags
&= ~MD_spc
;
1346 md_pre_Nd(struct roff_node
*n
)
1355 md_pre_Nm(struct roff_node
*n
)
1373 md_post_Nm(struct roff_node
*n
)
1389 md_pre_No(struct roff_node
*n
)
1391 outflags
|= MD_spc_force
;
1396 md_pre_Ns(struct roff_node
*n
)
1398 outflags
&= ~MD_spc
;
1403 md_post_Pf(struct roff_node
*n
)
1405 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1406 outflags
&= ~MD_spc
;
1410 md_pre_Pp(struct roff_node
*n
)
1417 md_pre_Rs(struct roff_node
*n
)
1419 if (n
->sec
== SEC_SEE_ALSO
)
1425 md_pre_Sh(struct roff_node
*n
)
1429 if (n
->sec
== SEC_AUTHORS
)
1430 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1434 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1446 md_pre_Sm(struct roff_node
*n
)
1448 if (n
->child
== NULL
)
1450 else if (strcmp("on", n
->child
->string
) == 0)
1455 if (outflags
& MD_Sm
)
1462 md_pre_Vt(struct roff_node
*n
)
1478 md_post_Vt(struct roff_node
*n
)
1491 md_pre_Xr(struct roff_node
*n
)
1500 outflags
&= ~MD_spc
;
1508 md_pre__T(struct roff_node
*n
)
1510 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1514 outflags
&= ~MD_spc
;
1519 md_post__T(struct roff_node
*n
)
1521 outflags
&= ~MD_spc
;
1522 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1530 md_pre_br(struct roff_node
*n
)