]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
04eda6a81ce83cd37b899a2fa70470cbc07b7608
1 /* $Id: mdoc_markdown.c,v 1.9 2017/03/08 14:30:06 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);
47 static int md_cond_head(struct roff_node
*);
48 static int md_cond_body(struct roff_node
*);
50 static int md_pre_raw(struct roff_node
*);
51 static int md_pre_word(struct roff_node
*);
52 static int md_pre_skip(struct roff_node
*);
53 static void md_pre_syn(struct roff_node
*);
54 static int md_pre_An(struct roff_node
*);
55 static int md_pre_Ap(struct roff_node
*);
56 static int md_pre_Bd(struct roff_node
*);
57 static int md_pre_Bk(struct roff_node
*);
58 static int md_pre_Bl(struct roff_node
*);
59 static int md_pre_D1(struct roff_node
*);
60 static int md_pre_Dl(struct roff_node
*);
61 static int md_pre_En(struct roff_node
*);
62 static int md_pre_Eo(struct roff_node
*);
63 static int md_pre_Fa(struct roff_node
*);
64 static int md_pre_Fd(struct roff_node
*);
65 static int md_pre_Fn(struct roff_node
*);
66 static int md_pre_Fo(struct roff_node
*);
67 static int md_pre_In(struct roff_node
*);
68 static int md_pre_It(struct roff_node
*);
69 static int md_pre_Lk(struct roff_node
*);
70 static int md_pre_Nd(struct roff_node
*);
71 static int md_pre_Nm(struct roff_node
*);
72 static int md_pre_No(struct roff_node
*);
73 static int md_pre_Ns(struct roff_node
*);
74 static int md_pre_Pp(struct roff_node
*);
75 static int md_pre_Rs(struct roff_node
*);
76 static int md_pre_Sh(struct roff_node
*);
77 static int md_pre_Sm(struct roff_node
*);
78 static int md_pre_Vt(struct roff_node
*);
79 static int md_pre_Xr(struct roff_node
*);
80 static int md_pre__T(struct roff_node
*);
81 static int md_pre_br(struct roff_node
*);
83 static void md_post_raw(struct roff_node
*);
84 static void md_post_word(struct roff_node
*);
85 static void md_post_pc(struct roff_node
*);
86 static void md_post_Bk(struct roff_node
*);
87 static void md_post_Bl(struct roff_node
*);
88 static void md_post_D1(struct roff_node
*);
89 static void md_post_En(struct roff_node
*);
90 static void md_post_Eo(struct roff_node
*);
91 static void md_post_Fa(struct roff_node
*);
92 static void md_post_Fd(struct roff_node
*);
93 static void md_post_Fl(struct roff_node
*);
94 static void md_post_Fn(struct roff_node
*);
95 static void md_post_Fo(struct roff_node
*);
96 static void md_post_In(struct roff_node
*);
97 static void md_post_It(struct roff_node
*);
98 static void md_post_Lb(struct roff_node
*);
99 static void md_post_Nm(struct roff_node
*);
100 static void md_post_Pf(struct roff_node
*);
101 static void md_post_Vt(struct roff_node
*);
102 static void md_post__T(struct roff_node
*);
104 static const struct md_act md_acts
[MDOC_MAX
+ 1] = {
105 { NULL
, md_pre_Ap
, NULL
, NULL
, NULL
}, /* Ap */
106 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
107 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
108 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
109 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Sh */
110 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Ss */
111 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Pp */
112 { md_cond_body
, md_pre_D1
, md_post_D1
, NULL
, NULL
}, /* D1 */
113 { md_cond_body
, md_pre_Dl
, md_post_D1
, NULL
, NULL
}, /* Dl */
114 { md_cond_body
, md_pre_Bd
, md_post_D1
, NULL
, NULL
}, /* Bd */
115 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
116 { md_cond_body
, md_pre_Bl
, md_post_Bl
, NULL
, NULL
}, /* Bl */
117 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
118 { NULL
, md_pre_It
, md_post_It
, NULL
, NULL
}, /* It */
119 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ad */
120 { NULL
, md_pre_An
, NULL
, NULL
, NULL
}, /* An */
121 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ar */
122 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cd */
123 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cm */
124 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Dv */
125 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Er */
126 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Ev */
127 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ex */
128 { NULL
, md_pre_Fa
, md_post_Fa
, NULL
, NULL
}, /* Fa */
129 { NULL
, md_pre_Fd
, md_post_Fd
, "**", "**" }, /* Fd */
130 { NULL
, md_pre_raw
, md_post_Fl
, "**-", "**" }, /* Fl */
131 { NULL
, md_pre_Fn
, md_post_Fn
, NULL
, NULL
}, /* Fn */
132 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ft */
133 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ic */
134 { NULL
, md_pre_In
, md_post_In
, NULL
, NULL
}, /* In */
135 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Li */
136 { md_cond_head
, md_pre_Nd
, NULL
, NULL
, NULL
}, /* Nd */
137 { NULL
, md_pre_Nm
, md_post_Nm
, "**", "**" }, /* Nm */
138 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Op */
139 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ot */
140 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Pa */
141 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Rv */
142 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
143 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Va */
144 { NULL
, md_pre_Vt
, md_post_Vt
, "*", "*" }, /* Vt */
145 { NULL
, md_pre_Xr
, NULL
, NULL
, NULL
}, /* Xr */
146 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %A */
147 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %B */
148 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %D */
149 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %I */
150 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %J */
151 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %N */
152 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %O */
153 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %P */
154 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %R */
155 { NULL
, md_pre__T
, md_post__T
, NULL
, NULL
}, /* %T */
156 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %V */
157 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
158 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Ao */
159 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Aq */
160 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
161 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
162 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bf XXX not implemented */
163 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bo */
164 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bq */
165 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
166 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
167 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
168 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
169 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Do */
170 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Dq */
171 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
172 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
173 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Em */
174 { md_cond_body
, md_pre_Eo
, md_post_Eo
, NULL
, NULL
}, /* Eo */
175 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
176 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ms */
177 { NULL
, md_pre_No
, NULL
, NULL
, NULL
}, /* No */
178 { NULL
, md_pre_Ns
, NULL
, NULL
, NULL
}, /* Ns */
179 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
180 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
182 { NULL
, NULL
, md_post_Pf
, NULL
, NULL
}, /* Pf */
183 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Po */
184 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Pq */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
186 { md_cond_body
, md_pre_raw
, md_post_raw
, "'`", "`'" }, /* Ql */
187 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qo */
188 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qq */
189 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
190 { md_cond_body
, md_pre_Rs
, NULL
, NULL
, NULL
}, /* Rs */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
192 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* So */
193 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* Sq */
194 { NULL
, md_pre_Sm
, NULL
, NULL
, NULL
}, /* Sm */
195 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Sx */
196 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Sy */
197 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Tn */
198 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
199 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
201 { NULL
, md_pre_Fo
, md_post_Fo
, "**", "**" }, /* Fo */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
203 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Oo */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
205 { NULL
, md_pre_Bk
, md_post_Bk
, NULL
, NULL
}, /* Bk */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
207 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
209 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Fr */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
211 { NULL
, NULL
, md_post_Lb
, NULL
, NULL
}, /* Lb */
212 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Lp */
213 { NULL
, md_pre_Lk
, NULL
, NULL
, NULL
}, /* Lk */
214 { NULL
, md_pre_raw
, md_post_raw
, "<", ">" }, /* Mt */
215 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Brq */
216 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Bro */
217 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
218 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %C */
219 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Es */
220 { md_cond_body
, md_pre_En
, md_post_En
, NULL
, NULL
}, /* En */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
222 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %Q */
223 { NULL
, md_pre_br
, NULL
, NULL
, NULL
}, /* br */
224 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* sp */
225 { NULL
, md_pre_Lk
, md_post_pc
, NULL
, NULL
}, /* %U */
226 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
227 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ll */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
232 #define MD_spc (1 << 0) /* Blank character before next word. */
233 #define MD_spc_force (1 << 1) /* Even before trailing punctuation. */
234 #define MD_nonl (1 << 2) /* Prevent linebreak in markdown code. */
235 #define MD_nl (1 << 3) /* Break markdown code line. */
236 #define MD_br (1 << 4) /* Insert an output line break. */
237 #define MD_sp (1 << 5) /* Insert a paragraph break. */
238 #define MD_Sm (1 << 6) /* Horizontal spacing mode. */
239 #define MD_Bk (1 << 7) /* Word keep mode. */
240 #define MD_An_split (1 << 8) /* Author mode is "split". */
241 #define MD_An_nosplit (1 << 9) /* Author mode is "nosplit". */
243 static int escflags
; /* Escape in generated markdown code: */
244 #define ESC_BOL (1 << 0) /* "#*+-" near the beginning of a line. */
245 #define ESC_NUM (1 << 1) /* "." after a leading number. */
246 #define ESC_HYP (1 << 2) /* "(" immediately after "]". */
247 #define ESC_PAR (1 << 3) /* ")" when "(" is open. */
248 #define ESC_SQU (1 << 4) /* "]" when "[" is open. */
249 #define ESC_FON (1 << 5) /* "*" immediately after unrelated "*". */
250 #define ESC_EOL (1 << 6) /* " " at the and of a line. */
252 static int code_blocks
, quote_blocks
, list_blocks
;
256 markdown_mdoc(void *arg
, const struct roff_man
*mdoc
)
259 md_word(mdoc
->meta
.title
);
260 if (mdoc
->meta
.msec
!= NULL
) {
263 md_word(mdoc
->meta
.msec
);
267 md_word(mdoc
->meta
.vol
);
268 if (mdoc
->meta
.arch
!= NULL
) {
270 md_word(mdoc
->meta
.arch
);
275 md_nodelist(mdoc
->first
->child
);
278 md_word(mdoc
->meta
.os
);
280 md_word(mdoc
->meta
.date
);
285 md_nodelist(struct roff_node
*n
)
294 md_node(struct roff_node
*n
)
296 const struct md_act
*act
;
297 int cond
, process_children
;
299 if (n
->flags
& NODE_NOPRT
)
302 if (outflags
& MD_nonl
)
303 outflags
&= ~(MD_nl
| MD_sp
);
304 else if (outflags
& MD_spc
&& n
->flags
& NODE_LINE
)
309 process_children
= 1;
310 n
->flags
&= ~NODE_ENDED
;
314 if (n
->flags
& NODE_DELIMC
)
315 outflags
&= ~(MD_spc
| MD_spc_force
);
316 else if (outflags
& MD_Sm
)
317 outflags
|= MD_spc_force
;
319 if (n
->flags
& NODE_DELIMO
)
320 outflags
&= ~(MD_spc
| MD_spc_force
);
321 else if (outflags
& MD_Sm
)
325 act
= md_acts
+ n
->tok
;
326 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
327 if (cond
&& act
->pre
!= NULL
&&
328 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
329 process_children
= (*act
->pre
)(n
);
333 if (process_children
&& n
->child
!= NULL
)
334 md_nodelist(n
->child
);
336 if (n
->flags
& NODE_ENDED
)
339 if (cond
&& act
->post
!= NULL
)
342 if (n
->end
!= ENDBODY_NOT
)
343 n
->body
->flags
|= NODE_ENDED
;
363 stack
= mandoc_realloc(stack
, sz
);
369 return stack
== NULL
? "" : stack
;
373 * Handle vertical and horizontal spacing.
381 * If a list block is nested inside a code block or a blockquote,
382 * blank lines for paragraph breaks no longer work; instead,
383 * they terminate the list. Work around this markdown issue
384 * by using mere line breaks instead.
387 if (list_blocks
&& outflags
& MD_sp
) {
393 * End the old line if requested.
394 * Escape whitespace at the end of the markdown line
395 * such that it won't look like an output line break.
398 if (outflags
& MD_sp
)
400 else if (outflags
& MD_br
) {
403 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
406 /* Start a new line if necessary. */
408 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
410 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
415 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
419 /* Handle horizontal spacing. */
421 } else if (outflags
& MD_spc
) {
422 if (outflags
& MD_Bk
)
423 fputs(" ", stdout
);
426 escflags
&= ~ESC_FON
;
430 outflags
&= ~(MD_spc_force
| MD_nonl
);
431 if (outflags
& MD_Sm
)
438 * Print markdown syntax elements.
439 * Can also be used for constant strings when neither escaping
440 * nor delimiter handling is required.
443 md_rawword(const char *s
)
450 if (escflags
& ESC_FON
) {
451 escflags
&= ~ESC_FON
;
452 if (*s
== '*' && !code_blocks
)
453 fputs("‌", stdout
);
462 escflags
|= ~ESC_PAR
;
473 escflags
&= ~ESC_SQU
;
483 escflags
&= ~ESC_EOL
;
487 * Print text and mdoc(7) syntax elements.
490 md_word(const char *s
)
492 const char *seq
, *prevfont
, *currfont
, *nextfont
;
496 /* No spacing before closing delimiters. */
497 if (s
[0] != '\0' && s
[1] == '\0' &&
498 strchr("!),.:;?]", s
[0]) != NULL
&&
499 (outflags
& MD_spc_force
) == 0)
507 /* No spacing after opening delimiters. */
508 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
511 prevfont
= currfont
= "";
512 while ((c
= *s
++) != '\0') {
524 bs
= escflags
& ESC_BOL
&& !code_blocks
;
532 bs
= escflags
& ESC_BOL
&& !code_blocks
;
535 bs
= escflags
& ESC_HYP
&& !code_blocks
;
538 bs
= escflags
& ESC_PAR
&& !code_blocks
;
547 bs
= escflags
& ESC_NUM
&& !code_blocks
;
550 if (code_blocks
== 0) {
556 if (escflags
& ESC_BOL
&& !code_blocks
) {
562 if (code_blocks
== 0) {
570 switch (mandoc_escape(&s
, &seq
, &sz
)) {
572 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
574 case ESCAPE_NUMBERED
:
575 uc
= mchars_num2char(seq
, sz
);
578 uc
= mchars_spec2cp(seq
, sz
);
580 case ESCAPE_FONTBOLD
:
583 case ESCAPE_FONTITALIC
:
590 case ESCAPE_FONTROMAN
:
593 case ESCAPE_FONTPREV
:
597 case ESCAPE_SKIPCHAR
:
598 case ESCAPE_OVERSTRIKE
:
599 /* XXX not implemented */
605 if (nextfont
!= NULL
&& !code_blocks
) {
606 if (*currfont
!= '\0') {
608 md_rawword(currfont
);
612 if (*currfont
!= '\0') {
614 md_rawword(currfont
);
618 if ((uc
< 0x20 && uc
!= 0x09) ||
619 (uc
> 0x7E && uc
< 0xA0))
622 seq
= mchars_uc2str(uc
);
624 outcount
+= strlen(seq
);
629 escflags
&= ~ESC_FON
;
634 bs
= escflags
& ESC_SQU
&& !code_blocks
;
644 if (*currfont
!= '\0') {
646 md_rawword(currfont
);
647 } else if (s
[-2] == ' ')
650 escflags
&= ~ESC_EOL
;
654 * Print a single HTML named character reference.
657 md_named(const char *s
)
660 escflags
&= ~(ESC_FON
| ESC_EOL
);
665 * Print a single raw character and maintain certain escape flags.
668 md_char(unsigned char c
)
675 escflags
&= ~ESC_FON
;
679 escflags
&= ~ESC_HYP
;
680 if (c
== ' ' || c
== '\t' || c
== '>')
683 escflags
&= ~ESC_NUM
;
684 else if (escflags
& ESC_BOL
)
686 escflags
&= ~ESC_BOL
;
690 md_cond_head(struct roff_node
*n
)
692 return n
->type
== ROFFT_HEAD
;
696 md_cond_body(struct roff_node
*n
)
698 return n
->type
== ROFFT_BODY
;
702 md_pre_raw(struct roff_node
*n
)
706 if ((prefix
= md_acts
[n
->tok
].prefix
) != NULL
) {
714 md_post_raw(struct roff_node
*n
)
718 if ((suffix
= md_acts
[n
->tok
].suffix
) != NULL
) {
719 outflags
&= ~(MD_spc
| MD_nl
);
725 md_pre_word(struct roff_node
*n
)
729 if ((prefix
= md_acts
[n
->tok
].prefix
) != NULL
) {
737 md_post_word(struct roff_node
*n
)
741 if ((suffix
= md_acts
[n
->tok
].suffix
) != NULL
) {
742 outflags
&= ~(MD_spc
| MD_nl
);
748 md_post_pc(struct roff_node
*n
)
751 if (n
->parent
->tok
!= MDOC_Rs
)
753 if (n
->next
!= NULL
) {
755 if (n
->prev
!= NULL
&&
756 n
->prev
->tok
== n
->tok
&&
757 n
->next
->tok
== n
->tok
)
766 md_pre_skip(struct roff_node
*n
)
772 md_pre_syn(struct roff_node
*n
)
774 if (n
->prev
== NULL
|| ! (n
->flags
& NODE_SYNPRETTY
))
777 if (n
->prev
->tok
== n
->tok
&&
785 switch (n
->prev
->tok
) {
794 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
806 md_pre_An(struct roff_node
*n
)
808 switch (n
->norm
->An
.auth
) {
810 outflags
&= ~MD_An_nosplit
;
811 outflags
|= MD_An_split
;
814 outflags
&= ~MD_An_split
;
815 outflags
|= MD_An_nosplit
;
818 if (outflags
& MD_An_split
)
820 else if (n
->sec
== SEC_AUTHORS
&&
821 ! (outflags
& MD_An_nosplit
))
822 outflags
|= MD_An_split
;
828 md_pre_Ap(struct roff_node
*n
)
837 md_pre_Bd(struct roff_node
*n
)
839 switch (n
->norm
->Bd
.type
) {
849 md_pre_Bk(struct roff_node
*n
)
863 md_post_Bk(struct roff_node
*n
)
865 if (n
->type
== ROFFT_BODY
)
870 md_pre_Bl(struct roff_node
*n
)
872 n
->norm
->Bl
.count
= 0;
873 if (n
->norm
->Bl
.type
== LIST_column
)
880 md_post_Bl(struct roff_node
*n
)
882 n
->norm
->Bl
.count
= 0;
883 if (n
->norm
->Bl
.type
== LIST_column
)
889 md_pre_D1(struct roff_node
*n
)
892 * Markdown blockquote syntax does not work inside code blocks.
893 * The best we can do is fall back to another nested code block.
907 md_post_D1(struct roff_node
*n
)
918 md_pre_Dl(struct roff_node
*n
)
921 * Markdown code block syntax does not work inside blockquotes.
922 * The best we can do is fall back to another nested blockquote.
936 md_pre_En(struct roff_node
*n
)
938 if (n
->norm
->Es
== NULL
||
939 n
->norm
->Es
->child
== NULL
)
942 md_word(n
->norm
->Es
->child
->string
);
948 md_post_En(struct roff_node
*n
)
950 if (n
->norm
->Es
== NULL
||
951 n
->norm
->Es
->child
== NULL
||
952 n
->norm
->Es
->child
->next
== NULL
)
956 md_word(n
->norm
->Es
->child
->next
->string
);
960 md_pre_Eo(struct roff_node
*n
)
962 if (n
->end
== ENDBODY_NOT
&&
963 n
->parent
->head
->child
== NULL
&&
965 n
->child
->end
!= ENDBODY_NOT
)
967 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
968 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
969 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
970 outflags
&= ~(MD_spc
| MD_nl
);
975 md_post_Eo(struct roff_node
*n
)
977 if (n
->end
!= ENDBODY_NOT
) {
982 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
985 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
992 md_pre_Fa(struct roff_node
*n
)
996 am_Fa
= n
->tok
== MDOC_Fa
;
1003 outflags
&= ~MD_spc
;
1005 outflags
&= ~MD_spc
;
1007 if ((n
= n
->next
) != NULL
)
1014 md_post_Fa(struct roff_node
*n
)
1016 if (n
->next
!= NULL
&& n
->next
->tok
== MDOC_Fa
)
1021 md_pre_Fd(struct roff_node
*n
)
1029 md_post_Fd(struct roff_node
*n
)
1036 md_post_Fl(struct roff_node
*n
)
1039 if (n
->child
== NULL
&& n
->next
!= NULL
&&
1040 n
->next
->type
!= ROFFT_TEXT
&& !(n
->next
->flags
& NODE_LINE
))
1041 outflags
&= ~MD_spc
;
1045 md_pre_Fn(struct roff_node
*n
)
1049 if ((n
= n
->child
) == NULL
)
1053 outflags
&= ~MD_spc
;
1055 outflags
&= ~MD_spc
;
1057 outflags
&= ~MD_spc
;
1060 if ((n
= n
->next
) != NULL
)
1066 md_post_Fn(struct roff_node
*n
)
1069 if (n
->flags
& NODE_SYNPRETTY
) {
1076 md_pre_Fo(struct roff_node
*n
)
1083 if (n
->child
== NULL
)
1088 outflags
&= ~(MD_spc
| MD_nl
);
1098 md_post_Fo(struct roff_node
*n
)
1102 if (n
->child
!= NULL
)
1114 md_pre_In(struct roff_node
*n
)
1116 if (n
->flags
& NODE_SYNPRETTY
) {
1119 outflags
&= ~MD_spc
;
1120 md_word("#include <");
1123 outflags
&= ~MD_spc
;
1126 outflags
&= ~MD_spc
;
1131 md_post_In(struct roff_node
*n
)
1133 if (n
->flags
& NODE_SYNPRETTY
) {
1134 outflags
&= ~MD_spc
;
1138 outflags
&= ~MD_spc
;
1144 md_pre_It(struct roff_node
*n
)
1146 struct roff_node
*bln
;
1153 bln
= n
->parent
->parent
;
1154 if (bln
->norm
->Bl
.comp
== 0)
1158 switch (bln
->norm
->Bl
.type
) {
1180 printf("%d.\t", ++bln
->norm
->Bl
.count
);
1181 escflags
&= ~ESC_FON
;
1186 outflags
&= ~MD_spc
;
1187 outflags
|= MD_nonl
;
1190 if (code_blocks
|| quote_blocks
)
1195 bln
= n
->parent
->parent
;
1196 switch (bln
->norm
->Bl
.type
) {
1215 md_post_It(struct roff_node
*n
)
1217 struct roff_node
*bln
;
1220 if (n
->type
!= ROFFT_BODY
)
1223 bln
= n
->parent
->parent
;
1224 switch (bln
->norm
->Bl
.type
) {
1230 if (code_blocks
|| quote_blocks
)
1239 if (n
->next
== NULL
)
1242 /* Calculate the array index of the current column. */
1245 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1249 * If a width was specified for this column,
1250 * subtract what printed, and
1251 * add the same spacing as in mdoc_term.c.
1254 nc
= bln
->norm
->Bl
.ncols
;
1255 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1256 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1262 outflags
&= ~MD_spc
;
1263 escflags
&= ~ESC_FON
;
1273 md_post_Lb(struct roff_node
*n
)
1275 if (n
->sec
== SEC_LIBRARY
)
1280 md_pre_Lk(struct roff_node
*n
)
1282 const struct roff_node
*link
, *descr
;
1283 const unsigned char *s
;
1285 if ((link
= n
->child
) == NULL
)
1288 if ((descr
= link
->next
) != NULL
) {
1290 outflags
&= ~MD_spc
;
1291 while (descr
!= NULL
) {
1292 md_word(descr
->string
);
1293 descr
= descr
->next
;
1295 outflags
&= ~MD_spc
;
1300 for (s
= link
->string
; *s
!= '\0'; s
++) {
1301 if (strchr("%)<>", *s
) != NULL
) {
1302 printf("%%%2.2hhX", *s
);
1310 outflags
&= ~MD_spc
;
1311 md_rawword(link
->next
== NULL
? ">" : ")");
1316 md_pre_Nd(struct roff_node
*n
)
1325 md_pre_Nm(struct roff_node
*n
)
1343 md_post_Nm(struct roff_node
*n
)
1359 md_pre_No(struct roff_node
*n
)
1361 outflags
|= MD_spc_force
;
1366 md_pre_Ns(struct roff_node
*n
)
1368 outflags
&= ~MD_spc
;
1373 md_post_Pf(struct roff_node
*n
)
1375 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1376 outflags
&= ~MD_spc
;
1380 md_pre_Pp(struct roff_node
*n
)
1387 md_pre_Rs(struct roff_node
*n
)
1389 if (n
->sec
== SEC_SEE_ALSO
)
1395 md_pre_Sh(struct roff_node
*n
)
1399 if (n
->sec
== SEC_AUTHORS
)
1400 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1404 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1416 md_pre_Sm(struct roff_node
*n
)
1418 if (n
->child
== NULL
)
1420 else if (strcmp("on", n
->child
->string
) == 0)
1425 if (outflags
& MD_Sm
)
1432 md_pre_Vt(struct roff_node
*n
)
1448 md_post_Vt(struct roff_node
*n
)
1461 md_pre_Xr(struct roff_node
*n
)
1470 outflags
&= ~MD_spc
;
1478 md_pre__T(struct roff_node
*n
)
1480 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1484 outflags
&= ~MD_spc
;
1489 md_post__T(struct roff_node
*n
)
1491 outflags
&= ~MD_spc
;
1492 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1500 md_pre_br(struct roff_node
*n
)