]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
Partial cleanup of argument count validation in mdoc(7):
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.140 2011/01/01 12:18:37 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
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.
8 *
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.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "mandoc.h"
31 #include "out.h"
32 #include "html.h"
33 #include "mdoc.h"
34 #include "main.h"
35
36 #define INDENT 5
37 #define HALFINDENT 3
38
39 #define MDOC_ARGS const struct mdoc_meta *m, \
40 const struct mdoc_node *n, \
41 struct html *h
42
43 #ifndef MIN
44 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
45 #endif
46
47 struct htmlmdoc {
48 int (*pre)(MDOC_ARGS);
49 void (*post)(MDOC_ARGS);
50 };
51
52 static void print_mdoc(MDOC_ARGS);
53 static void print_mdoc_head(MDOC_ARGS);
54 static void print_mdoc_node(MDOC_ARGS);
55 static void print_mdoc_nodelist(MDOC_ARGS);
56 static void synopsis_pre(struct html *,
57 const struct mdoc_node *);
58
59 static void a2width(const char *, struct roffsu *);
60 static void a2offs(const char *, struct roffsu *);
61
62 static void mdoc_root_post(MDOC_ARGS);
63 static int mdoc_root_pre(MDOC_ARGS);
64
65 static void mdoc__x_post(MDOC_ARGS);
66 static int mdoc__x_pre(MDOC_ARGS);
67 static int mdoc_ad_pre(MDOC_ARGS);
68 static int mdoc_an_pre(MDOC_ARGS);
69 static int mdoc_ap_pre(MDOC_ARGS);
70 static int mdoc_ar_pre(MDOC_ARGS);
71 static int mdoc_bd_pre(MDOC_ARGS);
72 static int mdoc_bf_pre(MDOC_ARGS);
73 static void mdoc_bk_post(MDOC_ARGS);
74 static int mdoc_bk_pre(MDOC_ARGS);
75 static int mdoc_bl_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 int mdoc_dv_pre(MDOC_ARGS);
81 static int mdoc_fa_pre(MDOC_ARGS);
82 static int mdoc_fd_pre(MDOC_ARGS);
83 static int mdoc_fl_pre(MDOC_ARGS);
84 static int mdoc_fn_pre(MDOC_ARGS);
85 static int mdoc_ft_pre(MDOC_ARGS);
86 static int mdoc_em_pre(MDOC_ARGS);
87 static int mdoc_er_pre(MDOC_ARGS);
88 static int mdoc_ev_pre(MDOC_ARGS);
89 static int mdoc_ex_pre(MDOC_ARGS);
90 static void mdoc_fo_post(MDOC_ARGS);
91 static int mdoc_fo_pre(MDOC_ARGS);
92 static int mdoc_ic_pre(MDOC_ARGS);
93 static int mdoc_igndelim_pre(MDOC_ARGS);
94 static int mdoc_in_pre(MDOC_ARGS);
95 static int mdoc_it_pre(MDOC_ARGS);
96 static int mdoc_lb_pre(MDOC_ARGS);
97 static int mdoc_li_pre(MDOC_ARGS);
98 static int mdoc_lk_pre(MDOC_ARGS);
99 static int mdoc_mt_pre(MDOC_ARGS);
100 static int mdoc_ms_pre(MDOC_ARGS);
101 static int mdoc_nd_pre(MDOC_ARGS);
102 static int mdoc_nm_pre(MDOC_ARGS);
103 static int mdoc_ns_pre(MDOC_ARGS);
104 static int mdoc_pa_pre(MDOC_ARGS);
105 static void mdoc_pf_post(MDOC_ARGS);
106 static int mdoc_pp_pre(MDOC_ARGS);
107 static void mdoc_quote_post(MDOC_ARGS);
108 static int mdoc_quote_pre(MDOC_ARGS);
109 static int mdoc_rs_pre(MDOC_ARGS);
110 static int mdoc_rv_pre(MDOC_ARGS);
111 static int mdoc_sh_pre(MDOC_ARGS);
112 static int mdoc_sm_pre(MDOC_ARGS);
113 static int mdoc_sp_pre(MDOC_ARGS);
114 static int mdoc_ss_pre(MDOC_ARGS);
115 static int mdoc_sx_pre(MDOC_ARGS);
116 static int mdoc_sy_pre(MDOC_ARGS);
117 static int mdoc_ud_pre(MDOC_ARGS);
118 static int mdoc_va_pre(MDOC_ARGS);
119 static int mdoc_vt_pre(MDOC_ARGS);
120 static int mdoc_xr_pre(MDOC_ARGS);
121 static int mdoc_xx_pre(MDOC_ARGS);
122
123 static const struct htmlmdoc mdocs[MDOC_MAX] = {
124 {mdoc_ap_pre, NULL}, /* Ap */
125 {NULL, NULL}, /* Dd */
126 {NULL, NULL}, /* Dt */
127 {NULL, NULL}, /* Os */
128 {mdoc_sh_pre, NULL }, /* Sh */
129 {mdoc_ss_pre, NULL }, /* Ss */
130 {mdoc_pp_pre, NULL}, /* Pp */
131 {mdoc_d1_pre, NULL}, /* D1 */
132 {mdoc_d1_pre, NULL}, /* Dl */
133 {mdoc_bd_pre, NULL}, /* Bd */
134 {NULL, NULL}, /* Ed */
135 {mdoc_bl_pre, NULL}, /* Bl */
136 {NULL, NULL}, /* El */
137 {mdoc_it_pre, NULL}, /* It */
138 {mdoc_ad_pre, NULL}, /* Ad */
139 {mdoc_an_pre, NULL}, /* An */
140 {mdoc_ar_pre, NULL}, /* Ar */
141 {mdoc_cd_pre, NULL}, /* Cd */
142 {mdoc_fl_pre, NULL}, /* Cm */
143 {mdoc_dv_pre, NULL}, /* Dv */
144 {mdoc_er_pre, NULL}, /* Er */
145 {mdoc_ev_pre, NULL}, /* Ev */
146 {mdoc_ex_pre, NULL}, /* Ex */
147 {mdoc_fa_pre, NULL}, /* Fa */
148 {mdoc_fd_pre, NULL}, /* Fd */
149 {mdoc_fl_pre, NULL}, /* Fl */
150 {mdoc_fn_pre, NULL}, /* Fn */
151 {mdoc_ft_pre, NULL}, /* Ft */
152 {mdoc_ic_pre, NULL}, /* Ic */
153 {mdoc_in_pre, NULL}, /* In */
154 {mdoc_li_pre, NULL}, /* Li */
155 {mdoc_nd_pre, NULL}, /* Nd */
156 {mdoc_nm_pre, NULL}, /* Nm */
157 {mdoc_quote_pre, mdoc_quote_post}, /* Op */
158 {NULL, NULL}, /* Ot */
159 {mdoc_pa_pre, NULL}, /* Pa */
160 {mdoc_rv_pre, NULL}, /* Rv */
161 {NULL, NULL}, /* St */
162 {mdoc_va_pre, NULL}, /* Va */
163 {mdoc_vt_pre, NULL}, /* Vt */
164 {mdoc_xr_pre, NULL}, /* Xr */
165 {mdoc__x_pre, mdoc__x_post}, /* %A */
166 {mdoc__x_pre, mdoc__x_post}, /* %B */
167 {mdoc__x_pre, mdoc__x_post}, /* %D */
168 {mdoc__x_pre, mdoc__x_post}, /* %I */
169 {mdoc__x_pre, mdoc__x_post}, /* %J */
170 {mdoc__x_pre, mdoc__x_post}, /* %N */
171 {mdoc__x_pre, mdoc__x_post}, /* %O */
172 {mdoc__x_pre, mdoc__x_post}, /* %P */
173 {mdoc__x_pre, mdoc__x_post}, /* %R */
174 {mdoc__x_pre, mdoc__x_post}, /* %T */
175 {mdoc__x_pre, mdoc__x_post}, /* %V */
176 {NULL, NULL}, /* Ac */
177 {mdoc_quote_pre, mdoc_quote_post}, /* Ao */
178 {mdoc_quote_pre, mdoc_quote_post}, /* Aq */
179 {NULL, NULL}, /* At */
180 {NULL, NULL}, /* Bc */
181 {mdoc_bf_pre, NULL}, /* Bf */
182 {mdoc_quote_pre, mdoc_quote_post}, /* Bo */
183 {mdoc_quote_pre, mdoc_quote_post}, /* Bq */
184 {mdoc_xx_pre, NULL}, /* Bsx */
185 {mdoc_bx_pre, NULL}, /* Bx */
186 {NULL, NULL}, /* Db */
187 {NULL, NULL}, /* Dc */
188 {mdoc_quote_pre, mdoc_quote_post}, /* Do */
189 {mdoc_quote_pre, mdoc_quote_post}, /* Dq */
190 {NULL, NULL}, /* Ec */ /* FIXME: no space */
191 {NULL, NULL}, /* Ef */
192 {mdoc_em_pre, NULL}, /* Em */
193 {NULL, NULL}, /* Eo */
194 {mdoc_xx_pre, NULL}, /* Fx */
195 {mdoc_ms_pre, NULL}, /* Ms */
196 {mdoc_igndelim_pre, NULL}, /* No */
197 {mdoc_ns_pre, NULL}, /* Ns */
198 {mdoc_xx_pre, NULL}, /* Nx */
199 {mdoc_xx_pre, NULL}, /* Ox */
200 {NULL, NULL}, /* Pc */
201 {mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
202 {mdoc_quote_pre, mdoc_quote_post}, /* Po */
203 {mdoc_quote_pre, mdoc_quote_post}, /* Pq */
204 {NULL, NULL}, /* Qc */
205 {mdoc_quote_pre, mdoc_quote_post}, /* Ql */
206 {mdoc_quote_pre, mdoc_quote_post}, /* Qo */
207 {mdoc_quote_pre, mdoc_quote_post}, /* Qq */
208 {NULL, NULL}, /* Re */
209 {mdoc_rs_pre, NULL}, /* Rs */
210 {NULL, NULL}, /* Sc */
211 {mdoc_quote_pre, mdoc_quote_post}, /* So */
212 {mdoc_quote_pre, mdoc_quote_post}, /* Sq */
213 {mdoc_sm_pre, NULL}, /* Sm */
214 {mdoc_sx_pre, NULL}, /* Sx */
215 {mdoc_sy_pre, NULL}, /* Sy */
216 {NULL, NULL}, /* Tn */
217 {mdoc_xx_pre, NULL}, /* Ux */
218 {NULL, NULL}, /* Xc */
219 {NULL, NULL}, /* Xo */
220 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
221 {NULL, NULL}, /* Fc */
222 {mdoc_quote_pre, mdoc_quote_post}, /* Oo */
223 {NULL, NULL}, /* Oc */
224 {mdoc_bk_pre, mdoc_bk_post}, /* Bk */
225 {NULL, NULL}, /* Ek */
226 {mdoc_bt_pre, NULL}, /* Bt */
227 {NULL, NULL}, /* Hf */
228 {NULL, NULL}, /* Fr */
229 {mdoc_ud_pre, NULL}, /* Ud */
230 {mdoc_lb_pre, NULL}, /* Lb */
231 {mdoc_pp_pre, NULL}, /* Lp */
232 {mdoc_lk_pre, NULL}, /* Lk */
233 {mdoc_mt_pre, NULL}, /* Mt */
234 {mdoc_quote_pre, mdoc_quote_post}, /* Brq */
235 {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
236 {NULL, NULL}, /* Brc */
237 {mdoc__x_pre, mdoc__x_post}, /* %C */
238 {NULL, NULL}, /* Es */ /* TODO */
239 {NULL, NULL}, /* En */ /* TODO */
240 {mdoc_xx_pre, NULL}, /* Dx */
241 {mdoc__x_pre, mdoc__x_post}, /* %Q */
242 {mdoc_sp_pre, NULL}, /* br */
243 {mdoc_sp_pre, NULL}, /* sp */
244 {mdoc__x_pre, mdoc__x_post}, /* %U */
245 {NULL, NULL}, /* Ta */
246 };
247
248 static const char * const lists[LIST_MAX] = {
249 NULL,
250 "list-bul",
251 "list-col",
252 "list-dash",
253 "list-diag",
254 "list-enum",
255 "list-hang",
256 "list-hyph",
257 "list-inset",
258 "list-item",
259 "list-ohang",
260 "list-tag"
261 };
262
263 void
264 html_mdoc(void *arg, const struct mdoc *m)
265 {
266 struct html *h;
267 struct tag *t;
268
269 h = (struct html *)arg;
270
271 print_gen_decls(h);
272 t = print_otag(h, TAG_HTML, 0, NULL);
273 print_mdoc(mdoc_meta(m), mdoc_node(m), h);
274 print_tagq(h, t);
275
276 printf("\n");
277 }
278
279
280 /*
281 * Calculate the scaling unit passed in a `-width' argument. This uses
282 * either a native scaling unit (e.g., 1i, 2m) or the string length of
283 * the value.
284 */
285 static void
286 a2width(const char *p, struct roffsu *su)
287 {
288
289 if ( ! a2roffsu(p, su, SCALE_MAX)) {
290 su->unit = SCALE_BU;
291 su->scale = (int)strlen(p);
292 }
293 }
294
295
296 /*
297 * See the same function in mdoc_term.c for documentation.
298 */
299 static void
300 synopsis_pre(struct html *h, const struct mdoc_node *n)
301 {
302
303 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
304 return;
305
306 if (n->prev->tok == n->tok &&
307 MDOC_Fo != n->tok &&
308 MDOC_Ft != n->tok &&
309 MDOC_Fn != n->tok) {
310 print_otag(h, TAG_BR, 0, NULL);
311 return;
312 }
313
314 switch (n->prev->tok) {
315 case (MDOC_Fd):
316 /* FALLTHROUGH */
317 case (MDOC_Fn):
318 /* FALLTHROUGH */
319 case (MDOC_Fo):
320 /* FALLTHROUGH */
321 case (MDOC_In):
322 /* FALLTHROUGH */
323 case (MDOC_Vt):
324 print_otag(h, TAG_P, 0, NULL);
325 break;
326 case (MDOC_Ft):
327 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
328 print_otag(h, TAG_P, 0, NULL);
329 break;
330 }
331 /* FALLTHROUGH */
332 default:
333 print_otag(h, TAG_BR, 0, NULL);
334 break;
335 }
336 }
337
338
339 /*
340 * Calculate the scaling unit passed in an `-offset' argument. This
341 * uses either a native scaling unit (e.g., 1i, 2m), one of a set of
342 * predefined strings (indent, etc.), or the string length of the value.
343 */
344 static void
345 a2offs(const char *p, struct roffsu *su)
346 {
347
348 /* FIXME: "right"? */
349
350 if (0 == strcmp(p, "left"))
351 SCALE_HS_INIT(su, 0);
352 else if (0 == strcmp(p, "indent"))
353 SCALE_HS_INIT(su, INDENT);
354 else if (0 == strcmp(p, "indent-two"))
355 SCALE_HS_INIT(su, INDENT * 2);
356 else if ( ! a2roffsu(p, su, SCALE_MAX)) {
357 su->unit = SCALE_BU;
358 su->scale = (int)strlen(p);
359 }
360 }
361
362
363 static void
364 print_mdoc(MDOC_ARGS)
365 {
366 struct tag *t;
367
368 t = print_otag(h, TAG_HEAD, 0, NULL);
369 print_mdoc_head(m, n, h);
370 print_tagq(h, t);
371
372 t = print_otag(h, TAG_BODY, 0, NULL);
373 print_mdoc_nodelist(m, n, h);
374 print_tagq(h, t);
375 }
376
377
378 /* ARGSUSED */
379 static void
380 print_mdoc_head(MDOC_ARGS)
381 {
382
383 print_gen_head(h);
384 bufinit(h);
385 buffmt(h, "%s(%s)", m->title, m->msec);
386
387 if (m->arch) {
388 bufcat(h, " (");
389 bufcat(h, m->arch);
390 bufcat(h, ")");
391 }
392
393 print_otag(h, TAG_TITLE, 0, NULL);
394 print_text(h, h->buf);
395 }
396
397
398 static void
399 print_mdoc_nodelist(MDOC_ARGS)
400 {
401
402 print_mdoc_node(m, n, h);
403 if (n->next)
404 print_mdoc_nodelist(m, n->next, h);
405 }
406
407
408 static void
409 print_mdoc_node(MDOC_ARGS)
410 {
411 int child;
412 struct tag *t;
413
414 child = 1;
415 t = h->tags.head;
416
417 bufinit(h);
418 switch (n->type) {
419 case (MDOC_ROOT):
420 child = mdoc_root_pre(m, n, h);
421 break;
422 case (MDOC_TEXT):
423 print_text(h, n->string);
424 return;
425 case (MDOC_TBL):
426 return;
427 default:
428 if (mdocs[n->tok].pre && ENDBODY_NOT == n->end)
429 child = (*mdocs[n->tok].pre)(m, n, h);
430 break;
431 }
432
433 if (HTML_KEEP & h->flags) {
434 if (n->prev && n->prev->line != n->line) {
435 h->flags &= ~HTML_KEEP;
436 h->flags |= HTML_PREKEEP;
437 } else if (NULL == n->prev) {
438 if (n->parent && n->parent->line != n->line) {
439 h->flags &= ~HTML_KEEP;
440 h->flags |= HTML_PREKEEP;
441 }
442 }
443 }
444
445 if (child && n->child)
446 print_mdoc_nodelist(m, n->child, h);
447
448 print_stagq(h, t);
449
450 bufinit(h);
451 switch (n->type) {
452 case (MDOC_ROOT):
453 mdoc_root_post(m, n, h);
454 break;
455 default:
456 if (mdocs[n->tok].post && ENDBODY_NOT == n->end)
457 (*mdocs[n->tok].post)(m, n, h);
458 break;
459 }
460 }
461
462 /* ARGSUSED */
463 static void
464 mdoc_root_post(MDOC_ARGS)
465 {
466 struct htmlpair tag[3];
467 struct tag *t, *tt;
468 char b[DATESIZ];
469
470 time2a(m->date, b, DATESIZ);
471
472 PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
473 PAIR_CLASS_INIT(&tag[1], "foot");
474 if (NULL == h->style) {
475 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
476 t = print_otag(h, TAG_TABLE, 3, tag);
477 PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
478 print_otag(h, TAG_COL, 1, tag);
479 print_otag(h, TAG_COL, 1, tag);
480 } else
481 t = print_otag(h, TAG_TABLE, 2, tag);
482
483 t = print_otag(h, TAG_TBODY, 0, NULL);
484
485 tt = print_otag(h, TAG_TR, 0, NULL);
486
487 PAIR_CLASS_INIT(&tag[0], "foot-date");
488 print_otag(h, TAG_TD, 1, tag);
489
490 print_text(h, b);
491 print_stagq(h, tt);
492
493 PAIR_CLASS_INIT(&tag[0], "foot-os");
494 if (NULL == h->style) {
495 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
496 print_otag(h, TAG_TD, 2, tag);
497 } else
498 print_otag(h, TAG_TD, 1, tag);
499
500 print_text(h, m->os);
501 print_tagq(h, t);
502 }
503
504
505 /* ARGSUSED */
506 static int
507 mdoc_root_pre(MDOC_ARGS)
508 {
509 struct htmlpair tag[3];
510 struct tag *t, *tt;
511 char b[BUFSIZ], title[BUFSIZ];
512
513 strlcpy(b, m->vol, BUFSIZ);
514
515 if (m->arch) {
516 strlcat(b, " (", BUFSIZ);
517 strlcat(b, m->arch, BUFSIZ);
518 strlcat(b, ")", BUFSIZ);
519 }
520
521 snprintf(title, BUFSIZ - 1, "%s(%s)", m->title, m->msec);
522
523 PAIR_SUMMARY_INIT(&tag[0], "Document Header");
524 PAIR_CLASS_INIT(&tag[1], "head");
525 if (NULL == h->style) {
526 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
527 t = print_otag(h, TAG_TABLE, 3, tag);
528 PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
529 print_otag(h, TAG_COL, 1, tag);
530 print_otag(h, TAG_COL, 1, tag);
531 print_otag(h, TAG_COL, 1, tag);
532 } else
533 t = print_otag(h, TAG_TABLE, 2, tag);
534
535 print_otag(h, TAG_TBODY, 0, NULL);
536
537 tt = print_otag(h, TAG_TR, 0, NULL);
538
539 PAIR_CLASS_INIT(&tag[0], "head-ltitle");
540 print_otag(h, TAG_TD, 1, tag);
541
542 print_text(h, title);
543 print_stagq(h, tt);
544
545 PAIR_CLASS_INIT(&tag[0], "head-vol");
546 if (NULL == h->style) {
547 PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
548 print_otag(h, TAG_TD, 2, tag);
549 } else
550 print_otag(h, TAG_TD, 1, tag);
551
552 print_text(h, b);
553 print_stagq(h, tt);
554
555 PAIR_CLASS_INIT(&tag[0], "head-rtitle");
556 if (NULL == h->style) {
557 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
558 print_otag(h, TAG_TD, 2, tag);
559 } else
560 print_otag(h, TAG_TD, 1, tag);
561
562 print_text(h, title);
563 print_tagq(h, t);
564 return(1);
565 }
566
567
568 /* ARGSUSED */
569 static int
570 mdoc_sh_pre(MDOC_ARGS)
571 {
572 struct htmlpair tag;
573 char buf[BUFSIZ];
574
575 if (MDOC_BLOCK == n->type) {
576 PAIR_CLASS_INIT(&tag, "section");
577 print_otag(h, TAG_DIV, 1, &tag);
578 return(1);
579 } else if (MDOC_BODY == n->type)
580 return(1);
581
582 buf[0] = '\0';
583 for (n = n->child; n; n = n->next) {
584 html_idcat(buf, n->string, BUFSIZ);
585 if (n->next)
586 html_idcat(buf, " ", BUFSIZ);
587 }
588
589 PAIR_ID_INIT(&tag, buf);
590 print_otag(h, TAG_H1, 1, &tag);
591 return(1);
592 }
593
594
595 /* ARGSUSED */
596 static int
597 mdoc_ss_pre(MDOC_ARGS)
598 {
599 struct htmlpair tag;
600 char buf[BUFSIZ];
601
602 if (MDOC_BLOCK == n->type) {
603 PAIR_CLASS_INIT(&tag, "subsection");
604 print_otag(h, TAG_DIV, 1, &tag);
605 return(1);
606 } else if (MDOC_BODY == n->type)
607 return(1);
608
609 buf[0] = '\0';
610 for (n = n->child; n; n = n->next) {
611 html_idcat(buf, n->string, BUFSIZ);
612 if (n->next)
613 html_idcat(buf, " ", BUFSIZ);
614 }
615
616 PAIR_ID_INIT(&tag, buf);
617 print_otag(h, TAG_H2, 1, &tag);
618 return(1);
619 }
620
621
622 /* ARGSUSED */
623 static int
624 mdoc_fl_pre(MDOC_ARGS)
625 {
626 struct htmlpair tag;
627
628 PAIR_CLASS_INIT(&tag, "flag");
629 print_otag(h, TAG_B, 1, &tag);
630
631 /* `Cm' has no leading hyphen. */
632
633 if (MDOC_Cm == n->tok)
634 return(1);
635
636 print_text(h, "\\-");
637
638 if (n->child)
639 h->flags |= HTML_NOSPACE;
640 else if (n->next && n->next->line == n->line)
641 h->flags |= HTML_NOSPACE;
642
643 return(1);
644 }
645
646
647 /* ARGSUSED */
648 static int
649 mdoc_nd_pre(MDOC_ARGS)
650 {
651 struct htmlpair tag;
652
653 if (MDOC_BODY != n->type)
654 return(1);
655
656 /* XXX: this tag in theory can contain block elements. */
657
658 print_text(h, "\\(em");
659 PAIR_CLASS_INIT(&tag, "desc");
660 print_otag(h, TAG_SPAN, 1, &tag);
661 return(1);
662 }
663
664
665 static int
666 mdoc_nm_pre(MDOC_ARGS)
667 {
668 struct htmlpair tag;
669 struct roffsu su;
670 size_t len;
671
672 switch (n->type) {
673 case (MDOC_ELEM):
674 synopsis_pre(h, n);
675 PAIR_CLASS_INIT(&tag, "name");
676 print_otag(h, TAG_B, 1, &tag);
677 if (NULL == n->child && m->name)
678 print_text(h, m->name);
679 return(1);
680 case (MDOC_HEAD):
681 print_otag(h, TAG_TD, 0, NULL);
682 if (NULL == n->child && m->name)
683 print_text(h, m->name);
684 return(1);
685 case (MDOC_BODY):
686 print_otag(h, TAG_TD, 0, NULL);
687 return(1);
688 default:
689 break;
690 }
691
692 synopsis_pre(h, n);
693 PAIR_CLASS_INIT(&tag, "synopsis");
694 print_otag(h, TAG_TABLE, 1, &tag);
695
696 for (len = 0, n = n->child; n; n = n->next)
697 if (MDOC_TEXT == n->type)
698 len += strlen(n->string);
699
700 if (0 == len && m->name)
701 len = strlen(m->name);
702
703 SCALE_HS_INIT(&su, (double)len);
704 bufcat_su(h, "width", &su);
705 PAIR_STYLE_INIT(&tag, h);
706 print_otag(h, TAG_COL, 1, &tag);
707 print_otag(h, TAG_COL, 0, NULL);
708 print_otag(h, TAG_TBODY, 0, NULL);
709 print_otag(h, TAG_TR, 0, NULL);
710 return(1);
711 }
712
713
714 /* ARGSUSED */
715 static int
716 mdoc_xr_pre(MDOC_ARGS)
717 {
718 struct htmlpair tag[2];
719 const struct mdoc_node *nn;
720
721 if (NULL == n->child)
722 return(0);
723
724 PAIR_CLASS_INIT(&tag[0], "link-man");
725
726 if (h->base_man) {
727 buffmt_man(h, n->child->string,
728 n->child->next ?
729 n->child->next->string : NULL);
730 PAIR_HREF_INIT(&tag[1], h->buf);
731 print_otag(h, TAG_A, 2, tag);
732 } else
733 print_otag(h, TAG_A, 1, tag);
734
735 nn = n->child;
736 print_text(h, nn->string);
737
738 if (NULL == (nn = nn->next))
739 return(0);
740
741 h->flags |= HTML_NOSPACE;
742 print_text(h, "(");
743 h->flags |= HTML_NOSPACE;
744 print_text(h, nn->string);
745 h->flags |= HTML_NOSPACE;
746 print_text(h, ")");
747 return(0);
748 }
749
750
751 /* ARGSUSED */
752 static int
753 mdoc_ns_pre(MDOC_ARGS)
754 {
755
756 h->flags |= HTML_NOSPACE;
757 return(1);
758 }
759
760
761 /* ARGSUSED */
762 static int
763 mdoc_ar_pre(MDOC_ARGS)
764 {
765 struct htmlpair tag;
766
767 PAIR_CLASS_INIT(&tag, "arg");
768 print_otag(h, TAG_I, 1, &tag);
769 return(1);
770 }
771
772
773 /* ARGSUSED */
774 static int
775 mdoc_xx_pre(MDOC_ARGS)
776 {
777 const char *pp;
778 struct htmlpair tag;
779
780 switch (n->tok) {
781 case (MDOC_Bsx):
782 pp = "BSD/OS";
783 break;
784 case (MDOC_Dx):
785 pp = "DragonFly";
786 break;
787 case (MDOC_Fx):
788 pp = "FreeBSD";
789 break;
790 case (MDOC_Nx):
791 pp = "NetBSD";
792 break;
793 case (MDOC_Ox):
794 pp = "OpenBSD";
795 break;
796 case (MDOC_Ux):
797 pp = "UNIX";
798 break;
799 default:
800 return(1);
801 }
802
803 PAIR_CLASS_INIT(&tag, "unix");
804 print_otag(h, TAG_SPAN, 1, &tag);
805 print_text(h, pp);
806 return(1);
807 }
808
809
810 /* ARGSUSED */
811 static int
812 mdoc_bx_pre(MDOC_ARGS)
813 {
814 const struct mdoc_node *nn;
815 struct htmlpair tag;
816
817 PAIR_CLASS_INIT(&tag, "unix");
818 print_otag(h, TAG_SPAN, 1, &tag);
819
820 for (nn = n->child; nn; nn = nn->next)
821 print_mdoc_node(m, nn, h);
822
823 if (n->child)
824 h->flags |= HTML_NOSPACE;
825
826 print_text(h, "BSD");
827 return(0);
828 }
829
830 static int
831 mdoc_it_pre(MDOC_ARGS)
832 {
833 struct roffsu su;
834 enum mdoc_list type;
835 struct htmlpair tag[2];
836 const struct mdoc_node *bl;
837
838 bl = n->parent;
839 while (bl && MDOC_Bl != bl->tok)
840 bl = bl->parent;
841
842 assert(bl);
843
844 type = bl->norm->Bl.type;
845
846 assert(lists[type]);
847 PAIR_CLASS_INIT(&tag[0], lists[type]);
848
849 if (MDOC_HEAD == n->type) {
850 switch (type) {
851 case(LIST_bullet):
852 /* FALLTHROUGH */
853 case(LIST_dash):
854 /* FALLTHROUGH */
855 case(LIST_item):
856 /* FALLTHROUGH */
857 case(LIST_hyphen):
858 /* FALLTHROUGH */
859 case(LIST_enum):
860 return(0);
861 case(LIST_diag):
862 /* FALLTHROUGH */
863 case(LIST_hang):
864 /* FALLTHROUGH */
865 case(LIST_inset):
866 /* FALLTHROUGH */
867 case(LIST_ohang):
868 /* FALLTHROUGH */
869 case(LIST_tag):
870 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
871 bufcat_su(h, "margin-top", &su);
872 PAIR_STYLE_INIT(&tag[1], h);
873 print_otag(h, TAG_DT, 2, tag);
874 if (LIST_diag != type)
875 break;
876 PAIR_CLASS_INIT(&tag[0], "diag");
877 print_otag(h, TAG_B, 1, tag);
878 break;
879 case(LIST_column):
880 break;
881 default:
882 break;
883 }
884 } else if (MDOC_BODY == n->type) {
885 switch (type) {
886 case(LIST_bullet):
887 /* FALLTHROUGH */
888 case(LIST_hyphen):
889 /* FALLTHROUGH */
890 case(LIST_dash):
891 /* FALLTHROUGH */
892 case(LIST_enum):
893 /* FALLTHROUGH */
894 case(LIST_item):
895 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
896 bufcat_su(h, "margin-top", &su);
897 PAIR_STYLE_INIT(&tag[1], h);
898 print_otag(h, TAG_LI, 2, tag);
899 break;
900 case(LIST_diag):
901 /* FALLTHROUGH */
902 case(LIST_hang):
903 /* FALLTHROUGH */
904 case(LIST_inset):
905 /* FALLTHROUGH */
906 case(LIST_ohang):
907 /* FALLTHROUGH */
908 case(LIST_tag):
909 if (NULL == bl->norm->Bl.width) {
910 print_otag(h, TAG_DD, 1, tag);
911 break;
912 }
913 a2width(bl->norm->Bl.width, &su);
914 bufcat_su(h, "margin-left", &su);
915 PAIR_STYLE_INIT(&tag[1], h);
916 print_otag(h, TAG_DD, 2, tag);
917 break;
918 case(LIST_column):
919 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
920 bufcat_su(h, "margin-top", &su);
921 PAIR_STYLE_INIT(&tag[1], h);
922 print_otag(h, TAG_TD, 2, tag);
923 break;
924 default:
925 break;
926 }
927 } else {
928 switch (type) {
929 case (LIST_column):
930 print_otag(h, TAG_TR, 1, tag);
931 break;
932 default:
933 break;
934 }
935 }
936
937 return(1);
938 }
939
940 /* ARGSUSED */
941 static int
942 mdoc_bl_pre(MDOC_ARGS)
943 {
944 size_t i;
945 struct htmlpair tag[3];
946 struct roffsu su;
947 char buf[BUFSIZ];
948
949 if (MDOC_BODY == n->type) {
950 if (LIST_column == n->norm->Bl.type)
951 print_otag(h, TAG_TBODY, 0, NULL);
952 return(1);
953 }
954
955 if (MDOC_HEAD == n->type) {
956 if (LIST_column != n->norm->Bl.type)
957 return(0);
958
959 /*
960 * For each column, print out the <COL> tag with our
961 * suggested width. The last column gets min-width, as
962 * in terminal mode it auto-sizes to the width of the
963 * screen and we want to preserve that behaviour.
964 */
965
966 for (i = 0; i < n->norm->Bl.ncols; i++) {
967 a2width(n->norm->Bl.cols[i], &su);
968 bufinit(h);
969 if (i < n->norm->Bl.ncols - 1)
970 bufcat_su(h, "width", &su);
971 else
972 bufcat_su(h, "min-width", &su);
973 PAIR_STYLE_INIT(&tag[0], h);
974 print_otag(h, TAG_COL, 1, tag);
975 }
976
977 return(0);
978 }
979
980 SCALE_VS_INIT(&su, 0);
981 bufcat_su(h, "margin-top", &su);
982 bufcat_su(h, "margin-bottom", &su);
983 PAIR_STYLE_INIT(&tag[0], h);
984
985 assert(lists[n->norm->Bl.type]);
986 strlcpy(buf, "list ", BUFSIZ);
987 strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
988 PAIR_INIT(&tag[1], ATTR_CLASS, buf);
989
990 /* Set the block's left-hand margin. */
991
992 if (n->norm->Bl.offs) {
993 a2offs(n->norm->Bl.offs, &su);
994 bufcat_su(h, "margin-left", &su);
995 }
996
997 switch (n->norm->Bl.type) {
998 case(LIST_bullet):
999 /* FALLTHROUGH */
1000 case(LIST_dash):
1001 /* FALLTHROUGH */
1002 case(LIST_hyphen):
1003 /* FALLTHROUGH */
1004 case(LIST_item):
1005 print_otag(h, TAG_UL, 2, tag);
1006 break;
1007 case(LIST_enum):
1008 print_otag(h, TAG_OL, 2, tag);
1009 break;
1010 case(LIST_diag):
1011 /* FALLTHROUGH */
1012 case(LIST_hang):
1013 /* FALLTHROUGH */
1014 case(LIST_inset):
1015 /* FALLTHROUGH */
1016 case(LIST_ohang):
1017 /* FALLTHROUGH */
1018 case(LIST_tag):
1019 print_otag(h, TAG_DL, 2, tag);
1020 break;
1021 case(LIST_column):
1022 print_otag(h, TAG_TABLE, 2, tag);
1023 break;
1024 default:
1025 abort();
1026 /* NOTREACHED */
1027 }
1028
1029 return(1);
1030 }
1031
1032 /* ARGSUSED */
1033 static int
1034 mdoc_ex_pre(MDOC_ARGS)
1035 {
1036 const struct mdoc_node *nn;
1037 struct tag *t;
1038 struct htmlpair tag;
1039
1040 if (n->prev)
1041 print_otag(h, TAG_BR, 0, NULL);
1042
1043 PAIR_CLASS_INIT(&tag, "utility");
1044
1045 print_text(h, "The");
1046 for (nn = n->child; nn; nn = nn->next) {
1047 t = print_otag(h, TAG_B, 1, &tag);
1048 print_text(h, nn->string);
1049 print_tagq(h, t);
1050
1051 h->flags |= HTML_NOSPACE;
1052
1053 if (nn->next && NULL == nn->next->next)
1054 print_text(h, ", and");
1055 else if (nn->next)
1056 print_text(h, ",");
1057 else
1058 h->flags &= ~HTML_NOSPACE;
1059 }
1060
1061 if (n->child && n->child->next)
1062 print_text(h, "utilities exit");
1063 else
1064 print_text(h, "utility exits");
1065
1066 print_text(h, "0 on success, and >0 if an error occurs.");
1067 return(0);
1068 }
1069
1070
1071 /* ARGSUSED */
1072 static int
1073 mdoc_em_pre(MDOC_ARGS)
1074 {
1075 struct htmlpair tag;
1076
1077 PAIR_CLASS_INIT(&tag, "emph");
1078 print_otag(h, TAG_SPAN, 1, &tag);
1079 return(1);
1080 }
1081
1082
1083 /* ARGSUSED */
1084 static int
1085 mdoc_d1_pre(MDOC_ARGS)
1086 {
1087 struct htmlpair tag[2];
1088 struct roffsu su;
1089
1090 if (MDOC_BLOCK != n->type)
1091 return(1);
1092
1093 SCALE_VS_INIT(&su, 0);
1094 bufcat_su(h, "margin-top", &su);
1095 bufcat_su(h, "margin-bottom", &su);
1096 PAIR_STYLE_INIT(&tag[0], h);
1097 print_otag(h, TAG_BLOCKQUOTE, 1, tag);
1098
1099 /* BLOCKQUOTE needs a block body. */
1100
1101 PAIR_CLASS_INIT(&tag[0], "display");
1102 print_otag(h, TAG_DIV, 1, tag);
1103
1104 if (MDOC_Dl == n->tok) {
1105 PAIR_CLASS_INIT(&tag[0], "lit");
1106 print_otag(h, TAG_CODE, 1, tag);
1107 }
1108
1109 return(1);
1110 }
1111
1112
1113 /* ARGSUSED */
1114 static int
1115 mdoc_sx_pre(MDOC_ARGS)
1116 {
1117 struct htmlpair tag[2];
1118 const struct mdoc_node *nn;
1119 char buf[BUFSIZ];
1120
1121 strlcpy(buf, "#", BUFSIZ);
1122 for (nn = n->child; nn; nn = nn->next) {
1123 html_idcat(buf, nn->string, BUFSIZ);
1124 if (nn->next)
1125 html_idcat(buf, " ", BUFSIZ);
1126 }
1127
1128 PAIR_CLASS_INIT(&tag[0], "link-sec");
1129 PAIR_HREF_INIT(&tag[1], buf);
1130
1131 print_otag(h, TAG_I, 1, tag);
1132 print_otag(h, TAG_A, 2, tag);
1133 return(1);
1134 }
1135
1136
1137 /* ARGSUSED */
1138 static int
1139 mdoc_bd_pre(MDOC_ARGS)
1140 {
1141 struct htmlpair tag[2];
1142 int comp;
1143 const struct mdoc_node *nn;
1144 struct roffsu su;
1145
1146 if (MDOC_HEAD == n->type)
1147 return(0);
1148
1149 if (MDOC_BLOCK == n->type) {
1150 comp = n->norm->Bd.comp;
1151 for (nn = n; nn && ! comp; nn = nn->parent) {
1152 if (MDOC_BLOCK != nn->type)
1153 continue;
1154 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
1155 comp = 1;
1156 if (nn->prev)
1157 break;
1158 }
1159 if ( ! comp)
1160 print_otag(h, TAG_P, 0, NULL);
1161 return(1);
1162 }
1163
1164 SCALE_HS_INIT(&su, 0);
1165 if (n->norm->Bd.offs)
1166 a2offs(n->norm->Bd.offs, &su);
1167
1168 bufcat_su(h, "margin-left", &su);
1169 PAIR_STYLE_INIT(&tag[0], h);
1170
1171 if (DISP_unfilled != n->norm->Bd.type &&
1172 DISP_literal != n->norm->Bd.type) {
1173 PAIR_CLASS_INIT(&tag[1], "display");
1174 print_otag(h, TAG_DIV, 2, tag);
1175 return(1);
1176 }
1177
1178 PAIR_CLASS_INIT(&tag[1], "lit display");
1179 print_otag(h, TAG_PRE, 2, tag);
1180
1181 for (nn = n->child; nn; nn = nn->next) {
1182 print_mdoc_node(m, nn, h);
1183 /*
1184 * If the printed node flushes its own line, then we
1185 * needn't do it here as well. This is hacky, but the
1186 * notion of selective eoln whitespace is pretty dumb
1187 * anyway, so don't sweat it.
1188 */
1189 switch (nn->tok) {
1190 case (MDOC_Sm):
1191 /* FALLTHROUGH */
1192 case (MDOC_br):
1193 /* FALLTHROUGH */
1194 case (MDOC_sp):
1195 /* FALLTHROUGH */
1196 case (MDOC_Bl):
1197 /* FALLTHROUGH */
1198 case (MDOC_D1):
1199 /* FALLTHROUGH */
1200 case (MDOC_Dl):
1201 /* FALLTHROUGH */
1202 case (MDOC_Lp):
1203 /* FALLTHROUGH */
1204 case (MDOC_Pp):
1205 continue;
1206 default:
1207 break;
1208 }
1209 if (nn->next && nn->next->line == nn->line)
1210 continue;
1211 else if (nn->next)
1212 print_text(h, "\n");
1213
1214 h->flags |= HTML_NOSPACE;
1215 }
1216
1217 return(0);
1218 }
1219
1220
1221 /* ARGSUSED */
1222 static int
1223 mdoc_pa_pre(MDOC_ARGS)
1224 {
1225 struct htmlpair tag;
1226
1227 PAIR_CLASS_INIT(&tag, "file");
1228 print_otag(h, TAG_I, 1, &tag);
1229 return(1);
1230 }
1231
1232
1233 /* ARGSUSED */
1234 static int
1235 mdoc_ad_pre(MDOC_ARGS)
1236 {
1237 struct htmlpair tag;
1238
1239 PAIR_CLASS_INIT(&tag, "addr");
1240 print_otag(h, TAG_I, 1, &tag);
1241 return(1);
1242 }
1243
1244
1245 /* ARGSUSED */
1246 static int
1247 mdoc_an_pre(MDOC_ARGS)
1248 {
1249 struct htmlpair tag;
1250
1251 /* TODO: -split and -nosplit (see termp_an_pre()). */
1252
1253 PAIR_CLASS_INIT(&tag, "author");
1254 print_otag(h, TAG_SPAN, 1, &tag);
1255 return(1);
1256 }
1257
1258
1259 /* ARGSUSED */
1260 static int
1261 mdoc_cd_pre(MDOC_ARGS)
1262 {
1263 struct htmlpair tag;
1264
1265 synopsis_pre(h, n);
1266 PAIR_CLASS_INIT(&tag, "config");
1267 print_otag(h, TAG_B, 1, &tag);
1268 return(1);
1269 }
1270
1271
1272 /* ARGSUSED */
1273 static int
1274 mdoc_dv_pre(MDOC_ARGS)
1275 {
1276 struct htmlpair tag;
1277
1278 PAIR_CLASS_INIT(&tag, "define");
1279 print_otag(h, TAG_SPAN, 1, &tag);
1280 return(1);
1281 }
1282
1283
1284 /* ARGSUSED */
1285 static int
1286 mdoc_ev_pre(MDOC_ARGS)
1287 {
1288 struct htmlpair tag;
1289
1290 PAIR_CLASS_INIT(&tag, "env");
1291 print_otag(h, TAG_SPAN, 1, &tag);
1292 return(1);
1293 }
1294
1295
1296 /* ARGSUSED */
1297 static int
1298 mdoc_er_pre(MDOC_ARGS)
1299 {
1300 struct htmlpair tag;
1301
1302 PAIR_CLASS_INIT(&tag, "errno");
1303 print_otag(h, TAG_SPAN, 1, &tag);
1304 return(1);
1305 }
1306
1307
1308 /* ARGSUSED */
1309 static int
1310 mdoc_fa_pre(MDOC_ARGS)
1311 {
1312 const struct mdoc_node *nn;
1313 struct htmlpair tag;
1314 struct tag *t;
1315
1316 PAIR_CLASS_INIT(&tag, "farg");
1317 if (n->parent->tok != MDOC_Fo) {
1318 print_otag(h, TAG_I, 1, &tag);
1319 return(1);
1320 }
1321
1322 for (nn = n->child; nn; nn = nn->next) {
1323 t = print_otag(h, TAG_I, 1, &tag);
1324 print_text(h, nn->string);
1325 print_tagq(h, t);
1326 if (nn->next)
1327 print_text(h, ",");
1328 }
1329
1330 if (n->child && n->next && n->next->tok == MDOC_Fa)
1331 print_text(h, ",");
1332
1333 return(0);
1334 }
1335
1336
1337 /* ARGSUSED */
1338 static int
1339 mdoc_fd_pre(MDOC_ARGS)
1340 {
1341 struct htmlpair tag;
1342
1343 synopsis_pre(h, n);
1344
1345 PAIR_CLASS_INIT(&tag, "macro");
1346 print_otag(h, TAG_B, 1, &tag);
1347 return(1);
1348 }
1349
1350
1351 /* ARGSUSED */
1352 static int
1353 mdoc_vt_pre(MDOC_ARGS)
1354 {
1355 struct htmlpair tag;
1356
1357 if (MDOC_BLOCK == n->type) {
1358 synopsis_pre(h, n);
1359 return(1);
1360 } else if (MDOC_ELEM == n->type) {
1361 synopsis_pre(h, n);
1362 } else if (MDOC_HEAD == n->type)
1363 return(0);
1364
1365 PAIR_CLASS_INIT(&tag, "type");
1366 print_otag(h, TAG_SPAN, 1, &tag);
1367 return(1);
1368 }
1369
1370
1371 /* ARGSUSED */
1372 static int
1373 mdoc_ft_pre(MDOC_ARGS)
1374 {
1375 struct htmlpair tag;
1376
1377 synopsis_pre(h, n);
1378 PAIR_CLASS_INIT(&tag, "ftype");
1379 print_otag(h, TAG_I, 1, &tag);
1380 return(1);
1381 }
1382
1383
1384 /* ARGSUSED */
1385 static int
1386 mdoc_fn_pre(MDOC_ARGS)
1387 {
1388 struct tag *t;
1389 struct htmlpair tag[2];
1390 const struct mdoc_node *nn;
1391 char nbuf[BUFSIZ];
1392 const char *sp, *ep;
1393 int sz, i;
1394
1395 synopsis_pre(h, n);
1396
1397 /* Split apart into type and name. */
1398 assert(n->child->string);
1399 sp = n->child->string;
1400
1401 ep = strchr(sp, ' ');
1402 if (NULL != ep) {
1403 PAIR_CLASS_INIT(&tag[0], "ftype");
1404 t = print_otag(h, TAG_I, 1, tag);
1405
1406 while (ep) {
1407 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1408 (void)memcpy(nbuf, sp, (size_t)sz);
1409 nbuf[sz] = '\0';
1410 print_text(h, nbuf);
1411 sp = ++ep;
1412 ep = strchr(sp, ' ');
1413 }
1414 print_tagq(h, t);
1415 }
1416
1417 PAIR_CLASS_INIT(&tag[0], "fname");
1418
1419 /*
1420 * FIXME: only refer to IDs that we know exist.
1421 */
1422
1423 #if 0
1424 if (MDOC_SYNPRETTY & n->flags) {
1425 nbuf[0] = '\0';
1426 html_idcat(nbuf, sp, BUFSIZ);
1427 PAIR_ID_INIT(&tag[1], nbuf);
1428 } else {
1429 strlcpy(nbuf, "#", BUFSIZ);
1430 html_idcat(nbuf, sp, BUFSIZ);
1431 PAIR_HREF_INIT(&tag[1], nbuf);
1432 }
1433 #endif
1434
1435 t = print_otag(h, TAG_B, 1, tag);
1436
1437 if (sp) {
1438 strlcpy(nbuf, sp, BUFSIZ);
1439 print_text(h, nbuf);
1440 }
1441
1442 print_tagq(h, t);
1443
1444 h->flags |= HTML_NOSPACE;
1445 print_text(h, "(");
1446
1447 bufinit(h);
1448 PAIR_CLASS_INIT(&tag[0], "farg");
1449 bufcat_style(h, "white-space", "nowrap");
1450 PAIR_STYLE_INIT(&tag[1], h);
1451
1452 for (nn = n->child->next; nn; nn = nn->next) {
1453 i = 1;
1454 if (MDOC_SYNPRETTY & n->flags)
1455 i = 2;
1456 t = print_otag(h, TAG_I, i, tag);
1457 print_text(h, nn->string);
1458 print_tagq(h, t);
1459 if (nn->next)
1460 print_text(h, ",");
1461 }
1462
1463 print_text(h, ")");
1464 if (MDOC_SYNPRETTY & n->flags)
1465 print_text(h, ";");
1466
1467 return(0);
1468 }
1469
1470
1471 /* ARGSUSED */
1472 static int
1473 mdoc_sm_pre(MDOC_ARGS)
1474 {
1475
1476 assert(n->child && MDOC_TEXT == n->child->type);
1477 if (0 == strcmp("on", n->child->string)) {
1478 /*
1479 * FIXME: no p->col to check. Thus, if we have
1480 * .Bd -literal
1481 * .Sm off
1482 * 1 2
1483 * .Sm on
1484 * 3
1485 * .Ed
1486 * the "3" is preceded by a space.
1487 */
1488 h->flags &= ~HTML_NOSPACE;
1489 h->flags &= ~HTML_NONOSPACE;
1490 } else
1491 h->flags |= HTML_NONOSPACE;
1492
1493 return(0);
1494 }
1495
1496 /* ARGSUSED */
1497 static int
1498 mdoc_pp_pre(MDOC_ARGS)
1499 {
1500
1501 print_otag(h, TAG_P, 0, NULL);
1502 return(0);
1503
1504 }
1505
1506 /* ARGSUSED */
1507 static int
1508 mdoc_sp_pre(MDOC_ARGS)
1509 {
1510 struct roffsu su;
1511 struct htmlpair tag;
1512
1513 SCALE_VS_INIT(&su, 1);
1514
1515 if (MDOC_sp == n->tok) {
1516 if (n->child)
1517 a2roffsu(n->child->string, &su, SCALE_VS);
1518 } else
1519 su.scale = 0;
1520
1521 bufcat_su(h, "height", &su);
1522 PAIR_STYLE_INIT(&tag, h);
1523 print_otag(h, TAG_DIV, 1, &tag);
1524
1525 /* So the div isn't empty: */
1526 print_text(h, "\\~");
1527
1528 return(0);
1529
1530 }
1531
1532 /* ARGSUSED */
1533 static int
1534 mdoc_lk_pre(MDOC_ARGS)
1535 {
1536 const struct mdoc_node *nn;
1537 struct htmlpair tag[2];
1538
1539 nn = n->child;
1540
1541 PAIR_CLASS_INIT(&tag[0], "link-ext");
1542 PAIR_HREF_INIT(&tag[1], nn->string);
1543 print_otag(h, TAG_A, 2, tag);
1544
1545 if (NULL == nn || NULL == nn->next)
1546 return(1);
1547
1548 for (nn = nn->next; nn; nn = nn->next)
1549 print_text(h, nn->string);
1550
1551 return(0);
1552 }
1553
1554
1555 /* ARGSUSED */
1556 static int
1557 mdoc_mt_pre(MDOC_ARGS)
1558 {
1559 struct htmlpair tag[2];
1560 struct tag *t;
1561 const struct mdoc_node *nn;
1562
1563 PAIR_CLASS_INIT(&tag[0], "link-mail");
1564
1565 for (nn = n->child; nn; nn = nn->next) {
1566 bufinit(h);
1567 bufcat(h, "mailto:");
1568 bufcat(h, nn->string);
1569 PAIR_HREF_INIT(&tag[1], h->buf);
1570 t = print_otag(h, TAG_A, 2, tag);
1571 print_text(h, nn->string);
1572 print_tagq(h, t);
1573 }
1574
1575 return(0);
1576 }
1577
1578
1579 /* ARGSUSED */
1580 static int
1581 mdoc_fo_pre(MDOC_ARGS)
1582 {
1583 struct htmlpair tag;
1584 struct tag *t;
1585
1586 if (MDOC_BODY == n->type) {
1587 h->flags |= HTML_NOSPACE;
1588 print_text(h, "(");
1589 h->flags |= HTML_NOSPACE;
1590 return(1);
1591 } else if (MDOC_BLOCK == n->type) {
1592 synopsis_pre(h, n);
1593 return(1);
1594 }
1595
1596 /* XXX: we drop non-initial arguments as per groff. */
1597
1598 assert(n->child);
1599 assert(n->child->string);
1600
1601 PAIR_CLASS_INIT(&tag, "fname");
1602 t = print_otag(h, TAG_B, 1, &tag);
1603 print_text(h, n->child->string);
1604 print_tagq(h, t);
1605 return(0);
1606 }
1607
1608
1609 /* ARGSUSED */
1610 static void
1611 mdoc_fo_post(MDOC_ARGS)
1612 {
1613
1614 if (MDOC_BODY != n->type)
1615 return;
1616 print_text(h, ")");
1617 print_text(h, ";");
1618 }
1619
1620
1621 /* ARGSUSED */
1622 static int
1623 mdoc_in_pre(MDOC_ARGS)
1624 {
1625 const struct mdoc_node *nn;
1626 struct tag *t;
1627 struct htmlpair tag[2];
1628 int i;
1629
1630 synopsis_pre(h, n);
1631
1632 PAIR_CLASS_INIT(&tag[0], "includes");
1633 print_otag(h, TAG_B, 1, tag);
1634
1635 if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags)
1636 print_text(h, "#include");
1637
1638 print_text(h, "<");
1639 h->flags |= HTML_NOSPACE;
1640
1641 for (nn = n->child; nn; nn = nn->next) {
1642 PAIR_CLASS_INIT(&tag[0], "link-includes");
1643 i = 1;
1644 bufinit(h);
1645 if (h->base_includes) {
1646 buffmt_includes(h, nn->string);
1647 PAIR_HREF_INIT(&tag[i], h->buf);
1648 i++;
1649 }
1650 t = print_otag(h, TAG_A, i, tag);
1651 print_mdoc_node(m, nn, h);
1652 print_tagq(h, t);
1653 }
1654
1655 h->flags |= HTML_NOSPACE;
1656 print_text(h, ">");
1657
1658 return(0);
1659 }
1660
1661
1662 /* ARGSUSED */
1663 static int
1664 mdoc_ic_pre(MDOC_ARGS)
1665 {
1666 struct htmlpair tag;
1667
1668 PAIR_CLASS_INIT(&tag, "cmd");
1669 print_otag(h, TAG_B, 1, &tag);
1670 return(1);
1671 }
1672
1673
1674 /* ARGSUSED */
1675 static int
1676 mdoc_rv_pre(MDOC_ARGS)
1677 {
1678 const struct mdoc_node *nn;
1679 struct htmlpair tag;
1680 struct tag *t;
1681
1682 if (n->prev)
1683 print_otag(h, TAG_BR, 0, NULL);
1684
1685 print_text(h, "The");
1686
1687 for (nn = n->child; nn; nn = nn->next) {
1688 PAIR_CLASS_INIT(&tag, "fname");
1689 t = print_otag(h, TAG_B, 1, &tag);
1690 print_text(h, nn->string);
1691 print_tagq(h, t);
1692
1693 h->flags |= HTML_NOSPACE;
1694 if (nn->next && NULL == nn->next->next)
1695 print_text(h, "(), and");
1696 else if (nn->next)
1697 print_text(h, "(),");
1698 else
1699 print_text(h, "()");
1700 }
1701
1702 if (n->child && n->child->next)
1703 print_text(h, "functions return");
1704 else
1705 print_text(h, "function returns");
1706
1707 print_text(h, "the value 0 if successful; otherwise the value "
1708 "-1 is returned and the global variable");
1709
1710 PAIR_CLASS_INIT(&tag, "var");
1711 t = print_otag(h, TAG_B, 1, &tag);
1712 print_text(h, "errno");
1713 print_tagq(h, t);
1714 print_text(h, "is set to indicate the error.");
1715 return(0);
1716 }
1717
1718
1719 /* ARGSUSED */
1720 static int
1721 mdoc_va_pre(MDOC_ARGS)
1722 {
1723 struct htmlpair tag;
1724
1725 PAIR_CLASS_INIT(&tag, "var");
1726 print_otag(h, TAG_B, 1, &tag);
1727 return(1);
1728 }
1729
1730
1731 /* ARGSUSED */
1732 static int
1733 mdoc_ap_pre(MDOC_ARGS)
1734 {
1735
1736 h->flags |= HTML_NOSPACE;
1737 print_text(h, "\\(aq");
1738 h->flags |= HTML_NOSPACE;
1739 return(1);
1740 }
1741
1742
1743 /* ARGSUSED */
1744 static int
1745 mdoc_bf_pre(MDOC_ARGS)
1746 {
1747 struct htmlpair tag[2];
1748 struct roffsu su;
1749
1750 if (MDOC_HEAD == n->type)
1751 return(0);
1752 else if (MDOC_BODY != n->type)
1753 return(1);
1754
1755 if (FONT_Em == n->norm->Bf.font)
1756 PAIR_CLASS_INIT(&tag[0], "emph");
1757 else if (FONT_Sy == n->norm->Bf.font)
1758 PAIR_CLASS_INIT(&tag[0], "symb");
1759 else if (FONT_Li == n->norm->Bf.font)
1760 PAIR_CLASS_INIT(&tag[0], "lit");
1761 else
1762 PAIR_CLASS_INIT(&tag[0], "none");
1763
1764 /*
1765 * We want this to be inline-formatted, but needs to be div to
1766 * accept block children.
1767 */
1768 bufcat_style(h, "display", "inline");
1769 SCALE_HS_INIT(&su, 1);
1770 /* Needs a left-margin for spacing. */
1771 bufcat_su(h, "margin-left", &su);
1772 PAIR_STYLE_INIT(&tag[1], h);
1773 print_otag(h, TAG_DIV, 2, tag);
1774 return(1);
1775 }
1776
1777
1778 /* ARGSUSED */
1779 static int
1780 mdoc_ms_pre(MDOC_ARGS)
1781 {
1782 struct htmlpair tag;
1783
1784 PAIR_CLASS_INIT(&tag, "symb");
1785 print_otag(h, TAG_SPAN, 1, &tag);
1786 return(1);
1787 }
1788
1789
1790 /* ARGSUSED */
1791 static int
1792 mdoc_igndelim_pre(MDOC_ARGS)
1793 {
1794
1795 h->flags |= HTML_IGNDELIM;
1796 return(1);
1797 }
1798
1799
1800 /* ARGSUSED */
1801 static void
1802 mdoc_pf_post(MDOC_ARGS)
1803 {
1804
1805 h->flags |= HTML_NOSPACE;
1806 }
1807
1808
1809 /* ARGSUSED */
1810 static int
1811 mdoc_rs_pre(MDOC_ARGS)
1812 {
1813 struct htmlpair tag;
1814
1815 if (MDOC_BLOCK != n->type)
1816 return(1);
1817
1818 if (n->prev && SEC_SEE_ALSO == n->sec)
1819 print_otag(h, TAG_P, 0, NULL);
1820
1821 PAIR_CLASS_INIT(&tag, "ref");
1822 print_otag(h, TAG_SPAN, 1, &tag);
1823 return(1);
1824 }
1825
1826
1827
1828 /* ARGSUSED */
1829 static int
1830 mdoc_li_pre(MDOC_ARGS)
1831 {
1832 struct htmlpair tag;
1833
1834 PAIR_CLASS_INIT(&tag, "lit");
1835 print_otag(h, TAG_SPAN, 1, &tag);
1836 return(1);
1837 }
1838
1839
1840 /* ARGSUSED */
1841 static int
1842 mdoc_sy_pre(MDOC_ARGS)
1843 {
1844 struct htmlpair tag;
1845
1846 PAIR_CLASS_INIT(&tag, "symb");
1847 print_otag(h, TAG_SPAN, 1, &tag);
1848 return(1);
1849 }
1850
1851
1852 /* ARGSUSED */
1853 static int
1854 mdoc_bt_pre(MDOC_ARGS)
1855 {
1856
1857 print_text(h, "is currently in beta test.");
1858 return(0);
1859 }
1860
1861
1862 /* ARGSUSED */
1863 static int
1864 mdoc_ud_pre(MDOC_ARGS)
1865 {
1866
1867 print_text(h, "currently under development.");
1868 return(0);
1869 }
1870
1871
1872 /* ARGSUSED */
1873 static int
1874 mdoc_lb_pre(MDOC_ARGS)
1875 {
1876 struct htmlpair tag;
1877
1878 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags && n->prev)
1879 print_otag(h, TAG_BR, 0, NULL);
1880
1881 PAIR_CLASS_INIT(&tag, "lib");
1882 print_otag(h, TAG_SPAN, 1, &tag);
1883 return(1);
1884 }
1885
1886
1887 /* ARGSUSED */
1888 static int
1889 mdoc__x_pre(MDOC_ARGS)
1890 {
1891 struct htmlpair tag[2];
1892 enum htmltag t;
1893
1894 t = TAG_SPAN;
1895
1896 switch (n->tok) {
1897 case(MDOC__A):
1898 PAIR_CLASS_INIT(&tag[0], "ref-auth");
1899 if (n->prev && MDOC__A == n->prev->tok)
1900 if (NULL == n->next || MDOC__A != n->next->tok)
1901 print_text(h, "and");
1902 break;
1903 case(MDOC__B):
1904 PAIR_CLASS_INIT(&tag[0], "ref-book");
1905 t = TAG_I;
1906 break;
1907 case(MDOC__C):
1908 PAIR_CLASS_INIT(&tag[0], "ref-city");
1909 break;
1910 case(MDOC__D):
1911 PAIR_CLASS_INIT(&tag[0], "ref-date");
1912 break;
1913 case(MDOC__I):
1914 PAIR_CLASS_INIT(&tag[0], "ref-issue");
1915 t = TAG_I;
1916 break;
1917 case(MDOC__J):
1918 PAIR_CLASS_INIT(&tag[0], "ref-jrnl");
1919 t = TAG_I;
1920 break;
1921 case(MDOC__N):
1922 PAIR_CLASS_INIT(&tag[0], "ref-num");
1923 break;
1924 case(MDOC__O):
1925 PAIR_CLASS_INIT(&tag[0], "ref-opt");
1926 break;
1927 case(MDOC__P):
1928 PAIR_CLASS_INIT(&tag[0], "ref-page");
1929 break;
1930 case(MDOC__Q):
1931 PAIR_CLASS_INIT(&tag[0], "ref-corp");
1932 break;
1933 case(MDOC__R):
1934 PAIR_CLASS_INIT(&tag[0], "ref-rep");
1935 break;
1936 case(MDOC__T):
1937 PAIR_CLASS_INIT(&tag[0], "ref-title");
1938 break;
1939 case(MDOC__U):
1940 PAIR_CLASS_INIT(&tag[0], "link-ref");
1941 break;
1942 case(MDOC__V):
1943 PAIR_CLASS_INIT(&tag[0], "ref-vol");
1944 break;
1945 default:
1946 abort();
1947 /* NOTREACHED */
1948 }
1949
1950 if (MDOC__U != n->tok) {
1951 print_otag(h, t, 1, tag);
1952 return(1);
1953 }
1954
1955 PAIR_HREF_INIT(&tag[1], n->child->string);
1956 print_otag(h, TAG_A, 2, tag);
1957
1958 return(1);
1959 }
1960
1961
1962 /* ARGSUSED */
1963 static void
1964 mdoc__x_post(MDOC_ARGS)
1965 {
1966
1967 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
1968 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
1969 if (NULL == n->prev || MDOC__A != n->prev->tok)
1970 return;
1971
1972 /* TODO: %U */
1973
1974 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
1975 return;
1976
1977 print_text(h, n->next ? "," : ".");
1978 }
1979
1980
1981 /* ARGSUSED */
1982 static int
1983 mdoc_bk_pre(MDOC_ARGS)
1984 {
1985
1986 switch (n->type) {
1987 case (MDOC_BLOCK):
1988 break;
1989 case (MDOC_HEAD):
1990 return(0);
1991 case (MDOC_BODY):
1992 if (n->parent->args || 0 == n->prev->nchild)
1993 h->flags |= HTML_PREKEEP;
1994 break;
1995 default:
1996 abort();
1997 /* NOTREACHED */
1998 }
1999
2000 return(1);
2001 }
2002
2003
2004 /* ARGSUSED */
2005 static void
2006 mdoc_bk_post(MDOC_ARGS)
2007 {
2008
2009 if (MDOC_BODY == n->type)
2010 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
2011 }
2012
2013
2014 /* ARGSUSED */
2015 static int
2016 mdoc_quote_pre(MDOC_ARGS)
2017 {
2018 struct htmlpair tag;
2019
2020 if (MDOC_BODY != n->type)
2021 return(1);
2022
2023 switch (n->tok) {
2024 case (MDOC_Ao):
2025 /* FALLTHROUGH */
2026 case (MDOC_Aq):
2027 print_text(h, "\\(la");
2028 break;
2029 case (MDOC_Bro):
2030 /* FALLTHROUGH */
2031 case (MDOC_Brq):
2032 print_text(h, "\\(lC");
2033 break;
2034 case (MDOC_Bo):
2035 /* FALLTHROUGH */
2036 case (MDOC_Bq):
2037 print_text(h, "\\(lB");
2038 break;
2039 case (MDOC_Oo):
2040 /* FALLTHROUGH */
2041 case (MDOC_Op):
2042 print_text(h, "\\(lB");
2043 h->flags |= HTML_NOSPACE;
2044 PAIR_CLASS_INIT(&tag, "opt");
2045 print_otag(h, TAG_SPAN, 1, &tag);
2046 break;
2047 case (MDOC_Do):
2048 /* FALLTHROUGH */
2049 case (MDOC_Dq):
2050 /* FALLTHROUGH */
2051 case (MDOC_Qo):
2052 /* FALLTHROUGH */
2053 case (MDOC_Qq):
2054 print_text(h, "\\(lq");
2055 break;
2056 case (MDOC_Po):
2057 /* FALLTHROUGH */
2058 case (MDOC_Pq):
2059 print_text(h, "(");
2060 break;
2061 case (MDOC_Ql):
2062 /* FALLTHROUGH */
2063 case (MDOC_So):
2064 /* FALLTHROUGH */
2065 case (MDOC_Sq):
2066 print_text(h, "\\(oq");
2067 break;
2068 default:
2069 abort();
2070 /* NOTREACHED */
2071 }
2072
2073 h->flags |= HTML_NOSPACE;
2074 return(1);
2075 }
2076
2077
2078 /* ARGSUSED */
2079 static void
2080 mdoc_quote_post(MDOC_ARGS)
2081 {
2082
2083 if (MDOC_BODY != n->type)
2084 return;
2085
2086 h->flags |= HTML_NOSPACE;
2087
2088 switch (n->tok) {
2089 case (MDOC_Ao):
2090 /* FALLTHROUGH */
2091 case (MDOC_Aq):
2092 print_text(h, "\\(ra");
2093 break;
2094 case (MDOC_Bro):
2095 /* FALLTHROUGH */
2096 case (MDOC_Brq):
2097 print_text(h, "\\(rC");
2098 break;
2099 case (MDOC_Oo):
2100 /* FALLTHROUGH */
2101 case (MDOC_Op):
2102 /* FALLTHROUGH */
2103 case (MDOC_Bo):
2104 /* FALLTHROUGH */
2105 case (MDOC_Bq):
2106 print_text(h, "\\(rB");
2107 break;
2108 case (MDOC_Qo):
2109 /* FALLTHROUGH */
2110 case (MDOC_Qq):
2111 /* FALLTHROUGH */
2112 case (MDOC_Do):
2113 /* FALLTHROUGH */
2114 case (MDOC_Dq):
2115 print_text(h, "\\(rq");
2116 break;
2117 case (MDOC_Po):
2118 /* FALLTHROUGH */
2119 case (MDOC_Pq):
2120 print_text(h, ")");
2121 break;
2122 case (MDOC_Ql):
2123 /* FALLTHROUGH */
2124 case (MDOC_So):
2125 /* FALLTHROUGH */
2126 case (MDOC_Sq):
2127 print_text(h, "\\(aq");
2128 break;
2129 default:
2130 abort();
2131 /* NOTREACHED */
2132 }
2133 }
2134
2135