1 /* $Id: mdoc_html.c,v 1.341 2020/10/16 17:22:43 schwarze Exp $ */
3 * Copyright (c) 2014-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * HTML formatter for mdoc(7) used by mandoc(1).
22 #include <sys/types.h>
31 #include "mandoc_aux.h"
39 #define MDOC_ARGS const struct roff_meta *meta, \
40 struct roff_node *n, \
44 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
47 struct mdoc_html_act
{
48 int (*pre
)(MDOC_ARGS
);
49 void (*post
)(MDOC_ARGS
);
52 static void print_mdoc_head(const struct roff_meta
*,
54 static void print_mdoc_node(MDOC_ARGS
);
55 static void print_mdoc_nodelist(MDOC_ARGS
);
56 static void synopsis_pre(struct html
*, struct roff_node
*);
58 static void mdoc_root_post(const struct roff_meta
*,
60 static int mdoc_root_pre(const struct roff_meta
*,
63 static void mdoc__x_post(MDOC_ARGS
);
64 static int mdoc__x_pre(MDOC_ARGS
);
65 static int mdoc_abort_pre(MDOC_ARGS
);
66 static int mdoc_ad_pre(MDOC_ARGS
);
67 static int mdoc_an_pre(MDOC_ARGS
);
68 static int mdoc_ap_pre(MDOC_ARGS
);
69 static int mdoc_ar_pre(MDOC_ARGS
);
70 static int mdoc_bd_pre(MDOC_ARGS
);
71 static int mdoc_bf_pre(MDOC_ARGS
);
72 static void mdoc_bk_post(MDOC_ARGS
);
73 static int mdoc_bk_pre(MDOC_ARGS
);
74 static int mdoc_bl_pre(MDOC_ARGS
);
75 static int mdoc_cd_pre(MDOC_ARGS
);
76 static int mdoc_code_pre(MDOC_ARGS
);
77 static int mdoc_d1_pre(MDOC_ARGS
);
78 static int mdoc_fa_pre(MDOC_ARGS
);
79 static int mdoc_fd_pre(MDOC_ARGS
);
80 static int mdoc_fl_pre(MDOC_ARGS
);
81 static int mdoc_fn_pre(MDOC_ARGS
);
82 static int mdoc_ft_pre(MDOC_ARGS
);
83 static int mdoc_em_pre(MDOC_ARGS
);
84 static void mdoc_eo_post(MDOC_ARGS
);
85 static int mdoc_eo_pre(MDOC_ARGS
);
86 static int mdoc_ex_pre(MDOC_ARGS
);
87 static void mdoc_fo_post(MDOC_ARGS
);
88 static int mdoc_fo_pre(MDOC_ARGS
);
89 static int mdoc_igndelim_pre(MDOC_ARGS
);
90 static int mdoc_in_pre(MDOC_ARGS
);
91 static int mdoc_it_pre(MDOC_ARGS
);
92 static int mdoc_lb_pre(MDOC_ARGS
);
93 static int mdoc_lk_pre(MDOC_ARGS
);
94 static int mdoc_mt_pre(MDOC_ARGS
);
95 static int mdoc_nd_pre(MDOC_ARGS
);
96 static int mdoc_nm_pre(MDOC_ARGS
);
97 static int mdoc_no_pre(MDOC_ARGS
);
98 static int mdoc_ns_pre(MDOC_ARGS
);
99 static int mdoc_pa_pre(MDOC_ARGS
);
100 static void mdoc_pf_post(MDOC_ARGS
);
101 static int mdoc_pp_pre(MDOC_ARGS
);
102 static void mdoc_quote_post(MDOC_ARGS
);
103 static int mdoc_quote_pre(MDOC_ARGS
);
104 static int mdoc_rs_pre(MDOC_ARGS
);
105 static int mdoc_sh_pre(MDOC_ARGS
);
106 static int mdoc_skip_pre(MDOC_ARGS
);
107 static int mdoc_sm_pre(MDOC_ARGS
);
108 static int mdoc_ss_pre(MDOC_ARGS
);
109 static int mdoc_st_pre(MDOC_ARGS
);
110 static int mdoc_sx_pre(MDOC_ARGS
);
111 static int mdoc_sy_pre(MDOC_ARGS
);
112 static int mdoc_tg_pre(MDOC_ARGS
);
113 static int mdoc_va_pre(MDOC_ARGS
);
114 static int mdoc_vt_pre(MDOC_ARGS
);
115 static int mdoc_xr_pre(MDOC_ARGS
);
116 static int mdoc_xx_pre(MDOC_ARGS
);
118 static const struct mdoc_html_act mdoc_html_acts
[MDOC_MAX
- MDOC_Dd
] = {
119 {NULL
, NULL
}, /* Dd */
120 {NULL
, NULL
}, /* Dt */
121 {NULL
, NULL
}, /* Os */
122 {mdoc_sh_pre
, NULL
}, /* Sh */
123 {mdoc_ss_pre
, NULL
}, /* Ss */
124 {mdoc_pp_pre
, NULL
}, /* Pp */
125 {mdoc_d1_pre
, NULL
}, /* D1 */
126 {mdoc_d1_pre
, NULL
}, /* Dl */
127 {mdoc_bd_pre
, NULL
}, /* Bd */
128 {NULL
, NULL
}, /* Ed */
129 {mdoc_bl_pre
, NULL
}, /* Bl */
130 {NULL
, NULL
}, /* El */
131 {mdoc_it_pre
, NULL
}, /* It */
132 {mdoc_ad_pre
, NULL
}, /* Ad */
133 {mdoc_an_pre
, NULL
}, /* An */
134 {mdoc_ap_pre
, NULL
}, /* Ap */
135 {mdoc_ar_pre
, NULL
}, /* Ar */
136 {mdoc_cd_pre
, NULL
}, /* Cd */
137 {mdoc_code_pre
, NULL
}, /* Cm */
138 {mdoc_code_pre
, NULL
}, /* Dv */
139 {mdoc_code_pre
, NULL
}, /* Er */
140 {mdoc_code_pre
, NULL
}, /* Ev */
141 {mdoc_ex_pre
, NULL
}, /* Ex */
142 {mdoc_fa_pre
, NULL
}, /* Fa */
143 {mdoc_fd_pre
, NULL
}, /* Fd */
144 {mdoc_fl_pre
, NULL
}, /* Fl */
145 {mdoc_fn_pre
, NULL
}, /* Fn */
146 {mdoc_ft_pre
, NULL
}, /* Ft */
147 {mdoc_code_pre
, NULL
}, /* Ic */
148 {mdoc_in_pre
, NULL
}, /* In */
149 {mdoc_code_pre
, NULL
}, /* Li */
150 {mdoc_nd_pre
, NULL
}, /* Nd */
151 {mdoc_nm_pre
, NULL
}, /* Nm */
152 {mdoc_quote_pre
, mdoc_quote_post
}, /* Op */
153 {mdoc_abort_pre
, NULL
}, /* Ot */
154 {mdoc_pa_pre
, NULL
}, /* Pa */
155 {mdoc_ex_pre
, NULL
}, /* Rv */
156 {mdoc_st_pre
, NULL
}, /* St */
157 {mdoc_va_pre
, NULL
}, /* Va */
158 {mdoc_vt_pre
, NULL
}, /* Vt */
159 {mdoc_xr_pre
, NULL
}, /* Xr */
160 {mdoc__x_pre
, mdoc__x_post
}, /* %A */
161 {mdoc__x_pre
, mdoc__x_post
}, /* %B */
162 {mdoc__x_pre
, mdoc__x_post
}, /* %D */
163 {mdoc__x_pre
, mdoc__x_post
}, /* %I */
164 {mdoc__x_pre
, mdoc__x_post
}, /* %J */
165 {mdoc__x_pre
, mdoc__x_post
}, /* %N */
166 {mdoc__x_pre
, mdoc__x_post
}, /* %O */
167 {mdoc__x_pre
, mdoc__x_post
}, /* %P */
168 {mdoc__x_pre
, mdoc__x_post
}, /* %R */
169 {mdoc__x_pre
, mdoc__x_post
}, /* %T */
170 {mdoc__x_pre
, mdoc__x_post
}, /* %V */
171 {NULL
, NULL
}, /* Ac */
172 {mdoc_quote_pre
, mdoc_quote_post
}, /* Ao */
173 {mdoc_quote_pre
, mdoc_quote_post
}, /* Aq */
174 {mdoc_xx_pre
, NULL
}, /* At */
175 {NULL
, NULL
}, /* Bc */
176 {mdoc_bf_pre
, NULL
}, /* Bf */
177 {mdoc_quote_pre
, mdoc_quote_post
}, /* Bo */
178 {mdoc_quote_pre
, mdoc_quote_post
}, /* Bq */
179 {mdoc_xx_pre
, NULL
}, /* Bsx */
180 {mdoc_xx_pre
, NULL
}, /* Bx */
181 {mdoc_skip_pre
, NULL
}, /* Db */
182 {NULL
, NULL
}, /* Dc */
183 {mdoc_quote_pre
, mdoc_quote_post
}, /* Do */
184 {mdoc_quote_pre
, mdoc_quote_post
}, /* Dq */
185 {NULL
, NULL
}, /* Ec */ /* FIXME: no space */
186 {NULL
, NULL
}, /* Ef */
187 {mdoc_em_pre
, NULL
}, /* Em */
188 {mdoc_eo_pre
, mdoc_eo_post
}, /* Eo */
189 {mdoc_xx_pre
, NULL
}, /* Fx */
190 {mdoc_no_pre
, NULL
}, /* Ms */
191 {mdoc_no_pre
, NULL
}, /* No */
192 {mdoc_ns_pre
, NULL
}, /* Ns */
193 {mdoc_xx_pre
, NULL
}, /* Nx */
194 {mdoc_xx_pre
, NULL
}, /* Ox */
195 {NULL
, NULL
}, /* Pc */
196 {mdoc_igndelim_pre
, mdoc_pf_post
}, /* Pf */
197 {mdoc_quote_pre
, mdoc_quote_post
}, /* Po */
198 {mdoc_quote_pre
, mdoc_quote_post
}, /* Pq */
199 {NULL
, NULL
}, /* Qc */
200 {mdoc_quote_pre
, mdoc_quote_post
}, /* Ql */
201 {mdoc_quote_pre
, mdoc_quote_post
}, /* Qo */
202 {mdoc_quote_pre
, mdoc_quote_post
}, /* Qq */
203 {NULL
, NULL
}, /* Re */
204 {mdoc_rs_pre
, NULL
}, /* Rs */
205 {NULL
, NULL
}, /* Sc */
206 {mdoc_quote_pre
, mdoc_quote_post
}, /* So */
207 {mdoc_quote_pre
, mdoc_quote_post
}, /* Sq */
208 {mdoc_sm_pre
, NULL
}, /* Sm */
209 {mdoc_sx_pre
, NULL
}, /* Sx */
210 {mdoc_sy_pre
, NULL
}, /* Sy */
211 {NULL
, NULL
}, /* Tn */
212 {mdoc_xx_pre
, NULL
}, /* Ux */
213 {NULL
, NULL
}, /* Xc */
214 {NULL
, NULL
}, /* Xo */
215 {mdoc_fo_pre
, mdoc_fo_post
}, /* Fo */
216 {NULL
, NULL
}, /* Fc */
217 {mdoc_quote_pre
, mdoc_quote_post
}, /* Oo */
218 {NULL
, NULL
}, /* Oc */
219 {mdoc_bk_pre
, mdoc_bk_post
}, /* Bk */
220 {NULL
, NULL
}, /* Ek */
221 {NULL
, NULL
}, /* Bt */
222 {NULL
, NULL
}, /* Hf */
223 {mdoc_em_pre
, NULL
}, /* Fr */
224 {NULL
, NULL
}, /* Ud */
225 {mdoc_lb_pre
, NULL
}, /* Lb */
226 {mdoc_abort_pre
, NULL
}, /* Lp */
227 {mdoc_lk_pre
, NULL
}, /* Lk */
228 {mdoc_mt_pre
, NULL
}, /* Mt */
229 {mdoc_quote_pre
, mdoc_quote_post
}, /* Brq */
230 {mdoc_quote_pre
, mdoc_quote_post
}, /* Bro */
231 {NULL
, NULL
}, /* Brc */
232 {mdoc__x_pre
, mdoc__x_post
}, /* %C */
233 {mdoc_skip_pre
, NULL
}, /* Es */
234 {mdoc_quote_pre
, mdoc_quote_post
}, /* En */
235 {mdoc_xx_pre
, NULL
}, /* Dx */
236 {mdoc__x_pre
, mdoc__x_post
}, /* %Q */
237 {mdoc__x_pre
, mdoc__x_post
}, /* %U */
238 {NULL
, NULL
}, /* Ta */
239 {mdoc_tg_pre
, NULL
}, /* Tg */
244 * See the same function in mdoc_term.c for documentation.
247 synopsis_pre(struct html
*h
, struct roff_node
*n
)
249 struct roff_node
*np
;
251 if ((n
->flags
& NODE_SYNPRETTY
) == 0 ||
252 (np
= roff_node_prev(n
)) == NULL
)
255 if (np
->tok
== n
->tok
&&
259 print_otag(h
, TAG_BR
, "");
271 if (n
->tok
!= MDOC_Fn
&& n
->tok
!= MDOC_Fo
)
275 print_otag(h
, TAG_BR
, "");
278 html_close_paragraph(h
);
279 print_otag(h
, TAG_P
, "c", "Pp");
283 html_mdoc(void *arg
, const struct roff_meta
*mdoc
)
289 h
= (struct html
*)arg
;
290 n
= mdoc
->first
->child
;
292 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
294 print_otag(h
, TAG_HTML
, "");
295 if (n
!= NULL
&& n
->type
== ROFFT_COMMENT
)
296 print_gen_comment(h
, n
);
297 t
= print_otag(h
, TAG_HEAD
, "");
298 print_mdoc_head(mdoc
, h
);
300 print_otag(h
, TAG_BODY
, "");
303 mdoc_root_pre(mdoc
, h
);
304 t
= print_otag(h
, TAG_DIV
, "c", "manual-text");
305 print_mdoc_nodelist(mdoc
, n
, h
);
307 mdoc_root_post(mdoc
, h
);
312 print_mdoc_head(const struct roff_meta
*meta
, struct html
*h
)
318 if (meta
->arch
!= NULL
&& meta
->msec
!= NULL
)
319 mandoc_asprintf(&cp
, "%s(%s) (%s)", meta
->title
,
320 meta
->msec
, meta
->arch
);
321 else if (meta
->msec
!= NULL
)
322 mandoc_asprintf(&cp
, "%s(%s)", meta
->title
, meta
->msec
);
323 else if (meta
->arch
!= NULL
)
324 mandoc_asprintf(&cp
, "%s (%s)", meta
->title
, meta
->arch
);
326 cp
= mandoc_strdup(meta
->title
);
328 print_otag(h
, TAG_TITLE
, "");
334 print_mdoc_nodelist(MDOC_ARGS
)
338 print_mdoc_node(meta
, n
, h
);
344 print_mdoc_node(MDOC_ARGS
)
349 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
352 if ((n
->flags
& NODE_NOFILL
) == 0)
353 html_fillmode(h
, ROFF_fi
);
354 else if (html_fillmode(h
, ROFF_nf
) == ROFF_nf
&&
355 n
->tok
!= ROFF_fi
&& n
->flags
& NODE_LINE
)
359 n
->flags
&= ~NODE_ENDED
;
362 if (n
->flags
& NODE_LINE
) {
363 switch (*n
->string
) {
369 if ((h
->flags
& HTML_NONEWLINE
) == 0 &&
370 (n
->flags
& NODE_NOFILL
) == 0)
371 print_otag(h
, TAG_BR
, "");
379 if (n
->flags
& NODE_DELIMC
)
380 h
->flags
|= HTML_NOSPACE
;
381 if (n
->flags
& NODE_HREF
)
382 print_tagged_text(h
, n
->string
, n
);
384 print_text(h
, n
->string
);
385 if (n
->flags
& NODE_DELIMO
)
386 h
->flags
|= HTML_NOSPACE
;
391 print_eqn(h
, n
->eqn
);
395 * This will take care of initialising all of the table
396 * state data for the first table, then tearing it down
399 print_tbl(h
, n
->span
);
403 * Close out the current table, if it's open, and unset
404 * the "meta" table state. This will be reopened on the
405 * next table element.
409 assert(h
->tblt
== NULL
);
412 if (n
->tok
< ROFF_MAX
) {
418 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
419 if (mdoc_html_acts
[n
->tok
- MDOC_Dd
].pre
!= NULL
&&
420 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
421 child
= (*mdoc_html_acts
[n
->tok
- MDOC_Dd
].pre
)(meta
,
426 if (h
->flags
& HTML_KEEP
&& n
->flags
& NODE_LINE
) {
427 h
->flags
&= ~HTML_KEEP
;
428 h
->flags
|= HTML_PREKEEP
;
431 if (child
&& n
->child
!= NULL
)
432 print_mdoc_nodelist(meta
, n
->child
, h
);
442 if (mdoc_html_acts
[n
->tok
- MDOC_Dd
].post
== NULL
||
443 n
->flags
& NODE_ENDED
)
445 (*mdoc_html_acts
[n
->tok
- MDOC_Dd
].post
)(meta
, n
, h
);
446 if (n
->end
!= ENDBODY_NOT
)
447 n
->body
->flags
|= NODE_ENDED
;
453 mdoc_root_post(const struct roff_meta
*meta
, struct html
*h
)
457 t
= print_otag(h
, TAG_TABLE
, "c", "foot");
458 tt
= print_otag(h
, TAG_TR
, "");
460 print_otag(h
, TAG_TD
, "c", "foot-date");
461 print_text(h
, meta
->date
);
464 print_otag(h
, TAG_TD
, "c", "foot-os");
465 print_text(h
, meta
->os
);
470 mdoc_root_pre(const struct roff_meta
*meta
, struct html
*h
)
473 char *volume
, *title
;
475 if (NULL
== meta
->arch
)
476 volume
= mandoc_strdup(meta
->vol
);
478 mandoc_asprintf(&volume
, "%s (%s)",
479 meta
->vol
, meta
->arch
);
481 if (NULL
== meta
->msec
)
482 title
= mandoc_strdup(meta
->title
);
484 mandoc_asprintf(&title
, "%s(%s)",
485 meta
->title
, meta
->msec
);
487 t
= print_otag(h
, TAG_TABLE
, "c", "head");
488 tt
= print_otag(h
, TAG_TR
, "");
490 print_otag(h
, TAG_TD
, "c", "head-ltitle");
491 print_text(h
, title
);
494 print_otag(h
, TAG_TD
, "c", "head-vol");
495 print_text(h
, volume
);
498 print_otag(h
, TAG_TD
, "c", "head-rtitle");
499 print_text(h
, title
);
508 mdoc_code_pre(MDOC_ARGS
)
510 print_otag_id(h
, TAG_CODE
, roff_name
[n
->tok
], n
);
515 mdoc_sh_pre(MDOC_ARGS
)
517 struct roff_node
*sn
, *subn
;
518 struct tag
*t
, *tsec
, *tsub
;
524 html_close_paragraph(h
);
525 if ((h
->oflags
& HTML_TOC
) == 0 ||
526 h
->flags
& HTML_TOCDONE
||
527 n
->sec
<= SEC_SYNOPSIS
) {
528 print_otag(h
, TAG_SECTION
, "c", "Sh");
531 h
->flags
|= HTML_TOCDONE
;
533 for (sn
= n
->next
; sn
!= NULL
; sn
= sn
->next
)
534 if (sn
->sec
== SEC_CUSTOM
)
539 t
= print_otag(h
, TAG_H1
, "c", "Sh");
540 print_text(h
, "TABLE OF CONTENTS");
542 t
= print_otag(h
, TAG_UL
, "c", "Bl-compact");
543 for (sn
= n
; sn
!= NULL
; sn
= sn
->next
) {
544 tsec
= print_otag(h
, TAG_LI
, "");
545 id
= html_make_id(sn
->head
, 0);
546 tsub
= print_otag(h
, TAG_A
, "hR", id
);
548 print_mdoc_nodelist(meta
, sn
->head
->child
, h
);
551 for (subn
= sn
->body
->child
; subn
!= NULL
;
553 if (subn
->tok
!= MDOC_Ss
)
555 id
= html_make_id(subn
->head
, 0);
559 print_otag(h
, TAG_UL
,
561 tsub
= print_otag(h
, TAG_LI
, "");
562 print_otag(h
, TAG_A
, "hR", id
);
564 print_mdoc_nodelist(meta
,
565 subn
->head
->child
, h
);
571 print_otag(h
, TAG_SECTION
, "c", "Sh");
574 print_otag_id(h
, TAG_H1
, "Sh", n
);
577 if (n
->sec
== SEC_AUTHORS
)
578 h
->flags
&= ~(HTML_SPLIT
|HTML_NOSPLIT
);
587 mdoc_ss_pre(MDOC_ARGS
)
591 html_close_paragraph(h
);
592 print_otag(h
, TAG_SECTION
, "c", "Ss");
595 print_otag_id(h
, TAG_H2
, "Ss", n
);
606 mdoc_fl_pre(MDOC_ARGS
)
608 struct roff_node
*nn
;
610 print_otag_id(h
, TAG_CODE
, "Fl", n
);
611 print_text(h
, "\\-");
612 if (n
->child
!= NULL
||
613 ((nn
= roff_node_next(n
)) != NULL
&&
614 nn
->type
!= ROFFT_TEXT
&&
615 (nn
->flags
& NODE_LINE
) == 0))
616 h
->flags
|= HTML_NOSPACE
;
622 mdoc_nd_pre(MDOC_ARGS
)
634 print_text(h
, "\\(em");
635 print_otag(h
, TAG_SPAN
, "c", "Nd");
640 mdoc_nm_pre(MDOC_ARGS
)
646 print_otag(h
, TAG_TD
, "");
649 print_otag(h
, TAG_CODE
, "c", "Nm");
652 print_otag(h
, TAG_TD
, "");
657 html_close_paragraph(h
);
659 print_otag(h
, TAG_TABLE
, "c", "Nm");
660 print_otag(h
, TAG_TR
, "");
665 mdoc_xr_pre(MDOC_ARGS
)
667 if (NULL
== n
->child
)
671 print_otag(h
, TAG_A
, "chM", "Xr",
672 n
->child
->string
, n
->child
->next
== NULL
?
673 NULL
: n
->child
->next
->string
);
675 print_otag(h
, TAG_A
, "c", "Xr");
678 print_text(h
, n
->string
);
680 if (NULL
== (n
= n
->next
))
683 h
->flags
|= HTML_NOSPACE
;
685 h
->flags
|= HTML_NOSPACE
;
686 print_text(h
, n
->string
);
687 h
->flags
|= HTML_NOSPACE
;
693 mdoc_tg_pre(MDOC_ARGS
)
697 if ((id
= html_make_id(n
, 1)) != NULL
) {
698 print_tagq(h
, print_otag(h
, TAG_MARK
, "i", id
));
705 mdoc_ns_pre(MDOC_ARGS
)
708 if ( ! (NODE_LINE
& n
->flags
))
709 h
->flags
|= HTML_NOSPACE
;
714 mdoc_ar_pre(MDOC_ARGS
)
716 print_otag(h
, TAG_VAR
, "c", "Ar");
721 mdoc_xx_pre(MDOC_ARGS
)
723 print_otag(h
, TAG_SPAN
, "c", "Ux");
728 mdoc_it_pre(MDOC_ARGS
)
730 const struct roff_node
*bl
;
734 while (bl
->tok
!= MDOC_Bl
)
736 type
= bl
->norm
->Bl
.type
;
748 print_otag_id(h
, TAG_LI
, NULL
, n
);
760 print_otag_id(h
, TAG_DT
, NULL
, n
);
763 print_otag(h
, TAG_DD
, "");
772 print_otag_id(h
, TAG_DT
, NULL
, n
);
775 if (n
->child
== NULL
) {
776 print_otag(h
, TAG_DD
, "s", "width", "auto");
777 print_text(h
, "\\ ");
779 print_otag(h
, TAG_DD
, "");
790 print_otag(h
, TAG_TD
, "");
793 print_otag_id(h
, TAG_TR
, NULL
, n
);
803 mdoc_bl_pre(MDOC_ARGS
)
807 enum htmltag elemtype
;
811 html_close_paragraph(h
);
825 (void)strlcpy(cattr
, "Bl-bullet", sizeof(cattr
));
830 (void)strlcpy(cattr
, "Bl-dash", sizeof(cattr
));
834 (void)strlcpy(cattr
, "Bl-item", sizeof(cattr
));
838 (void)strlcpy(cattr
, "Bl-enum", sizeof(cattr
));
842 (void)strlcpy(cattr
, "Bl-diag", sizeof(cattr
));
846 (void)strlcpy(cattr
, "Bl-hang", sizeof(cattr
));
850 (void)strlcpy(cattr
, "Bl-inset", sizeof(cattr
));
854 (void)strlcpy(cattr
, "Bl-ohang", sizeof(cattr
));
858 print_otag(h
, TAG_DIV
, "c", "Bd-indent");
859 print_otag_id(h
, TAG_DL
,
860 bl
->comp
? "Bl-tag Bl-compact" : "Bl-tag", n
->body
);
863 elemtype
= TAG_TABLE
;
864 (void)strlcpy(cattr
, "Bl-column", sizeof(cattr
));
869 if (bl
->offs
!= NULL
)
870 (void)strlcat(cattr
, " Bd-indent", sizeof(cattr
));
872 (void)strlcat(cattr
, " Bl-compact", sizeof(cattr
));
873 print_otag_id(h
, elemtype
, cattr
, n
->body
);
878 mdoc_ex_pre(MDOC_ARGS
)
880 if (roff_node_prev(n
) != NULL
)
881 print_otag(h
, TAG_BR
, "");
886 mdoc_st_pre(MDOC_ARGS
)
888 print_otag(h
, TAG_SPAN
, "c", "St");
893 mdoc_em_pre(MDOC_ARGS
)
895 print_otag_id(h
, TAG_I
, "Em", n
);
900 mdoc_d1_pre(MDOC_ARGS
)
904 html_close_paragraph(h
);
913 print_otag_id(h
, TAG_DIV
, "Bd Bd-indent", n
);
914 if (n
->tok
== MDOC_Dl
)
915 print_otag(h
, TAG_CODE
, "c", "Li");
920 mdoc_sx_pre(MDOC_ARGS
)
924 id
= html_make_id(n
, 0);
925 print_otag(h
, TAG_A
, "chR", "Sx", id
);
931 mdoc_bd_pre(MDOC_ARGS
)
934 struct roff_node
*nn
;
939 html_close_paragraph(h
);
949 /* Handle preceding whitespace. */
951 comp
= n
->norm
->Bd
.comp
;
952 for (nn
= n
; nn
!= NULL
&& comp
== 0; nn
= nn
->parent
) {
953 if (nn
->type
!= ROFFT_BLOCK
)
955 if (nn
->tok
== MDOC_Sh
|| nn
->tok
== MDOC_Ss
)
957 if (roff_node_prev(nn
) != NULL
)
960 (void)strlcpy(buf
, "Bd", sizeof(buf
));
962 (void)strlcat(buf
, " Pp", sizeof(buf
));
964 /* Handle the -offset argument. */
966 if (n
->norm
->Bd
.offs
!= NULL
&&
967 strcmp(n
->norm
->Bd
.offs
, "left") != 0)
968 (void)strlcat(buf
, " Bd-indent", sizeof(buf
));
970 print_otag_id(h
, TAG_DIV
, buf
, n
);
975 mdoc_pa_pre(MDOC_ARGS
)
977 print_otag(h
, TAG_SPAN
, "c", "Pa");
982 mdoc_ad_pre(MDOC_ARGS
)
984 print_otag(h
, TAG_SPAN
, "c", "Ad");
989 mdoc_an_pre(MDOC_ARGS
)
991 if (n
->norm
->An
.auth
== AUTH_split
) {
992 h
->flags
&= ~HTML_NOSPLIT
;
993 h
->flags
|= HTML_SPLIT
;
996 if (n
->norm
->An
.auth
== AUTH_nosplit
) {
997 h
->flags
&= ~HTML_SPLIT
;
998 h
->flags
|= HTML_NOSPLIT
;
1002 if (h
->flags
& HTML_SPLIT
)
1003 print_otag(h
, TAG_BR
, "");
1005 if (n
->sec
== SEC_AUTHORS
&& ! (h
->flags
& HTML_NOSPLIT
))
1006 h
->flags
|= HTML_SPLIT
;
1008 print_otag(h
, TAG_SPAN
, "c", "An");
1013 mdoc_cd_pre(MDOC_ARGS
)
1016 print_otag(h
, TAG_CODE
, "c", "Cd");
1021 mdoc_fa_pre(MDOC_ARGS
)
1023 const struct roff_node
*nn
;
1026 if (n
->parent
->tok
!= MDOC_Fo
) {
1027 print_otag(h
, TAG_VAR
, "c", "Fa");
1030 for (nn
= n
->child
; nn
!= NULL
; nn
= nn
->next
) {
1031 t
= print_otag(h
, TAG_VAR
, "c", "Fa");
1032 print_text(h
, nn
->string
);
1034 if (nn
->next
!= NULL
) {
1035 h
->flags
|= HTML_NOSPACE
;
1039 if (n
->child
!= NULL
&&
1040 (nn
= roff_node_next(n
)) != NULL
&&
1041 nn
->tok
== MDOC_Fa
) {
1042 h
->flags
|= HTML_NOSPACE
;
1049 mdoc_fd_pre(MDOC_ARGS
)
1056 if (NULL
== (n
= n
->child
))
1059 assert(n
->type
== ROFFT_TEXT
);
1061 if (strcmp(n
->string
, "#include")) {
1062 print_otag(h
, TAG_CODE
, "c", "Fd");
1066 print_otag(h
, TAG_CODE
, "c", "In");
1067 print_text(h
, n
->string
);
1069 if (NULL
!= (n
= n
->next
)) {
1070 assert(n
->type
== ROFFT_TEXT
);
1072 if (h
->base_includes
) {
1074 if (*cp
== '<' || *cp
== '"')
1076 buf
= mandoc_strdup(cp
);
1077 cp
= strchr(buf
, '\0') - 1;
1078 if (cp
>= buf
&& (*cp
== '>' || *cp
== '"'))
1080 t
= print_otag(h
, TAG_A
, "chI", "In", buf
);
1083 t
= print_otag(h
, TAG_A
, "c", "In");
1085 print_text(h
, n
->string
);
1091 for ( ; n
; n
= n
->next
) {
1092 assert(n
->type
== ROFFT_TEXT
);
1093 print_text(h
, n
->string
);
1100 mdoc_vt_pre(MDOC_ARGS
)
1102 if (n
->type
== ROFFT_BLOCK
) {
1105 } else if (n
->type
== ROFFT_ELEM
) {
1107 } else if (n
->type
== ROFFT_HEAD
)
1110 print_otag(h
, TAG_VAR
, "c", "Vt");
1115 mdoc_ft_pre(MDOC_ARGS
)
1118 print_otag(h
, TAG_VAR
, "c", "Ft");
1123 mdoc_fn_pre(MDOC_ARGS
)
1127 const char *sp
, *ep
;
1130 pretty
= NODE_SYNPRETTY
& n
->flags
;
1133 /* Split apart into type and name. */
1134 assert(n
->child
->string
);
1135 sp
= n
->child
->string
;
1137 ep
= strchr(sp
, ' ');
1139 t
= print_otag(h
, TAG_VAR
, "c", "Ft");
1142 sz
= MIN((int)(ep
- sp
), BUFSIZ
- 1);
1143 (void)memcpy(nbuf
, sp
, (size_t)sz
);
1145 print_text(h
, nbuf
);
1147 ep
= strchr(sp
, ' ');
1152 t
= print_otag_id(h
, TAG_CODE
, "Fn", n
);
1159 h
->flags
|= HTML_NOSPACE
;
1161 h
->flags
|= HTML_NOSPACE
;
1163 for (n
= n
->child
->next
; n
; n
= n
->next
) {
1164 if (NODE_SYNPRETTY
& n
->flags
)
1165 t
= print_otag(h
, TAG_VAR
, "cs", "Fa",
1166 "white-space", "nowrap");
1168 t
= print_otag(h
, TAG_VAR
, "c", "Fa");
1169 print_text(h
, n
->string
);
1172 h
->flags
|= HTML_NOSPACE
;
1177 h
->flags
|= HTML_NOSPACE
;
1181 h
->flags
|= HTML_NOSPACE
;
1189 mdoc_sm_pre(MDOC_ARGS
)
1192 if (NULL
== n
->child
)
1193 h
->flags
^= HTML_NONOSPACE
;
1194 else if (0 == strcmp("on", n
->child
->string
))
1195 h
->flags
&= ~HTML_NONOSPACE
;
1197 h
->flags
|= HTML_NONOSPACE
;
1199 if ( ! (HTML_NONOSPACE
& h
->flags
))
1200 h
->flags
&= ~HTML_NOSPACE
;
1206 mdoc_skip_pre(MDOC_ARGS
)
1213 mdoc_pp_pre(MDOC_ARGS
)
1217 if (n
->flags
& NODE_NOFILL
) {
1219 if (n
->flags
& NODE_ID
)
1220 mdoc_tg_pre(meta
, n
, h
);
1226 html_close_paragraph(h
);
1227 id
= n
->flags
& NODE_ID
? html_make_id(n
, 1) : NULL
;
1228 print_otag(h
, TAG_P
, "ci", "Pp", id
);
1235 mdoc_lk_pre(MDOC_ARGS
)
1237 const struct roff_node
*link
, *descr
, *punct
;
1240 if ((link
= n
->child
) == NULL
)
1243 /* Find beginning of trailing punctuation. */
1245 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1246 punct
= punct
->prev
;
1247 punct
= punct
->next
;
1249 /* Link target and link text. */
1252 descr
= link
; /* no text */
1253 t
= print_otag(h
, TAG_A
, "ch", "Lk", link
->string
);
1255 if (descr
->flags
& (NODE_DELIMC
| NODE_DELIMO
))
1256 h
->flags
|= HTML_NOSPACE
;
1257 print_text(h
, descr
->string
);
1258 descr
= descr
->next
;
1259 } while (descr
!= punct
);
1262 /* Trailing punctuation. */
1263 while (punct
!= NULL
) {
1264 h
->flags
|= HTML_NOSPACE
;
1265 print_text(h
, punct
->string
);
1266 punct
= punct
->next
;
1272 mdoc_mt_pre(MDOC_ARGS
)
1277 for (n
= n
->child
; n
; n
= n
->next
) {
1278 assert(n
->type
== ROFFT_TEXT
);
1279 mandoc_asprintf(&cp
, "mailto:%s", n
->string
);
1280 t
= print_otag(h
, TAG_A
, "ch", "Mt", cp
);
1281 print_text(h
, n
->string
);
1289 mdoc_fo_pre(MDOC_ARGS
)
1298 if (n
->child
!= NULL
) {
1299 t
= print_otag_id(h
, TAG_CODE
, "Fn", n
);
1300 print_text(h
, n
->child
->string
);
1305 h
->flags
|= HTML_NOSPACE
;
1307 h
->flags
|= HTML_NOSPACE
;
1315 mdoc_fo_post(MDOC_ARGS
)
1317 if (n
->type
!= ROFFT_BODY
)
1319 h
->flags
|= HTML_NOSPACE
;
1321 h
->flags
|= HTML_NOSPACE
;
1326 mdoc_in_pre(MDOC_ARGS
)
1331 print_otag(h
, TAG_CODE
, "c", "In");
1334 * The first argument of the `In' gets special treatment as
1335 * being a linked value. Subsequent values are printed
1336 * afterward. groff does similarly. This also handles the case
1340 if (NODE_SYNPRETTY
& n
->flags
&& NODE_LINE
& n
->flags
)
1341 print_text(h
, "#include");
1344 h
->flags
|= HTML_NOSPACE
;
1346 if (NULL
!= (n
= n
->child
)) {
1347 assert(n
->type
== ROFFT_TEXT
);
1349 if (h
->base_includes
)
1350 t
= print_otag(h
, TAG_A
, "chI", "In", n
->string
);
1352 t
= print_otag(h
, TAG_A
, "c", "In");
1353 print_text(h
, n
->string
);
1359 h
->flags
|= HTML_NOSPACE
;
1362 for ( ; n
; n
= n
->next
) {
1363 assert(n
->type
== ROFFT_TEXT
);
1364 print_text(h
, n
->string
);
1370 mdoc_va_pre(MDOC_ARGS
)
1372 print_otag(h
, TAG_VAR
, "c", "Va");
1377 mdoc_ap_pre(MDOC_ARGS
)
1379 h
->flags
|= HTML_NOSPACE
;
1380 print_text(h
, "\\(aq");
1381 h
->flags
|= HTML_NOSPACE
;
1386 mdoc_bf_pre(MDOC_ARGS
)
1392 html_close_paragraph(h
);
1402 if (FONT_Em
== n
->norm
->Bf
.font
)
1404 else if (FONT_Sy
== n
->norm
->Bf
.font
)
1406 else if (FONT_Li
== n
->norm
->Bf
.font
)
1411 /* Cannot use TAG_SPAN because it may contain blocks. */
1412 print_otag(h
, TAG_DIV
, "c", cattr
);
1417 mdoc_igndelim_pre(MDOC_ARGS
)
1419 h
->flags
|= HTML_IGNDELIM
;
1424 mdoc_pf_post(MDOC_ARGS
)
1426 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1427 h
->flags
|= HTML_NOSPACE
;
1431 mdoc_rs_pre(MDOC_ARGS
)
1435 if (n
->sec
== SEC_SEE_ALSO
)
1436 html_close_paragraph(h
);
1441 if (n
->sec
== SEC_SEE_ALSO
)
1442 print_otag(h
, TAG_P
, "c", "Pp");
1443 print_otag(h
, TAG_CITE
, "c", "Rs");
1452 mdoc_no_pre(MDOC_ARGS
)
1454 print_otag_id(h
, TAG_SPAN
, roff_name
[n
->tok
], n
);
1459 mdoc_sy_pre(MDOC_ARGS
)
1461 print_otag_id(h
, TAG_B
, "Sy", n
);
1466 mdoc_lb_pre(MDOC_ARGS
)
1468 if (n
->sec
== SEC_LIBRARY
&&
1469 n
->flags
& NODE_LINE
&&
1470 roff_node_prev(n
) != NULL
)
1471 print_otag(h
, TAG_BR
, "");
1473 print_otag(h
, TAG_SPAN
, "c", "Lb");
1478 mdoc__x_pre(MDOC_ARGS
)
1480 struct roff_node
*nn
;
1489 if ((nn
= roff_node_prev(n
)) != NULL
&& nn
->tok
== MDOC__A
&&
1490 ((nn
= roff_node_next(n
)) == NULL
|| nn
->tok
!= MDOC__A
))
1491 print_text(h
, "and");
1530 print_otag(h
, TAG_A
, "ch", "RsU", n
->child
->string
);
1539 print_otag(h
, t
, "c", cattr
);
1544 mdoc__x_post(MDOC_ARGS
)
1546 struct roff_node
*nn
;
1548 if (n
->tok
== MDOC__A
&&
1549 (nn
= roff_node_next(n
)) != NULL
&& nn
->tok
== MDOC__A
&&
1550 ((nn
= roff_node_next(nn
)) == NULL
|| nn
->tok
!= MDOC__A
) &&
1551 ((nn
= roff_node_prev(n
)) == NULL
|| nn
->tok
!= MDOC__A
))
1556 if (n
->parent
== NULL
|| n
->parent
->tok
!= MDOC_Rs
)
1559 h
->flags
|= HTML_NOSPACE
;
1560 print_text(h
, roff_node_next(n
) ? "," : ".");
1564 mdoc_bk_pre(MDOC_ARGS
)
1573 if (n
->parent
->args
!= NULL
|| n
->prev
->child
== NULL
)
1574 h
->flags
|= HTML_PREKEEP
;
1584 mdoc_bk_post(MDOC_ARGS
)
1587 if (n
->type
== ROFFT_BODY
)
1588 h
->flags
&= ~(HTML_KEEP
| HTML_PREKEEP
);
1592 mdoc_quote_pre(MDOC_ARGS
)
1594 if (n
->type
!= ROFFT_BODY
)
1600 print_text(h
, n
->child
!= NULL
&& n
->child
->next
== NULL
&&
1601 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
1605 print_text(h
, "\\(lC");
1609 print_text(h
, "\\(lB");
1613 print_text(h
, "\\(lB");
1615 * Give up on semantic markup for now.
1616 * We cannot use TAG_SPAN because .Oo may contain blocks.
1617 * We cannot use TAG_DIV because we might be in a
1618 * phrasing context (like .Dl or .Pp); we cannot
1619 * close out a .Pp at this point either because
1620 * that would break the line.
1622 /* XXX print_otag(h, TAG_???, "c", "Op"); */
1625 if (NULL
== n
->norm
->Es
||
1626 NULL
== n
->norm
->Es
->child
)
1628 print_text(h
, n
->norm
->Es
->child
->string
);
1632 print_text(h
, "\\(lq");
1636 print_text(h
, "\"");
1643 print_text(h
, "\\(oq");
1644 h
->flags
|= HTML_NOSPACE
;
1645 print_otag(h
, TAG_CODE
, "c", "Li");
1649 print_text(h
, "\\(oq");
1655 h
->flags
|= HTML_NOSPACE
;
1660 mdoc_quote_post(MDOC_ARGS
)
1663 if (n
->type
!= ROFFT_BODY
&& n
->type
!= ROFFT_ELEM
)
1666 h
->flags
|= HTML_NOSPACE
;
1671 print_text(h
, n
->child
!= NULL
&& n
->child
->next
== NULL
&&
1672 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
1676 print_text(h
, "\\(rC");
1682 print_text(h
, "\\(rB");
1685 if (n
->norm
->Es
== NULL
||
1686 n
->norm
->Es
->child
== NULL
||
1687 n
->norm
->Es
->child
->next
== NULL
)
1688 h
->flags
&= ~HTML_NOSPACE
;
1690 print_text(h
, n
->norm
->Es
->child
->next
->string
);
1694 print_text(h
, "\\(rq");
1698 print_text(h
, "\"");
1707 print_text(h
, "\\(cq");
1715 mdoc_eo_pre(MDOC_ARGS
)
1718 if (n
->type
!= ROFFT_BODY
)
1721 if (n
->end
== ENDBODY_NOT
&&
1722 n
->parent
->head
->child
== NULL
&&
1724 n
->child
->end
!= ENDBODY_NOT
)
1725 print_text(h
, "\\&");
1726 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1727 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1728 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1729 h
->flags
|= HTML_NOSPACE
;
1734 mdoc_eo_post(MDOC_ARGS
)
1738 if (n
->type
!= ROFFT_BODY
)
1741 if (n
->end
!= ENDBODY_NOT
) {
1742 h
->flags
&= ~HTML_NOSPACE
;
1746 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1747 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1750 h
->flags
|= HTML_NOSPACE
;
1752 h
->flags
&= ~HTML_NOSPACE
;
1756 mdoc_abort_pre(MDOC_ARGS
)