]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
1 /* $Id: mdoc_markdown.c,v 1.14 2017/03/08 19:23:43 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_SQU (1 << 4) /* "]" when "[" is open. */
248 #define ESC_FON (1 << 5) /* "*" immediately after unrelated "*". */
249 #define ESC_EOL (1 << 6) /* " " at the and of a line. */
251 static int code_blocks
, quote_blocks
, list_blocks
;
255 markdown_mdoc(void *arg
, const struct roff_man
*mdoc
)
258 md_word(mdoc
->meta
.title
);
259 if (mdoc
->meta
.msec
!= NULL
) {
262 md_word(mdoc
->meta
.msec
);
266 md_word(mdoc
->meta
.vol
);
267 if (mdoc
->meta
.arch
!= NULL
) {
269 md_word(mdoc
->meta
.arch
);
274 md_nodelist(mdoc
->first
->child
);
277 md_word(mdoc
->meta
.os
);
279 md_word(mdoc
->meta
.date
);
284 md_nodelist(struct roff_node
*n
)
293 md_node(struct roff_node
*n
)
295 const struct md_act
*act
;
296 int cond
, process_children
;
298 if (n
->flags
& NODE_NOPRT
)
301 if (outflags
& MD_nonl
)
302 outflags
&= ~(MD_nl
| MD_sp
);
303 else if (outflags
& MD_spc
&& n
->flags
& NODE_LINE
)
308 process_children
= 1;
309 n
->flags
&= ~NODE_ENDED
;
313 if (n
->flags
& NODE_DELIMC
)
314 outflags
&= ~(MD_spc
| MD_spc_force
);
315 else if (outflags
& MD_Sm
)
316 outflags
|= MD_spc_force
;
318 if (n
->flags
& NODE_DELIMO
)
319 outflags
&= ~(MD_spc
| MD_spc_force
);
320 else if (outflags
& MD_Sm
)
324 act
= md_acts
+ n
->tok
;
325 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
326 if (cond
&& act
->pre
!= NULL
&&
327 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
328 process_children
= (*act
->pre
)(n
);
332 if (process_children
&& n
->child
!= NULL
)
333 md_nodelist(n
->child
);
335 if (n
->flags
& NODE_ENDED
)
338 if (cond
&& act
->post
!= NULL
)
341 if (n
->end
!= ENDBODY_NOT
)
342 n
->body
->flags
|= NODE_ENDED
;
362 stack
= mandoc_realloc(stack
, sz
);
368 return stack
== NULL
? "" : stack
;
372 * Handle vertical and horizontal spacing.
380 * If a list block is nested inside a code block or a blockquote,
381 * blank lines for paragraph breaks no longer work; instead,
382 * they terminate the list. Work around this markdown issue
383 * by using mere line breaks instead.
386 if (list_blocks
&& outflags
& MD_sp
) {
392 * End the old line if requested.
393 * Escape whitespace at the end of the markdown line
394 * such that it won't look like an output line break.
397 if (outflags
& MD_sp
)
399 else if (outflags
& MD_br
) {
402 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
405 /* Start a new line if necessary. */
407 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
409 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
414 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
418 /* Handle horizontal spacing. */
420 } else if (outflags
& MD_spc
) {
421 if (outflags
& MD_Bk
)
422 fputs(" ", stdout
);
425 escflags
&= ~ESC_FON
;
429 outflags
&= ~(MD_spc_force
| MD_nonl
);
430 if (outflags
& MD_Sm
)
437 * Print markdown syntax elements.
438 * Can also be used for constant strings when neither escaping
439 * nor delimiter handling is required.
442 md_rawword(const char *s
)
449 if (escflags
& ESC_FON
) {
450 escflags
&= ~ESC_FON
;
451 if (*s
== '*' && !code_blocks
)
452 fputs("‌", stdout
);
466 escflags
&= ~ESC_SQU
;
476 escflags
&= ~ESC_EOL
;
480 * Print text and mdoc(7) syntax elements.
483 md_word(const char *s
)
485 const char *seq
, *prevfont
, *currfont
, *nextfont
;
489 /* No spacing before closing delimiters. */
490 if (s
[0] != '\0' && s
[1] == '\0' &&
491 strchr("!),.:;?]", s
[0]) != NULL
&&
492 (outflags
& MD_spc_force
) == 0)
500 /* No spacing after opening delimiters. */
501 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
504 prevfont
= currfont
= "";
505 while ((c
= *s
++) != '\0') {
517 bs
= escflags
& ESC_BOL
&& !code_blocks
;
525 bs
= escflags
& ESC_BOL
&& !code_blocks
;
528 bs
= escflags
& ESC_HYP
&& !code_blocks
;
531 bs
= escflags
& ESC_NUM
&& !code_blocks
;
540 bs
= escflags
& ESC_NUM
&& !code_blocks
;
543 if (code_blocks
== 0) {
549 if (escflags
& ESC_BOL
&& !code_blocks
) {
555 if (code_blocks
== 0) {
563 switch (mandoc_escape(&s
, &seq
, &sz
)) {
565 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
567 case ESCAPE_NUMBERED
:
568 uc
= mchars_num2char(seq
, sz
);
571 uc
= mchars_spec2cp(seq
, sz
);
573 case ESCAPE_FONTBOLD
:
576 case ESCAPE_FONTITALIC
:
583 case ESCAPE_FONTROMAN
:
586 case ESCAPE_FONTPREV
:
590 case ESCAPE_SKIPCHAR
:
591 case ESCAPE_OVERSTRIKE
:
592 /* XXX not implemented */
598 if (nextfont
!= NULL
&& !code_blocks
) {
599 if (*currfont
!= '\0') {
601 md_rawword(currfont
);
605 if (*currfont
!= '\0') {
607 md_rawword(currfont
);
611 if ((uc
< 0x20 && uc
!= 0x09) ||
612 (uc
> 0x7E && uc
< 0xA0))
615 seq
= mchars_uc2str(uc
);
617 outcount
+= strlen(seq
);
622 escflags
&= ~ESC_FON
;
627 bs
= escflags
& ESC_SQU
&& !code_blocks
;
637 if (*currfont
!= '\0') {
639 md_rawword(currfont
);
640 } else if (s
[-2] == ' ')
643 escflags
&= ~ESC_EOL
;
647 * Print a single HTML named character reference.
650 md_named(const char *s
)
653 escflags
&= ~(ESC_FON
| ESC_EOL
);
658 * Print a single raw character and maintain certain escape flags.
661 md_char(unsigned char c
)
668 escflags
&= ~ESC_FON
;
672 escflags
&= ~ESC_HYP
;
673 if (c
== ' ' || c
== '\t' || c
== '>')
676 escflags
&= ~ESC_NUM
;
677 else if (escflags
& ESC_BOL
)
679 escflags
&= ~ESC_BOL
;
683 md_cond_head(struct roff_node
*n
)
685 return n
->type
== ROFFT_HEAD
;
689 md_cond_body(struct roff_node
*n
)
691 return n
->type
== ROFFT_BODY
;
695 md_pre_raw(struct roff_node
*n
)
699 if ((prefix
= md_acts
[n
->tok
].prefix
) != NULL
) {
709 md_post_raw(struct roff_node
*n
)
713 if ((suffix
= md_acts
[n
->tok
].suffix
) != NULL
) {
714 outflags
&= ~(MD_spc
| MD_nl
);
722 md_pre_word(struct roff_node
*n
)
726 if ((prefix
= md_acts
[n
->tok
].prefix
) != NULL
) {
734 md_post_word(struct roff_node
*n
)
738 if ((suffix
= md_acts
[n
->tok
].suffix
) != NULL
) {
739 outflags
&= ~(MD_spc
| MD_nl
);
745 md_post_pc(struct roff_node
*n
)
748 if (n
->parent
->tok
!= MDOC_Rs
)
750 if (n
->next
!= NULL
) {
752 if (n
->prev
!= NULL
&&
753 n
->prev
->tok
== n
->tok
&&
754 n
->next
->tok
== n
->tok
)
763 md_pre_skip(struct roff_node
*n
)
769 md_pre_syn(struct roff_node
*n
)
771 if (n
->prev
== NULL
|| ! (n
->flags
& NODE_SYNPRETTY
))
774 if (n
->prev
->tok
== n
->tok
&&
782 switch (n
->prev
->tok
) {
791 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
803 md_pre_An(struct roff_node
*n
)
805 switch (n
->norm
->An
.auth
) {
807 outflags
&= ~MD_An_nosplit
;
808 outflags
|= MD_An_split
;
811 outflags
&= ~MD_An_split
;
812 outflags
|= MD_An_nosplit
;
815 if (outflags
& MD_An_split
)
817 else if (n
->sec
== SEC_AUTHORS
&&
818 ! (outflags
& MD_An_nosplit
))
819 outflags
|= MD_An_split
;
825 md_pre_Ap(struct roff_node
*n
)
834 md_pre_Bd(struct roff_node
*n
)
836 switch (n
->norm
->Bd
.type
) {
846 md_pre_Bk(struct roff_node
*n
)
860 md_post_Bk(struct roff_node
*n
)
862 if (n
->type
== ROFFT_BODY
)
867 md_pre_Bl(struct roff_node
*n
)
869 n
->norm
->Bl
.count
= 0;
870 if (n
->norm
->Bl
.type
== LIST_column
)
877 md_post_Bl(struct roff_node
*n
)
879 n
->norm
->Bl
.count
= 0;
880 if (n
->norm
->Bl
.type
== LIST_column
)
886 md_pre_D1(struct roff_node
*n
)
889 * Markdown blockquote syntax does not work inside code blocks.
890 * The best we can do is fall back to another nested code block.
904 md_post_D1(struct roff_node
*n
)
915 md_pre_Dl(struct roff_node
*n
)
918 * Markdown code block syntax does not work inside blockquotes.
919 * The best we can do is fall back to another nested blockquote.
933 md_pre_En(struct roff_node
*n
)
935 if (n
->norm
->Es
== NULL
||
936 n
->norm
->Es
->child
== NULL
)
939 md_word(n
->norm
->Es
->child
->string
);
945 md_post_En(struct roff_node
*n
)
947 if (n
->norm
->Es
== NULL
||
948 n
->norm
->Es
->child
== NULL
||
949 n
->norm
->Es
->child
->next
== NULL
)
953 md_word(n
->norm
->Es
->child
->next
->string
);
957 md_pre_Eo(struct roff_node
*n
)
959 if (n
->end
== ENDBODY_NOT
&&
960 n
->parent
->head
->child
== NULL
&&
962 n
->child
->end
!= ENDBODY_NOT
)
964 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
965 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
966 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
967 outflags
&= ~(MD_spc
| MD_nl
);
972 md_post_Eo(struct roff_node
*n
)
974 if (n
->end
!= ENDBODY_NOT
) {
979 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
982 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
989 md_pre_Fa(struct roff_node
*n
)
993 am_Fa
= n
->tok
== MDOC_Fa
;
1000 outflags
&= ~MD_spc
;
1002 outflags
&= ~MD_spc
;
1004 if ((n
= n
->next
) != NULL
)
1011 md_post_Fa(struct roff_node
*n
)
1013 if (n
->next
!= NULL
&& n
->next
->tok
== MDOC_Fa
)
1018 md_pre_Fd(struct roff_node
*n
)
1026 md_post_Fd(struct roff_node
*n
)
1033 md_post_Fl(struct roff_node
*n
)
1036 if (n
->child
== NULL
&& n
->next
!= NULL
&&
1037 n
->next
->type
!= ROFFT_TEXT
&& !(n
->next
->flags
& NODE_LINE
))
1038 outflags
&= ~MD_spc
;
1042 md_pre_Fn(struct roff_node
*n
)
1046 if ((n
= n
->child
) == NULL
)
1050 outflags
&= ~MD_spc
;
1052 outflags
&= ~MD_spc
;
1054 outflags
&= ~MD_spc
;
1057 if ((n
= n
->next
) != NULL
)
1063 md_post_Fn(struct roff_node
*n
)
1066 if (n
->flags
& NODE_SYNPRETTY
) {
1073 md_pre_Fo(struct roff_node
*n
)
1080 if (n
->child
== NULL
)
1085 outflags
&= ~(MD_spc
| MD_nl
);
1095 md_post_Fo(struct roff_node
*n
)
1099 if (n
->child
!= NULL
)
1111 md_pre_In(struct roff_node
*n
)
1113 if (n
->flags
& NODE_SYNPRETTY
) {
1116 outflags
&= ~MD_spc
;
1117 md_word("#include <");
1120 outflags
&= ~MD_spc
;
1123 outflags
&= ~MD_spc
;
1128 md_post_In(struct roff_node
*n
)
1130 if (n
->flags
& NODE_SYNPRETTY
) {
1131 outflags
&= ~MD_spc
;
1135 outflags
&= ~MD_spc
;
1141 md_pre_It(struct roff_node
*n
)
1143 struct roff_node
*bln
;
1150 bln
= n
->parent
->parent
;
1151 if (bln
->norm
->Bl
.comp
== 0 &&
1152 bln
->norm
->Bl
.type
!= LIST_column
)
1156 switch (bln
->norm
->Bl
.type
) {
1178 if (bln
->norm
->Bl
.count
< 99)
1179 bln
->norm
->Bl
.count
++;
1180 printf("%d.\t", bln
->norm
->Bl
.count
);
1181 escflags
&= ~ESC_FON
;
1189 outflags
&= ~MD_spc
;
1190 outflags
|= MD_nonl
;
1193 if (code_blocks
|| quote_blocks
)
1198 bln
= n
->parent
->parent
;
1199 switch (bln
->norm
->Bl
.type
) {
1218 md_post_It(struct roff_node
*n
)
1220 struct roff_node
*bln
;
1223 if (n
->type
!= ROFFT_BODY
)
1226 bln
= n
->parent
->parent
;
1227 switch (bln
->norm
->Bl
.type
) {
1233 if (code_blocks
|| quote_blocks
)
1242 if (n
->next
== NULL
)
1245 /* Calculate the array index of the current column. */
1248 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1252 * If a width was specified for this column,
1253 * subtract what printed, and
1254 * add the same spacing as in mdoc_term.c.
1257 nc
= bln
->norm
->Bl
.ncols
;
1258 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1259 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1265 outflags
&= ~MD_spc
;
1266 escflags
&= ~ESC_FON
;
1276 md_post_Lb(struct roff_node
*n
)
1278 if (n
->sec
== SEC_LIBRARY
)
1283 md_pre_Lk(struct roff_node
*n
)
1285 const struct roff_node
*link
, *descr
;
1286 const unsigned char *s
;
1288 if ((link
= n
->child
) == NULL
)
1291 if ((descr
= link
->next
) != NULL
) {
1293 outflags
&= ~MD_spc
;
1294 while (descr
!= NULL
) {
1295 md_word(descr
->string
);
1296 descr
= descr
->next
;
1298 outflags
&= ~MD_spc
;
1303 for (s
= link
->string
; *s
!= '\0'; s
++) {
1304 if (strchr("%()<>", *s
) != NULL
) {
1305 printf("%%%2.2hhX", *s
);
1313 outflags
&= ~MD_spc
;
1314 md_rawword(link
->next
== NULL
? ">" : ")");
1319 md_pre_Nd(struct roff_node
*n
)
1328 md_pre_Nm(struct roff_node
*n
)
1346 md_post_Nm(struct roff_node
*n
)
1362 md_pre_No(struct roff_node
*n
)
1364 outflags
|= MD_spc_force
;
1369 md_pre_Ns(struct roff_node
*n
)
1371 outflags
&= ~MD_spc
;
1376 md_post_Pf(struct roff_node
*n
)
1378 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1379 outflags
&= ~MD_spc
;
1383 md_pre_Pp(struct roff_node
*n
)
1390 md_pre_Rs(struct roff_node
*n
)
1392 if (n
->sec
== SEC_SEE_ALSO
)
1398 md_pre_Sh(struct roff_node
*n
)
1402 if (n
->sec
== SEC_AUTHORS
)
1403 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1407 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1419 md_pre_Sm(struct roff_node
*n
)
1421 if (n
->child
== NULL
)
1423 else if (strcmp("on", n
->child
->string
) == 0)
1428 if (outflags
& MD_Sm
)
1435 md_pre_Vt(struct roff_node
*n
)
1451 md_post_Vt(struct roff_node
*n
)
1464 md_pre_Xr(struct roff_node
*n
)
1473 outflags
&= ~MD_spc
;
1481 md_pre__T(struct roff_node
*n
)
1483 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1487 outflags
&= ~MD_spc
;
1492 md_post__T(struct roff_node
*n
)
1494 outflags
&= ~MD_spc
;
1495 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1503 md_pre_br(struct roff_node
*n
)