]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
1 /* $Id: mdoc_markdown.c,v 1.35 2020/04/03 11:35:01 schwarze Exp $ */
3 * Copyright (c) 2017, 2018, 2020 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 * Markdown formatter for mdoc(7) used by mandoc(1).
19 #include <sys/types.h>
27 #include "mandoc_aux.h"
34 int (*cond
)(struct roff_node
*);
35 int (*pre
)(struct roff_node
*);
36 void (*post
)(struct roff_node
*);
37 const char *prefix
; /* pre-node string constant */
38 const char *suffix
; /* post-node string constant */
41 static void md_nodelist(struct roff_node
*);
42 static void md_node(struct roff_node
*);
43 static const char *md_stack(char);
44 static void md_preword(void);
45 static void md_rawword(const char *);
46 static void md_word(const char *);
47 static void md_named(const char *);
48 static void md_char(unsigned char);
49 static void md_uri(const char *);
51 static int md_cond_head(struct roff_node
*);
52 static int md_cond_body(struct roff_node
*);
54 static int md_pre_abort(struct roff_node
*);
55 static int md_pre_raw(struct roff_node
*);
56 static int md_pre_word(struct roff_node
*);
57 static int md_pre_skip(struct roff_node
*);
58 static void md_pre_syn(struct roff_node
*);
59 static int md_pre_An(struct roff_node
*);
60 static int md_pre_Ap(struct roff_node
*);
61 static int md_pre_Bd(struct roff_node
*);
62 static int md_pre_Bk(struct roff_node
*);
63 static int md_pre_Bl(struct roff_node
*);
64 static int md_pre_D1(struct roff_node
*);
65 static int md_pre_Dl(struct roff_node
*);
66 static int md_pre_En(struct roff_node
*);
67 static int md_pre_Eo(struct roff_node
*);
68 static int md_pre_Fa(struct roff_node
*);
69 static int md_pre_Fd(struct roff_node
*);
70 static int md_pre_Fn(struct roff_node
*);
71 static int md_pre_Fo(struct roff_node
*);
72 static int md_pre_In(struct roff_node
*);
73 static int md_pre_It(struct roff_node
*);
74 static int md_pre_Lk(struct roff_node
*);
75 static int md_pre_Mt(struct roff_node
*);
76 static int md_pre_Nd(struct roff_node
*);
77 static int md_pre_Nm(struct roff_node
*);
78 static int md_pre_No(struct roff_node
*);
79 static int md_pre_Ns(struct roff_node
*);
80 static int md_pre_Pp(struct roff_node
*);
81 static int md_pre_Rs(struct roff_node
*);
82 static int md_pre_Sh(struct roff_node
*);
83 static int md_pre_Sm(struct roff_node
*);
84 static int md_pre_Vt(struct roff_node
*);
85 static int md_pre_Xr(struct roff_node
*);
86 static int md_pre__T(struct roff_node
*);
87 static int md_pre_br(struct roff_node
*);
89 static void md_post_raw(struct roff_node
*);
90 static void md_post_word(struct roff_node
*);
91 static void md_post_pc(struct roff_node
*);
92 static void md_post_Bk(struct roff_node
*);
93 static void md_post_Bl(struct roff_node
*);
94 static void md_post_D1(struct roff_node
*);
95 static void md_post_En(struct roff_node
*);
96 static void md_post_Eo(struct roff_node
*);
97 static void md_post_Fa(struct roff_node
*);
98 static void md_post_Fd(struct roff_node
*);
99 static void md_post_Fl(struct roff_node
*);
100 static void md_post_Fn(struct roff_node
*);
101 static void md_post_Fo(struct roff_node
*);
102 static void md_post_In(struct roff_node
*);
103 static void md_post_It(struct roff_node
*);
104 static void md_post_Lb(struct roff_node
*);
105 static void md_post_Nm(struct roff_node
*);
106 static void md_post_Pf(struct roff_node
*);
107 static void md_post_Vt(struct roff_node
*);
108 static void md_post__T(struct roff_node
*);
110 static const struct md_act md_acts
[MDOC_MAX
- MDOC_Dd
] = {
111 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
112 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
113 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
114 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Sh */
115 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Ss */
116 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Pp */
117 { md_cond_body
, md_pre_D1
, md_post_D1
, NULL
, NULL
}, /* D1 */
118 { md_cond_body
, md_pre_Dl
, md_post_D1
, NULL
, NULL
}, /* Dl */
119 { md_cond_body
, md_pre_Bd
, md_post_D1
, NULL
, NULL
}, /* Bd */
120 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
121 { md_cond_body
, md_pre_Bl
, md_post_Bl
, NULL
, NULL
}, /* Bl */
122 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
123 { NULL
, md_pre_It
, md_post_It
, NULL
, NULL
}, /* It */
124 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ad */
125 { NULL
, md_pre_An
, NULL
, NULL
, NULL
}, /* An */
126 { NULL
, md_pre_Ap
, NULL
, NULL
, NULL
}, /* Ap */
127 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ar */
128 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cd */
129 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cm */
130 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Dv */
131 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Er */
132 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Ev */
133 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ex */
134 { NULL
, md_pre_Fa
, md_post_Fa
, NULL
, NULL
}, /* Fa */
135 { NULL
, md_pre_Fd
, md_post_Fd
, "**", "**" }, /* Fd */
136 { NULL
, md_pre_raw
, md_post_Fl
, "**-", "**" }, /* Fl */
137 { NULL
, md_pre_Fn
, md_post_Fn
, NULL
, NULL
}, /* Fn */
138 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ft */
139 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ic */
140 { NULL
, md_pre_In
, md_post_In
, NULL
, NULL
}, /* In */
141 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Li */
142 { md_cond_head
, md_pre_Nd
, NULL
, NULL
, NULL
}, /* Nd */
143 { NULL
, md_pre_Nm
, md_post_Nm
, "**", "**" }, /* Nm */
144 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Op */
145 { NULL
, md_pre_abort
, NULL
, NULL
, NULL
}, /* Ot */
146 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Pa */
147 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Rv */
148 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
149 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Va */
150 { NULL
, md_pre_Vt
, md_post_Vt
, "*", "*" }, /* Vt */
151 { NULL
, md_pre_Xr
, NULL
, NULL
, NULL
}, /* Xr */
152 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %A */
153 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %B */
154 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %D */
155 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %I */
156 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %J */
157 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %N */
158 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %O */
159 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %P */
160 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %R */
161 { NULL
, md_pre__T
, md_post__T
, NULL
, NULL
}, /* %T */
162 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %V */
163 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
164 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Ao */
165 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Aq */
166 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
167 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
168 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bf XXX not implemented */
169 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bo */
170 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bq */
171 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
172 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
173 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
174 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
175 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Do */
176 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Dq */
177 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
178 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
179 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Em */
180 { md_cond_body
, md_pre_Eo
, md_post_Eo
, NULL
, NULL
}, /* Eo */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
182 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ms */
183 { NULL
, md_pre_No
, NULL
, NULL
, NULL
}, /* No */
184 { NULL
, md_pre_Ns
, NULL
, NULL
, NULL
}, /* Ns */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
186 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
187 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
188 { NULL
, NULL
, md_post_Pf
, NULL
, NULL
}, /* Pf */
189 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Po */
190 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Pq */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
192 { md_cond_body
, md_pre_raw
, md_post_raw
, "'`", "`'" }, /* Ql */
193 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qo */
194 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qq */
195 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
196 { md_cond_body
, md_pre_Rs
, NULL
, NULL
, NULL
}, /* Rs */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
198 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* So */
199 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* Sq */
200 { NULL
, md_pre_Sm
, NULL
, NULL
, NULL
}, /* Sm */
201 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Sx */
202 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Sy */
203 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Tn */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
207 { NULL
, md_pre_Fo
, md_post_Fo
, "**", "**" }, /* Fo */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
209 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Oo */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
211 { NULL
, md_pre_Bk
, md_post_Bk
, NULL
, NULL
}, /* Bk */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
213 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
214 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
215 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Fr */
216 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
217 { NULL
, NULL
, md_post_Lb
, NULL
, NULL
}, /* Lb */
218 { NULL
, md_pre_abort
, NULL
, NULL
, NULL
}, /* Lp */
219 { NULL
, md_pre_Lk
, NULL
, NULL
, NULL
}, /* Lk */
220 { NULL
, md_pre_Mt
, NULL
, NULL
, NULL
}, /* Mt */
221 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Brq */
222 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Bro */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
224 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %C */
225 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Es */
226 { md_cond_body
, md_pre_En
, md_post_En
, NULL
, NULL
}, /* En */
227 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
228 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %Q */
229 { NULL
, md_pre_Lk
, md_post_pc
, NULL
, NULL
}, /* %U */
230 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
231 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Tg */
233 static const struct md_act
*md_act(enum roff_tok
);
236 #define MD_spc (1 << 0) /* Blank character before next word. */
237 #define MD_spc_force (1 << 1) /* Even before trailing punctuation. */
238 #define MD_nonl (1 << 2) /* Prevent linebreak in markdown code. */
239 #define MD_nl (1 << 3) /* Break markdown code line. */
240 #define MD_br (1 << 4) /* Insert an output line break. */
241 #define MD_sp (1 << 5) /* Insert a paragraph break. */
242 #define MD_Sm (1 << 6) /* Horizontal spacing mode. */
243 #define MD_Bk (1 << 7) /* Word keep mode. */
244 #define MD_An_split (1 << 8) /* Author mode is "split". */
245 #define MD_An_nosplit (1 << 9) /* Author mode is "nosplit". */
247 static int escflags
; /* Escape in generated markdown code: */
248 #define ESC_BOL (1 << 0) /* "#*+-" near the beginning of a line. */
249 #define ESC_NUM (1 << 1) /* "." after a leading number. */
250 #define ESC_HYP (1 << 2) /* "(" immediately after "]". */
251 #define ESC_SQU (1 << 4) /* "]" when "[" is open. */
252 #define ESC_FON (1 << 5) /* "*" immediately after unrelated "*". */
253 #define ESC_EOL (1 << 6) /* " " at the and of a line. */
255 static int code_blocks
, quote_blocks
, list_blocks
;
259 static const struct md_act
*
260 md_act(enum roff_tok tok
)
262 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
263 return md_acts
+ (tok
- MDOC_Dd
);
267 markdown_mdoc(void *arg
, const struct roff_meta
*mdoc
)
270 md_word(mdoc
->title
);
271 if (mdoc
->msec
!= NULL
) {
279 if (mdoc
->arch
!= NULL
) {
286 md_nodelist(mdoc
->first
->child
);
296 md_nodelist(struct roff_node
*n
)
305 md_node(struct roff_node
*n
)
307 const struct md_act
*act
;
308 int cond
, process_children
;
310 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
313 if (outflags
& MD_nonl
)
314 outflags
&= ~(MD_nl
| MD_sp
);
315 else if (outflags
& MD_spc
&&
316 n
->flags
& NODE_LINE
&&
317 !roff_node_transparent(n
))
322 process_children
= 1;
323 n
->flags
&= ~NODE_ENDED
;
325 if (n
->type
== ROFFT_TEXT
) {
326 if (n
->flags
& NODE_DELIMC
)
327 outflags
&= ~(MD_spc
| MD_spc_force
);
328 else if (outflags
& MD_Sm
)
329 outflags
|= MD_spc_force
;
331 if (n
->flags
& NODE_DELIMO
)
332 outflags
&= ~(MD_spc
| MD_spc_force
);
333 else if (outflags
& MD_Sm
)
335 } else if (n
->tok
< ROFF_MAX
) {
338 process_children
= md_pre_br(n
);
341 process_children
= md_pre_Pp(n
);
344 process_children
= 0;
348 act
= md_act(n
->tok
);
349 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
350 if (cond
&& act
->pre
!= NULL
&&
351 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
352 process_children
= (*act
->pre
)(n
);
355 if (process_children
&& n
->child
!= NULL
)
356 md_nodelist(n
->child
);
358 if (n
->flags
& NODE_ENDED
)
361 if (cond
&& act
->post
!= NULL
)
364 if (n
->end
!= ENDBODY_NOT
)
365 n
->body
->flags
|= NODE_ENDED
;
385 stack
= mandoc_realloc(stack
, sz
);
391 return stack
== NULL
? "" : stack
;
395 * Handle vertical and horizontal spacing.
403 * If a list block is nested inside a code block or a blockquote,
404 * blank lines for paragraph breaks no longer work; instead,
405 * they terminate the list. Work around this markdown issue
406 * by using mere line breaks instead.
409 if (list_blocks
&& outflags
& MD_sp
) {
415 * End the old line if requested.
416 * Escape whitespace at the end of the markdown line
417 * such that it won't look like an output line break.
420 if (outflags
& MD_sp
)
422 else if (outflags
& MD_br
) {
425 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
428 /* Start a new line if necessary. */
430 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
432 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
437 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
441 /* Handle horizontal spacing. */
443 } else if (outflags
& MD_spc
) {
444 if (outflags
& MD_Bk
)
445 fputs(" ", stdout
);
448 escflags
&= ~ESC_FON
;
452 outflags
&= ~(MD_spc_force
| MD_nonl
);
453 if (outflags
& MD_Sm
)
460 * Print markdown syntax elements.
461 * Can also be used for constant strings when neither escaping
462 * nor delimiter handling is required.
465 md_rawword(const char *s
)
472 if (escflags
& ESC_FON
) {
473 escflags
&= ~ESC_FON
;
474 if (*s
== '*' && !code_blocks
)
475 fputs("‌", stdout
);
489 escflags
&= ~ESC_SQU
;
499 escflags
&= ~ESC_EOL
;
503 * Print text and mdoc(7) syntax elements.
506 md_word(const char *s
)
508 const char *seq
, *prevfont
, *currfont
, *nextfont
;
510 int bs
, sz
, uc
, breakline
;
512 /* No spacing before closing delimiters. */
513 if (s
[0] != '\0' && s
[1] == '\0' &&
514 strchr("!),.:;?]", s
[0]) != NULL
&&
515 (outflags
& MD_spc_force
) == 0)
523 /* No spacing after opening delimiters. */
524 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
528 prevfont
= currfont
= "";
529 while ((c
= *s
++) != '\0') {
541 bs
= escflags
& ESC_BOL
&& !code_blocks
;
549 bs
= escflags
& ESC_BOL
&& !code_blocks
;
552 bs
= escflags
& ESC_HYP
&& !code_blocks
;
555 bs
= escflags
& ESC_NUM
&& !code_blocks
;
564 bs
= escflags
& ESC_NUM
&& !code_blocks
;
567 if (code_blocks
== 0) {
573 if (escflags
& ESC_BOL
&& !code_blocks
) {
579 if (code_blocks
== 0) {
587 switch (mandoc_escape(&s
, &seq
, &sz
)) {
589 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
591 case ESCAPE_NUMBERED
:
592 uc
= mchars_num2char(seq
, sz
);
595 uc
= mchars_spec2cp(seq
, sz
);
601 md_rawword("markdown");
603 case ESCAPE_FONTBOLD
:
606 case ESCAPE_FONTITALIC
:
614 case ESCAPE_FONTROMAN
:
617 case ESCAPE_FONTPREV
:
624 case ESCAPE_SKIPCHAR
:
625 case ESCAPE_OVERSTRIKE
:
626 /* XXX not implemented */
632 if (nextfont
!= NULL
&& !code_blocks
) {
633 if (*currfont
!= '\0') {
635 md_rawword(currfont
);
639 if (*currfont
!= '\0') {
641 md_rawword(currfont
);
645 if ((uc
< 0x20 && uc
!= 0x09) ||
646 (uc
> 0x7E && uc
< 0xA0))
649 seq
= mchars_uc2str(uc
);
651 outcount
+= strlen(seq
);
656 escflags
&= ~ESC_FON
;
661 bs
= escflags
& ESC_SQU
&& !code_blocks
;
671 (*s
== '\0' || *s
== ' ' || *s
== ASCII_NBRSP
)) {
674 while (*s
== ' ' || *s
== ASCII_NBRSP
)
678 if (*currfont
!= '\0') {
680 md_rawword(currfont
);
681 } else if (s
[-2] == ' ')
684 escflags
&= ~ESC_EOL
;
688 * Print a single HTML named character reference.
691 md_named(const char *s
)
694 escflags
&= ~(ESC_FON
| ESC_EOL
);
699 * Print a single raw character and maintain certain escape flags.
702 md_char(unsigned char c
)
709 escflags
&= ~ESC_FON
;
713 escflags
&= ~ESC_HYP
;
714 if (c
== ' ' || c
== '\t' || c
== '>')
717 escflags
&= ~ESC_NUM
;
718 else if (escflags
& ESC_BOL
)
720 escflags
&= ~ESC_BOL
;
724 md_cond_head(struct roff_node
*n
)
726 return n
->type
== ROFFT_HEAD
;
730 md_cond_body(struct roff_node
*n
)
732 return n
->type
== ROFFT_BODY
;
736 md_pre_abort(struct roff_node
*n
)
742 md_pre_raw(struct roff_node
*n
)
746 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
756 md_post_raw(struct roff_node
*n
)
760 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
761 outflags
&= ~(MD_spc
| MD_nl
);
769 md_pre_word(struct roff_node
*n
)
773 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
781 md_post_word(struct roff_node
*n
)
785 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
786 outflags
&= ~(MD_spc
| MD_nl
);
792 md_post_pc(struct roff_node
*n
)
794 struct roff_node
*nn
;
797 if (n
->parent
->tok
!= MDOC_Rs
)
800 if ((nn
= roff_node_next(n
)) != NULL
) {
802 if (nn
->tok
== n
->tok
&&
803 (nn
= roff_node_prev(n
)) != NULL
&&
813 md_pre_skip(struct roff_node
*n
)
819 md_pre_syn(struct roff_node
*n
)
821 struct roff_node
*np
;
823 if ((n
->flags
& NODE_SYNPRETTY
) == 0 ||
824 (np
= roff_node_prev(n
)) == NULL
)
827 if (np
->tok
== n
->tok
&&
844 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
856 md_pre_An(struct roff_node
*n
)
858 switch (n
->norm
->An
.auth
) {
860 outflags
&= ~MD_An_nosplit
;
861 outflags
|= MD_An_split
;
864 outflags
&= ~MD_An_split
;
865 outflags
|= MD_An_nosplit
;
868 if (outflags
& MD_An_split
)
870 else if (n
->sec
== SEC_AUTHORS
&&
871 ! (outflags
& MD_An_nosplit
))
872 outflags
|= MD_An_split
;
878 md_pre_Ap(struct roff_node
*n
)
887 md_pre_Bd(struct roff_node
*n
)
889 switch (n
->norm
->Bd
.type
) {
899 md_pre_Bk(struct roff_node
*n
)
913 md_post_Bk(struct roff_node
*n
)
915 if (n
->type
== ROFFT_BODY
)
920 md_pre_Bl(struct roff_node
*n
)
922 n
->norm
->Bl
.count
= 0;
923 if (n
->norm
->Bl
.type
== LIST_column
)
930 md_post_Bl(struct roff_node
*n
)
932 n
->norm
->Bl
.count
= 0;
933 if (n
->norm
->Bl
.type
== LIST_column
)
939 md_pre_D1(struct roff_node
*n
)
942 * Markdown blockquote syntax does not work inside code blocks.
943 * The best we can do is fall back to another nested code block.
957 md_post_D1(struct roff_node
*n
)
968 md_pre_Dl(struct roff_node
*n
)
971 * Markdown code block syntax does not work inside blockquotes.
972 * The best we can do is fall back to another nested blockquote.
986 md_pre_En(struct roff_node
*n
)
988 if (n
->norm
->Es
== NULL
||
989 n
->norm
->Es
->child
== NULL
)
992 md_word(n
->norm
->Es
->child
->string
);
998 md_post_En(struct roff_node
*n
)
1000 if (n
->norm
->Es
== NULL
||
1001 n
->norm
->Es
->child
== NULL
||
1002 n
->norm
->Es
->child
->next
== NULL
)
1005 outflags
&= ~MD_spc
;
1006 md_word(n
->norm
->Es
->child
->next
->string
);
1010 md_pre_Eo(struct roff_node
*n
)
1012 if (n
->end
== ENDBODY_NOT
&&
1013 n
->parent
->head
->child
== NULL
&&
1015 n
->child
->end
!= ENDBODY_NOT
)
1017 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1018 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1019 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1020 outflags
&= ~(MD_spc
| MD_nl
);
1025 md_post_Eo(struct roff_node
*n
)
1027 if (n
->end
!= ENDBODY_NOT
) {
1032 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
1035 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
1036 outflags
&= ~MD_spc
;
1042 md_pre_Fa(struct roff_node
*n
)
1046 am_Fa
= n
->tok
== MDOC_Fa
;
1053 outflags
&= ~MD_spc
;
1055 outflags
&= ~MD_spc
;
1057 if ((n
= n
->next
) != NULL
)
1064 md_post_Fa(struct roff_node
*n
)
1066 struct roff_node
*nn
;
1068 if ((nn
= roff_node_next(n
)) != NULL
&& nn
->tok
== MDOC_Fa
)
1073 md_pre_Fd(struct roff_node
*n
)
1081 md_post_Fd(struct roff_node
*n
)
1088 md_post_Fl(struct roff_node
*n
)
1090 struct roff_node
*nn
;
1093 if (n
->child
== NULL
&& (nn
= roff_node_next(n
)) != NULL
&&
1094 nn
->type
!= ROFFT_TEXT
&& (nn
->flags
& NODE_LINE
) == 0)
1095 outflags
&= ~MD_spc
;
1099 md_pre_Fn(struct roff_node
*n
)
1103 if ((n
= n
->child
) == NULL
)
1107 outflags
&= ~MD_spc
;
1109 outflags
&= ~MD_spc
;
1111 outflags
&= ~MD_spc
;
1114 if ((n
= n
->next
) != NULL
)
1120 md_post_Fn(struct roff_node
*n
)
1123 if (n
->flags
& NODE_SYNPRETTY
) {
1130 md_pre_Fo(struct roff_node
*n
)
1137 if (n
->child
== NULL
)
1142 outflags
&= ~(MD_spc
| MD_nl
);
1152 md_post_Fo(struct roff_node
*n
)
1156 if (n
->child
!= NULL
)
1168 md_pre_In(struct roff_node
*n
)
1170 if (n
->flags
& NODE_SYNPRETTY
) {
1173 outflags
&= ~MD_spc
;
1174 md_word("#include <");
1177 outflags
&= ~MD_spc
;
1180 outflags
&= ~MD_spc
;
1185 md_post_In(struct roff_node
*n
)
1187 if (n
->flags
& NODE_SYNPRETTY
) {
1188 outflags
&= ~MD_spc
;
1192 outflags
&= ~MD_spc
;
1198 md_pre_It(struct roff_node
*n
)
1200 struct roff_node
*bln
;
1207 bln
= n
->parent
->parent
;
1208 if (bln
->norm
->Bl
.comp
== 0 &&
1209 bln
->norm
->Bl
.type
!= LIST_column
)
1213 switch (bln
->norm
->Bl
.type
) {
1235 if (bln
->norm
->Bl
.count
< 99)
1236 bln
->norm
->Bl
.count
++;
1237 printf("%d.\t", bln
->norm
->Bl
.count
);
1238 escflags
&= ~ESC_FON
;
1246 outflags
&= ~MD_spc
;
1247 outflags
|= MD_nonl
;
1250 if (code_blocks
|| quote_blocks
)
1255 bln
= n
->parent
->parent
;
1256 switch (bln
->norm
->Bl
.type
) {
1275 md_post_It(struct roff_node
*n
)
1277 struct roff_node
*bln
;
1280 if (n
->type
!= ROFFT_BODY
)
1283 bln
= n
->parent
->parent
;
1284 switch (bln
->norm
->Bl
.type
) {
1290 if (code_blocks
|| quote_blocks
)
1299 if (n
->next
== NULL
)
1302 /* Calculate the array index of the current column. */
1305 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1309 * If a width was specified for this column,
1310 * subtract what printed, and
1311 * add the same spacing as in mdoc_term.c.
1314 nc
= bln
->norm
->Bl
.ncols
;
1315 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1316 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1322 outflags
&= ~MD_spc
;
1323 escflags
&= ~ESC_FON
;
1333 md_post_Lb(struct roff_node
*n
)
1335 if (n
->sec
== SEC_LIBRARY
)
1340 md_uri(const char *s
)
1342 while (*s
!= '\0') {
1343 if (strchr("%()<>", *s
) != NULL
) {
1344 printf("%%%2.2hhX", *s
);
1355 md_pre_Lk(struct roff_node
*n
)
1357 const struct roff_node
*link
, *descr
, *punct
;
1359 if ((link
= n
->child
) == NULL
)
1362 /* Find beginning of trailing punctuation. */
1364 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1365 punct
= punct
->prev
;
1366 punct
= punct
->next
;
1371 descr
= link
; /* no text */
1373 outflags
&= ~MD_spc
;
1375 md_word(descr
->string
);
1376 descr
= descr
->next
;
1377 } while (descr
!= punct
);
1378 outflags
&= ~MD_spc
;
1382 md_uri(link
->string
);
1383 outflags
&= ~MD_spc
;
1386 /* Trailing punctuation. */
1387 while (punct
!= NULL
) {
1388 md_word(punct
->string
);
1389 punct
= punct
->next
;
1395 md_pre_Mt(struct roff_node
*n
)
1397 const struct roff_node
*nch
;
1400 outflags
&= ~MD_spc
;
1401 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
1402 md_word(nch
->string
);
1403 outflags
&= ~MD_spc
;
1404 md_rawword("](mailto:");
1405 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
1406 md_uri(nch
->string
);
1407 if (nch
->next
!= NULL
) {
1412 outflags
&= ~MD_spc
;
1418 md_pre_Nd(struct roff_node
*n
)
1427 md_pre_Nm(struct roff_node
*n
)
1445 md_post_Nm(struct roff_node
*n
)
1461 md_pre_No(struct roff_node
*n
)
1463 outflags
|= MD_spc_force
;
1468 md_pre_Ns(struct roff_node
*n
)
1470 outflags
&= ~MD_spc
;
1475 md_post_Pf(struct roff_node
*n
)
1477 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1478 outflags
&= ~MD_spc
;
1482 md_pre_Pp(struct roff_node
*n
)
1489 md_pre_Rs(struct roff_node
*n
)
1491 if (n
->sec
== SEC_SEE_ALSO
)
1497 md_pre_Sh(struct roff_node
*n
)
1501 if (n
->sec
== SEC_AUTHORS
)
1502 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1506 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1518 md_pre_Sm(struct roff_node
*n
)
1520 if (n
->child
== NULL
)
1522 else if (strcmp("on", n
->child
->string
) == 0)
1527 if (outflags
& MD_Sm
)
1534 md_pre_Vt(struct roff_node
*n
)
1550 md_post_Vt(struct roff_node
*n
)
1563 md_pre_Xr(struct roff_node
*n
)
1572 outflags
&= ~MD_spc
;
1580 md_pre__T(struct roff_node
*n
)
1582 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1586 outflags
&= ~MD_spc
;
1591 md_post__T(struct roff_node
*n
)
1593 outflags
&= ~MD_spc
;
1594 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1602 md_pre_br(struct roff_node
*n
)