]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
If `Ns' is specified on its own line, it should be ignored. This is
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.150 2011/02/02 21:40:45 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 /* No tables in this mode... */
424 assert(NULL == h->tblt);
425
426 /*
427 * Make sure that if we're in a literal mode already
428 * (i.e., within a <PRE>) don't print the newline.
429 */
430 if (' ' == *n->string && MDOC_LINE & n->flags)
431 if ( ! (HTML_LITERAL & h->flags))
432 print_otag(h, TAG_BR, 0, NULL);
433 print_text(h, n->string);
434 return;
435 case (MDOC_TBL):
436 /*
437 * This will take care of initialising all of the table
438 * state data for the first table, then tearing it down
439 * for the last one.
440 */
441 print_tbl(h, n->span);
442 return;
443 default:
444 /*
445 * Close out the current table, if it's open, and unset
446 * the "meta" table state. This will be reopened on the
447 * next table element.
448 */
449 if (h->tblt) {
450 print_tblclose(h);
451 t = h->tags.head;
452 }
453
454 assert(NULL == h->tblt);
455 if (mdocs[n->tok].pre && ENDBODY_NOT == n->end)
456 child = (*mdocs[n->tok].pre)(m, n, h);
457 break;
458 }
459
460 if (HTML_KEEP & h->flags) {
461 if (n->prev && n->prev->line != n->line) {
462 h->flags &= ~HTML_KEEP;
463 h->flags |= HTML_PREKEEP;
464 } else if (NULL == n->prev) {
465 if (n->parent && n->parent->line != n->line) {
466 h->flags &= ~HTML_KEEP;
467 h->flags |= HTML_PREKEEP;
468 }
469 }
470 }
471
472 if (child && n->child)
473 print_mdoc_nodelist(m, n->child, h);
474
475 print_stagq(h, t);
476
477 bufinit(h);
478 switch (n->type) {
479 case (MDOC_ROOT):
480 mdoc_root_post(m, n, h);
481 break;
482 default:
483 if (mdocs[n->tok].post && ENDBODY_NOT == n->end)
484 (*mdocs[n->tok].post)(m, n, h);
485 break;
486 }
487 }
488
489 /* ARGSUSED */
490 static void
491 mdoc_root_post(MDOC_ARGS)
492 {
493 struct htmlpair tag[3];
494 struct tag *t, *tt;
495 char b[DATESIZ];
496
497 time2a(m->date, b, DATESIZ);
498
499 PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
500 PAIR_CLASS_INIT(&tag[1], "foot");
501 if (NULL == h->style) {
502 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
503 t = print_otag(h, TAG_TABLE, 3, tag);
504 PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
505 print_otag(h, TAG_COL, 1, tag);
506 print_otag(h, TAG_COL, 1, tag);
507 } else
508 t = print_otag(h, TAG_TABLE, 2, tag);
509
510 t = print_otag(h, TAG_TBODY, 0, NULL);
511
512 tt = print_otag(h, TAG_TR, 0, NULL);
513
514 PAIR_CLASS_INIT(&tag[0], "foot-date");
515 print_otag(h, TAG_TD, 1, tag);
516
517 print_text(h, b);
518 print_stagq(h, tt);
519
520 PAIR_CLASS_INIT(&tag[0], "foot-os");
521 if (NULL == h->style) {
522 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
523 print_otag(h, TAG_TD, 2, tag);
524 } else
525 print_otag(h, TAG_TD, 1, tag);
526
527 print_text(h, m->os);
528 print_tagq(h, t);
529 }
530
531
532 /* ARGSUSED */
533 static int
534 mdoc_root_pre(MDOC_ARGS)
535 {
536 struct htmlpair tag[3];
537 struct tag *t, *tt;
538 char b[BUFSIZ], title[BUFSIZ];
539
540 strlcpy(b, m->vol, BUFSIZ);
541
542 if (m->arch) {
543 strlcat(b, " (", BUFSIZ);
544 strlcat(b, m->arch, BUFSIZ);
545 strlcat(b, ")", BUFSIZ);
546 }
547
548 snprintf(title, BUFSIZ - 1, "%s(%s)", m->title, m->msec);
549
550 PAIR_SUMMARY_INIT(&tag[0], "Document Header");
551 PAIR_CLASS_INIT(&tag[1], "head");
552 if (NULL == h->style) {
553 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
554 t = print_otag(h, TAG_TABLE, 3, tag);
555 PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
556 print_otag(h, TAG_COL, 1, tag);
557 print_otag(h, TAG_COL, 1, tag);
558 print_otag(h, TAG_COL, 1, tag);
559 } else
560 t = print_otag(h, TAG_TABLE, 2, tag);
561
562 print_otag(h, TAG_TBODY, 0, NULL);
563
564 tt = print_otag(h, TAG_TR, 0, NULL);
565
566 PAIR_CLASS_INIT(&tag[0], "head-ltitle");
567 print_otag(h, TAG_TD, 1, tag);
568
569 print_text(h, title);
570 print_stagq(h, tt);
571
572 PAIR_CLASS_INIT(&tag[0], "head-vol");
573 if (NULL == h->style) {
574 PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
575 print_otag(h, TAG_TD, 2, tag);
576 } else
577 print_otag(h, TAG_TD, 1, tag);
578
579 print_text(h, b);
580 print_stagq(h, tt);
581
582 PAIR_CLASS_INIT(&tag[0], "head-rtitle");
583 if (NULL == h->style) {
584 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
585 print_otag(h, TAG_TD, 2, tag);
586 } else
587 print_otag(h, TAG_TD, 1, tag);
588
589 print_text(h, title);
590 print_tagq(h, t);
591 return(1);
592 }
593
594
595 /* ARGSUSED */
596 static int
597 mdoc_sh_pre(MDOC_ARGS)
598 {
599 struct htmlpair tag;
600 char buf[BUFSIZ];
601
602 if (MDOC_BLOCK == n->type) {
603 PAIR_CLASS_INIT(&tag, "section");
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_H1, 1, &tag);
618 return(1);
619 }
620
621
622 /* ARGSUSED */
623 static int
624 mdoc_ss_pre(MDOC_ARGS)
625 {
626 struct htmlpair tag;
627 char buf[BUFSIZ];
628
629 if (MDOC_BLOCK == n->type) {
630 PAIR_CLASS_INIT(&tag, "subsection");
631 print_otag(h, TAG_DIV, 1, &tag);
632 return(1);
633 } else if (MDOC_BODY == n->type)
634 return(1);
635
636 buf[0] = '\0';
637 for (n = n->child; n; n = n->next) {
638 html_idcat(buf, n->string, BUFSIZ);
639 if (n->next)
640 html_idcat(buf, " ", BUFSIZ);
641 }
642
643 PAIR_ID_INIT(&tag, buf);
644 print_otag(h, TAG_H2, 1, &tag);
645 return(1);
646 }
647
648
649 /* ARGSUSED */
650 static int
651 mdoc_fl_pre(MDOC_ARGS)
652 {
653 struct htmlpair tag;
654
655 PAIR_CLASS_INIT(&tag, "flag");
656 print_otag(h, TAG_B, 1, &tag);
657
658 /* `Cm' has no leading hyphen. */
659
660 if (MDOC_Cm == n->tok)
661 return(1);
662
663 print_text(h, "\\-");
664
665 if (n->child)
666 h->flags |= HTML_NOSPACE;
667 else if (n->next && n->next->line == n->line)
668 h->flags |= HTML_NOSPACE;
669
670 return(1);
671 }
672
673
674 /* ARGSUSED */
675 static int
676 mdoc_nd_pre(MDOC_ARGS)
677 {
678 struct htmlpair tag;
679
680 if (MDOC_BODY != n->type)
681 return(1);
682
683 /* XXX: this tag in theory can contain block elements. */
684
685 print_text(h, "\\(em");
686 PAIR_CLASS_INIT(&tag, "desc");
687 print_otag(h, TAG_SPAN, 1, &tag);
688 return(1);
689 }
690
691
692 static int
693 mdoc_nm_pre(MDOC_ARGS)
694 {
695 struct htmlpair tag;
696 struct roffsu su;
697 size_t len;
698
699 switch (n->type) {
700 case (MDOC_ELEM):
701 synopsis_pre(h, n);
702 PAIR_CLASS_INIT(&tag, "name");
703 print_otag(h, TAG_B, 1, &tag);
704 if (NULL == n->child && m->name)
705 print_text(h, m->name);
706 return(1);
707 case (MDOC_HEAD):
708 print_otag(h, TAG_TD, 0, NULL);
709 if (NULL == n->child && m->name)
710 print_text(h, m->name);
711 return(1);
712 case (MDOC_BODY):
713 print_otag(h, TAG_TD, 0, NULL);
714 return(1);
715 default:
716 break;
717 }
718
719 synopsis_pre(h, n);
720 PAIR_CLASS_INIT(&tag, "synopsis");
721 print_otag(h, TAG_TABLE, 1, &tag);
722
723 for (len = 0, n = n->child; n; n = n->next)
724 if (MDOC_TEXT == n->type)
725 len += strlen(n->string);
726
727 if (0 == len && m->name)
728 len = strlen(m->name);
729
730 SCALE_HS_INIT(&su, (double)len);
731 bufcat_su(h, "width", &su);
732 PAIR_STYLE_INIT(&tag, h);
733 print_otag(h, TAG_COL, 1, &tag);
734 print_otag(h, TAG_COL, 0, NULL);
735 print_otag(h, TAG_TBODY, 0, NULL);
736 print_otag(h, TAG_TR, 0, NULL);
737 return(1);
738 }
739
740
741 /* ARGSUSED */
742 static int
743 mdoc_xr_pre(MDOC_ARGS)
744 {
745 struct htmlpair tag[2];
746 const struct mdoc_node *nn;
747
748 if (NULL == n->child)
749 return(0);
750
751 PAIR_CLASS_INIT(&tag[0], "link-man");
752
753 if (h->base_man) {
754 buffmt_man(h, n->child->string,
755 n->child->next ?
756 n->child->next->string : NULL);
757 PAIR_HREF_INIT(&tag[1], h->buf);
758 print_otag(h, TAG_A, 2, tag);
759 } else
760 print_otag(h, TAG_A, 1, tag);
761
762 nn = n->child;
763 print_text(h, nn->string);
764
765 if (NULL == (nn = nn->next))
766 return(0);
767
768 h->flags |= HTML_NOSPACE;
769 print_text(h, "(");
770 h->flags |= HTML_NOSPACE;
771 print_text(h, nn->string);
772 h->flags |= HTML_NOSPACE;
773 print_text(h, ")");
774 return(0);
775 }
776
777
778 /* ARGSUSED */
779 static int
780 mdoc_ns_pre(MDOC_ARGS)
781 {
782
783 if ( ! (MDOC_LINE & n->flags))
784 h->flags |= HTML_NOSPACE;
785 return(1);
786 }
787
788
789 /* ARGSUSED */
790 static int
791 mdoc_ar_pre(MDOC_ARGS)
792 {
793 struct htmlpair tag;
794
795 PAIR_CLASS_INIT(&tag, "arg");
796 print_otag(h, TAG_I, 1, &tag);
797 return(1);
798 }
799
800
801 /* ARGSUSED */
802 static int
803 mdoc_xx_pre(MDOC_ARGS)
804 {
805 const char *pp;
806 struct htmlpair tag;
807
808 switch (n->tok) {
809 case (MDOC_Bsx):
810 pp = "BSD/OS";
811 break;
812 case (MDOC_Dx):
813 pp = "DragonFly";
814 break;
815 case (MDOC_Fx):
816 pp = "FreeBSD";
817 break;
818 case (MDOC_Nx):
819 pp = "NetBSD";
820 break;
821 case (MDOC_Ox):
822 pp = "OpenBSD";
823 break;
824 case (MDOC_Ux):
825 pp = "UNIX";
826 break;
827 default:
828 return(1);
829 }
830
831 PAIR_CLASS_INIT(&tag, "unix");
832 print_otag(h, TAG_SPAN, 1, &tag);
833
834 print_text(h, pp);
835 if (n->child) {
836 h->flags |= HTML_KEEP;
837 print_text(h, n->child->string);
838 h->flags &= ~HTML_KEEP;
839 }
840 return(0);
841 }
842
843
844 /* ARGSUSED */
845 static int
846 mdoc_bx_pre(MDOC_ARGS)
847 {
848 struct htmlpair tag;
849
850 PAIR_CLASS_INIT(&tag, "unix");
851 print_otag(h, TAG_SPAN, 1, &tag);
852
853 if (NULL != (n = n->child)) {
854 print_text(h, n->string);
855 h->flags |= HTML_NOSPACE;
856 print_text(h, "BSD");
857 } else {
858 print_text(h, "BSD");
859 return(0);
860 }
861
862 if (NULL != (n = n->next)) {
863 h->flags |= HTML_NOSPACE;
864 print_text(h, "-");
865 h->flags |= HTML_NOSPACE;
866 print_text(h, n->string);
867 }
868
869 return(0);
870 }
871
872 /* ARGSUSED */
873 static int
874 mdoc_it_pre(MDOC_ARGS)
875 {
876 struct roffsu su;
877 enum mdoc_list type;
878 struct htmlpair tag[2];
879 const struct mdoc_node *bl;
880
881 bl = n->parent;
882 while (bl && MDOC_Bl != bl->tok)
883 bl = bl->parent;
884
885 assert(bl);
886
887 type = bl->norm->Bl.type;
888
889 assert(lists[type]);
890 PAIR_CLASS_INIT(&tag[0], lists[type]);
891
892 if (MDOC_HEAD == n->type) {
893 switch (type) {
894 case(LIST_bullet):
895 /* FALLTHROUGH */
896 case(LIST_dash):
897 /* FALLTHROUGH */
898 case(LIST_item):
899 /* FALLTHROUGH */
900 case(LIST_hyphen):
901 /* FALLTHROUGH */
902 case(LIST_enum):
903 return(0);
904 case(LIST_diag):
905 /* FALLTHROUGH */
906 case(LIST_hang):
907 /* FALLTHROUGH */
908 case(LIST_inset):
909 /* FALLTHROUGH */
910 case(LIST_ohang):
911 /* FALLTHROUGH */
912 case(LIST_tag):
913 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
914 bufcat_su(h, "margin-top", &su);
915 PAIR_STYLE_INIT(&tag[1], h);
916 print_otag(h, TAG_DT, 2, tag);
917 if (LIST_diag != type)
918 break;
919 PAIR_CLASS_INIT(&tag[0], "diag");
920 print_otag(h, TAG_B, 1, tag);
921 break;
922 case(LIST_column):
923 break;
924 default:
925 break;
926 }
927 } else if (MDOC_BODY == n->type) {
928 switch (type) {
929 case(LIST_bullet):
930 /* FALLTHROUGH */
931 case(LIST_hyphen):
932 /* FALLTHROUGH */
933 case(LIST_dash):
934 /* FALLTHROUGH */
935 case(LIST_enum):
936 /* FALLTHROUGH */
937 case(LIST_item):
938 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
939 bufcat_su(h, "margin-top", &su);
940 PAIR_STYLE_INIT(&tag[1], h);
941 print_otag(h, TAG_LI, 2, tag);
942 break;
943 case(LIST_diag):
944 /* FALLTHROUGH */
945 case(LIST_hang):
946 /* FALLTHROUGH */
947 case(LIST_inset):
948 /* FALLTHROUGH */
949 case(LIST_ohang):
950 /* FALLTHROUGH */
951 case(LIST_tag):
952 if (NULL == bl->norm->Bl.width) {
953 print_otag(h, TAG_DD, 1, tag);
954 break;
955 }
956 a2width(bl->norm->Bl.width, &su);
957 bufcat_su(h, "margin-left", &su);
958 PAIR_STYLE_INIT(&tag[1], h);
959 print_otag(h, TAG_DD, 2, tag);
960 break;
961 case(LIST_column):
962 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
963 bufcat_su(h, "margin-top", &su);
964 PAIR_STYLE_INIT(&tag[1], h);
965 print_otag(h, TAG_TD, 2, tag);
966 break;
967 default:
968 break;
969 }
970 } else {
971 switch (type) {
972 case (LIST_column):
973 print_otag(h, TAG_TR, 1, tag);
974 break;
975 default:
976 break;
977 }
978 }
979
980 return(1);
981 }
982
983 /* ARGSUSED */
984 static int
985 mdoc_bl_pre(MDOC_ARGS)
986 {
987 int i;
988 struct htmlpair tag[3];
989 struct roffsu su;
990 char buf[BUFSIZ];
991
992 if (MDOC_BODY == n->type) {
993 if (LIST_column == n->norm->Bl.type)
994 print_otag(h, TAG_TBODY, 0, NULL);
995 return(1);
996 }
997
998 if (MDOC_HEAD == n->type) {
999 if (LIST_column != n->norm->Bl.type)
1000 return(0);
1001
1002 /*
1003 * For each column, print out the <COL> tag with our
1004 * suggested width. The last column gets min-width, as
1005 * in terminal mode it auto-sizes to the width of the
1006 * screen and we want to preserve that behaviour.
1007 */
1008
1009 for (i = 0; i < (int)n->norm->Bl.ncols; i++) {
1010 a2width(n->norm->Bl.cols[i], &su);
1011 bufinit(h);
1012 if (i < (int)n->norm->Bl.ncols - 1)
1013 bufcat_su(h, "width", &su);
1014 else
1015 bufcat_su(h, "min-width", &su);
1016 PAIR_STYLE_INIT(&tag[0], h);
1017 print_otag(h, TAG_COL, 1, tag);
1018 }
1019
1020 return(0);
1021 }
1022
1023 SCALE_VS_INIT(&su, 0);
1024 bufcat_su(h, "margin-top", &su);
1025 bufcat_su(h, "margin-bottom", &su);
1026 PAIR_STYLE_INIT(&tag[0], h);
1027
1028 assert(lists[n->norm->Bl.type]);
1029 strlcpy(buf, "list ", BUFSIZ);
1030 strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
1031 PAIR_INIT(&tag[1], ATTR_CLASS, buf);
1032
1033 /* Set the block's left-hand margin. */
1034
1035 if (n->norm->Bl.offs) {
1036 a2offs(n->norm->Bl.offs, &su);
1037 bufcat_su(h, "margin-left", &su);
1038 }
1039
1040 switch (n->norm->Bl.type) {
1041 case(LIST_bullet):
1042 /* FALLTHROUGH */
1043 case(LIST_dash):
1044 /* FALLTHROUGH */
1045 case(LIST_hyphen):
1046 /* FALLTHROUGH */
1047 case(LIST_item):
1048 print_otag(h, TAG_UL, 2, tag);
1049 break;
1050 case(LIST_enum):
1051 print_otag(h, TAG_OL, 2, tag);
1052 break;
1053 case(LIST_diag):
1054 /* FALLTHROUGH */
1055 case(LIST_hang):
1056 /* FALLTHROUGH */
1057 case(LIST_inset):
1058 /* FALLTHROUGH */
1059 case(LIST_ohang):
1060 /* FALLTHROUGH */
1061 case(LIST_tag):
1062 print_otag(h, TAG_DL, 2, tag);
1063 break;
1064 case(LIST_column):
1065 print_otag(h, TAG_TABLE, 2, tag);
1066 break;
1067 default:
1068 abort();
1069 /* NOTREACHED */
1070 }
1071
1072 return(1);
1073 }
1074
1075 /* ARGSUSED */
1076 static int
1077 mdoc_ex_pre(MDOC_ARGS)
1078 {
1079 const struct mdoc_node *nn;
1080 struct tag *t;
1081 struct htmlpair tag;
1082
1083 if (n->prev)
1084 print_otag(h, TAG_BR, 0, NULL);
1085
1086 PAIR_CLASS_INIT(&tag, "utility");
1087
1088 print_text(h, "The");
1089 for (nn = n->child; nn; nn = nn->next) {
1090 t = print_otag(h, TAG_B, 1, &tag);
1091 print_text(h, nn->string);
1092 print_tagq(h, t);
1093
1094 h->flags |= HTML_NOSPACE;
1095
1096 if (nn->next && NULL == nn->next->next)
1097 print_text(h, ", and");
1098 else if (nn->next)
1099 print_text(h, ",");
1100 else
1101 h->flags &= ~HTML_NOSPACE;
1102 }
1103
1104 if (n->child && n->child->next)
1105 print_text(h, "utilities exit");
1106 else
1107 print_text(h, "utility exits");
1108
1109 print_text(h, "0 on success, and >0 if an error occurs.");
1110 return(0);
1111 }
1112
1113
1114 /* ARGSUSED */
1115 static int
1116 mdoc_em_pre(MDOC_ARGS)
1117 {
1118 struct htmlpair tag;
1119
1120 PAIR_CLASS_INIT(&tag, "emph");
1121 print_otag(h, TAG_SPAN, 1, &tag);
1122 return(1);
1123 }
1124
1125
1126 /* ARGSUSED */
1127 static int
1128 mdoc_d1_pre(MDOC_ARGS)
1129 {
1130 struct htmlpair tag[2];
1131 struct roffsu su;
1132
1133 if (MDOC_BLOCK != n->type)
1134 return(1);
1135
1136 SCALE_VS_INIT(&su, 0);
1137 bufcat_su(h, "margin-top", &su);
1138 bufcat_su(h, "margin-bottom", &su);
1139 PAIR_STYLE_INIT(&tag[0], h);
1140 print_otag(h, TAG_BLOCKQUOTE, 1, tag);
1141
1142 /* BLOCKQUOTE needs a block body. */
1143
1144 PAIR_CLASS_INIT(&tag[0], "display");
1145 print_otag(h, TAG_DIV, 1, tag);
1146
1147 if (MDOC_Dl == n->tok) {
1148 PAIR_CLASS_INIT(&tag[0], "lit");
1149 print_otag(h, TAG_CODE, 1, tag);
1150 }
1151
1152 return(1);
1153 }
1154
1155
1156 /* ARGSUSED */
1157 static int
1158 mdoc_sx_pre(MDOC_ARGS)
1159 {
1160 struct htmlpair tag[2];
1161 const struct mdoc_node *nn;
1162 char buf[BUFSIZ];
1163
1164 strlcpy(buf, "#", BUFSIZ);
1165 for (nn = n->child; nn; nn = nn->next) {
1166 html_idcat(buf, nn->string, BUFSIZ);
1167 if (nn->next)
1168 html_idcat(buf, " ", BUFSIZ);
1169 }
1170
1171 PAIR_CLASS_INIT(&tag[0], "link-sec");
1172 PAIR_HREF_INIT(&tag[1], buf);
1173
1174 print_otag(h, TAG_I, 1, tag);
1175 print_otag(h, TAG_A, 2, tag);
1176 return(1);
1177 }
1178
1179
1180 /* ARGSUSED */
1181 static int
1182 mdoc_bd_pre(MDOC_ARGS)
1183 {
1184 struct htmlpair tag[2];
1185 int comp, sv;
1186 const struct mdoc_node *nn;
1187 struct roffsu su;
1188
1189 if (MDOC_HEAD == n->type)
1190 return(0);
1191
1192 if (MDOC_BLOCK == n->type) {
1193 comp = n->norm->Bd.comp;
1194 for (nn = n; nn && ! comp; nn = nn->parent) {
1195 if (MDOC_BLOCK != nn->type)
1196 continue;
1197 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
1198 comp = 1;
1199 if (nn->prev)
1200 break;
1201 }
1202 if ( ! comp)
1203 print_otag(h, TAG_P, 0, NULL);
1204 return(1);
1205 }
1206
1207 SCALE_HS_INIT(&su, 0);
1208 if (n->norm->Bd.offs)
1209 a2offs(n->norm->Bd.offs, &su);
1210
1211 bufcat_su(h, "margin-left", &su);
1212 PAIR_STYLE_INIT(&tag[0], h);
1213
1214 if (DISP_unfilled != n->norm->Bd.type &&
1215 DISP_literal != n->norm->Bd.type) {
1216 PAIR_CLASS_INIT(&tag[1], "display");
1217 print_otag(h, TAG_DIV, 2, tag);
1218 return(1);
1219 }
1220
1221 PAIR_CLASS_INIT(&tag[1], "lit display");
1222 print_otag(h, TAG_PRE, 2, tag);
1223
1224 /* This can be recursive: save & set our literal state. */
1225
1226 sv = h->flags & HTML_LITERAL;
1227 h->flags |= HTML_LITERAL;
1228
1229 for (nn = n->child; nn; nn = nn->next) {
1230 print_mdoc_node(m, nn, h);
1231 /*
1232 * If the printed node flushes its own line, then we
1233 * needn't do it here as well. This is hacky, but the
1234 * notion of selective eoln whitespace is pretty dumb
1235 * anyway, so don't sweat it.
1236 */
1237 switch (nn->tok) {
1238 case (MDOC_Sm):
1239 /* FALLTHROUGH */
1240 case (MDOC_br):
1241 /* FALLTHROUGH */
1242 case (MDOC_sp):
1243 /* FALLTHROUGH */
1244 case (MDOC_Bl):
1245 /* FALLTHROUGH */
1246 case (MDOC_D1):
1247 /* FALLTHROUGH */
1248 case (MDOC_Dl):
1249 /* FALLTHROUGH */
1250 case (MDOC_Lp):
1251 /* FALLTHROUGH */
1252 case (MDOC_Pp):
1253 continue;
1254 default:
1255 break;
1256 }
1257 if (nn->next && nn->next->line == nn->line)
1258 continue;
1259 else if (nn->next)
1260 print_text(h, "\n");
1261
1262 h->flags |= HTML_NOSPACE;
1263 }
1264
1265 if (0 == sv)
1266 h->flags &= ~HTML_LITERAL;
1267
1268 return(0);
1269 }
1270
1271
1272 /* ARGSUSED */
1273 static int
1274 mdoc_pa_pre(MDOC_ARGS)
1275 {
1276 struct htmlpair tag;
1277
1278 PAIR_CLASS_INIT(&tag, "file");
1279 print_otag(h, TAG_I, 1, &tag);
1280 return(1);
1281 }
1282
1283
1284 /* ARGSUSED */
1285 static int
1286 mdoc_ad_pre(MDOC_ARGS)
1287 {
1288 struct htmlpair tag;
1289
1290 PAIR_CLASS_INIT(&tag, "addr");
1291 print_otag(h, TAG_I, 1, &tag);
1292 return(1);
1293 }
1294
1295
1296 /* ARGSUSED */
1297 static int
1298 mdoc_an_pre(MDOC_ARGS)
1299 {
1300 struct htmlpair tag;
1301
1302 /* TODO: -split and -nosplit (see termp_an_pre()). */
1303
1304 PAIR_CLASS_INIT(&tag, "author");
1305 print_otag(h, TAG_SPAN, 1, &tag);
1306 return(1);
1307 }
1308
1309
1310 /* ARGSUSED */
1311 static int
1312 mdoc_cd_pre(MDOC_ARGS)
1313 {
1314 struct htmlpair tag;
1315
1316 synopsis_pre(h, n);
1317 PAIR_CLASS_INIT(&tag, "config");
1318 print_otag(h, TAG_B, 1, &tag);
1319 return(1);
1320 }
1321
1322
1323 /* ARGSUSED */
1324 static int
1325 mdoc_dv_pre(MDOC_ARGS)
1326 {
1327 struct htmlpair tag;
1328
1329 PAIR_CLASS_INIT(&tag, "define");
1330 print_otag(h, TAG_SPAN, 1, &tag);
1331 return(1);
1332 }
1333
1334
1335 /* ARGSUSED */
1336 static int
1337 mdoc_ev_pre(MDOC_ARGS)
1338 {
1339 struct htmlpair tag;
1340
1341 PAIR_CLASS_INIT(&tag, "env");
1342 print_otag(h, TAG_SPAN, 1, &tag);
1343 return(1);
1344 }
1345
1346
1347 /* ARGSUSED */
1348 static int
1349 mdoc_er_pre(MDOC_ARGS)
1350 {
1351 struct htmlpair tag;
1352
1353 PAIR_CLASS_INIT(&tag, "errno");
1354 print_otag(h, TAG_SPAN, 1, &tag);
1355 return(1);
1356 }
1357
1358
1359 /* ARGSUSED */
1360 static int
1361 mdoc_fa_pre(MDOC_ARGS)
1362 {
1363 const struct mdoc_node *nn;
1364 struct htmlpair tag;
1365 struct tag *t;
1366
1367 PAIR_CLASS_INIT(&tag, "farg");
1368 if (n->parent->tok != MDOC_Fo) {
1369 print_otag(h, TAG_I, 1, &tag);
1370 return(1);
1371 }
1372
1373 for (nn = n->child; nn; nn = nn->next) {
1374 t = print_otag(h, TAG_I, 1, &tag);
1375 print_text(h, nn->string);
1376 print_tagq(h, t);
1377 if (nn->next)
1378 print_text(h, ",");
1379 }
1380
1381 if (n->child && n->next && n->next->tok == MDOC_Fa)
1382 print_text(h, ",");
1383
1384 return(0);
1385 }
1386
1387
1388 /* ARGSUSED */
1389 static int
1390 mdoc_fd_pre(MDOC_ARGS)
1391 {
1392 struct htmlpair tag;
1393
1394 synopsis_pre(h, n);
1395
1396 PAIR_CLASS_INIT(&tag, "macro");
1397 print_otag(h, TAG_B, 1, &tag);
1398 return(1);
1399 }
1400
1401
1402 /* ARGSUSED */
1403 static int
1404 mdoc_vt_pre(MDOC_ARGS)
1405 {
1406 struct htmlpair tag;
1407
1408 if (MDOC_BLOCK == n->type) {
1409 synopsis_pre(h, n);
1410 return(1);
1411 } else if (MDOC_ELEM == n->type) {
1412 synopsis_pre(h, n);
1413 } else if (MDOC_HEAD == n->type)
1414 return(0);
1415
1416 PAIR_CLASS_INIT(&tag, "type");
1417 print_otag(h, TAG_SPAN, 1, &tag);
1418 return(1);
1419 }
1420
1421
1422 /* ARGSUSED */
1423 static int
1424 mdoc_ft_pre(MDOC_ARGS)
1425 {
1426 struct htmlpair tag;
1427
1428 synopsis_pre(h, n);
1429 PAIR_CLASS_INIT(&tag, "ftype");
1430 print_otag(h, TAG_I, 1, &tag);
1431 return(1);
1432 }
1433
1434
1435 /* ARGSUSED */
1436 static int
1437 mdoc_fn_pre(MDOC_ARGS)
1438 {
1439 struct tag *t;
1440 struct htmlpair tag[2];
1441 const struct mdoc_node *nn;
1442 char nbuf[BUFSIZ];
1443 const char *sp, *ep;
1444 int sz, i;
1445
1446 synopsis_pre(h, n);
1447
1448 /* Split apart into type and name. */
1449 assert(n->child->string);
1450 sp = n->child->string;
1451
1452 ep = strchr(sp, ' ');
1453 if (NULL != ep) {
1454 PAIR_CLASS_INIT(&tag[0], "ftype");
1455 t = print_otag(h, TAG_I, 1, tag);
1456
1457 while (ep) {
1458 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1459 (void)memcpy(nbuf, sp, (size_t)sz);
1460 nbuf[sz] = '\0';
1461 print_text(h, nbuf);
1462 sp = ++ep;
1463 ep = strchr(sp, ' ');
1464 }
1465 print_tagq(h, t);
1466 }
1467
1468 PAIR_CLASS_INIT(&tag[0], "fname");
1469
1470 /*
1471 * FIXME: only refer to IDs that we know exist.
1472 */
1473
1474 #if 0
1475 if (MDOC_SYNPRETTY & n->flags) {
1476 nbuf[0] = '\0';
1477 html_idcat(nbuf, sp, BUFSIZ);
1478 PAIR_ID_INIT(&tag[1], nbuf);
1479 } else {
1480 strlcpy(nbuf, "#", BUFSIZ);
1481 html_idcat(nbuf, sp, BUFSIZ);
1482 PAIR_HREF_INIT(&tag[1], nbuf);
1483 }
1484 #endif
1485
1486 t = print_otag(h, TAG_B, 1, tag);
1487
1488 if (sp) {
1489 strlcpy(nbuf, sp, BUFSIZ);
1490 print_text(h, nbuf);
1491 }
1492
1493 print_tagq(h, t);
1494
1495 h->flags |= HTML_NOSPACE;
1496 print_text(h, "(");
1497
1498 bufinit(h);
1499 PAIR_CLASS_INIT(&tag[0], "farg");
1500 bufcat_style(h, "white-space", "nowrap");
1501 PAIR_STYLE_INIT(&tag[1], h);
1502
1503 for (nn = n->child->next; nn; nn = nn->next) {
1504 i = 1;
1505 if (MDOC_SYNPRETTY & n->flags)
1506 i = 2;
1507 t = print_otag(h, TAG_I, i, tag);
1508 print_text(h, nn->string);
1509 print_tagq(h, t);
1510 if (nn->next)
1511 print_text(h, ",");
1512 }
1513
1514 print_text(h, ")");
1515 if (MDOC_SYNPRETTY & n->flags)
1516 print_text(h, ";");
1517
1518 return(0);
1519 }
1520
1521
1522 /* ARGSUSED */
1523 static int
1524 mdoc_sm_pre(MDOC_ARGS)
1525 {
1526
1527 assert(n->child && MDOC_TEXT == n->child->type);
1528 if (0 == strcmp("on", n->child->string)) {
1529 /*
1530 * FIXME: no p->col to check. Thus, if we have
1531 * .Bd -literal
1532 * .Sm off
1533 * 1 2
1534 * .Sm on
1535 * 3
1536 * .Ed
1537 * the "3" is preceded by a space.
1538 */
1539 h->flags &= ~HTML_NOSPACE;
1540 h->flags &= ~HTML_NONOSPACE;
1541 } else
1542 h->flags |= HTML_NONOSPACE;
1543
1544 return(0);
1545 }
1546
1547 /* ARGSUSED */
1548 static int
1549 mdoc_pp_pre(MDOC_ARGS)
1550 {
1551
1552 print_otag(h, TAG_P, 0, NULL);
1553 return(0);
1554
1555 }
1556
1557 /* ARGSUSED */
1558 static int
1559 mdoc_sp_pre(MDOC_ARGS)
1560 {
1561 struct roffsu su;
1562 struct htmlpair tag;
1563
1564 SCALE_VS_INIT(&su, 1);
1565
1566 if (MDOC_sp == n->tok) {
1567 if (n->child)
1568 a2roffsu(n->child->string, &su, SCALE_VS);
1569 } else
1570 su.scale = 0;
1571
1572 bufcat_su(h, "height", &su);
1573 PAIR_STYLE_INIT(&tag, h);
1574 print_otag(h, TAG_DIV, 1, &tag);
1575
1576 /* So the div isn't empty: */
1577 print_text(h, "\\~");
1578
1579 return(0);
1580
1581 }
1582
1583 /* ARGSUSED */
1584 static int
1585 mdoc_lk_pre(MDOC_ARGS)
1586 {
1587 const struct mdoc_node *nn;
1588 struct htmlpair tag[2];
1589
1590 nn = n->child;
1591
1592 PAIR_CLASS_INIT(&tag[0], "link-ext");
1593 PAIR_HREF_INIT(&tag[1], nn->string);
1594 print_otag(h, TAG_A, 2, tag);
1595
1596 if (NULL == nn || NULL == nn->next)
1597 return(1);
1598
1599 for (nn = nn->next; nn; nn = nn->next)
1600 print_text(h, nn->string);
1601
1602 return(0);
1603 }
1604
1605
1606 /* ARGSUSED */
1607 static int
1608 mdoc_mt_pre(MDOC_ARGS)
1609 {
1610 struct htmlpair tag[2];
1611 struct tag *t;
1612 const struct mdoc_node *nn;
1613
1614 PAIR_CLASS_INIT(&tag[0], "link-mail");
1615
1616 for (nn = n->child; nn; nn = nn->next) {
1617 bufinit(h);
1618 bufcat(h, "mailto:");
1619 bufcat(h, nn->string);
1620 PAIR_HREF_INIT(&tag[1], h->buf);
1621 t = print_otag(h, TAG_A, 2, tag);
1622 print_text(h, nn->string);
1623 print_tagq(h, t);
1624 }
1625
1626 return(0);
1627 }
1628
1629
1630 /* ARGSUSED */
1631 static int
1632 mdoc_fo_pre(MDOC_ARGS)
1633 {
1634 struct htmlpair tag;
1635 struct tag *t;
1636
1637 if (MDOC_BODY == n->type) {
1638 h->flags |= HTML_NOSPACE;
1639 print_text(h, "(");
1640 h->flags |= HTML_NOSPACE;
1641 return(1);
1642 } else if (MDOC_BLOCK == n->type) {
1643 synopsis_pre(h, n);
1644 return(1);
1645 }
1646
1647 /* XXX: we drop non-initial arguments as per groff. */
1648
1649 assert(n->child);
1650 assert(n->child->string);
1651
1652 PAIR_CLASS_INIT(&tag, "fname");
1653 t = print_otag(h, TAG_B, 1, &tag);
1654 print_text(h, n->child->string);
1655 print_tagq(h, t);
1656 return(0);
1657 }
1658
1659
1660 /* ARGSUSED */
1661 static void
1662 mdoc_fo_post(MDOC_ARGS)
1663 {
1664
1665 if (MDOC_BODY != n->type)
1666 return;
1667 print_text(h, ")");
1668 print_text(h, ";");
1669 }
1670
1671
1672 /* ARGSUSED */
1673 static int
1674 mdoc_in_pre(MDOC_ARGS)
1675 {
1676 const struct mdoc_node *nn;
1677 struct tag *t;
1678 struct htmlpair tag[2];
1679 int i;
1680
1681 synopsis_pre(h, n);
1682
1683 PAIR_CLASS_INIT(&tag[0], "includes");
1684 print_otag(h, TAG_B, 1, tag);
1685
1686 if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags)
1687 print_text(h, "#include");
1688
1689 print_text(h, "<");
1690 h->flags |= HTML_NOSPACE;
1691
1692 for (nn = n->child; nn; nn = nn->next) {
1693 PAIR_CLASS_INIT(&tag[0], "link-includes");
1694 i = 1;
1695 bufinit(h);
1696 if (h->base_includes) {
1697 buffmt_includes(h, nn->string);
1698 PAIR_HREF_INIT(&tag[i], h->buf);
1699 i++;
1700 }
1701 t = print_otag(h, TAG_A, i, tag);
1702 print_mdoc_node(m, nn, h);
1703 print_tagq(h, t);
1704 }
1705
1706 h->flags |= HTML_NOSPACE;
1707 print_text(h, ">");
1708
1709 return(0);
1710 }
1711
1712
1713 /* ARGSUSED */
1714 static int
1715 mdoc_ic_pre(MDOC_ARGS)
1716 {
1717 struct htmlpair tag;
1718
1719 PAIR_CLASS_INIT(&tag, "cmd");
1720 print_otag(h, TAG_B, 1, &tag);
1721 return(1);
1722 }
1723
1724
1725 /* ARGSUSED */
1726 static int
1727 mdoc_rv_pre(MDOC_ARGS)
1728 {
1729 const struct mdoc_node *nn;
1730 struct htmlpair tag;
1731 struct tag *t;
1732
1733 if (n->prev)
1734 print_otag(h, TAG_BR, 0, NULL);
1735
1736 print_text(h, "The");
1737
1738 for (nn = n->child; nn; nn = nn->next) {
1739 PAIR_CLASS_INIT(&tag, "fname");
1740 t = print_otag(h, TAG_B, 1, &tag);
1741 print_text(h, nn->string);
1742 print_tagq(h, t);
1743
1744 h->flags |= HTML_NOSPACE;
1745 if (nn->next && NULL == nn->next->next)
1746 print_text(h, "(), and");
1747 else if (nn->next)
1748 print_text(h, "(),");
1749 else
1750 print_text(h, "()");
1751 }
1752
1753 if (n->child && n->child->next)
1754 print_text(h, "functions return");
1755 else
1756 print_text(h, "function returns");
1757
1758 print_text(h, "the value 0 if successful; otherwise the value "
1759 "-1 is returned and the global variable");
1760
1761 PAIR_CLASS_INIT(&tag, "var");
1762 t = print_otag(h, TAG_B, 1, &tag);
1763 print_text(h, "errno");
1764 print_tagq(h, t);
1765 print_text(h, "is set to indicate the error.");
1766 return(0);
1767 }
1768
1769
1770 /* ARGSUSED */
1771 static int
1772 mdoc_va_pre(MDOC_ARGS)
1773 {
1774 struct htmlpair tag;
1775
1776 PAIR_CLASS_INIT(&tag, "var");
1777 print_otag(h, TAG_B, 1, &tag);
1778 return(1);
1779 }
1780
1781
1782 /* ARGSUSED */
1783 static int
1784 mdoc_ap_pre(MDOC_ARGS)
1785 {
1786
1787 h->flags |= HTML_NOSPACE;
1788 print_text(h, "\\(aq");
1789 h->flags |= HTML_NOSPACE;
1790 return(1);
1791 }
1792
1793
1794 /* ARGSUSED */
1795 static int
1796 mdoc_bf_pre(MDOC_ARGS)
1797 {
1798 struct htmlpair tag[2];
1799 struct roffsu su;
1800
1801 if (MDOC_HEAD == n->type)
1802 return(0);
1803 else if (MDOC_BODY != n->type)
1804 return(1);
1805
1806 if (FONT_Em == n->norm->Bf.font)
1807 PAIR_CLASS_INIT(&tag[0], "emph");
1808 else if (FONT_Sy == n->norm->Bf.font)
1809 PAIR_CLASS_INIT(&tag[0], "symb");
1810 else if (FONT_Li == n->norm->Bf.font)
1811 PAIR_CLASS_INIT(&tag[0], "lit");
1812 else
1813 PAIR_CLASS_INIT(&tag[0], "none");
1814
1815 /*
1816 * We want this to be inline-formatted, but needs to be div to
1817 * accept block children.
1818 */
1819 bufcat_style(h, "display", "inline");
1820 SCALE_HS_INIT(&su, 1);
1821 /* Needs a left-margin for spacing. */
1822 bufcat_su(h, "margin-left", &su);
1823 PAIR_STYLE_INIT(&tag[1], h);
1824 print_otag(h, TAG_DIV, 2, tag);
1825 return(1);
1826 }
1827
1828
1829 /* ARGSUSED */
1830 static int
1831 mdoc_ms_pre(MDOC_ARGS)
1832 {
1833 struct htmlpair tag;
1834
1835 PAIR_CLASS_INIT(&tag, "symb");
1836 print_otag(h, TAG_SPAN, 1, &tag);
1837 return(1);
1838 }
1839
1840
1841 /* ARGSUSED */
1842 static int
1843 mdoc_igndelim_pre(MDOC_ARGS)
1844 {
1845
1846 h->flags |= HTML_IGNDELIM;
1847 return(1);
1848 }
1849
1850
1851 /* ARGSUSED */
1852 static void
1853 mdoc_pf_post(MDOC_ARGS)
1854 {
1855
1856 h->flags |= HTML_NOSPACE;
1857 }
1858
1859
1860 /* ARGSUSED */
1861 static int
1862 mdoc_rs_pre(MDOC_ARGS)
1863 {
1864 struct htmlpair tag;
1865
1866 if (MDOC_BLOCK != n->type)
1867 return(1);
1868
1869 if (n->prev && SEC_SEE_ALSO == n->sec)
1870 print_otag(h, TAG_P, 0, NULL);
1871
1872 PAIR_CLASS_INIT(&tag, "ref");
1873 print_otag(h, TAG_SPAN, 1, &tag);
1874 return(1);
1875 }
1876
1877
1878
1879 /* ARGSUSED */
1880 static int
1881 mdoc_li_pre(MDOC_ARGS)
1882 {
1883 struct htmlpair tag;
1884
1885 PAIR_CLASS_INIT(&tag, "lit");
1886 print_otag(h, TAG_SPAN, 1, &tag);
1887 return(1);
1888 }
1889
1890
1891 /* ARGSUSED */
1892 static int
1893 mdoc_sy_pre(MDOC_ARGS)
1894 {
1895 struct htmlpair tag;
1896
1897 PAIR_CLASS_INIT(&tag, "symb");
1898 print_otag(h, TAG_SPAN, 1, &tag);
1899 return(1);
1900 }
1901
1902
1903 /* ARGSUSED */
1904 static int
1905 mdoc_bt_pre(MDOC_ARGS)
1906 {
1907
1908 print_text(h, "is currently in beta test.");
1909 return(0);
1910 }
1911
1912
1913 /* ARGSUSED */
1914 static int
1915 mdoc_ud_pre(MDOC_ARGS)
1916 {
1917
1918 print_text(h, "currently under development.");
1919 return(0);
1920 }
1921
1922
1923 /* ARGSUSED */
1924 static int
1925 mdoc_lb_pre(MDOC_ARGS)
1926 {
1927 struct htmlpair tag;
1928
1929 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags && n->prev)
1930 print_otag(h, TAG_BR, 0, NULL);
1931
1932 PAIR_CLASS_INIT(&tag, "lib");
1933 print_otag(h, TAG_SPAN, 1, &tag);
1934 return(1);
1935 }
1936
1937
1938 /* ARGSUSED */
1939 static int
1940 mdoc__x_pre(MDOC_ARGS)
1941 {
1942 struct htmlpair tag[2];
1943 enum htmltag t;
1944
1945 t = TAG_SPAN;
1946
1947 switch (n->tok) {
1948 case(MDOC__A):
1949 PAIR_CLASS_INIT(&tag[0], "ref-auth");
1950 if (n->prev && MDOC__A == n->prev->tok)
1951 if (NULL == n->next || MDOC__A != n->next->tok)
1952 print_text(h, "and");
1953 break;
1954 case(MDOC__B):
1955 PAIR_CLASS_INIT(&tag[0], "ref-book");
1956 t = TAG_I;
1957 break;
1958 case(MDOC__C):
1959 PAIR_CLASS_INIT(&tag[0], "ref-city");
1960 break;
1961 case(MDOC__D):
1962 PAIR_CLASS_INIT(&tag[0], "ref-date");
1963 break;
1964 case(MDOC__I):
1965 PAIR_CLASS_INIT(&tag[0], "ref-issue");
1966 t = TAG_I;
1967 break;
1968 case(MDOC__J):
1969 PAIR_CLASS_INIT(&tag[0], "ref-jrnl");
1970 t = TAG_I;
1971 break;
1972 case(MDOC__N):
1973 PAIR_CLASS_INIT(&tag[0], "ref-num");
1974 break;
1975 case(MDOC__O):
1976 PAIR_CLASS_INIT(&tag[0], "ref-opt");
1977 break;
1978 case(MDOC__P):
1979 PAIR_CLASS_INIT(&tag[0], "ref-page");
1980 break;
1981 case(MDOC__Q):
1982 PAIR_CLASS_INIT(&tag[0], "ref-corp");
1983 break;
1984 case(MDOC__R):
1985 PAIR_CLASS_INIT(&tag[0], "ref-rep");
1986 break;
1987 case(MDOC__T):
1988 PAIR_CLASS_INIT(&tag[0], "ref-title");
1989 break;
1990 case(MDOC__U):
1991 PAIR_CLASS_INIT(&tag[0], "link-ref");
1992 break;
1993 case(MDOC__V):
1994 PAIR_CLASS_INIT(&tag[0], "ref-vol");
1995 break;
1996 default:
1997 abort();
1998 /* NOTREACHED */
1999 }
2000
2001 if (MDOC__U != n->tok) {
2002 print_otag(h, t, 1, tag);
2003 return(1);
2004 }
2005
2006 PAIR_HREF_INIT(&tag[1], n->child->string);
2007 print_otag(h, TAG_A, 2, tag);
2008
2009 return(1);
2010 }
2011
2012
2013 /* ARGSUSED */
2014 static void
2015 mdoc__x_post(MDOC_ARGS)
2016 {
2017
2018 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
2019 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
2020 if (NULL == n->prev || MDOC__A != n->prev->tok)
2021 return;
2022
2023 /* TODO: %U */
2024
2025 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
2026 return;
2027
2028 print_text(h, n->next ? "," : ".");
2029 }
2030
2031
2032 /* ARGSUSED */
2033 static int
2034 mdoc_bk_pre(MDOC_ARGS)
2035 {
2036
2037 switch (n->type) {
2038 case (MDOC_BLOCK):
2039 break;
2040 case (MDOC_HEAD):
2041 return(0);
2042 case (MDOC_BODY):
2043 if (n->parent->args || 0 == n->prev->nchild)
2044 h->flags |= HTML_PREKEEP;
2045 break;
2046 default:
2047 abort();
2048 /* NOTREACHED */
2049 }
2050
2051 return(1);
2052 }
2053
2054
2055 /* ARGSUSED */
2056 static void
2057 mdoc_bk_post(MDOC_ARGS)
2058 {
2059
2060 if (MDOC_BODY == n->type)
2061 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
2062 }
2063
2064
2065 /* ARGSUSED */
2066 static int
2067 mdoc_quote_pre(MDOC_ARGS)
2068 {
2069 struct htmlpair tag;
2070
2071 if (MDOC_BODY != n->type)
2072 return(1);
2073
2074 switch (n->tok) {
2075 case (MDOC_Ao):
2076 /* FALLTHROUGH */
2077 case (MDOC_Aq):
2078 print_text(h, "\\(la");
2079 break;
2080 case (MDOC_Bro):
2081 /* FALLTHROUGH */
2082 case (MDOC_Brq):
2083 print_text(h, "\\(lC");
2084 break;
2085 case (MDOC_Bo):
2086 /* FALLTHROUGH */
2087 case (MDOC_Bq):
2088 print_text(h, "\\(lB");
2089 break;
2090 case (MDOC_Oo):
2091 /* FALLTHROUGH */
2092 case (MDOC_Op):
2093 print_text(h, "\\(lB");
2094 h->flags |= HTML_NOSPACE;
2095 PAIR_CLASS_INIT(&tag, "opt");
2096 print_otag(h, TAG_SPAN, 1, &tag);
2097 break;
2098 case (MDOC_Do):
2099 /* FALLTHROUGH */
2100 case (MDOC_Dq):
2101 /* FALLTHROUGH */
2102 case (MDOC_Qo):
2103 /* FALLTHROUGH */
2104 case (MDOC_Qq):
2105 print_text(h, "\\(lq");
2106 break;
2107 case (MDOC_Po):
2108 /* FALLTHROUGH */
2109 case (MDOC_Pq):
2110 print_text(h, "(");
2111 break;
2112 case (MDOC_Ql):
2113 /* FALLTHROUGH */
2114 case (MDOC_So):
2115 /* FALLTHROUGH */
2116 case (MDOC_Sq):
2117 print_text(h, "\\(oq");
2118 break;
2119 default:
2120 abort();
2121 /* NOTREACHED */
2122 }
2123
2124 h->flags |= HTML_NOSPACE;
2125 return(1);
2126 }
2127
2128
2129 /* ARGSUSED */
2130 static void
2131 mdoc_quote_post(MDOC_ARGS)
2132 {
2133
2134 if (MDOC_BODY != n->type)
2135 return;
2136
2137 h->flags |= HTML_NOSPACE;
2138
2139 switch (n->tok) {
2140 case (MDOC_Ao):
2141 /* FALLTHROUGH */
2142 case (MDOC_Aq):
2143 print_text(h, "\\(ra");
2144 break;
2145 case (MDOC_Bro):
2146 /* FALLTHROUGH */
2147 case (MDOC_Brq):
2148 print_text(h, "\\(rC");
2149 break;
2150 case (MDOC_Oo):
2151 /* FALLTHROUGH */
2152 case (MDOC_Op):
2153 /* FALLTHROUGH */
2154 case (MDOC_Bo):
2155 /* FALLTHROUGH */
2156 case (MDOC_Bq):
2157 print_text(h, "\\(rB");
2158 break;
2159 case (MDOC_Qo):
2160 /* FALLTHROUGH */
2161 case (MDOC_Qq):
2162 /* FALLTHROUGH */
2163 case (MDOC_Do):
2164 /* FALLTHROUGH */
2165 case (MDOC_Dq):
2166 print_text(h, "\\(rq");
2167 break;
2168 case (MDOC_Po):
2169 /* FALLTHROUGH */
2170 case (MDOC_Pq):
2171 print_text(h, ")");
2172 break;
2173 case (MDOC_Ql):
2174 /* FALLTHROUGH */
2175 case (MDOC_So):
2176 /* FALLTHROUGH */
2177 case (MDOC_Sq):
2178 print_text(h, "\\(aq");
2179 break;
2180 default:
2181 abort();
2182 /* NOTREACHED */
2183 }
2184 }
2185
2186