mandoc(1): Update to 1.9.15.
[dragonfly.git] / usr.bin / mandoc / mdoc_html.c
blob3d862fed597fb66e9d87b8fb3e94fb4dbcf67c42
1 /* $Id: mdoc_html.c,v 1.54 2010/01/30 08:42:20 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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 AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include "out.h"
27 #include "html.h"
28 #include "mdoc.h"
29 #include "main.h"
31 #define INDENT 5
32 #define HALFINDENT 3
34 #define MDOC_ARGS const struct mdoc_meta *m, \
35 const struct mdoc_node *n, \
36 struct html *h
38 #ifndef MIN
39 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
40 #endif
42 struct htmlmdoc {
43 int (*pre)(MDOC_ARGS);
44 void (*post)(MDOC_ARGS);
47 static void print_mdoc(MDOC_ARGS);
48 static void print_mdoc_head(MDOC_ARGS);
49 static void print_mdoc_node(MDOC_ARGS);
50 static void print_mdoc_nodelist(MDOC_ARGS);
52 static void a2width(const char *, struct roffsu *);
53 static void a2offs(const char *, struct roffsu *);
55 static int a2list(const struct mdoc_node *);
57 static void mdoc_root_post(MDOC_ARGS);
58 static int mdoc_root_pre(MDOC_ARGS);
60 static void mdoc__x_post(MDOC_ARGS);
61 static int mdoc__x_pre(MDOC_ARGS);
62 static int mdoc_ad_pre(MDOC_ARGS);
63 static int mdoc_an_pre(MDOC_ARGS);
64 static int mdoc_ap_pre(MDOC_ARGS);
65 static void mdoc_aq_post(MDOC_ARGS);
66 static int mdoc_aq_pre(MDOC_ARGS);
67 static int mdoc_ar_pre(MDOC_ARGS);
68 static int mdoc_bd_pre(MDOC_ARGS);
69 static int mdoc_bf_pre(MDOC_ARGS);
70 static void mdoc_bl_post(MDOC_ARGS);
71 static int mdoc_bl_pre(MDOC_ARGS);
72 static void mdoc_bq_post(MDOC_ARGS);
73 static int mdoc_bq_pre(MDOC_ARGS);
74 static void mdoc_brq_post(MDOC_ARGS);
75 static int mdoc_brq_pre(MDOC_ARGS);
76 static int mdoc_bt_pre(MDOC_ARGS);
77 static int mdoc_bx_pre(MDOC_ARGS);
78 static int mdoc_cd_pre(MDOC_ARGS);
79 static int mdoc_d1_pre(MDOC_ARGS);
80 static void mdoc_dq_post(MDOC_ARGS);
81 static int mdoc_dq_pre(MDOC_ARGS);
82 static int mdoc_dv_pre(MDOC_ARGS);
83 static int mdoc_fa_pre(MDOC_ARGS);
84 static int mdoc_fd_pre(MDOC_ARGS);
85 static int mdoc_fl_pre(MDOC_ARGS);
86 static int mdoc_fn_pre(MDOC_ARGS);
87 static int mdoc_ft_pre(MDOC_ARGS);
88 static int mdoc_em_pre(MDOC_ARGS);
89 static int mdoc_er_pre(MDOC_ARGS);
90 static int mdoc_ev_pre(MDOC_ARGS);
91 static int mdoc_ex_pre(MDOC_ARGS);
92 static void mdoc_fo_post(MDOC_ARGS);
93 static int mdoc_fo_pre(MDOC_ARGS);
94 static int mdoc_ic_pre(MDOC_ARGS);
95 static int mdoc_in_pre(MDOC_ARGS);
96 static int mdoc_it_block_pre(MDOC_ARGS, int, int,
97 struct roffsu *, struct roffsu *);
98 static int mdoc_it_head_pre(MDOC_ARGS, int,
99 struct roffsu *);
100 static int mdoc_it_body_pre(MDOC_ARGS, int);
101 static int mdoc_it_pre(MDOC_ARGS);
102 static int mdoc_lb_pre(MDOC_ARGS);
103 static int mdoc_li_pre(MDOC_ARGS);
104 static int mdoc_lk_pre(MDOC_ARGS);
105 static int mdoc_mt_pre(MDOC_ARGS);
106 static int mdoc_ms_pre(MDOC_ARGS);
107 static int mdoc_nd_pre(MDOC_ARGS);
108 static int mdoc_nm_pre(MDOC_ARGS);
109 static int mdoc_ns_pre(MDOC_ARGS);
110 static void mdoc_op_post(MDOC_ARGS);
111 static int mdoc_op_pre(MDOC_ARGS);
112 static int mdoc_pa_pre(MDOC_ARGS);
113 static void mdoc_pf_post(MDOC_ARGS);
114 static int mdoc_pf_pre(MDOC_ARGS);
115 static void mdoc_pq_post(MDOC_ARGS);
116 static int mdoc_pq_pre(MDOC_ARGS);
117 static int mdoc_rs_pre(MDOC_ARGS);
118 static int mdoc_rv_pre(MDOC_ARGS);
119 static int mdoc_sh_pre(MDOC_ARGS);
120 static int mdoc_sp_pre(MDOC_ARGS);
121 static void mdoc_sq_post(MDOC_ARGS);
122 static int mdoc_sq_pre(MDOC_ARGS);
123 static int mdoc_ss_pre(MDOC_ARGS);
124 static int mdoc_sx_pre(MDOC_ARGS);
125 static int mdoc_sy_pre(MDOC_ARGS);
126 static int mdoc_ud_pre(MDOC_ARGS);
127 static int mdoc_va_pre(MDOC_ARGS);
128 static int mdoc_vt_pre(MDOC_ARGS);
129 static int mdoc_xr_pre(MDOC_ARGS);
130 static int mdoc_xx_pre(MDOC_ARGS);
132 static const struct htmlmdoc mdocs[MDOC_MAX] = {
133 {mdoc_ap_pre, NULL}, /* Ap */
134 {NULL, NULL}, /* Dd */
135 {NULL, NULL}, /* Dt */
136 {NULL, NULL}, /* Os */
137 {mdoc_sh_pre, NULL }, /* Sh */
138 {mdoc_ss_pre, NULL }, /* Ss */
139 {mdoc_sp_pre, NULL}, /* Pp */
140 {mdoc_d1_pre, NULL}, /* D1 */
141 {mdoc_d1_pre, NULL}, /* Dl */
142 {mdoc_bd_pre, NULL}, /* Bd */
143 {NULL, NULL}, /* Ed */
144 {mdoc_bl_pre, mdoc_bl_post}, /* Bl */
145 {NULL, NULL}, /* El */
146 {mdoc_it_pre, NULL}, /* It */
147 {mdoc_ad_pre, NULL}, /* Ad */
148 {mdoc_an_pre, NULL}, /* An */
149 {mdoc_ar_pre, NULL}, /* Ar */
150 {mdoc_cd_pre, NULL}, /* Cd */
151 {mdoc_fl_pre, NULL}, /* Cm */
152 {mdoc_dv_pre, NULL}, /* Dv */
153 {mdoc_er_pre, NULL}, /* Er */
154 {mdoc_ev_pre, NULL}, /* Ev */
155 {mdoc_ex_pre, NULL}, /* Ex */
156 {mdoc_fa_pre, NULL}, /* Fa */
157 {mdoc_fd_pre, NULL}, /* Fd */
158 {mdoc_fl_pre, NULL}, /* Fl */
159 {mdoc_fn_pre, NULL}, /* Fn */
160 {mdoc_ft_pre, NULL}, /* Ft */
161 {mdoc_ic_pre, NULL}, /* Ic */
162 {mdoc_in_pre, NULL}, /* In */
163 {mdoc_li_pre, NULL}, /* Li */
164 {mdoc_nd_pre, NULL}, /* Nd */
165 {mdoc_nm_pre, NULL}, /* Nm */
166 {mdoc_op_pre, mdoc_op_post}, /* Op */
167 {NULL, NULL}, /* Ot */
168 {mdoc_pa_pre, NULL}, /* Pa */
169 {mdoc_rv_pre, NULL}, /* Rv */
170 {NULL, NULL}, /* St */
171 {mdoc_va_pre, NULL}, /* Va */
172 {mdoc_vt_pre, NULL}, /* Vt */
173 {mdoc_xr_pre, NULL}, /* Xr */
174 {mdoc__x_pre, mdoc__x_post}, /* %A */
175 {mdoc__x_pre, mdoc__x_post}, /* %B */
176 {mdoc__x_pre, mdoc__x_post}, /* %D */
177 {mdoc__x_pre, mdoc__x_post}, /* %I */
178 {mdoc__x_pre, mdoc__x_post}, /* %J */
179 {mdoc__x_pre, mdoc__x_post}, /* %N */
180 {mdoc__x_pre, mdoc__x_post}, /* %O */
181 {mdoc__x_pre, mdoc__x_post}, /* %P */
182 {mdoc__x_pre, mdoc__x_post}, /* %R */
183 {mdoc__x_pre, mdoc__x_post}, /* %T */
184 {mdoc__x_pre, mdoc__x_post}, /* %V */
185 {NULL, NULL}, /* Ac */
186 {mdoc_aq_pre, mdoc_aq_post}, /* Ao */
187 {mdoc_aq_pre, mdoc_aq_post}, /* Aq */
188 {NULL, NULL}, /* At */
189 {NULL, NULL}, /* Bc */
190 {mdoc_bf_pre, NULL}, /* Bf */
191 {mdoc_bq_pre, mdoc_bq_post}, /* Bo */
192 {mdoc_bq_pre, mdoc_bq_post}, /* Bq */
193 {mdoc_xx_pre, NULL}, /* Bsx */
194 {mdoc_bx_pre, NULL}, /* Bx */
195 {NULL, NULL}, /* Db */
196 {NULL, NULL}, /* Dc */
197 {mdoc_dq_pre, mdoc_dq_post}, /* Do */
198 {mdoc_dq_pre, mdoc_dq_post}, /* Dq */
199 {NULL, NULL}, /* Ec */
200 {NULL, NULL}, /* Ef */
201 {mdoc_em_pre, NULL}, /* Em */
202 {NULL, NULL}, /* Eo */
203 {mdoc_xx_pre, NULL}, /* Fx */
204 {mdoc_ms_pre, NULL}, /* Ms */ /* FIXME: convert to symbol? */
205 {NULL, NULL}, /* No */
206 {mdoc_ns_pre, NULL}, /* Ns */
207 {mdoc_xx_pre, NULL}, /* Nx */
208 {mdoc_xx_pre, NULL}, /* Ox */
209 {NULL, NULL}, /* Pc */
210 {mdoc_pf_pre, mdoc_pf_post}, /* Pf */
211 {mdoc_pq_pre, mdoc_pq_post}, /* Po */
212 {mdoc_pq_pre, mdoc_pq_post}, /* Pq */
213 {NULL, NULL}, /* Qc */
214 {mdoc_sq_pre, mdoc_sq_post}, /* Ql */
215 {mdoc_dq_pre, mdoc_dq_post}, /* Qo */
216 {mdoc_dq_pre, mdoc_dq_post}, /* Qq */
217 {NULL, NULL}, /* Re */
218 {mdoc_rs_pre, NULL}, /* Rs */
219 {NULL, NULL}, /* Sc */
220 {mdoc_sq_pre, mdoc_sq_post}, /* So */
221 {mdoc_sq_pre, mdoc_sq_post}, /* Sq */
222 {NULL, NULL}, /* Sm */ /* FIXME - no idea. */
223 {mdoc_sx_pre, NULL}, /* Sx */
224 {mdoc_sy_pre, NULL}, /* Sy */
225 {NULL, NULL}, /* Tn */
226 {mdoc_xx_pre, NULL}, /* Ux */
227 {NULL, NULL}, /* Xc */
228 {NULL, NULL}, /* Xo */
229 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
230 {NULL, NULL}, /* Fc */
231 {mdoc_op_pre, mdoc_op_post}, /* Oo */
232 {NULL, NULL}, /* Oc */
233 {NULL, NULL}, /* Bk */
234 {NULL, NULL}, /* Ek */
235 {mdoc_bt_pre, NULL}, /* Bt */
236 {NULL, NULL}, /* Hf */
237 {NULL, NULL}, /* Fr */
238 {mdoc_ud_pre, NULL}, /* Ud */
239 {mdoc_lb_pre, NULL}, /* Lb */
240 {mdoc_sp_pre, NULL}, /* Lp */
241 {mdoc_lk_pre, NULL}, /* Lk */
242 {mdoc_mt_pre, NULL}, /* Mt */
243 {mdoc_brq_pre, mdoc_brq_post}, /* Brq */
244 {mdoc_brq_pre, mdoc_brq_post}, /* Bro */
245 {NULL, NULL}, /* Brc */
246 {mdoc__x_pre, mdoc__x_post}, /* %C */
247 {NULL, NULL}, /* Es */ /* TODO */
248 {NULL, NULL}, /* En */ /* TODO */
249 {mdoc_xx_pre, NULL}, /* Dx */
250 {mdoc__x_pre, mdoc__x_post}, /* %Q */
251 {mdoc_sp_pre, NULL}, /* br */
252 {mdoc_sp_pre, NULL}, /* sp */
253 {mdoc__x_pre, mdoc__x_post}, /* %U */
257 void
258 html_mdoc(void *arg, const struct mdoc *m)
260 struct html *h;
261 struct tag *t;
263 h = (struct html *)arg;
265 print_gen_decls(h);
266 t = print_otag(h, TAG_HTML, 0, NULL);
267 print_mdoc(mdoc_meta(m), mdoc_node(m), h);
268 print_tagq(h, t);
270 printf("\n");
275 * Return the list type for `Bl', e.g., `Bl -column' returns
276 * MDOC_Column. This can ONLY be run for lists; it will abort() if no
277 * list type is found.
279 static int
280 a2list(const struct mdoc_node *n)
282 int i;
284 assert(n->args);
285 for (i = 0; i < (int)n->args->argc; i++)
286 switch (n->args->argv[i].arg) {
287 case (MDOC_Enum):
288 /* FALLTHROUGH */
289 case (MDOC_Dash):
290 /* FALLTHROUGH */
291 case (MDOC_Hyphen):
292 /* FALLTHROUGH */
293 case (MDOC_Bullet):
294 /* FALLTHROUGH */
295 case (MDOC_Tag):
296 /* FALLTHROUGH */
297 case (MDOC_Hang):
298 /* FALLTHROUGH */
299 case (MDOC_Inset):
300 /* FALLTHROUGH */
301 case (MDOC_Diag):
302 /* FALLTHROUGH */
303 case (MDOC_Item):
304 /* FALLTHROUGH */
305 case (MDOC_Column):
306 /* FALLTHROUGH */
307 case (MDOC_Ohang):
308 return(n->args->argv[i].arg);
309 default:
310 break;
313 abort();
314 /* NOTREACHED */
319 * Calculate the scaling unit passed in a `-width' argument. This uses
320 * either a native scaling unit (e.g., 1i, 2m) or the string length of
321 * the value.
323 static void
324 a2width(const char *p, struct roffsu *su)
327 if ( ! a2roffsu(p, su, SCALE_MAX)) {
328 su->unit = SCALE_EM;
329 su->scale = (int)strlen(p);
335 * Calculate the scaling unit passed in an `-offset' argument. This
336 * uses either a native scaling unit (e.g., 1i, 2m), one of a set of
337 * predefined strings (indent, etc.), or the string length of the value.
339 static void
340 a2offs(const char *p, struct roffsu *su)
343 /* FIXME: "right"? */
345 if (0 == strcmp(p, "left"))
346 SCALE_HS_INIT(su, 0);
347 else if (0 == strcmp(p, "indent"))
348 SCALE_HS_INIT(su, INDENT);
349 else if (0 == strcmp(p, "indent-two"))
350 SCALE_HS_INIT(su, INDENT * 2);
351 else if ( ! a2roffsu(p, su, SCALE_MAX)) {
352 su->unit = SCALE_EM;
353 su->scale = (int)strlen(p);
358 static void
359 print_mdoc(MDOC_ARGS)
361 struct tag *t;
362 struct htmlpair tag;
364 t = print_otag(h, TAG_HEAD, 0, NULL);
365 print_mdoc_head(m, n, h);
366 print_tagq(h, t);
368 t = print_otag(h, TAG_BODY, 0, NULL);
370 tag.key = ATTR_CLASS;
371 tag.val = "body";
372 print_otag(h, TAG_DIV, 1, &tag);
374 print_mdoc_nodelist(m, n, h);
375 print_tagq(h, t);
379 /* ARGSUSED */
380 static void
381 print_mdoc_head(MDOC_ARGS)
384 print_gen_head(h);
385 bufinit(h);
386 buffmt(h, "%s(%d)", m->title, m->msec);
388 if (m->arch) {
389 bufcat(h, " (");
390 bufcat(h, m->arch);
391 bufcat(h, ")");
394 print_otag(h, TAG_TITLE, 0, NULL);
395 print_text(h, h->buf);
399 static void
400 print_mdoc_nodelist(MDOC_ARGS)
403 print_mdoc_node(m, n, h);
404 if (n->next)
405 print_mdoc_nodelist(m, n->next, h);
409 static void
410 print_mdoc_node(MDOC_ARGS)
412 int child;
413 struct tag *t;
415 child = 1;
416 t = h->tags.head;
418 bufinit(h);
419 switch (n->type) {
420 case (MDOC_ROOT):
421 child = mdoc_root_pre(m, n, h);
422 break;
423 case (MDOC_TEXT):
424 print_text(h, n->string);
425 return;
426 default:
427 if (mdocs[n->tok].pre)
428 child = (*mdocs[n->tok].pre)(m, n, h);
429 break;
432 if (child && n->child)
433 print_mdoc_nodelist(m, n->child, h);
435 print_stagq(h, t);
437 bufinit(h);
438 switch (n->type) {
439 case (MDOC_ROOT):
440 mdoc_root_post(m, n, h);
441 break;
442 default:
443 if (mdocs[n->tok].post)
444 (*mdocs[n->tok].post)(m, n, h);
445 break;
450 /* ARGSUSED */
451 static void
452 mdoc_root_post(MDOC_ARGS)
454 struct htmlpair tag[3];
455 struct tag *t, *tt;
456 char b[DATESIZ];
458 time2a(m->date, b, DATESIZ);
461 * XXX: this should use divs, but in Firefox, divs with nested
462 * divs for some reason puke when trying to put a border line
463 * below. So I use tables, instead.
466 PAIR_CLASS_INIT(&tag[0], "footer");
467 bufcat_style(h, "width", "100%");
468 PAIR_STYLE_INIT(&tag[1], h);
469 PAIR_SUMMARY_INIT(&tag[2], "footer");
471 t = print_otag(h, TAG_TABLE, 3, tag);
472 tt = print_otag(h, TAG_TR, 0, NULL);
474 bufinit(h);
475 bufcat_style(h, "width", "50%");
476 PAIR_STYLE_INIT(&tag[0], h);
477 print_otag(h, TAG_TD, 1, tag);
478 print_text(h, b);
479 print_stagq(h, tt);
481 bufinit(h);
482 bufcat_style(h, "width", "50%");
483 bufcat_style(h, "text-align", "right");
484 PAIR_STYLE_INIT(&tag[0], h);
485 print_otag(h, TAG_TD, 1, tag);
486 print_text(h, m->os);
487 print_tagq(h, t);
491 /* ARGSUSED */
492 static int
493 mdoc_root_pre(MDOC_ARGS)
495 struct htmlpair tag[3];
496 struct tag *t, *tt;
497 char b[BUFSIZ], title[BUFSIZ];
499 (void)strlcpy(b, m->vol, BUFSIZ);
501 if (m->arch) {
502 (void)strlcat(b, " (", BUFSIZ);
503 (void)strlcat(b, m->arch, BUFSIZ);
504 (void)strlcat(b, ")", BUFSIZ);
507 (void)snprintf(title, BUFSIZ - 1,
508 "%s(%d)", m->title, m->msec);
510 /* XXX: see note in mdoc_root_post() about divs. */
512 PAIR_CLASS_INIT(&tag[0], "header");
513 bufcat_style(h, "width", "100%");
514 PAIR_STYLE_INIT(&tag[1], h);
515 PAIR_SUMMARY_INIT(&tag[2], "header");
517 t = print_otag(h, TAG_TABLE, 3, tag);
519 tt = print_otag(h, TAG_TR, 0, NULL);
521 bufinit(h);
522 bufcat_style(h, "width", "10%");
523 PAIR_STYLE_INIT(&tag[0], h);
524 print_otag(h, TAG_TD, 1, tag);
525 print_text(h, title);
526 print_stagq(h, tt);
528 bufinit(h);
529 bufcat_style(h, "text-align", "center");
530 bufcat_style(h, "white-space", "nowrap");
531 bufcat_style(h, "width", "80%");
532 PAIR_STYLE_INIT(&tag[0], h);
533 print_otag(h, TAG_TD, 1, tag);
534 print_text(h, b);
535 print_stagq(h, tt);
537 bufinit(h);
538 bufcat_style(h, "text-align", "right");
539 bufcat_style(h, "width", "10%");
540 PAIR_STYLE_INIT(&tag[0], h);
541 print_otag(h, TAG_TD, 1, tag);
542 print_text(h, title);
543 print_tagq(h, t);
544 return(1);
548 /* ARGSUSED */
549 static int
550 mdoc_sh_pre(MDOC_ARGS)
552 struct htmlpair tag[2];
553 const struct mdoc_node *nn;
554 char buf[BUFSIZ];
555 struct roffsu su;
557 if (MDOC_BODY == n->type) {
558 SCALE_HS_INIT(&su, INDENT);
559 bufcat_su(h, "margin-left", &su);
560 PAIR_CLASS_INIT(&tag[0], "sec-body");
561 PAIR_STYLE_INIT(&tag[1], h);
562 print_otag(h, TAG_DIV, 2, tag);
563 return(1);
564 } else if (MDOC_BLOCK == n->type) {
565 PAIR_CLASS_INIT(&tag[0], "sec-block");
566 if (n->prev && NULL == n->prev->body->child) {
567 print_otag(h, TAG_DIV, 1, tag);
568 return(1);
571 SCALE_VS_INIT(&su, 1);
572 bufcat_su(h, "margin-top", &su);
573 if (NULL == n->next)
574 bufcat_su(h, "margin-bottom", &su);
576 PAIR_STYLE_INIT(&tag[1], h);
577 print_otag(h, TAG_DIV, 2, tag);
578 return(1);
581 buf[0] = '\0';
582 for (nn = n->child; nn; nn = nn->next) {
583 html_idcat(buf, nn->string, BUFSIZ);
584 if (nn->next)
585 html_idcat(buf, " ", BUFSIZ);
589 * TODO: make sure there are no duplicates, as HTML does not
590 * allow for multiple `id' tags of the same name.
593 PAIR_CLASS_INIT(&tag[0], "sec-head");
594 tag[1].key = ATTR_ID;
595 tag[1].val = buf;
596 print_otag(h, TAG_DIV, 2, tag);
597 return(1);
601 /* ARGSUSED */
602 static int
603 mdoc_ss_pre(MDOC_ARGS)
605 struct htmlpair tag[3];
606 const struct mdoc_node *nn;
607 char buf[BUFSIZ];
608 struct roffsu su;
610 SCALE_VS_INIT(&su, 1);
612 if (MDOC_BODY == n->type) {
613 PAIR_CLASS_INIT(&tag[0], "ssec-body");
614 if (n->parent->next && n->child) {
615 bufcat_su(h, "margin-bottom", &su);
616 PAIR_STYLE_INIT(&tag[1], h);
617 print_otag(h, TAG_DIV, 2, tag);
618 } else
619 print_otag(h, TAG_DIV, 1, tag);
620 return(1);
621 } else if (MDOC_BLOCK == n->type) {
622 PAIR_CLASS_INIT(&tag[0], "ssec-block");
623 if (n->prev) {
624 bufcat_su(h, "margin-top", &su);
625 PAIR_STYLE_INIT(&tag[1], h);
626 print_otag(h, TAG_DIV, 2, tag);
627 } else
628 print_otag(h, TAG_DIV, 1, tag);
629 return(1);
632 /* TODO: see note in mdoc_sh_pre() about duplicates. */
634 buf[0] = '\0';
635 for (nn = n->child; nn; nn = nn->next) {
636 html_idcat(buf, nn->string, BUFSIZ);
637 if (nn->next)
638 html_idcat(buf, " ", BUFSIZ);
641 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
642 su.scale = -su.scale;
643 bufcat_su(h, "margin-left", &su);
645 PAIR_CLASS_INIT(&tag[0], "ssec-head");
646 PAIR_STYLE_INIT(&tag[1], h);
647 tag[2].key = ATTR_ID;
648 tag[2].val = buf;
649 print_otag(h, TAG_DIV, 3, tag);
650 return(1);
654 /* ARGSUSED */
655 static int
656 mdoc_fl_pre(MDOC_ARGS)
658 struct htmlpair tag;
660 PAIR_CLASS_INIT(&tag, "flag");
661 print_otag(h, TAG_SPAN, 1, &tag);
663 /* `Cm' has no leading hyphen. */
665 if (MDOC_Cm == n->tok)
666 return(1);
668 print_text(h, "\\-");
670 /* A blank `Fl' should incur a subsequent space. */
672 if (n->child)
673 h->flags |= HTML_NOSPACE;
675 return(1);
679 /* ARGSUSED */
680 static int
681 mdoc_nd_pre(MDOC_ARGS)
683 struct htmlpair tag;
685 if (MDOC_BODY != n->type)
686 return(1);
688 /* XXX: this tag in theory can contain block elements. */
690 print_text(h, "\\(em");
691 PAIR_CLASS_INIT(&tag, "desc-body");
692 print_otag(h, TAG_SPAN, 1, &tag);
693 return(1);
697 /* ARGSUSED */
698 static int
699 mdoc_op_pre(MDOC_ARGS)
701 struct htmlpair tag;
703 if (MDOC_BODY != n->type)
704 return(1);
706 /* XXX: this tag in theory can contain block elements. */
708 print_text(h, "\\(lB");
709 h->flags |= HTML_NOSPACE;
710 PAIR_CLASS_INIT(&tag, "opt");
711 print_otag(h, TAG_SPAN, 1, &tag);
712 return(1);
716 /* ARGSUSED */
717 static void
718 mdoc_op_post(MDOC_ARGS)
721 if (MDOC_BODY != n->type)
722 return;
723 h->flags |= HTML_NOSPACE;
724 print_text(h, "\\(rB");
728 static int
729 mdoc_nm_pre(MDOC_ARGS)
731 struct htmlpair tag;
733 if (SEC_SYNOPSIS == n->sec && n->prev) {
734 bufcat_style(h, "clear", "both");
735 PAIR_STYLE_INIT(&tag, h);
736 print_otag(h, TAG_BR, 1, &tag);
739 PAIR_CLASS_INIT(&tag, "name");
740 print_otag(h, TAG_SPAN, 1, &tag);
741 if (NULL == n->child)
742 print_text(h, m->name);
744 return(1);
748 /* ARGSUSED */
749 static int
750 mdoc_xr_pre(MDOC_ARGS)
752 struct htmlpair tag[2];
753 const struct mdoc_node *nn;
755 PAIR_CLASS_INIT(&tag[0], "link-man");
757 if (h->base_man) {
758 buffmt_man(h, n->child->string,
759 n->child->next ?
760 n->child->next->string : NULL);
761 tag[1].key = ATTR_HREF;
762 tag[1].val = h->buf;
763 print_otag(h, TAG_A, 2, tag);
764 } else
765 print_otag(h, TAG_A, 1, tag);
767 nn = n->child;
768 print_text(h, nn->string);
770 if (NULL == (nn = nn->next))
771 return(0);
773 h->flags |= HTML_NOSPACE;
774 print_text(h, "(");
775 h->flags |= HTML_NOSPACE;
776 print_text(h, nn->string);
777 h->flags |= HTML_NOSPACE;
778 print_text(h, ")");
779 return(0);
783 /* ARGSUSED */
784 static int
785 mdoc_ns_pre(MDOC_ARGS)
788 h->flags |= HTML_NOSPACE;
789 return(1);
793 /* ARGSUSED */
794 static int
795 mdoc_ar_pre(MDOC_ARGS)
797 struct htmlpair tag;
799 PAIR_CLASS_INIT(&tag, "arg");
800 print_otag(h, TAG_SPAN, 1, &tag);
801 return(1);
805 /* ARGSUSED */
806 static int
807 mdoc_xx_pre(MDOC_ARGS)
809 const char *pp;
810 struct htmlpair tag;
812 switch (n->tok) {
813 case (MDOC_Bsx):
814 pp = "BSDI BSD/OS";
815 break;
816 case (MDOC_Dx):
817 pp = "DragonFly";
818 break;
819 case (MDOC_Fx):
820 pp = "FreeBSD";
821 break;
822 case (MDOC_Nx):
823 pp = "NetBSD";
824 break;
825 case (MDOC_Ox):
826 pp = "OpenBSD";
827 break;
828 case (MDOC_Ux):
829 pp = "UNIX";
830 break;
831 default:
832 return(1);
835 PAIR_CLASS_INIT(&tag, "unix");
836 print_otag(h, TAG_SPAN, 1, &tag);
837 print_text(h, pp);
838 return(1);
842 /* ARGSUSED */
843 static int
844 mdoc_bx_pre(MDOC_ARGS)
846 const struct mdoc_node *nn;
847 struct htmlpair tag;
849 PAIR_CLASS_INIT(&tag, "unix");
850 print_otag(h, TAG_SPAN, 1, &tag);
852 for (nn = n->child; nn; nn = nn->next)
853 print_mdoc_node(m, nn, h);
855 if (n->child)
856 h->flags |= HTML_NOSPACE;
858 print_text(h, "BSD");
859 return(0);
863 /* ARGSUSED */
864 static int
865 mdoc_it_block_pre(MDOC_ARGS, int type, int comp,
866 struct roffsu *offs, struct roffsu *width)
868 struct htmlpair tag;
869 const struct mdoc_node *nn;
870 struct roffsu su;
872 nn = n->parent->parent;
873 assert(nn->args);
875 /* XXX: see notes in mdoc_it_pre(). */
877 if (MDOC_Column == type) {
878 /* Don't width-pad on the left. */
879 SCALE_HS_INIT(width, 0);
880 /* Also disallow non-compact. */
881 comp = 1;
883 if (MDOC_Diag == type)
884 /* Mandate non-compact with empty prior. */
885 if (n->prev && NULL == n->prev->body->child)
886 comp = 1;
888 bufcat_style(h, "clear", "both");
889 if (offs->scale > 0)
890 bufcat_su(h, "margin-left", offs);
891 if (width->scale > 0)
892 bufcat_su(h, "padding-left", width);
894 PAIR_STYLE_INIT(&tag, h);
896 /* Mandate compact following `Ss' and `Sh' starts. */
898 for (nn = n; nn && ! comp; nn = nn->parent) {
899 if (MDOC_BLOCK != nn->type)
900 continue;
901 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
902 comp = 1;
903 if (nn->prev)
904 break;
907 if ( ! comp) {
908 SCALE_VS_INIT(&su, 1);
909 bufcat_su(h, "padding-top", &su);
912 PAIR_STYLE_INIT(&tag, h);
913 print_otag(h, TAG_DIV, 1, &tag);
914 return(1);
918 /* ARGSUSED */
919 static int
920 mdoc_it_body_pre(MDOC_ARGS, int type)
922 struct htmlpair tag;
923 struct roffsu su;
925 switch (type) {
926 case (MDOC_Item):
927 /* FALLTHROUGH */
928 case (MDOC_Ohang):
929 /* FALLTHROUGH */
930 case (MDOC_Column):
931 break;
932 default:
934 * XXX: this tricks CSS into aligning the bodies with
935 * the right-padding in the head.
937 SCALE_HS_INIT(&su, 2);
938 bufcat_su(h, "margin-left", &su);
939 PAIR_STYLE_INIT(&tag, h);
940 print_otag(h, TAG_DIV, 1, &tag);
941 break;
944 return(1);
948 /* ARGSUSED */
949 static int
950 mdoc_it_head_pre(MDOC_ARGS, int type, struct roffsu *width)
952 struct htmlpair tag;
953 struct ord *ord;
954 char nbuf[BUFSIZ];
956 switch (type) {
957 case (MDOC_Item):
958 return(0);
959 case (MDOC_Ohang):
960 print_otag(h, TAG_DIV, 0, &tag);
961 return(1);
962 case (MDOC_Column):
963 bufcat_su(h, "min-width", width);
964 bufcat_style(h, "clear", "none");
965 if (n->next && MDOC_HEAD == n->next->type)
966 bufcat_style(h, "float", "left");
967 PAIR_STYLE_INIT(&tag, h);
968 print_otag(h, TAG_DIV, 1, &tag);
969 break;
970 default:
971 bufcat_su(h, "min-width", width);
972 SCALE_INVERT(width);
973 bufcat_su(h, "margin-left", width);
974 if (n->next && n->next->child)
975 bufcat_style(h, "float", "left");
977 /* XXX: buffer if we run into body. */
978 SCALE_HS_INIT(width, 1);
979 bufcat_su(h, "margin-right", width);
980 PAIR_STYLE_INIT(&tag, h);
981 print_otag(h, TAG_DIV, 1, &tag);
982 break;
985 switch (type) {
986 case (MDOC_Diag):
987 PAIR_CLASS_INIT(&tag, "diag");
988 print_otag(h, TAG_SPAN, 1, &tag);
989 break;
990 case (MDOC_Enum):
991 ord = h->ords.head;
992 assert(ord);
993 nbuf[BUFSIZ - 1] = 0;
994 (void)snprintf(nbuf, BUFSIZ - 1, "%d.", ord->pos++);
995 print_text(h, nbuf);
996 return(0);
997 case (MDOC_Dash):
998 print_text(h, "\\(en");
999 return(0);
1000 case (MDOC_Hyphen):
1001 print_text(h, "\\(hy");
1002 return(0);
1003 case (MDOC_Bullet):
1004 print_text(h, "\\(bu");
1005 return(0);
1006 default:
1007 break;
1010 return(1);
1014 static int
1015 mdoc_it_pre(MDOC_ARGS)
1017 int i, type, wp, comp;
1018 const struct mdoc_node *bl, *nn;
1019 struct roffsu width, offs;
1022 * XXX: be very careful in changing anything, here. Lists in
1023 * mandoc have many peculiarities; furthermore, they don't
1024 * translate well into HTML and require a bit of mangling.
1027 bl = n->parent->parent;
1028 if (MDOC_BLOCK != n->type)
1029 bl = bl->parent;
1031 type = a2list(bl);
1033 /* Set default width and offset. */
1035 SCALE_HS_INIT(&offs, 0);
1037 switch (type) {
1038 case (MDOC_Enum):
1039 /* FALLTHROUGH */
1040 case (MDOC_Dash):
1041 /* FALLTHROUGH */
1042 case (MDOC_Hyphen):
1043 /* FALLTHROUGH */
1044 case (MDOC_Bullet):
1045 SCALE_HS_INIT(&width, 2);
1046 break;
1047 default:
1048 SCALE_HS_INIT(&width, INDENT);
1049 break;
1052 /* Get width, offset, and compact arguments. */
1054 for (wp = -1, comp = i = 0; i < (int)bl->args->argc; i++)
1055 switch (bl->args->argv[i].arg) {
1056 case (MDOC_Column):
1057 wp = i; /* Save for later. */
1058 break;
1059 case (MDOC_Width):
1060 a2width(bl->args->argv[i].value[0], &width);
1061 break;
1062 case (MDOC_Offset):
1063 a2offs(bl->args->argv[i].value[0], &offs);
1064 break;
1065 case (MDOC_Compact):
1066 comp = 1;
1067 break;
1068 default:
1069 break;
1072 /* Override width in some cases. */
1074 switch (type) {
1075 case (MDOC_Ohang):
1076 /* FALLTHROUGH */
1077 case (MDOC_Item):
1078 /* FALLTHROUGH */
1079 case (MDOC_Inset):
1080 /* FALLTHROUGH */
1081 case (MDOC_Diag):
1082 SCALE_HS_INIT(&width, 0);
1083 break;
1084 default:
1085 if (0 == width.scale)
1086 SCALE_HS_INIT(&width, INDENT);
1087 break;
1090 /* Flip to body/block processing. */
1092 if (MDOC_BODY == n->type)
1093 return(mdoc_it_body_pre(m, n, h, type));
1094 if (MDOC_BLOCK == n->type)
1095 return(mdoc_it_block_pre(m, n, h, type, comp,
1096 &offs, &width));
1098 /* Override column widths. */
1100 if (MDOC_Column == type) {
1101 nn = n->parent->child;
1102 for (i = 0; nn && nn != n; nn = nn->next, i++)
1103 /* Counter... */ ;
1104 if (i < (int)bl->args->argv[wp].sz)
1105 a2width(bl->args->argv[wp].value[i], &width);
1108 return(mdoc_it_head_pre(m, n, h, type, &width));
1112 /* ARGSUSED */
1113 static int
1114 mdoc_bl_pre(MDOC_ARGS)
1116 struct ord *ord;
1118 if (MDOC_BLOCK != n->type)
1119 return(1);
1120 if (MDOC_Enum != a2list(n))
1121 return(1);
1123 ord = malloc(sizeof(struct ord));
1124 if (NULL == ord) {
1125 perror(NULL);
1126 exit(EXIT_FAILURE);
1128 ord->cookie = n;
1129 ord->pos = 1;
1130 ord->next = h->ords.head;
1131 h->ords.head = ord;
1132 return(1);
1136 /* ARGSUSED */
1137 static void
1138 mdoc_bl_post(MDOC_ARGS)
1140 struct ord *ord;
1142 if (MDOC_BLOCK != n->type)
1143 return;
1144 if (MDOC_Enum != a2list(n))
1145 return;
1147 ord = h->ords.head;
1148 assert(ord);
1149 h->ords.head = ord->next;
1150 free(ord);
1154 /* ARGSUSED */
1155 static int
1156 mdoc_ex_pre(MDOC_ARGS)
1158 const struct mdoc_node *nn;
1159 struct tag *t;
1160 struct htmlpair tag;
1162 PAIR_CLASS_INIT(&tag, "utility");
1164 print_text(h, "The");
1165 for (nn = n->child; nn; nn = nn->next) {
1166 t = print_otag(h, TAG_SPAN, 1, &tag);
1167 print_text(h, nn->string);
1168 print_tagq(h, t);
1170 h->flags |= HTML_NOSPACE;
1172 if (nn->next && NULL == nn->next->next)
1173 print_text(h, ", and");
1174 else if (nn->next)
1175 print_text(h, ",");
1176 else
1177 h->flags &= ~HTML_NOSPACE;
1180 if (n->child->next)
1181 print_text(h, "utilities exit");
1182 else
1183 print_text(h, "utility exits");
1185 print_text(h, "0 on success, and >0 if an error occurs.");
1186 return(0);
1190 /* ARGSUSED */
1191 static int
1192 mdoc_dq_pre(MDOC_ARGS)
1195 if (MDOC_BODY != n->type)
1196 return(1);
1197 print_text(h, "\\(lq");
1198 h->flags |= HTML_NOSPACE;
1199 return(1);
1203 /* ARGSUSED */
1204 static void
1205 mdoc_dq_post(MDOC_ARGS)
1208 if (MDOC_BODY != n->type)
1209 return;
1210 h->flags |= HTML_NOSPACE;
1211 print_text(h, "\\(rq");
1215 /* ARGSUSED */
1216 static int
1217 mdoc_pq_pre(MDOC_ARGS)
1220 if (MDOC_BODY != n->type)
1221 return(1);
1222 print_text(h, "\\&(");
1223 h->flags |= HTML_NOSPACE;
1224 return(1);
1228 /* ARGSUSED */
1229 static void
1230 mdoc_pq_post(MDOC_ARGS)
1233 if (MDOC_BODY != n->type)
1234 return;
1235 print_text(h, ")");
1239 /* ARGSUSED */
1240 static int
1241 mdoc_sq_pre(MDOC_ARGS)
1244 if (MDOC_BODY != n->type)
1245 return(1);
1246 print_text(h, "\\(oq");
1247 h->flags |= HTML_NOSPACE;
1248 return(1);
1252 /* ARGSUSED */
1253 static void
1254 mdoc_sq_post(MDOC_ARGS)
1257 if (MDOC_BODY != n->type)
1258 return;
1259 h->flags |= HTML_NOSPACE;
1260 print_text(h, "\\(aq");
1264 /* ARGSUSED */
1265 static int
1266 mdoc_em_pre(MDOC_ARGS)
1268 struct htmlpair tag;
1270 PAIR_CLASS_INIT(&tag, "emph");
1271 print_otag(h, TAG_SPAN, 1, &tag);
1272 return(1);
1276 /* ARGSUSED */
1277 static int
1278 mdoc_d1_pre(MDOC_ARGS)
1280 struct htmlpair tag[2];
1281 struct roffsu su;
1283 if (MDOC_BLOCK != n->type)
1284 return(1);
1286 /* FIXME: D1 shouldn't be literal. */
1288 SCALE_VS_INIT(&su, INDENT - 2);
1289 bufcat_su(h, "margin-left", &su);
1290 PAIR_CLASS_INIT(&tag[0], "lit");
1291 PAIR_STYLE_INIT(&tag[1], h);
1292 print_otag(h, TAG_DIV, 2, tag);
1293 return(1);
1297 /* ARGSUSED */
1298 static int
1299 mdoc_sx_pre(MDOC_ARGS)
1301 struct htmlpair tag[2];
1302 const struct mdoc_node *nn;
1303 char buf[BUFSIZ];
1305 /* FIXME: duplicates? */
1307 strlcpy(buf, "#", BUFSIZ);
1308 for (nn = n->child; nn; nn = nn->next) {
1309 html_idcat(buf, nn->string, BUFSIZ);
1310 if (nn->next)
1311 html_idcat(buf, " ", BUFSIZ);
1314 PAIR_CLASS_INIT(&tag[0], "link-sec");
1315 tag[1].key = ATTR_HREF;
1316 tag[1].val = buf;
1318 print_otag(h, TAG_A, 2, tag);
1319 return(1);
1323 /* ARGSUSED */
1324 static int
1325 mdoc_aq_pre(MDOC_ARGS)
1328 if (MDOC_BODY != n->type)
1329 return(1);
1330 print_text(h, "\\(la");
1331 h->flags |= HTML_NOSPACE;
1332 return(1);
1336 /* ARGSUSED */
1337 static void
1338 mdoc_aq_post(MDOC_ARGS)
1341 if (MDOC_BODY != n->type)
1342 return;
1343 h->flags |= HTML_NOSPACE;
1344 print_text(h, "\\(ra");
1348 /* ARGSUSED */
1349 static int
1350 mdoc_bd_pre(MDOC_ARGS)
1352 struct htmlpair tag[2];
1353 int type, comp, i;
1354 const struct mdoc_node *bl, *nn;
1355 struct roffsu su;
1357 if (MDOC_BLOCK == n->type)
1358 bl = n;
1359 else if (MDOC_HEAD == n->type)
1360 return(0);
1361 else
1362 bl = n->parent;
1364 SCALE_VS_INIT(&su, 0);
1366 type = comp = 0;
1367 for (i = 0; i < (int)bl->args->argc; i++)
1368 switch (bl->args->argv[i].arg) {
1369 case (MDOC_Offset):
1370 a2offs(bl->args->argv[i].value[0], &su);
1371 break;
1372 case (MDOC_Compact):
1373 comp = 1;
1374 break;
1375 case (MDOC_Centred):
1376 /* FALLTHROUGH */
1377 case (MDOC_Ragged):
1378 /* FALLTHROUGH */
1379 case (MDOC_Filled):
1380 /* FALLTHROUGH */
1381 case (MDOC_Unfilled):
1382 /* FALLTHROUGH */
1383 case (MDOC_Literal):
1384 type = bl->args->argv[i].arg;
1385 break;
1386 default:
1387 break;
1390 /* FIXME: -centered, etc. formatting. */
1392 if (MDOC_BLOCK == n->type) {
1393 bufcat_su(h, "margin-left", &su);
1394 for (nn = n; nn && ! comp; nn = nn->parent) {
1395 if (MDOC_BLOCK != nn->type)
1396 continue;
1397 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
1398 comp = 1;
1399 if (nn->prev)
1400 break;
1402 if (comp) {
1403 print_otag(h, TAG_DIV, 0, tag);
1404 return(1);
1406 SCALE_VS_INIT(&su, 1);
1407 bufcat_su(h, "margin-top", &su);
1408 PAIR_STYLE_INIT(&tag[0], h);
1409 print_otag(h, TAG_DIV, 1, tag);
1410 return(1);
1413 if (MDOC_Unfilled != type && MDOC_Literal != type)
1414 return(1);
1416 PAIR_CLASS_INIT(&tag[0], "lit");
1417 bufcat_style(h, "white-space", "pre");
1418 PAIR_STYLE_INIT(&tag[1], h);
1419 print_otag(h, TAG_DIV, 2, tag);
1421 for (nn = n->child; nn; nn = nn->next) {
1422 h->flags |= HTML_NOSPACE;
1423 print_mdoc_node(m, nn, h);
1424 if (NULL == nn->next)
1425 continue;
1426 if (nn->prev && nn->prev->line < nn->line)
1427 print_text(h, "\n");
1428 else if (NULL == nn->prev)
1429 print_text(h, "\n");
1432 return(0);
1436 /* ARGSUSED */
1437 static int
1438 mdoc_pa_pre(MDOC_ARGS)
1440 struct htmlpair tag;
1442 PAIR_CLASS_INIT(&tag, "file");
1443 print_otag(h, TAG_SPAN, 1, &tag);
1444 return(1);
1448 /* ARGSUSED */
1449 static int
1450 mdoc_ad_pre(MDOC_ARGS)
1452 struct htmlpair tag;
1454 PAIR_CLASS_INIT(&tag, "addr");
1455 print_otag(h, TAG_SPAN, 1, &tag);
1456 return(1);
1460 /* ARGSUSED */
1461 static int
1462 mdoc_an_pre(MDOC_ARGS)
1464 struct htmlpair tag;
1466 /* TODO: -split and -nosplit (see termp_an_pre()). */
1468 PAIR_CLASS_INIT(&tag, "author");
1469 print_otag(h, TAG_SPAN, 1, &tag);
1470 return(1);
1474 /* ARGSUSED */
1475 static int
1476 mdoc_cd_pre(MDOC_ARGS)
1478 struct htmlpair tag;
1480 print_otag(h, TAG_DIV, 0, NULL);
1481 PAIR_CLASS_INIT(&tag, "config");
1482 print_otag(h, TAG_SPAN, 1, &tag);
1483 return(1);
1487 /* ARGSUSED */
1488 static int
1489 mdoc_dv_pre(MDOC_ARGS)
1491 struct htmlpair tag;
1493 PAIR_CLASS_INIT(&tag, "define");
1494 print_otag(h, TAG_SPAN, 1, &tag);
1495 return(1);
1499 /* ARGSUSED */
1500 static int
1501 mdoc_ev_pre(MDOC_ARGS)
1503 struct htmlpair tag;
1505 PAIR_CLASS_INIT(&tag, "env");
1506 print_otag(h, TAG_SPAN, 1, &tag);
1507 return(1);
1511 /* ARGSUSED */
1512 static int
1513 mdoc_er_pre(MDOC_ARGS)
1515 struct htmlpair tag;
1517 PAIR_CLASS_INIT(&tag, "errno");
1518 print_otag(h, TAG_SPAN, 1, &tag);
1519 return(1);
1523 /* ARGSUSED */
1524 static int
1525 mdoc_fa_pre(MDOC_ARGS)
1527 const struct mdoc_node *nn;
1528 struct htmlpair tag;
1529 struct tag *t;
1531 PAIR_CLASS_INIT(&tag, "farg");
1532 if (n->parent->tok != MDOC_Fo) {
1533 print_otag(h, TAG_SPAN, 1, &tag);
1534 return(1);
1537 for (nn = n->child; nn; nn = nn->next) {
1538 t = print_otag(h, TAG_SPAN, 1, &tag);
1539 print_text(h, nn->string);
1540 print_tagq(h, t);
1541 if (nn->next)
1542 print_text(h, ",");
1545 if (n->child && n->next && n->next->tok == MDOC_Fa)
1546 print_text(h, ",");
1548 return(0);
1552 /* ARGSUSED */
1553 static int
1554 mdoc_fd_pre(MDOC_ARGS)
1556 struct htmlpair tag;
1557 struct roffsu su;
1559 if (SEC_SYNOPSIS == n->sec) {
1560 if (n->next && MDOC_Fd != n->next->tok) {
1561 SCALE_VS_INIT(&su, 1);
1562 bufcat_su(h, "margin-bottom", &su);
1563 PAIR_STYLE_INIT(&tag, h);
1564 print_otag(h, TAG_DIV, 1, &tag);
1565 } else
1566 print_otag(h, TAG_DIV, 0, NULL);
1569 PAIR_CLASS_INIT(&tag, "macro");
1570 print_otag(h, TAG_SPAN, 1, &tag);
1571 return(1);
1575 /* ARGSUSED */
1576 static int
1577 mdoc_vt_pre(MDOC_ARGS)
1579 struct htmlpair tag;
1580 struct roffsu su;
1582 if (MDOC_BLOCK == n->type) {
1583 if (n->prev && MDOC_Vt != n->prev->tok) {
1584 SCALE_VS_INIT(&su, 1);
1585 bufcat_su(h, "margin-top", &su);
1586 PAIR_STYLE_INIT(&tag, h);
1587 print_otag(h, TAG_DIV, 1, &tag);
1588 } else
1589 print_otag(h, TAG_DIV, 0, NULL);
1591 return(1);
1592 } else if (MDOC_HEAD == n->type)
1593 return(0);
1595 PAIR_CLASS_INIT(&tag, "type");
1596 print_otag(h, TAG_SPAN, 1, &tag);
1597 return(1);
1601 /* ARGSUSED */
1602 static int
1603 mdoc_ft_pre(MDOC_ARGS)
1605 struct htmlpair tag;
1606 struct roffsu su;
1608 if (SEC_SYNOPSIS == n->sec) {
1609 if (n->prev && MDOC_Fo == n->prev->tok) {
1610 SCALE_VS_INIT(&su, 1);
1611 bufcat_su(h, "margin-top", &su);
1612 PAIR_STYLE_INIT(&tag, h);
1613 print_otag(h, TAG_DIV, 1, &tag);
1614 } else
1615 print_otag(h, TAG_DIV, 0, NULL);
1618 PAIR_CLASS_INIT(&tag, "ftype");
1619 print_otag(h, TAG_SPAN, 1, &tag);
1620 return(1);
1624 /* ARGSUSED */
1625 static int
1626 mdoc_fn_pre(MDOC_ARGS)
1628 struct tag *t;
1629 struct htmlpair tag[2];
1630 const struct mdoc_node *nn;
1631 char nbuf[BUFSIZ];
1632 const char *sp, *ep;
1633 int sz, i;
1634 struct roffsu su;
1636 if (SEC_SYNOPSIS == n->sec) {
1637 SCALE_HS_INIT(&su, INDENT);
1638 bufcat_su(h, "margin-left", &su);
1639 su.scale = -su.scale;
1640 bufcat_su(h, "text-indent", &su);
1641 if (n->next) {
1642 SCALE_VS_INIT(&su, 1);
1643 bufcat_su(h, "margin-bottom", &su);
1645 PAIR_STYLE_INIT(&tag[0], h);
1646 print_otag(h, TAG_DIV, 1, tag);
1649 /* Split apart into type and name. */
1650 assert(n->child->string);
1651 sp = n->child->string;
1653 ep = strchr(sp, ' ');
1654 if (NULL != ep) {
1655 PAIR_CLASS_INIT(&tag[0], "ftype");
1656 t = print_otag(h, TAG_SPAN, 1, tag);
1658 while (ep) {
1659 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1660 (void)memcpy(nbuf, sp, (size_t)sz);
1661 nbuf[sz] = '\0';
1662 print_text(h, nbuf);
1663 sp = ++ep;
1664 ep = strchr(sp, ' ');
1666 print_tagq(h, t);
1669 PAIR_CLASS_INIT(&tag[0], "fname");
1670 t = print_otag(h, TAG_SPAN, 1, tag);
1672 if (sp) {
1673 (void)strlcpy(nbuf, sp, BUFSIZ);
1674 print_text(h, nbuf);
1677 print_tagq(h, t);
1679 h->flags |= HTML_NOSPACE;
1680 print_text(h, "(");
1682 bufinit(h);
1683 PAIR_CLASS_INIT(&tag[0], "farg");
1684 bufcat_style(h, "white-space", "nowrap");
1685 PAIR_STYLE_INIT(&tag[1], h);
1687 for (nn = n->child->next; nn; nn = nn->next) {
1688 i = 1;
1689 if (SEC_SYNOPSIS == n->sec)
1690 i = 2;
1691 t = print_otag(h, TAG_SPAN, i, tag);
1692 print_text(h, nn->string);
1693 print_tagq(h, t);
1694 if (nn->next)
1695 print_text(h, ",");
1698 print_text(h, ")");
1699 if (SEC_SYNOPSIS == n->sec)
1700 print_text(h, ";");
1702 return(0);
1706 /* ARGSUSED */
1707 static int
1708 mdoc_sp_pre(MDOC_ARGS)
1710 int len;
1711 struct htmlpair tag;
1712 struct roffsu su;
1714 switch (n->tok) {
1715 case (MDOC_sp):
1716 /* FIXME: can this have a scaling indicator? */
1717 len = n->child ? atoi(n->child->string) : 1;
1718 break;
1719 case (MDOC_br):
1720 len = 0;
1721 break;
1722 default:
1723 len = 1;
1724 break;
1727 SCALE_VS_INIT(&su, len);
1728 bufcat_su(h, "height", &su);
1729 PAIR_STYLE_INIT(&tag, h);
1730 print_otag(h, TAG_DIV, 1, &tag);
1731 /* So the div isn't empty: */
1732 print_text(h, "\\~");
1734 return(0);
1739 /* ARGSUSED */
1740 static int
1741 mdoc_brq_pre(MDOC_ARGS)
1744 if (MDOC_BODY != n->type)
1745 return(1);
1746 print_text(h, "\\(lC");
1747 h->flags |= HTML_NOSPACE;
1748 return(1);
1752 /* ARGSUSED */
1753 static void
1754 mdoc_brq_post(MDOC_ARGS)
1757 if (MDOC_BODY != n->type)
1758 return;
1759 h->flags |= HTML_NOSPACE;
1760 print_text(h, "\\(rC");
1764 /* ARGSUSED */
1765 static int
1766 mdoc_lk_pre(MDOC_ARGS)
1768 const struct mdoc_node *nn;
1769 struct htmlpair tag[2];
1771 nn = n->child;
1773 PAIR_CLASS_INIT(&tag[0], "link-ext");
1774 tag[1].key = ATTR_HREF;
1775 tag[1].val = nn->string;
1776 print_otag(h, TAG_A, 2, tag);
1778 if (NULL == nn->next)
1779 return(1);
1781 for (nn = nn->next; nn; nn = nn->next)
1782 print_text(h, nn->string);
1784 return(0);
1788 /* ARGSUSED */
1789 static int
1790 mdoc_mt_pre(MDOC_ARGS)
1792 struct htmlpair tag[2];
1793 struct tag *t;
1794 const struct mdoc_node *nn;
1796 PAIR_CLASS_INIT(&tag[0], "link-mail");
1798 for (nn = n->child; nn; nn = nn->next) {
1799 bufinit(h);
1800 bufcat(h, "mailto:");
1801 bufcat(h, nn->string);
1802 PAIR_STYLE_INIT(&tag[1], h);
1803 t = print_otag(h, TAG_A, 2, tag);
1804 print_text(h, nn->string);
1805 print_tagq(h, t);
1808 return(0);
1812 /* ARGSUSED */
1813 static int
1814 mdoc_fo_pre(MDOC_ARGS)
1816 struct htmlpair tag;
1818 if (MDOC_BODY == n->type) {
1819 h->flags |= HTML_NOSPACE;
1820 print_text(h, "(");
1821 h->flags |= HTML_NOSPACE;
1822 return(1);
1823 } else if (MDOC_BLOCK == n->type)
1824 return(1);
1826 PAIR_CLASS_INIT(&tag, "fname");
1827 print_otag(h, TAG_SPAN, 1, &tag);
1828 return(1);
1832 /* ARGSUSED */
1833 static void
1834 mdoc_fo_post(MDOC_ARGS)
1836 if (MDOC_BODY != n->type)
1837 return;
1838 h->flags |= HTML_NOSPACE;
1839 print_text(h, ")");
1840 h->flags |= HTML_NOSPACE;
1841 print_text(h, ";");
1845 /* ARGSUSED */
1846 static int
1847 mdoc_in_pre(MDOC_ARGS)
1849 const struct mdoc_node *nn;
1850 struct tag *t;
1851 struct htmlpair tag[2];
1852 int i;
1853 struct roffsu su;
1855 if (SEC_SYNOPSIS == n->sec) {
1856 if (n->next && MDOC_In != n->next->tok) {
1857 SCALE_VS_INIT(&su, 1);
1858 bufcat_su(h, "margin-bottom", &su);
1859 PAIR_STYLE_INIT(&tag[0], h);
1860 print_otag(h, TAG_DIV, 1, tag);
1861 } else
1862 print_otag(h, TAG_DIV, 0, NULL);
1865 /* FIXME: there's a buffer bug in here somewhere. */
1867 PAIR_CLASS_INIT(&tag[0], "includes");
1868 print_otag(h, TAG_SPAN, 1, tag);
1870 if (SEC_SYNOPSIS == n->sec)
1871 print_text(h, "#include");
1873 print_text(h, "<");
1874 h->flags |= HTML_NOSPACE;
1876 /* XXX -- see warning in termp_in_post(). */
1878 for (nn = n->child; nn; nn = nn->next) {
1879 PAIR_CLASS_INIT(&tag[0], "link-includes");
1880 i = 1;
1881 bufinit(h);
1882 if (h->base_includes) {
1883 buffmt_includes(h, nn->string);
1884 tag[i].key = ATTR_HREF;
1885 tag[i++].val = h->buf;
1887 t = print_otag(h, TAG_A, i, tag);
1888 print_mdoc_node(m, nn, h);
1889 print_tagq(h, t);
1892 h->flags |= HTML_NOSPACE;
1893 print_text(h, ">");
1895 return(0);
1899 /* ARGSUSED */
1900 static int
1901 mdoc_ic_pre(MDOC_ARGS)
1903 struct htmlpair tag;
1905 PAIR_CLASS_INIT(&tag, "cmd");
1906 print_otag(h, TAG_SPAN, 1, &tag);
1907 return(1);
1911 /* ARGSUSED */
1912 static int
1913 mdoc_rv_pre(MDOC_ARGS)
1915 const struct mdoc_node *nn;
1916 struct htmlpair tag;
1917 struct tag *t;
1919 print_otag(h, TAG_DIV, 0, NULL);
1920 print_text(h, "The");
1922 for (nn = n->child; nn; nn = nn->next) {
1923 PAIR_CLASS_INIT(&tag, "fname");
1924 t = print_otag(h, TAG_SPAN, 1, &tag);
1925 print_text(h, nn->string);
1926 print_tagq(h, t);
1928 h->flags |= HTML_NOSPACE;
1929 if (nn->next && NULL == nn->next->next)
1930 print_text(h, "(), and");
1931 else if (nn->next)
1932 print_text(h, "(),");
1933 else
1934 print_text(h, "()");
1937 if (n->child->next)
1938 print_text(h, "functions return");
1939 else
1940 print_text(h, "function returns");
1942 print_text(h, "the value 0 if successful; otherwise the value "
1943 "-1 is returned and the global variable");
1945 PAIR_CLASS_INIT(&tag, "var");
1946 t = print_otag(h, TAG_SPAN, 1, &tag);
1947 print_text(h, "errno");
1948 print_tagq(h, t);
1949 print_text(h, "is set to indicate the error.");
1950 return(0);
1954 /* ARGSUSED */
1955 static int
1956 mdoc_va_pre(MDOC_ARGS)
1958 struct htmlpair tag;
1960 PAIR_CLASS_INIT(&tag, "var");
1961 print_otag(h, TAG_SPAN, 1, &tag);
1962 return(1);
1966 /* ARGSUSED */
1967 static int
1968 mdoc_bq_pre(MDOC_ARGS)
1971 if (MDOC_BODY != n->type)
1972 return(1);
1973 print_text(h, "\\(lB");
1974 h->flags |= HTML_NOSPACE;
1975 return(1);
1979 /* ARGSUSED */
1980 static void
1981 mdoc_bq_post(MDOC_ARGS)
1984 if (MDOC_BODY != n->type)
1985 return;
1986 h->flags |= HTML_NOSPACE;
1987 print_text(h, "\\(rB");
1991 /* ARGSUSED */
1992 static int
1993 mdoc_ap_pre(MDOC_ARGS)
1996 h->flags |= HTML_NOSPACE;
1997 print_text(h, "\\(aq");
1998 h->flags |= HTML_NOSPACE;
1999 return(1);
2003 /* ARGSUSED */
2004 static int
2005 mdoc_bf_pre(MDOC_ARGS)
2007 int i;
2008 struct htmlpair tag[2];
2009 struct roffsu su;
2011 if (MDOC_HEAD == n->type)
2012 return(0);
2013 else if (MDOC_BLOCK != n->type)
2014 return(1);
2016 PAIR_CLASS_INIT(&tag[0], "lit");
2018 if (n->head->child) {
2019 if ( ! strcmp("Em", n->head->child->string))
2020 PAIR_CLASS_INIT(&tag[0], "emph");
2021 else if ( ! strcmp("Sy", n->head->child->string))
2022 PAIR_CLASS_INIT(&tag[0], "symb");
2023 else if ( ! strcmp("Li", n->head->child->string))
2024 PAIR_CLASS_INIT(&tag[0], "lit");
2025 } else {
2026 assert(n->args);
2027 for (i = 0; i < (int)n->args->argc; i++)
2028 switch (n->args->argv[i].arg) {
2029 case (MDOC_Symbolic):
2030 PAIR_CLASS_INIT(&tag[0], "symb");
2031 break;
2032 case (MDOC_Literal):
2033 PAIR_CLASS_INIT(&tag[0], "lit");
2034 break;
2035 case (MDOC_Emphasis):
2036 PAIR_CLASS_INIT(&tag[0], "emph");
2037 break;
2038 default:
2039 break;
2043 /* FIXME: div's have spaces stripped--we want them. */
2045 bufcat_style(h, "display", "inline");
2046 SCALE_HS_INIT(&su, 1);
2047 bufcat_su(h, "margin-right", &su);
2048 PAIR_STYLE_INIT(&tag[1], h);
2049 print_otag(h, TAG_DIV, 2, tag);
2050 return(1);
2054 /* ARGSUSED */
2055 static int
2056 mdoc_ms_pre(MDOC_ARGS)
2058 struct htmlpair tag;
2060 PAIR_CLASS_INIT(&tag, "symb");
2061 print_otag(h, TAG_SPAN, 1, &tag);
2062 return(1);
2066 /* ARGSUSED */
2067 static int
2068 mdoc_pf_pre(MDOC_ARGS)
2071 h->flags |= HTML_IGNDELIM;
2072 return(1);
2076 /* ARGSUSED */
2077 static void
2078 mdoc_pf_post(MDOC_ARGS)
2081 h->flags &= ~HTML_IGNDELIM;
2082 h->flags |= HTML_NOSPACE;
2086 /* ARGSUSED */
2087 static int
2088 mdoc_rs_pre(MDOC_ARGS)
2090 struct htmlpair tag;
2091 struct roffsu su;
2093 if (MDOC_BLOCK != n->type)
2094 return(1);
2096 if (n->prev && SEC_SEE_ALSO == n->sec) {
2097 SCALE_VS_INIT(&su, 1);
2098 bufcat_su(h, "margin-top", &su);
2099 PAIR_STYLE_INIT(&tag, h);
2100 print_otag(h, TAG_DIV, 1, &tag);
2103 PAIR_CLASS_INIT(&tag, "ref");
2104 print_otag(h, TAG_SPAN, 1, &tag);
2105 return(1);
2110 /* ARGSUSED */
2111 static int
2112 mdoc_li_pre(MDOC_ARGS)
2114 struct htmlpair tag;
2116 PAIR_CLASS_INIT(&tag, "lit");
2117 print_otag(h, TAG_SPAN, 1, &tag);
2118 return(1);
2122 /* ARGSUSED */
2123 static int
2124 mdoc_sy_pre(MDOC_ARGS)
2126 struct htmlpair tag;
2128 PAIR_CLASS_INIT(&tag, "symb");
2129 print_otag(h, TAG_SPAN, 1, &tag);
2130 return(1);
2134 /* ARGSUSED */
2135 static int
2136 mdoc_bt_pre(MDOC_ARGS)
2139 print_text(h, "is currently in beta test.");
2140 return(0);
2144 /* ARGSUSED */
2145 static int
2146 mdoc_ud_pre(MDOC_ARGS)
2149 print_text(h, "currently under development.");
2150 return(0);
2154 /* ARGSUSED */
2155 static int
2156 mdoc_lb_pre(MDOC_ARGS)
2158 struct htmlpair tag;
2160 if (SEC_SYNOPSIS == n->sec)
2161 print_otag(h, TAG_DIV, 0, NULL);
2162 PAIR_CLASS_INIT(&tag, "lib");
2163 print_otag(h, TAG_SPAN, 1, &tag);
2164 return(1);
2168 /* ARGSUSED */
2169 static int
2170 mdoc__x_pre(MDOC_ARGS)
2172 struct htmlpair tag[2];
2174 switch (n->tok) {
2175 case(MDOC__A):
2176 PAIR_CLASS_INIT(&tag[0], "ref-auth");
2177 break;
2178 case(MDOC__B):
2179 PAIR_CLASS_INIT(&tag[0], "ref-book");
2180 break;
2181 case(MDOC__C):
2182 PAIR_CLASS_INIT(&tag[0], "ref-city");
2183 break;
2184 case(MDOC__D):
2185 PAIR_CLASS_INIT(&tag[0], "ref-date");
2186 break;
2187 case(MDOC__I):
2188 PAIR_CLASS_INIT(&tag[0], "ref-issue");
2189 break;
2190 case(MDOC__J):
2191 PAIR_CLASS_INIT(&tag[0], "ref-jrnl");
2192 break;
2193 case(MDOC__N):
2194 PAIR_CLASS_INIT(&tag[0], "ref-num");
2195 break;
2196 case(MDOC__O):
2197 PAIR_CLASS_INIT(&tag[0], "ref-opt");
2198 break;
2199 case(MDOC__P):
2200 PAIR_CLASS_INIT(&tag[0], "ref-page");
2201 break;
2202 case(MDOC__Q):
2203 PAIR_CLASS_INIT(&tag[0], "ref-corp");
2204 break;
2205 case(MDOC__R):
2206 PAIR_CLASS_INIT(&tag[0], "ref-rep");
2207 break;
2208 case(MDOC__T):
2209 PAIR_CLASS_INIT(&tag[0], "ref-title");
2210 print_text(h, "\\(lq");
2211 h->flags |= HTML_NOSPACE;
2212 break;
2213 case(MDOC__U):
2214 PAIR_CLASS_INIT(&tag[0], "link-ref");
2215 break;
2216 case(MDOC__V):
2217 PAIR_CLASS_INIT(&tag[0], "ref-vol");
2218 break;
2219 default:
2220 abort();
2221 /* NOTREACHED */
2224 if (MDOC__U != n->tok) {
2225 print_otag(h, TAG_SPAN, 1, tag);
2226 return(1);
2229 PAIR_HREF_INIT(&tag[1], n->child->string);
2230 print_otag(h, TAG_A, 2, tag);
2231 return(1);
2235 /* ARGSUSED */
2236 static void
2237 mdoc__x_post(MDOC_ARGS)
2240 h->flags |= HTML_NOSPACE;
2241 switch (n->tok) {
2242 case (MDOC__T):
2243 print_text(h, "\\(rq");
2244 h->flags |= HTML_NOSPACE;
2245 break;
2246 default:
2247 break;
2249 print_text(h, n->next ? "," : ".");