1 /* $Id: mdoc_html.c,v 1.342 2021/03/30 19:26:20 schwarze Exp $ */
3 * Copyright (c) 2014-2021 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 if (n->norm->Bd.type == DISP_literal)
971 (void)strlcat(buf, " Li", sizeof(buf));
973 print_otag_id(h, TAG_DIV, buf, n);
978 mdoc_pa_pre(MDOC_ARGS)
980 print_otag(h, TAG_SPAN, "c", "Pa");
985 mdoc_ad_pre(MDOC_ARGS)
987 print_otag(h, TAG_SPAN, "c", "Ad");
992 mdoc_an_pre(MDOC_ARGS)
994 if (n->norm->An.auth == AUTH_split) {
995 h->flags &= ~HTML_NOSPLIT;
996 h->flags |= HTML_SPLIT;
999 if (n->norm->An.auth == AUTH_nosplit) {
1000 h->flags &= ~HTML_SPLIT;
1001 h->flags |= HTML_NOSPLIT;
1005 if (h->flags & HTML_SPLIT)
1006 print_otag(h, TAG_BR, "");
1008 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1009 h->flags |= HTML_SPLIT;
1011 print_otag(h, TAG_SPAN, "c", "An");
1016 mdoc_cd_pre(MDOC_ARGS)
1019 print_otag(h, TAG_CODE, "c", "Cd");
1024 mdoc_fa_pre(MDOC_ARGS)
1026 const struct roff_node *nn;
1029 if (n->parent->tok != MDOC_Fo) {
1030 print_otag(h, TAG_VAR, "c", "Fa");
1033 for (nn = n->child; nn != NULL; nn = nn->next) {
1034 t = print_otag(h, TAG_VAR, "c", "Fa");
1035 print_text(h, nn->string);
1037 if (nn->next != NULL) {
1038 h->flags |= HTML_NOSPACE;
1042 if (n->child != NULL &&
1043 (nn = roff_node_next(n)) != NULL &&
1044 nn->tok == MDOC_Fa) {
1045 h->flags |= HTML_NOSPACE;
1052 mdoc_fd_pre(MDOC_ARGS)
1059 if (NULL == (n = n->child))
1062 assert(n->type == ROFFT_TEXT);
1064 if (strcmp(n->string, "#include")) {
1065 print_otag(h, TAG_CODE, "c", "Fd");
1069 print_otag(h, TAG_CODE, "c", "In");
1070 print_text(h, n->string);
1072 if (NULL != (n = n->next)) {
1073 assert(n->type == ROFFT_TEXT);
1075 if (h->base_includes) {
1077 if (*cp == '<' || *cp == '"')
1079 buf = mandoc_strdup(cp);
1080 cp = strchr(buf, '\0') - 1;
1081 if (cp >= buf && (*cp == '>' || *cp == '"'))
1083 t = print_otag(h, TAG_A, "chI", "In", buf);
1086 t = print_otag(h, TAG_A, "c", "In");
1088 print_text(h, n->string);
1094 for ( ; n; n = n->next) {
1095 assert(n->type == ROFFT_TEXT);
1096 print_text(h, n->string);
1103 mdoc_vt_pre(MDOC_ARGS)
1105 if (n->type == ROFFT_BLOCK) {
1108 } else if (n->type == ROFFT_ELEM) {
1110 } else if (n->type == ROFFT_HEAD)
1113 print_otag(h, TAG_VAR, "c", "Vt");
1118 mdoc_ft_pre(MDOC_ARGS)
1121 print_otag(h, TAG_VAR, "c", "Ft");
1126 mdoc_fn_pre(MDOC_ARGS)
1130 const char *sp, *ep;
1133 pretty = NODE_SYNPRETTY & n->flags;
1136 /* Split apart into type and name. */
1137 assert(n->child->string);
1138 sp = n->child->string;
1140 ep = strchr(sp, ' ');
1142 t = print_otag(h, TAG_VAR, "c", "Ft");
1145 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1146 (void)memcpy(nbuf, sp, (size_t)sz);
1148 print_text(h, nbuf);
1150 ep = strchr(sp, ' ');
1155 t = print_otag_id(h, TAG_CODE, "Fn", n);
1162 h->flags |= HTML_NOSPACE;
1164 h->flags |= HTML_NOSPACE;
1166 for (n = n->child->next; n; n = n->next) {
1167 if (NODE_SYNPRETTY & n->flags)
1168 t = print_otag(h, TAG_VAR, "cs", "Fa",
1169 "white-space", "nowrap");
1171 t = print_otag(h, TAG_VAR, "c", "Fa");
1172 print_text(h, n->string);
1175 h->flags |= HTML_NOSPACE;
1180 h->flags |= HTML_NOSPACE;
1184 h->flags |= HTML_NOSPACE;
1192 mdoc_sm_pre(MDOC_ARGS)
1195 if (NULL == n->child)
1196 h->flags ^= HTML_NONOSPACE;
1197 else if (0 == strcmp("on", n->child->string))
1198 h->flags &= ~HTML_NONOSPACE;
1200 h->flags |= HTML_NONOSPACE;
1202 if ( ! (HTML_NONOSPACE & h->flags))
1203 h->flags &= ~HTML_NOSPACE;
1209 mdoc_skip_pre(MDOC_ARGS)
1216 mdoc_pp_pre(MDOC_ARGS)
1220 if (n->flags & NODE_NOFILL) {
1222 if (n->flags & NODE_ID)
1223 mdoc_tg_pre(meta, n, h);
1229 html_close_paragraph(h);
1230 id = n->flags & NODE_ID ? html_make_id(n, 1) : NULL;
1231 print_otag(h, TAG_P, "ci", "Pp", id);
1238 mdoc_lk_pre(MDOC_ARGS)
1240 const struct roff_node *link, *descr, *punct;
1243 if ((link = n->child) == NULL)
1246 /* Find beginning of trailing punctuation. */
1248 while (punct != link && punct->flags & NODE_DELIMC)
1249 punct = punct->prev;
1250 punct = punct->next;
1252 /* Link target and link text. */
1255 descr = link; /* no text */
1256 t = print_otag(h, TAG_A, "ch", "Lk", link->string);
1258 if (descr->flags & (NODE_DELIMC | NODE_DELIMO))
1259 h->flags |= HTML_NOSPACE;
1260 print_text(h, descr->string);
1261 descr = descr->next;
1262 } while (descr != punct);
1265 /* Trailing punctuation. */
1266 while (punct != NULL) {
1267 h->flags |= HTML_NOSPACE;
1268 print_text(h, punct->string);
1269 punct = punct->next;
1275 mdoc_mt_pre(MDOC_ARGS)
1280 for (n = n->child; n; n = n->next) {
1281 assert(n->type == ROFFT_TEXT);
1282 mandoc_asprintf(&cp, "mailto:%s", n->string);
1283 t = print_otag(h, TAG_A, "ch", "Mt", cp);
1284 print_text(h, n->string);
1292 mdoc_fo_pre(MDOC_ARGS)
1301 if (n->child != NULL) {
1302 t = print_otag_id(h, TAG_CODE, "Fn", n);
1303 print_text(h, n->child->string);
1308 h->flags |= HTML_NOSPACE;
1310 h->flags |= HTML_NOSPACE;
1318 mdoc_fo_post(MDOC_ARGS)
1320 if (n->type != ROFFT_BODY)
1322 h->flags |= HTML_NOSPACE;
1324 h->flags |= HTML_NOSPACE;
1329 mdoc_in_pre(MDOC_ARGS)
1334 print_otag(h, TAG_CODE, "c", "In");
1337 * The first argument of the `In' gets special treatment as
1338 * being a linked value. Subsequent values are printed
1339 * afterward. groff does similarly. This also handles the case
1343 if (NODE_SYNPRETTY & n->flags && NODE_LINE & n->flags)
1344 print_text(h, "#include");
1347 h->flags |= HTML_NOSPACE;
1349 if (NULL != (n = n->child)) {
1350 assert(n->type == ROFFT_TEXT);
1352 if (h->base_includes)
1353 t = print_otag(h, TAG_A, "chI", "In", n->string);
1355 t = print_otag(h, TAG_A, "c", "In");
1356 print_text(h, n->string);
1362 h->flags |= HTML_NOSPACE;
1365 for ( ; n; n = n->next) {
1366 assert(n->type == ROFFT_TEXT);
1367 print_text(h, n->string);
1373 mdoc_va_pre(MDOC_ARGS)
1375 print_otag(h, TAG_VAR, "c", "Va");
1380 mdoc_ap_pre(MDOC_ARGS)
1382 h->flags |= HTML_NOSPACE;
1383 print_text(h, "\\(aq");
1384 h->flags |= HTML_NOSPACE;
1389 mdoc_bf_pre(MDOC_ARGS)
1395 html_close_paragraph(h);
1405 if (FONT_Em == n->norm->Bf.font)
1407 else if (FONT_Sy == n->norm->Bf.font)
1409 else if (FONT_Li == n->norm->Bf.font)
1414 /* Cannot use TAG_SPAN because it may contain blocks. */
1415 print_otag(h, TAG_DIV, "c", cattr);
1420 mdoc_igndelim_pre(MDOC_ARGS)
1422 h->flags |= HTML_IGNDELIM;
1427 mdoc_pf_post(MDOC_ARGS)
1429 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1430 h->flags |= HTML_NOSPACE;
1434 mdoc_rs_pre(MDOC_ARGS)
1438 if (n->sec == SEC_SEE_ALSO)
1439 html_close_paragraph(h);
1444 if (n->sec == SEC_SEE_ALSO)
1445 print_otag(h, TAG_P, "c", "Pp");
1446 print_otag(h, TAG_CITE, "c", "Rs");
1455 mdoc_no_pre(MDOC_ARGS)
1457 print_otag_id(h, TAG_SPAN, roff_name[n->tok], n);
1462 mdoc_sy_pre(MDOC_ARGS)
1464 print_otag_id(h, TAG_B, "Sy", n);
1469 mdoc_lb_pre(MDOC_ARGS)
1471 if (n->sec == SEC_LIBRARY &&
1472 n->flags & NODE_LINE &&
1473 roff_node_prev(n) != NULL)
1474 print_otag(h, TAG_BR, "");
1476 print_otag(h, TAG_SPAN, "c", "Lb");
1481 mdoc__x_pre(MDOC_ARGS)
1483 struct roff_node *nn;
1492 if ((nn = roff_node_prev(n)) != NULL && nn->tok == MDOC__A &&
1493 ((nn = roff_node_next(n)) == NULL || nn->tok != MDOC__A))
1494 print_text(h, "and");
1533 print_otag(h, TAG_A, "ch", "RsU", n->child->string);
1542 print_otag(h, t, "c", cattr);
1547 mdoc__x_post(MDOC_ARGS)
1549 struct roff_node *nn;
1551 if (n->tok == MDOC__A &&
1552 (nn = roff_node_next(n)) != NULL && nn->tok == MDOC__A &&
1553 ((nn = roff_node_next(nn)) == NULL || nn->tok != MDOC__A) &&
1554 ((nn = roff_node_prev(n)) == NULL || nn->tok != MDOC__A))
1559 if (n->parent == NULL || n->parent->tok != MDOC_Rs)
1562 h->flags |= HTML_NOSPACE;
1563 print_text(h, roff_node_next(n) ? "," : ".");
1567 mdoc_bk_pre(MDOC_ARGS)
1576 if (n->parent->args != NULL || n->prev->child == NULL)
1577 h->flags |= HTML_PREKEEP;
1587 mdoc_bk_post(MDOC_ARGS)
1590 if (n->type == ROFFT_BODY)
1591 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
1595 mdoc_quote_pre(MDOC_ARGS)
1597 if (n->type != ROFFT_BODY)
1603 print_text(h, n->child != NULL && n->child->next == NULL &&
1604 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1608 print_text(h, "\\(lC");
1612 print_text(h, "\\(lB");
1616 print_text(h, "\\(lB");
1618 * Give up on semantic markup for now.
1619 * We cannot use TAG_SPAN because .Oo may contain blocks.
1620 * We cannot use TAG_DIV because we might be in a
1621 * phrasing context (like .Dl or .Pp); we cannot
1622 * close out a .Pp at this point either because
1623 * that would break the line.
1625 /* XXX print_otag(h, TAG_???, "c", "Op"); */
1628 if (NULL == n->norm->Es ||
1629 NULL == n->norm->Es->child)
1631 print_text(h, n->norm->Es->child->string);
1635 print_text(h, "\\(lq");
1639 print_text(h, "\"");
1646 print_text(h, "\\(oq");
1647 h->flags |= HTML_NOSPACE;
1648 print_otag(h, TAG_CODE, "c", "Li");
1652 print_text(h, "\\(oq");
1658 h->flags |= HTML_NOSPACE;
1663 mdoc_quote_post(MDOC_ARGS)
1666 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1669 h->flags |= HTML_NOSPACE;
1674 print_text(h, n->child != NULL && n->child->next == NULL &&
1675 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1679 print_text(h, "\\(rC");
1685 print_text(h, "\\(rB");
1688 if (n->norm->Es == NULL ||
1689 n->norm->Es->child == NULL ||
1690 n->norm->Es->child->next == NULL)
1691 h->flags &= ~HTML_NOSPACE;
1693 print_text(h, n->norm->Es->child->next->string);
1697 print_text(h, "\\(rq");
1701 print_text(h, "\"");
1710 print_text(h, "\\(cq");
1718 mdoc_eo_pre(MDOC_ARGS)
1721 if (n->type != ROFFT_BODY)
1724 if (n->end == ENDBODY_NOT &&
1725 n->parent->head->child == NULL &&
1727 n->child->end != ENDBODY_NOT)
1728 print_text(h, "\\&");
1729 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1730 n->parent->head->child != NULL && (n->child != NULL ||
1731 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1732 h->flags |= HTML_NOSPACE;
1737 mdoc_eo_post(MDOC_ARGS)
1741 if (n->type != ROFFT_BODY)
1744 if (n->end != ENDBODY_NOT) {
1745 h->flags &= ~HTML_NOSPACE;
1749 body = n->child != NULL || n->parent->head->child != NULL;
1750 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1753 h->flags |= HTML_NOSPACE;
1755 h->flags &= ~HTML_NOSPACE;
1759 mdoc_abort_pre(MDOC_ARGS)