]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
1 /* $Id: mdoc_markdown.c,v 1.28 2018/12/03 21:00:10 schwarze Exp $ */
3 * Copyright (c) 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
25 #include "mandoc_aux.h"
32 int (*cond
)(struct roff_node
*n
);
33 int (*pre
)(struct roff_node
*n
);
34 void (*post
)(struct roff_node
*n
);
35 const char *prefix
; /* pre-node string constant */
36 const char *suffix
; /* post-node string constant */
39 static void md_nodelist(struct roff_node
*);
40 static void md_node(struct roff_node
*);
41 static const char *md_stack(char c
);
42 static void md_preword(void);
43 static void md_rawword(const char *);
44 static void md_word(const char *);
45 static void md_named(const char *);
46 static void md_char(unsigned char);
47 static void md_uri(const char *);
49 static int md_cond_head(struct roff_node
*);
50 static int md_cond_body(struct roff_node
*);
52 static int md_pre_abort(struct roff_node
*);
53 static int md_pre_raw(struct roff_node
*);
54 static int md_pre_word(struct roff_node
*);
55 static int md_pre_skip(struct roff_node
*);
56 static void md_pre_syn(struct roff_node
*);
57 static int md_pre_An(struct roff_node
*);
58 static int md_pre_Ap(struct roff_node
*);
59 static int md_pre_Bd(struct roff_node
*);
60 static int md_pre_Bk(struct roff_node
*);
61 static int md_pre_Bl(struct roff_node
*);
62 static int md_pre_D1(struct roff_node
*);
63 static int md_pre_Dl(struct roff_node
*);
64 static int md_pre_En(struct roff_node
*);
65 static int md_pre_Eo(struct roff_node
*);
66 static int md_pre_Fa(struct roff_node
*);
67 static int md_pre_Fd(struct roff_node
*);
68 static int md_pre_Fn(struct roff_node
*);
69 static int md_pre_Fo(struct roff_node
*);
70 static int md_pre_In(struct roff_node
*);
71 static int md_pre_It(struct roff_node
*);
72 static int md_pre_Lk(struct roff_node
*);
73 static int md_pre_Mt(struct roff_node
*);
74 static int md_pre_Nd(struct roff_node
*);
75 static int md_pre_Nm(struct roff_node
*);
76 static int md_pre_No(struct roff_node
*);
77 static int md_pre_Ns(struct roff_node
*);
78 static int md_pre_Pp(struct roff_node
*);
79 static int md_pre_Rs(struct roff_node
*);
80 static int md_pre_Sh(struct roff_node
*);
81 static int md_pre_Sm(struct roff_node
*);
82 static int md_pre_Vt(struct roff_node
*);
83 static int md_pre_Xr(struct roff_node
*);
84 static int md_pre__T(struct roff_node
*);
85 static int md_pre_br(struct roff_node
*);
87 static void md_post_raw(struct roff_node
*);
88 static void md_post_word(struct roff_node
*);
89 static void md_post_pc(struct roff_node
*);
90 static void md_post_Bk(struct roff_node
*);
91 static void md_post_Bl(struct roff_node
*);
92 static void md_post_D1(struct roff_node
*);
93 static void md_post_En(struct roff_node
*);
94 static void md_post_Eo(struct roff_node
*);
95 static void md_post_Fa(struct roff_node
*);
96 static void md_post_Fd(struct roff_node
*);
97 static void md_post_Fl(struct roff_node
*);
98 static void md_post_Fn(struct roff_node
*);
99 static void md_post_Fo(struct roff_node
*);
100 static void md_post_In(struct roff_node
*);
101 static void md_post_It(struct roff_node
*);
102 static void md_post_Lb(struct roff_node
*);
103 static void md_post_Nm(struct roff_node
*);
104 static void md_post_Pf(struct roff_node
*);
105 static void md_post_Vt(struct roff_node
*);
106 static void md_post__T(struct roff_node
*);
108 static const struct md_act md_acts
[MDOC_MAX
- MDOC_Dd
] = {
109 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
110 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
111 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
112 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Sh */
113 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Ss */
114 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Pp */
115 { md_cond_body
, md_pre_D1
, md_post_D1
, NULL
, NULL
}, /* D1 */
116 { md_cond_body
, md_pre_Dl
, md_post_D1
, NULL
, NULL
}, /* Dl */
117 { md_cond_body
, md_pre_Bd
, md_post_D1
, NULL
, NULL
}, /* Bd */
118 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
119 { md_cond_body
, md_pre_Bl
, md_post_Bl
, NULL
, NULL
}, /* Bl */
120 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
121 { NULL
, md_pre_It
, md_post_It
, NULL
, NULL
}, /* It */
122 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ad */
123 { NULL
, md_pre_An
, NULL
, NULL
, NULL
}, /* An */
124 { NULL
, md_pre_Ap
, NULL
, NULL
, NULL
}, /* Ap */
125 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ar */
126 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cd */
127 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cm */
128 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Dv */
129 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Er */
130 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Ev */
131 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ex */
132 { NULL
, md_pre_Fa
, md_post_Fa
, NULL
, NULL
}, /* Fa */
133 { NULL
, md_pre_Fd
, md_post_Fd
, "**", "**" }, /* Fd */
134 { NULL
, md_pre_raw
, md_post_Fl
, "**-", "**" }, /* Fl */
135 { NULL
, md_pre_Fn
, md_post_Fn
, NULL
, NULL
}, /* Fn */
136 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ft */
137 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ic */
138 { NULL
, md_pre_In
, md_post_In
, NULL
, NULL
}, /* In */
139 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Li */
140 { md_cond_head
, md_pre_Nd
, NULL
, NULL
, NULL
}, /* Nd */
141 { NULL
, md_pre_Nm
, md_post_Nm
, "**", "**" }, /* Nm */
142 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Op */
143 { NULL
, md_pre_abort
, NULL
, NULL
, NULL
}, /* Ot */
144 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Pa */
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Rv */
146 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
147 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Va */
148 { NULL
, md_pre_Vt
, md_post_Vt
, "*", "*" }, /* Vt */
149 { NULL
, md_pre_Xr
, NULL
, NULL
, NULL
}, /* Xr */
150 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %A */
151 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %B */
152 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %D */
153 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %I */
154 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %J */
155 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %N */
156 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %O */
157 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %P */
158 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %R */
159 { NULL
, md_pre__T
, md_post__T
, NULL
, NULL
}, /* %T */
160 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %V */
161 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
162 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Ao */
163 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Aq */
164 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
165 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
166 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bf XXX not implemented */
167 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bo */
168 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bq */
169 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
170 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
171 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
172 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
173 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Do */
174 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Dq */
175 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
176 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
177 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Em */
178 { md_cond_body
, md_pre_Eo
, md_post_Eo
, NULL
, NULL
}, /* Eo */
179 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
180 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ms */
181 { NULL
, md_pre_No
, NULL
, NULL
, NULL
}, /* No */
182 { NULL
, md_pre_Ns
, NULL
, NULL
, NULL
}, /* Ns */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
184 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
186 { NULL
, NULL
, md_post_Pf
, NULL
, NULL
}, /* Pf */
187 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Po */
188 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Pq */
189 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
190 { md_cond_body
, md_pre_raw
, md_post_raw
, "'`", "`'" }, /* Ql */
191 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qo */
192 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qq */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
194 { md_cond_body
, md_pre_Rs
, NULL
, NULL
, NULL
}, /* Rs */
195 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
196 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* So */
197 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* Sq */
198 { NULL
, md_pre_Sm
, NULL
, NULL
, NULL
}, /* Sm */
199 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Sx */
200 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Sy */
201 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Tn */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
203 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
205 { NULL
, md_pre_Fo
, md_post_Fo
, "**", "**" }, /* Fo */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
207 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Oo */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
209 { NULL
, md_pre_Bk
, md_post_Bk
, NULL
, NULL
}, /* Bk */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
213 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Fr */
214 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
215 { NULL
, NULL
, md_post_Lb
, NULL
, NULL
}, /* Lb */
216 { NULL
, md_pre_abort
, NULL
, NULL
, NULL
}, /* Lp */
217 { NULL
, md_pre_Lk
, NULL
, NULL
, NULL
}, /* Lk */
218 { NULL
, md_pre_Mt
, NULL
, NULL
, NULL
}, /* Mt */
219 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Brq */
220 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Bro */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
222 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %C */
223 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Es */
224 { md_cond_body
, md_pre_En
, md_post_En
, NULL
, NULL
}, /* En */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
226 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %Q */
227 { NULL
, md_pre_Lk
, md_post_pc
, NULL
, NULL
}, /* %U */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
230 static const struct md_act
*md_act(enum roff_tok
);
233 #define MD_spc (1 << 0) /* Blank character before next word. */
234 #define MD_spc_force (1 << 1) /* Even before trailing punctuation. */
235 #define MD_nonl (1 << 2) /* Prevent linebreak in markdown code. */
236 #define MD_nl (1 << 3) /* Break markdown code line. */
237 #define MD_br (1 << 4) /* Insert an output line break. */
238 #define MD_sp (1 << 5) /* Insert a paragraph break. */
239 #define MD_Sm (1 << 6) /* Horizontal spacing mode. */
240 #define MD_Bk (1 << 7) /* Word keep mode. */
241 #define MD_An_split (1 << 8) /* Author mode is "split". */
242 #define MD_An_nosplit (1 << 9) /* Author mode is "nosplit". */
244 static int escflags
; /* Escape in generated markdown code: */
245 #define ESC_BOL (1 << 0) /* "#*+-" near the beginning of a line. */
246 #define ESC_NUM (1 << 1) /* "." after a leading number. */
247 #define ESC_HYP (1 << 2) /* "(" immediately after "]". */
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 static const struct md_act
*
257 md_act(enum roff_tok tok
)
259 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
260 return md_acts
+ (tok
- MDOC_Dd
);
264 markdown_mdoc(void *arg
, const struct roff_man
*mdoc
)
267 md_word(mdoc
->meta
.title
);
268 if (mdoc
->meta
.msec
!= NULL
) {
271 md_word(mdoc
->meta
.msec
);
275 md_word(mdoc
->meta
.vol
);
276 if (mdoc
->meta
.arch
!= NULL
) {
278 md_word(mdoc
->meta
.arch
);
283 md_nodelist(mdoc
->first
->child
);
286 md_word(mdoc
->meta
.os
);
288 md_word(mdoc
->meta
.date
);
293 md_nodelist(struct roff_node
*n
)
302 md_node(struct roff_node
*n
)
304 const struct md_act
*act
;
305 int cond
, process_children
;
307 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
310 if (outflags
& MD_nonl
)
311 outflags
&= ~(MD_nl
| MD_sp
);
312 else if (outflags
& MD_spc
&& n
->flags
& NODE_LINE
)
317 process_children
= 1;
318 n
->flags
&= ~NODE_ENDED
;
320 if (n
->type
== ROFFT_TEXT
) {
321 if (n
->flags
& NODE_DELIMC
)
322 outflags
&= ~(MD_spc
| MD_spc_force
);
323 else if (outflags
& MD_Sm
)
324 outflags
|= MD_spc_force
;
326 if (n
->flags
& NODE_DELIMO
)
327 outflags
&= ~(MD_spc
| MD_spc_force
);
328 else if (outflags
& MD_Sm
)
330 } else if (n
->tok
< ROFF_MAX
) {
333 process_children
= md_pre_br(n
);
336 process_children
= md_pre_Pp(n
);
339 process_children
= 0;
343 act
= md_act(n
->tok
);
344 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
345 if (cond
&& act
->pre
!= NULL
&&
346 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
347 process_children
= (*act
->pre
)(n
);
350 if (process_children
&& n
->child
!= NULL
)
351 md_nodelist(n
->child
);
353 if (n
->flags
& NODE_ENDED
)
356 if (cond
&& act
->post
!= NULL
)
359 if (n
->end
!= ENDBODY_NOT
)
360 n
->body
->flags
|= NODE_ENDED
;
380 stack
= mandoc_realloc(stack
, sz
);
386 return stack
== NULL
? "" : stack
;
390 * Handle vertical and horizontal spacing.
398 * If a list block is nested inside a code block or a blockquote,
399 * blank lines for paragraph breaks no longer work; instead,
400 * they terminate the list. Work around this markdown issue
401 * by using mere line breaks instead.
404 if (list_blocks
&& outflags
& MD_sp
) {
410 * End the old line if requested.
411 * Escape whitespace at the end of the markdown line
412 * such that it won't look like an output line break.
415 if (outflags
& MD_sp
)
417 else if (outflags
& MD_br
) {
420 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
423 /* Start a new line if necessary. */
425 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
427 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
432 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
436 /* Handle horizontal spacing. */
438 } else if (outflags
& MD_spc
) {
439 if (outflags
& MD_Bk
)
440 fputs(" ", stdout
);
443 escflags
&= ~ESC_FON
;
447 outflags
&= ~(MD_spc_force
| MD_nonl
);
448 if (outflags
& MD_Sm
)
455 * Print markdown syntax elements.
456 * Can also be used for constant strings when neither escaping
457 * nor delimiter handling is required.
460 md_rawword(const char *s
)
467 if (escflags
& ESC_FON
) {
468 escflags
&= ~ESC_FON
;
469 if (*s
== '*' && !code_blocks
)
470 fputs("‌", stdout
);
484 escflags
&= ~ESC_SQU
;
494 escflags
&= ~ESC_EOL
;
498 * Print text and mdoc(7) syntax elements.
501 md_word(const char *s
)
503 const char *seq
, *prevfont
, *currfont
, *nextfont
;
505 int bs
, sz
, uc
, breakline
;
507 /* No spacing before closing delimiters. */
508 if (s
[0] != '\0' && s
[1] == '\0' &&
509 strchr("!),.:;?]", s
[0]) != NULL
&&
510 (outflags
& MD_spc_force
) == 0)
518 /* No spacing after opening delimiters. */
519 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
523 prevfont
= currfont
= "";
524 while ((c
= *s
++) != '\0') {
536 bs
= escflags
& ESC_BOL
&& !code_blocks
;
544 bs
= escflags
& ESC_BOL
&& !code_blocks
;
547 bs
= escflags
& ESC_HYP
&& !code_blocks
;
550 bs
= escflags
& ESC_NUM
&& !code_blocks
;
559 bs
= escflags
& ESC_NUM
&& !code_blocks
;
562 if (code_blocks
== 0) {
568 if (escflags
& ESC_BOL
&& !code_blocks
) {
574 if (code_blocks
== 0) {
582 switch (mandoc_escape(&s
, &seq
, &sz
)) {
584 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
586 case ESCAPE_NUMBERED
:
587 uc
= mchars_num2char(seq
, sz
);
590 uc
= mchars_spec2cp(seq
, sz
);
593 md_rawword("markdown");
595 case ESCAPE_FONTBOLD
:
598 case ESCAPE_FONTITALIC
:
606 case ESCAPE_FONTROMAN
:
609 case ESCAPE_FONTPREV
:
616 case ESCAPE_SKIPCHAR
:
617 case ESCAPE_OVERSTRIKE
:
618 /* XXX not implemented */
624 if (nextfont
!= NULL
&& !code_blocks
) {
625 if (*currfont
!= '\0') {
627 md_rawword(currfont
);
631 if (*currfont
!= '\0') {
633 md_rawword(currfont
);
637 if ((uc
< 0x20 && uc
!= 0x09) ||
638 (uc
> 0x7E && uc
< 0xA0))
641 seq
= mchars_uc2str(uc
);
643 outcount
+= strlen(seq
);
648 escflags
&= ~ESC_FON
;
653 bs
= escflags
& ESC_SQU
&& !code_blocks
;
663 (*s
== '\0' || *s
== ' ' || *s
== ASCII_NBRSP
)) {
666 while (*s
== ' ' || *s
== ASCII_NBRSP
)
670 if (*currfont
!= '\0') {
672 md_rawword(currfont
);
673 } else if (s
[-2] == ' ')
676 escflags
&= ~ESC_EOL
;
680 * Print a single HTML named character reference.
683 md_named(const char *s
)
686 escflags
&= ~(ESC_FON
| ESC_EOL
);
691 * Print a single raw character and maintain certain escape flags.
694 md_char(unsigned char c
)
701 escflags
&= ~ESC_FON
;
705 escflags
&= ~ESC_HYP
;
706 if (c
== ' ' || c
== '\t' || c
== '>')
709 escflags
&= ~ESC_NUM
;
710 else if (escflags
& ESC_BOL
)
712 escflags
&= ~ESC_BOL
;
716 md_cond_head(struct roff_node
*n
)
718 return n
->type
== ROFFT_HEAD
;
722 md_cond_body(struct roff_node
*n
)
724 return n
->type
== ROFFT_BODY
;
728 md_pre_abort(struct roff_node
*n
)
734 md_pre_raw(struct roff_node
*n
)
738 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
748 md_post_raw(struct roff_node
*n
)
752 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
753 outflags
&= ~(MD_spc
| MD_nl
);
761 md_pre_word(struct roff_node
*n
)
765 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
773 md_post_word(struct roff_node
*n
)
777 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
778 outflags
&= ~(MD_spc
| MD_nl
);
784 md_post_pc(struct roff_node
*n
)
787 if (n
->parent
->tok
!= MDOC_Rs
)
789 if (n
->next
!= NULL
) {
791 if (n
->prev
!= NULL
&&
792 n
->prev
->tok
== n
->tok
&&
793 n
->next
->tok
== n
->tok
)
802 md_pre_skip(struct roff_node
*n
)
808 md_pre_syn(struct roff_node
*n
)
810 if (n
->prev
== NULL
|| ! (n
->flags
& NODE_SYNPRETTY
))
813 if (n
->prev
->tok
== n
->tok
&&
821 switch (n
->prev
->tok
) {
830 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
842 md_pre_An(struct roff_node
*n
)
844 switch (n
->norm
->An
.auth
) {
846 outflags
&= ~MD_An_nosplit
;
847 outflags
|= MD_An_split
;
850 outflags
&= ~MD_An_split
;
851 outflags
|= MD_An_nosplit
;
854 if (outflags
& MD_An_split
)
856 else if (n
->sec
== SEC_AUTHORS
&&
857 ! (outflags
& MD_An_nosplit
))
858 outflags
|= MD_An_split
;
864 md_pre_Ap(struct roff_node
*n
)
873 md_pre_Bd(struct roff_node
*n
)
875 switch (n
->norm
->Bd
.type
) {
885 md_pre_Bk(struct roff_node
*n
)
899 md_post_Bk(struct roff_node
*n
)
901 if (n
->type
== ROFFT_BODY
)
906 md_pre_Bl(struct roff_node
*n
)
908 n
->norm
->Bl
.count
= 0;
909 if (n
->norm
->Bl
.type
== LIST_column
)
916 md_post_Bl(struct roff_node
*n
)
918 n
->norm
->Bl
.count
= 0;
919 if (n
->norm
->Bl
.type
== LIST_column
)
925 md_pre_D1(struct roff_node
*n
)
928 * Markdown blockquote syntax does not work inside code blocks.
929 * The best we can do is fall back to another nested code block.
943 md_post_D1(struct roff_node
*n
)
954 md_pre_Dl(struct roff_node
*n
)
957 * Markdown code block syntax does not work inside blockquotes.
958 * The best we can do is fall back to another nested blockquote.
972 md_pre_En(struct roff_node
*n
)
974 if (n
->norm
->Es
== NULL
||
975 n
->norm
->Es
->child
== NULL
)
978 md_word(n
->norm
->Es
->child
->string
);
984 md_post_En(struct roff_node
*n
)
986 if (n
->norm
->Es
== NULL
||
987 n
->norm
->Es
->child
== NULL
||
988 n
->norm
->Es
->child
->next
== NULL
)
992 md_word(n
->norm
->Es
->child
->next
->string
);
996 md_pre_Eo(struct roff_node
*n
)
998 if (n
->end
== ENDBODY_NOT
&&
999 n
->parent
->head
->child
== NULL
&&
1001 n
->child
->end
!= ENDBODY_NOT
)
1003 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1004 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1005 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1006 outflags
&= ~(MD_spc
| MD_nl
);
1011 md_post_Eo(struct roff_node
*n
)
1013 if (n
->end
!= ENDBODY_NOT
) {
1018 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
1021 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
1022 outflags
&= ~MD_spc
;
1028 md_pre_Fa(struct roff_node
*n
)
1032 am_Fa
= n
->tok
== MDOC_Fa
;
1039 outflags
&= ~MD_spc
;
1041 outflags
&= ~MD_spc
;
1043 if ((n
= n
->next
) != NULL
)
1050 md_post_Fa(struct roff_node
*n
)
1052 if (n
->next
!= NULL
&& n
->next
->tok
== MDOC_Fa
)
1057 md_pre_Fd(struct roff_node
*n
)
1065 md_post_Fd(struct roff_node
*n
)
1072 md_post_Fl(struct roff_node
*n
)
1075 if (n
->child
== NULL
&& n
->next
!= NULL
&&
1076 n
->next
->type
!= ROFFT_TEXT
&& !(n
->next
->flags
& NODE_LINE
))
1077 outflags
&= ~MD_spc
;
1081 md_pre_Fn(struct roff_node
*n
)
1085 if ((n
= n
->child
) == NULL
)
1089 outflags
&= ~MD_spc
;
1091 outflags
&= ~MD_spc
;
1093 outflags
&= ~MD_spc
;
1096 if ((n
= n
->next
) != NULL
)
1102 md_post_Fn(struct roff_node
*n
)
1105 if (n
->flags
& NODE_SYNPRETTY
) {
1112 md_pre_Fo(struct roff_node
*n
)
1119 if (n
->child
== NULL
)
1124 outflags
&= ~(MD_spc
| MD_nl
);
1134 md_post_Fo(struct roff_node
*n
)
1138 if (n
->child
!= NULL
)
1150 md_pre_In(struct roff_node
*n
)
1152 if (n
->flags
& NODE_SYNPRETTY
) {
1155 outflags
&= ~MD_spc
;
1156 md_word("#include <");
1159 outflags
&= ~MD_spc
;
1162 outflags
&= ~MD_spc
;
1167 md_post_In(struct roff_node
*n
)
1169 if (n
->flags
& NODE_SYNPRETTY
) {
1170 outflags
&= ~MD_spc
;
1174 outflags
&= ~MD_spc
;
1180 md_pre_It(struct roff_node
*n
)
1182 struct roff_node
*bln
;
1189 bln
= n
->parent
->parent
;
1190 if (bln
->norm
->Bl
.comp
== 0 &&
1191 bln
->norm
->Bl
.type
!= LIST_column
)
1195 switch (bln
->norm
->Bl
.type
) {
1217 if (bln
->norm
->Bl
.count
< 99)
1218 bln
->norm
->Bl
.count
++;
1219 printf("%d.\t", bln
->norm
->Bl
.count
);
1220 escflags
&= ~ESC_FON
;
1228 outflags
&= ~MD_spc
;
1229 outflags
|= MD_nonl
;
1232 if (code_blocks
|| quote_blocks
)
1237 bln
= n
->parent
->parent
;
1238 switch (bln
->norm
->Bl
.type
) {
1257 md_post_It(struct roff_node
*n
)
1259 struct roff_node
*bln
;
1262 if (n
->type
!= ROFFT_BODY
)
1265 bln
= n
->parent
->parent
;
1266 switch (bln
->norm
->Bl
.type
) {
1272 if (code_blocks
|| quote_blocks
)
1281 if (n
->next
== NULL
)
1284 /* Calculate the array index of the current column. */
1287 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1291 * If a width was specified for this column,
1292 * subtract what printed, and
1293 * add the same spacing as in mdoc_term.c.
1296 nc
= bln
->norm
->Bl
.ncols
;
1297 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1298 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1304 outflags
&= ~MD_spc
;
1305 escflags
&= ~ESC_FON
;
1315 md_post_Lb(struct roff_node
*n
)
1317 if (n
->sec
== SEC_LIBRARY
)
1322 md_uri(const char *s
)
1324 while (*s
!= '\0') {
1325 if (strchr("%()<>", *s
) != NULL
) {
1326 printf("%%%2.2hhX", *s
);
1337 md_pre_Lk(struct roff_node
*n
)
1339 const struct roff_node
*link
, *descr
, *punct
;
1341 if ((link
= n
->child
) == NULL
)
1344 /* Find beginning of trailing punctuation. */
1346 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1347 punct
= punct
->prev
;
1348 punct
= punct
->next
;
1353 descr
= link
; /* no text */
1355 outflags
&= ~MD_spc
;
1357 md_word(descr
->string
);
1358 descr
= descr
->next
;
1359 } while (descr
!= punct
);
1360 outflags
&= ~MD_spc
;
1364 md_uri(link
->string
);
1365 outflags
&= ~MD_spc
;
1368 /* Trailing punctuation. */
1369 while (punct
!= NULL
) {
1370 md_word(punct
->string
);
1371 punct
= punct
->next
;
1377 md_pre_Mt(struct roff_node
*n
)
1379 const struct roff_node
*nch
;
1382 outflags
&= ~MD_spc
;
1383 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
1384 md_word(nch
->string
);
1385 outflags
&= ~MD_spc
;
1386 md_rawword("](mailto:");
1387 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
1388 md_uri(nch
->string
);
1389 if (nch
->next
!= NULL
) {
1394 outflags
&= ~MD_spc
;
1400 md_pre_Nd(struct roff_node
*n
)
1409 md_pre_Nm(struct roff_node
*n
)
1427 md_post_Nm(struct roff_node
*n
)
1443 md_pre_No(struct roff_node
*n
)
1445 outflags
|= MD_spc_force
;
1450 md_pre_Ns(struct roff_node
*n
)
1452 outflags
&= ~MD_spc
;
1457 md_post_Pf(struct roff_node
*n
)
1459 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1460 outflags
&= ~MD_spc
;
1464 md_pre_Pp(struct roff_node
*n
)
1471 md_pre_Rs(struct roff_node
*n
)
1473 if (n
->sec
== SEC_SEE_ALSO
)
1479 md_pre_Sh(struct roff_node
*n
)
1483 if (n
->sec
== SEC_AUTHORS
)
1484 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1488 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1500 md_pre_Sm(struct roff_node
*n
)
1502 if (n
->child
== NULL
)
1504 else if (strcmp("on", n
->child
->string
) == 0)
1509 if (outflags
& MD_Sm
)
1516 md_pre_Vt(struct roff_node
*n
)
1532 md_post_Vt(struct roff_node
*n
)
1545 md_pre_Xr(struct roff_node
*n
)
1554 outflags
&= ~MD_spc
;
1562 md_pre__T(struct roff_node
*n
)
1564 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1568 outflags
&= ~MD_spc
;
1573 md_post__T(struct roff_node
*n
)
1575 outflags
&= ~MD_spc
;
1576 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1584 md_pre_br(struct roff_node
*n
)