]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_markdown.c
1 /* $Id: mdoc_markdown.c,v 1.26 2018/08/17 20:33:38 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>
24 #include "mandoc_aux.h"
31 int (*cond
)(struct roff_node
*n
);
32 int (*pre
)(struct roff_node
*n
);
33 void (*post
)(struct roff_node
*n
);
34 const char *prefix
; /* pre-node string constant */
35 const char *suffix
; /* post-node string constant */
38 static void md_nodelist(struct roff_node
*);
39 static void md_node(struct roff_node
*);
40 static const char *md_stack(char c
);
41 static void md_preword(void);
42 static void md_rawword(const char *);
43 static void md_word(const char *);
44 static void md_named(const char *);
45 static void md_char(unsigned char);
46 static void md_uri(const char *);
48 static int md_cond_head(struct roff_node
*);
49 static int md_cond_body(struct roff_node
*);
51 static int md_pre_raw(struct roff_node
*);
52 static int md_pre_word(struct roff_node
*);
53 static int md_pre_skip(struct roff_node
*);
54 static void md_pre_syn(struct roff_node
*);
55 static int md_pre_An(struct roff_node
*);
56 static int md_pre_Ap(struct roff_node
*);
57 static int md_pre_Bd(struct roff_node
*);
58 static int md_pre_Bk(struct roff_node
*);
59 static int md_pre_Bl(struct roff_node
*);
60 static int md_pre_D1(struct roff_node
*);
61 static int md_pre_Dl(struct roff_node
*);
62 static int md_pre_En(struct roff_node
*);
63 static int md_pre_Eo(struct roff_node
*);
64 static int md_pre_Fa(struct roff_node
*);
65 static int md_pre_Fd(struct roff_node
*);
66 static int md_pre_Fn(struct roff_node
*);
67 static int md_pre_Fo(struct roff_node
*);
68 static int md_pre_In(struct roff_node
*);
69 static int md_pre_It(struct roff_node
*);
70 static int md_pre_Lk(struct roff_node
*);
71 static int md_pre_Mt(struct roff_node
*);
72 static int md_pre_Nd(struct roff_node
*);
73 static int md_pre_Nm(struct roff_node
*);
74 static int md_pre_No(struct roff_node
*);
75 static int md_pre_Ns(struct roff_node
*);
76 static int md_pre_Pp(struct roff_node
*);
77 static int md_pre_Rs(struct roff_node
*);
78 static int md_pre_Sh(struct roff_node
*);
79 static int md_pre_Sm(struct roff_node
*);
80 static int md_pre_Vt(struct roff_node
*);
81 static int md_pre_Xr(struct roff_node
*);
82 static int md_pre__T(struct roff_node
*);
83 static int md_pre_br(struct roff_node
*);
85 static void md_post_raw(struct roff_node
*);
86 static void md_post_word(struct roff_node
*);
87 static void md_post_pc(struct roff_node
*);
88 static void md_post_Bk(struct roff_node
*);
89 static void md_post_Bl(struct roff_node
*);
90 static void md_post_D1(struct roff_node
*);
91 static void md_post_En(struct roff_node
*);
92 static void md_post_Eo(struct roff_node
*);
93 static void md_post_Fa(struct roff_node
*);
94 static void md_post_Fd(struct roff_node
*);
95 static void md_post_Fl(struct roff_node
*);
96 static void md_post_Fn(struct roff_node
*);
97 static void md_post_Fo(struct roff_node
*);
98 static void md_post_In(struct roff_node
*);
99 static void md_post_It(struct roff_node
*);
100 static void md_post_Lb(struct roff_node
*);
101 static void md_post_Nm(struct roff_node
*);
102 static void md_post_Pf(struct roff_node
*);
103 static void md_post_Vt(struct roff_node
*);
104 static void md_post__T(struct roff_node
*);
106 static const struct md_act md_acts
[MDOC_MAX
- MDOC_Dd
] = {
107 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
108 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
109 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
110 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Sh */
111 { NULL
, md_pre_Sh
, NULL
, NULL
, NULL
}, /* Ss */
112 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Pp */
113 { md_cond_body
, md_pre_D1
, md_post_D1
, NULL
, NULL
}, /* D1 */
114 { md_cond_body
, md_pre_Dl
, md_post_D1
, NULL
, NULL
}, /* Dl */
115 { md_cond_body
, md_pre_Bd
, md_post_D1
, NULL
, NULL
}, /* Bd */
116 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
117 { md_cond_body
, md_pre_Bl
, md_post_Bl
, NULL
, NULL
}, /* Bl */
118 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
119 { NULL
, md_pre_It
, md_post_It
, NULL
, NULL
}, /* It */
120 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ad */
121 { NULL
, md_pre_An
, NULL
, NULL
, NULL
}, /* An */
122 { NULL
, md_pre_Ap
, NULL
, NULL
, NULL
}, /* Ap */
123 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Ar */
124 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cd */
125 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Cm */
126 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Dv */
127 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Er */
128 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Ev */
129 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ex */
130 { NULL
, md_pre_Fa
, md_post_Fa
, NULL
, NULL
}, /* Fa */
131 { NULL
, md_pre_Fd
, md_post_Fd
, "**", "**" }, /* Fd */
132 { NULL
, md_pre_raw
, md_post_Fl
, "**-", "**" }, /* Fl */
133 { NULL
, md_pre_Fn
, md_post_Fn
, NULL
, NULL
}, /* Fn */
134 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ft */
135 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ic */
136 { NULL
, md_pre_In
, md_post_In
, NULL
, NULL
}, /* In */
137 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Li */
138 { md_cond_head
, md_pre_Nd
, NULL
, NULL
, NULL
}, /* Nd */
139 { NULL
, md_pre_Nm
, md_post_Nm
, "**", "**" }, /* Nm */
140 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Op */
141 { NULL
, md_pre_Fd
, md_post_raw
, "*", "*" }, /* Ot */
142 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Pa */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Rv */
144 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
145 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Va */
146 { NULL
, md_pre_Vt
, md_post_Vt
, "*", "*" }, /* Vt */
147 { NULL
, md_pre_Xr
, NULL
, NULL
, NULL
}, /* Xr */
148 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %A */
149 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %B */
150 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %D */
151 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %I */
152 { NULL
, md_pre_raw
, md_post_pc
, "*", "*" }, /* %J */
153 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %N */
154 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %O */
155 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %P */
156 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %R */
157 { NULL
, md_pre__T
, md_post__T
, NULL
, NULL
}, /* %T */
158 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %V */
159 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
160 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Ao */
161 { md_cond_body
, md_pre_word
, md_post_word
, "<", ">" }, /* Aq */
162 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
163 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
164 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bf XXX not implemented */
165 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bo */
166 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Bq */
167 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
168 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
169 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
170 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
171 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Do */
172 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Dq */
173 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
174 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
175 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Em */
176 { md_cond_body
, md_pre_Eo
, md_post_Eo
, NULL
, NULL
}, /* Eo */
177 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
178 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Ms */
179 { NULL
, md_pre_No
, NULL
, NULL
, NULL
}, /* No */
180 { NULL
, md_pre_Ns
, NULL
, NULL
, NULL
}, /* Ns */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
182 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
183 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
184 { NULL
, NULL
, md_post_Pf
, NULL
, NULL
}, /* Pf */
185 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Po */
186 { md_cond_body
, md_pre_word
, md_post_word
, "(", ")" }, /* Pq */
187 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
188 { md_cond_body
, md_pre_raw
, md_post_raw
, "'`", "`'" }, /* Ql */
189 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qo */
190 { md_cond_body
, md_pre_word
, md_post_word
, "\"", "\"" }, /* Qq */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
192 { md_cond_body
, md_pre_Rs
, NULL
, NULL
, NULL
}, /* Rs */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
194 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* So */
195 { md_cond_body
, md_pre_word
, md_post_word
, "'", "'" }, /* Sq */
196 { NULL
, md_pre_Sm
, NULL
, NULL
, NULL
}, /* Sm */
197 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Sx */
198 { NULL
, md_pre_raw
, md_post_raw
, "**", "**" }, /* Sy */
199 { NULL
, md_pre_raw
, md_post_raw
, "`", "`" }, /* Tn */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
203 { NULL
, md_pre_Fo
, md_post_Fo
, "**", "**" }, /* Fo */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
205 { md_cond_body
, md_pre_word
, md_post_word
, "[", "]" }, /* Oo */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
207 { NULL
, md_pre_Bk
, md_post_Bk
, NULL
, NULL
}, /* Bk */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
211 { NULL
, md_pre_raw
, md_post_raw
, "*", "*" }, /* Fr */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
213 { NULL
, NULL
, md_post_Lb
, NULL
, NULL
}, /* Lb */
214 { NULL
, md_pre_Pp
, NULL
, NULL
, NULL
}, /* Lp */
215 { NULL
, md_pre_Lk
, NULL
, NULL
, NULL
}, /* Lk */
216 { NULL
, md_pre_Mt
, NULL
, NULL
, NULL
}, /* Mt */
217 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Brq */
218 { md_cond_body
, md_pre_word
, md_post_word
, "{", "}" }, /* Bro */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
220 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %C */
221 { NULL
, md_pre_skip
, NULL
, NULL
, NULL
}, /* Es */
222 { md_cond_body
, md_pre_En
, md_post_En
, NULL
, NULL
}, /* En */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
224 { NULL
, NULL
, md_post_pc
, NULL
, NULL
}, /* %Q */
225 { NULL
, md_pre_Lk
, md_post_pc
, NULL
, NULL
}, /* %U */
226 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
228 static const struct md_act
*md_act(enum roff_tok
);
231 #define MD_spc (1 << 0) /* Blank character before next word. */
232 #define MD_spc_force (1 << 1) /* Even before trailing punctuation. */
233 #define MD_nonl (1 << 2) /* Prevent linebreak in markdown code. */
234 #define MD_nl (1 << 3) /* Break markdown code line. */
235 #define MD_br (1 << 4) /* Insert an output line break. */
236 #define MD_sp (1 << 5) /* Insert a paragraph break. */
237 #define MD_Sm (1 << 6) /* Horizontal spacing mode. */
238 #define MD_Bk (1 << 7) /* Word keep mode. */
239 #define MD_An_split (1 << 8) /* Author mode is "split". */
240 #define MD_An_nosplit (1 << 9) /* Author mode is "nosplit". */
242 static int escflags
; /* Escape in generated markdown code: */
243 #define ESC_BOL (1 << 0) /* "#*+-" near the beginning of a line. */
244 #define ESC_NUM (1 << 1) /* "." after a leading number. */
245 #define ESC_HYP (1 << 2) /* "(" immediately after "]". */
246 #define ESC_SQU (1 << 4) /* "]" when "[" is open. */
247 #define ESC_FON (1 << 5) /* "*" immediately after unrelated "*". */
248 #define ESC_EOL (1 << 6) /* " " at the and of a line. */
250 static int code_blocks
, quote_blocks
, list_blocks
;
254 static const struct md_act
*
255 md_act(enum roff_tok tok
)
257 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
258 return md_acts
+ (tok
- MDOC_Dd
);
262 markdown_mdoc(void *arg
, const struct roff_man
*mdoc
)
265 md_word(mdoc
->meta
.title
);
266 if (mdoc
->meta
.msec
!= NULL
) {
269 md_word(mdoc
->meta
.msec
);
273 md_word(mdoc
->meta
.vol
);
274 if (mdoc
->meta
.arch
!= NULL
) {
276 md_word(mdoc
->meta
.arch
);
281 md_nodelist(mdoc
->first
->child
);
284 md_word(mdoc
->meta
.os
);
286 md_word(mdoc
->meta
.date
);
291 md_nodelist(struct roff_node
*n
)
300 md_node(struct roff_node
*n
)
302 const struct md_act
*act
;
303 int cond
, process_children
;
305 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
308 if (outflags
& MD_nonl
)
309 outflags
&= ~(MD_nl
| MD_sp
);
310 else if (outflags
& MD_spc
&& n
->flags
& NODE_LINE
)
315 process_children
= 1;
316 n
->flags
&= ~NODE_ENDED
;
318 if (n
->type
== ROFFT_TEXT
) {
319 if (n
->flags
& NODE_DELIMC
)
320 outflags
&= ~(MD_spc
| MD_spc_force
);
321 else if (outflags
& MD_Sm
)
322 outflags
|= MD_spc_force
;
324 if (n
->flags
& NODE_DELIMO
)
325 outflags
&= ~(MD_spc
| MD_spc_force
);
326 else if (outflags
& MD_Sm
)
328 } else if (n
->tok
< ROFF_MAX
) {
331 process_children
= md_pre_br(n
);
334 process_children
= md_pre_Pp(n
);
337 process_children
= 0;
341 act
= md_act(n
->tok
);
342 cond
= act
->cond
== NULL
|| (*act
->cond
)(n
);
343 if (cond
&& act
->pre
!= NULL
&&
344 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
345 process_children
= (*act
->pre
)(n
);
348 if (process_children
&& n
->child
!= NULL
)
349 md_nodelist(n
->child
);
351 if (n
->flags
& NODE_ENDED
)
354 if (cond
&& act
->post
!= NULL
)
357 if (n
->end
!= ENDBODY_NOT
)
358 n
->body
->flags
|= NODE_ENDED
;
378 stack
= mandoc_realloc(stack
, sz
);
384 return stack
== NULL
? "" : stack
;
388 * Handle vertical and horizontal spacing.
396 * If a list block is nested inside a code block or a blockquote,
397 * blank lines for paragraph breaks no longer work; instead,
398 * they terminate the list. Work around this markdown issue
399 * by using mere line breaks instead.
402 if (list_blocks
&& outflags
& MD_sp
) {
408 * End the old line if requested.
409 * Escape whitespace at the end of the markdown line
410 * such that it won't look like an output line break.
413 if (outflags
& MD_sp
)
415 else if (outflags
& MD_br
) {
418 } else if (outflags
& MD_nl
&& escflags
& ESC_EOL
)
421 /* Start a new line if necessary. */
423 if (outflags
& (MD_nl
| MD_br
| MD_sp
)) {
425 for (cp
= md_stack('\0'); *cp
!= '\0'; cp
++) {
430 outflags
&= ~(MD_nl
| MD_br
| MD_sp
);
434 /* Handle horizontal spacing. */
436 } else if (outflags
& MD_spc
) {
437 if (outflags
& MD_Bk
)
438 fputs(" ", stdout
);
441 escflags
&= ~ESC_FON
;
445 outflags
&= ~(MD_spc_force
| MD_nonl
);
446 if (outflags
& MD_Sm
)
453 * Print markdown syntax elements.
454 * Can also be used for constant strings when neither escaping
455 * nor delimiter handling is required.
458 md_rawword(const char *s
)
465 if (escflags
& ESC_FON
) {
466 escflags
&= ~ESC_FON
;
467 if (*s
== '*' && !code_blocks
)
468 fputs("‌", stdout
);
482 escflags
&= ~ESC_SQU
;
492 escflags
&= ~ESC_EOL
;
496 * Print text and mdoc(7) syntax elements.
499 md_word(const char *s
)
501 const char *seq
, *prevfont
, *currfont
, *nextfont
;
503 int bs
, sz
, uc
, breakline
;
505 /* No spacing before closing delimiters. */
506 if (s
[0] != '\0' && s
[1] == '\0' &&
507 strchr("!),.:;?]", s
[0]) != NULL
&&
508 (outflags
& MD_spc_force
) == 0)
516 /* No spacing after opening delimiters. */
517 if ((s
[0] == '(' || s
[0] == '[') && s
[1] == '\0')
521 prevfont
= currfont
= "";
522 while ((c
= *s
++) != '\0') {
534 bs
= escflags
& ESC_BOL
&& !code_blocks
;
542 bs
= escflags
& ESC_BOL
&& !code_blocks
;
545 bs
= escflags
& ESC_HYP
&& !code_blocks
;
548 bs
= escflags
& ESC_NUM
&& !code_blocks
;
557 bs
= escflags
& ESC_NUM
&& !code_blocks
;
560 if (code_blocks
== 0) {
566 if (escflags
& ESC_BOL
&& !code_blocks
) {
572 if (code_blocks
== 0) {
580 switch (mandoc_escape(&s
, &seq
, &sz
)) {
582 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
584 case ESCAPE_NUMBERED
:
585 uc
= mchars_num2char(seq
, sz
);
588 uc
= mchars_spec2cp(seq
, sz
);
591 md_rawword("markdown");
593 case ESCAPE_FONTBOLD
:
596 case ESCAPE_FONTITALIC
:
603 case ESCAPE_FONTROMAN
:
606 case ESCAPE_FONTPREV
:
613 case ESCAPE_SKIPCHAR
:
614 case ESCAPE_OVERSTRIKE
:
615 /* XXX not implemented */
621 if (nextfont
!= NULL
&& !code_blocks
) {
622 if (*currfont
!= '\0') {
624 md_rawword(currfont
);
628 if (*currfont
!= '\0') {
630 md_rawword(currfont
);
634 if ((uc
< 0x20 && uc
!= 0x09) ||
635 (uc
> 0x7E && uc
< 0xA0))
638 seq
= mchars_uc2str(uc
);
640 outcount
+= strlen(seq
);
645 escflags
&= ~ESC_FON
;
650 bs
= escflags
& ESC_SQU
&& !code_blocks
;
660 (*s
== '\0' || *s
== ' ' || *s
== ASCII_NBRSP
)) {
663 while (*s
== ' ' || *s
== ASCII_NBRSP
)
667 if (*currfont
!= '\0') {
669 md_rawword(currfont
);
670 } else if (s
[-2] == ' ')
673 escflags
&= ~ESC_EOL
;
677 * Print a single HTML named character reference.
680 md_named(const char *s
)
683 escflags
&= ~(ESC_FON
| ESC_EOL
);
688 * Print a single raw character and maintain certain escape flags.
691 md_char(unsigned char c
)
698 escflags
&= ~ESC_FON
;
702 escflags
&= ~ESC_HYP
;
703 if (c
== ' ' || c
== '\t' || c
== '>')
706 escflags
&= ~ESC_NUM
;
707 else if (escflags
& ESC_BOL
)
709 escflags
&= ~ESC_BOL
;
713 md_cond_head(struct roff_node
*n
)
715 return n
->type
== ROFFT_HEAD
;
719 md_cond_body(struct roff_node
*n
)
721 return n
->type
== ROFFT_BODY
;
725 md_pre_raw(struct roff_node
*n
)
729 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
739 md_post_raw(struct roff_node
*n
)
743 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
744 outflags
&= ~(MD_spc
| MD_nl
);
752 md_pre_word(struct roff_node
*n
)
756 if ((prefix
= md_act(n
->tok
)->prefix
) != NULL
) {
764 md_post_word(struct roff_node
*n
)
768 if ((suffix
= md_act(n
->tok
)->suffix
) != NULL
) {
769 outflags
&= ~(MD_spc
| MD_nl
);
775 md_post_pc(struct roff_node
*n
)
778 if (n
->parent
->tok
!= MDOC_Rs
)
780 if (n
->next
!= NULL
) {
782 if (n
->prev
!= NULL
&&
783 n
->prev
->tok
== n
->tok
&&
784 n
->next
->tok
== n
->tok
)
793 md_pre_skip(struct roff_node
*n
)
799 md_pre_syn(struct roff_node
*n
)
801 if (n
->prev
== NULL
|| ! (n
->flags
& NODE_SYNPRETTY
))
804 if (n
->prev
->tok
== n
->tok
&&
812 switch (n
->prev
->tok
) {
821 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
) {
833 md_pre_An(struct roff_node
*n
)
835 switch (n
->norm
->An
.auth
) {
837 outflags
&= ~MD_An_nosplit
;
838 outflags
|= MD_An_split
;
841 outflags
&= ~MD_An_split
;
842 outflags
|= MD_An_nosplit
;
845 if (outflags
& MD_An_split
)
847 else if (n
->sec
== SEC_AUTHORS
&&
848 ! (outflags
& MD_An_nosplit
))
849 outflags
|= MD_An_split
;
855 md_pre_Ap(struct roff_node
*n
)
864 md_pre_Bd(struct roff_node
*n
)
866 switch (n
->norm
->Bd
.type
) {
876 md_pre_Bk(struct roff_node
*n
)
890 md_post_Bk(struct roff_node
*n
)
892 if (n
->type
== ROFFT_BODY
)
897 md_pre_Bl(struct roff_node
*n
)
899 n
->norm
->Bl
.count
= 0;
900 if (n
->norm
->Bl
.type
== LIST_column
)
907 md_post_Bl(struct roff_node
*n
)
909 n
->norm
->Bl
.count
= 0;
910 if (n
->norm
->Bl
.type
== LIST_column
)
916 md_pre_D1(struct roff_node
*n
)
919 * Markdown blockquote syntax does not work inside code blocks.
920 * The best we can do is fall back to another nested code block.
934 md_post_D1(struct roff_node
*n
)
945 md_pre_Dl(struct roff_node
*n
)
948 * Markdown code block syntax does not work inside blockquotes.
949 * The best we can do is fall back to another nested blockquote.
963 md_pre_En(struct roff_node
*n
)
965 if (n
->norm
->Es
== NULL
||
966 n
->norm
->Es
->child
== NULL
)
969 md_word(n
->norm
->Es
->child
->string
);
975 md_post_En(struct roff_node
*n
)
977 if (n
->norm
->Es
== NULL
||
978 n
->norm
->Es
->child
== NULL
||
979 n
->norm
->Es
->child
->next
== NULL
)
983 md_word(n
->norm
->Es
->child
->next
->string
);
987 md_pre_Eo(struct roff_node
*n
)
989 if (n
->end
== ENDBODY_NOT
&&
990 n
->parent
->head
->child
== NULL
&&
992 n
->child
->end
!= ENDBODY_NOT
)
994 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
995 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
996 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
997 outflags
&= ~(MD_spc
| MD_nl
);
1002 md_post_Eo(struct roff_node
*n
)
1004 if (n
->end
!= ENDBODY_NOT
) {
1009 if (n
->child
== NULL
&& n
->parent
->head
->child
== NULL
)
1012 if (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)
1013 outflags
&= ~MD_spc
;
1019 md_pre_Fa(struct roff_node
*n
)
1023 am_Fa
= n
->tok
== MDOC_Fa
;
1030 outflags
&= ~MD_spc
;
1032 outflags
&= ~MD_spc
;
1034 if ((n
= n
->next
) != NULL
)
1041 md_post_Fa(struct roff_node
*n
)
1043 if (n
->next
!= NULL
&& n
->next
->tok
== MDOC_Fa
)
1048 md_pre_Fd(struct roff_node
*n
)
1056 md_post_Fd(struct roff_node
*n
)
1063 md_post_Fl(struct roff_node
*n
)
1066 if (n
->child
== NULL
&& n
->next
!= NULL
&&
1067 n
->next
->type
!= ROFFT_TEXT
&& !(n
->next
->flags
& NODE_LINE
))
1068 outflags
&= ~MD_spc
;
1072 md_pre_Fn(struct roff_node
*n
)
1076 if ((n
= n
->child
) == NULL
)
1080 outflags
&= ~MD_spc
;
1082 outflags
&= ~MD_spc
;
1084 outflags
&= ~MD_spc
;
1087 if ((n
= n
->next
) != NULL
)
1093 md_post_Fn(struct roff_node
*n
)
1096 if (n
->flags
& NODE_SYNPRETTY
) {
1103 md_pre_Fo(struct roff_node
*n
)
1110 if (n
->child
== NULL
)
1115 outflags
&= ~(MD_spc
| MD_nl
);
1125 md_post_Fo(struct roff_node
*n
)
1129 if (n
->child
!= NULL
)
1141 md_pre_In(struct roff_node
*n
)
1143 if (n
->flags
& NODE_SYNPRETTY
) {
1146 outflags
&= ~MD_spc
;
1147 md_word("#include <");
1150 outflags
&= ~MD_spc
;
1153 outflags
&= ~MD_spc
;
1158 md_post_In(struct roff_node
*n
)
1160 if (n
->flags
& NODE_SYNPRETTY
) {
1161 outflags
&= ~MD_spc
;
1165 outflags
&= ~MD_spc
;
1171 md_pre_It(struct roff_node
*n
)
1173 struct roff_node
*bln
;
1180 bln
= n
->parent
->parent
;
1181 if (bln
->norm
->Bl
.comp
== 0 &&
1182 bln
->norm
->Bl
.type
!= LIST_column
)
1186 switch (bln
->norm
->Bl
.type
) {
1208 if (bln
->norm
->Bl
.count
< 99)
1209 bln
->norm
->Bl
.count
++;
1210 printf("%d.\t", bln
->norm
->Bl
.count
);
1211 escflags
&= ~ESC_FON
;
1219 outflags
&= ~MD_spc
;
1220 outflags
|= MD_nonl
;
1223 if (code_blocks
|| quote_blocks
)
1228 bln
= n
->parent
->parent
;
1229 switch (bln
->norm
->Bl
.type
) {
1248 md_post_It(struct roff_node
*n
)
1250 struct roff_node
*bln
;
1253 if (n
->type
!= ROFFT_BODY
)
1256 bln
= n
->parent
->parent
;
1257 switch (bln
->norm
->Bl
.type
) {
1263 if (code_blocks
|| quote_blocks
)
1272 if (n
->next
== NULL
)
1275 /* Calculate the array index of the current column. */
1278 while ((n
= n
->prev
) != NULL
&& n
->type
!= ROFFT_HEAD
)
1282 * If a width was specified for this column,
1283 * subtract what printed, and
1284 * add the same spacing as in mdoc_term.c.
1287 nc
= bln
->norm
->Bl
.ncols
;
1288 i
= i
< nc
? strlen(bln
->norm
->Bl
.cols
[i
]) - outcount
+
1289 (nc
< 5 ? 4 : nc
== 5 ? 3 : 1) : 1;
1295 outflags
&= ~MD_spc
;
1296 escflags
&= ~ESC_FON
;
1306 md_post_Lb(struct roff_node
*n
)
1308 if (n
->sec
== SEC_LIBRARY
)
1313 md_uri(const char *s
)
1315 while (*s
!= '\0') {
1316 if (strchr("%()<>", *s
) != NULL
) {
1317 printf("%%%2.2hhX", *s
);
1328 md_pre_Lk(struct roff_node
*n
)
1330 const struct roff_node
*link
, *descr
, *punct
;
1332 if ((link
= n
->child
) == NULL
)
1335 /* Find beginning of trailing punctuation. */
1337 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1338 punct
= punct
->prev
;
1339 punct
= punct
->next
;
1344 descr
= link
; /* no text */
1346 outflags
&= ~MD_spc
;
1348 md_word(descr
->string
);
1349 descr
= descr
->next
;
1350 } while (descr
!= punct
);
1351 outflags
&= ~MD_spc
;
1355 md_uri(link
->string
);
1356 outflags
&= ~MD_spc
;
1359 /* Trailing punctuation. */
1360 while (punct
!= NULL
) {
1361 md_word(punct
->string
);
1362 punct
= punct
->next
;
1368 md_pre_Mt(struct roff_node
*n
)
1370 const struct roff_node
*nch
;
1373 outflags
&= ~MD_spc
;
1374 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
1375 md_word(nch
->string
);
1376 outflags
&= ~MD_spc
;
1377 md_rawword("](mailto:");
1378 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
) {
1379 md_uri(nch
->string
);
1380 if (nch
->next
!= NULL
) {
1385 outflags
&= ~MD_spc
;
1391 md_pre_Nd(struct roff_node
*n
)
1400 md_pre_Nm(struct roff_node
*n
)
1418 md_post_Nm(struct roff_node
*n
)
1434 md_pre_No(struct roff_node
*n
)
1436 outflags
|= MD_spc_force
;
1441 md_pre_Ns(struct roff_node
*n
)
1443 outflags
&= ~MD_spc
;
1448 md_post_Pf(struct roff_node
*n
)
1450 if (n
->next
!= NULL
&& (n
->next
->flags
& NODE_LINE
) == 0)
1451 outflags
&= ~MD_spc
;
1455 md_pre_Pp(struct roff_node
*n
)
1462 md_pre_Rs(struct roff_node
*n
)
1464 if (n
->sec
== SEC_SEE_ALSO
)
1470 md_pre_Sh(struct roff_node
*n
)
1474 if (n
->sec
== SEC_AUTHORS
)
1475 outflags
&= ~(MD_An_split
| MD_An_nosplit
);
1479 md_rawword(n
->tok
== MDOC_Sh
? "#" : "##");
1491 md_pre_Sm(struct roff_node
*n
)
1493 if (n
->child
== NULL
)
1495 else if (strcmp("on", n
->child
->string
) == 0)
1500 if (outflags
& MD_Sm
)
1507 md_pre_Vt(struct roff_node
*n
)
1523 md_post_Vt(struct roff_node
*n
)
1536 md_pre_Xr(struct roff_node
*n
)
1545 outflags
&= ~MD_spc
;
1553 md_pre__T(struct roff_node
*n
)
1555 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1559 outflags
&= ~MD_spc
;
1564 md_post__T(struct roff_node
*n
)
1566 outflags
&= ~MD_spc
;
1567 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
)
1575 md_pre_br(struct roff_node
*n
)