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