]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
Multiple fixes with respect to in-line macros:
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.210 2014/11/17 06:44:58 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "mandoc.h"
30 #include "mandoc_aux.h"
31 #include "out.h"
32 #include "html.h"
33 #include "mdoc.h"
34 #include "main.h"
35
36 #define INDENT 5
37
38 #define MDOC_ARGS const struct mdoc_meta *meta, \
39 const struct mdoc_node *n, \
40 struct html *h
41
42 #ifndef MIN
43 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
44 #endif
45
46 struct htmlmdoc {
47 int (*pre)(MDOC_ARGS);
48 void (*post)(MDOC_ARGS);
49 };
50
51 static void print_mdoc(MDOC_ARGS);
52 static void print_mdoc_head(MDOC_ARGS);
53 static void print_mdoc_node(MDOC_ARGS);
54 static void print_mdoc_nodelist(MDOC_ARGS);
55 static void synopsis_pre(struct html *,
56 const struct mdoc_node *);
57
58 static void a2width(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 int mdoc_ar_pre(MDOC_ARGS);
69 static int mdoc_bd_pre(MDOC_ARGS);
70 static int mdoc_bf_pre(MDOC_ARGS);
71 static void mdoc_bk_post(MDOC_ARGS);
72 static int mdoc_bk_pre(MDOC_ARGS);
73 static int mdoc_bl_pre(MDOC_ARGS);
74 static int mdoc_bt_pre(MDOC_ARGS);
75 static int mdoc_bx_pre(MDOC_ARGS);
76 static int mdoc_cd_pre(MDOC_ARGS);
77 static int mdoc_d1_pre(MDOC_ARGS);
78 static int mdoc_dv_pre(MDOC_ARGS);
79 static int mdoc_fa_pre(MDOC_ARGS);
80 static int mdoc_fd_pre(MDOC_ARGS);
81 static int mdoc_fl_pre(MDOC_ARGS);
82 static int mdoc_fn_pre(MDOC_ARGS);
83 static int mdoc_ft_pre(MDOC_ARGS);
84 static int mdoc_em_pre(MDOC_ARGS);
85 static int mdoc_er_pre(MDOC_ARGS);
86 static int mdoc_ev_pre(MDOC_ARGS);
87 static int mdoc_ex_pre(MDOC_ARGS);
88 static void mdoc_fo_post(MDOC_ARGS);
89 static int mdoc_fo_pre(MDOC_ARGS);
90 static int mdoc_ic_pre(MDOC_ARGS);
91 static int mdoc_igndelim_pre(MDOC_ARGS);
92 static int mdoc_in_pre(MDOC_ARGS);
93 static int mdoc_it_pre(MDOC_ARGS);
94 static int mdoc_lb_pre(MDOC_ARGS);
95 static int mdoc_li_pre(MDOC_ARGS);
96 static int mdoc_lk_pre(MDOC_ARGS);
97 static int mdoc_mt_pre(MDOC_ARGS);
98 static int mdoc_ms_pre(MDOC_ARGS);
99 static int mdoc_nd_pre(MDOC_ARGS);
100 static int mdoc_nm_pre(MDOC_ARGS);
101 static int mdoc_no_pre(MDOC_ARGS);
102 static int mdoc_ns_pre(MDOC_ARGS);
103 static int mdoc_pa_pre(MDOC_ARGS);
104 static void mdoc_pf_post(MDOC_ARGS);
105 static int mdoc_pp_pre(MDOC_ARGS);
106 static void mdoc_quote_post(MDOC_ARGS);
107 static int mdoc_quote_pre(MDOC_ARGS);
108 static int mdoc_rs_pre(MDOC_ARGS);
109 static int mdoc_rv_pre(MDOC_ARGS);
110 static int mdoc_sh_pre(MDOC_ARGS);
111 static int mdoc_skip_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 {mdoc_ft_pre, 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 {mdoc_quote_pre, mdoc_quote_post}, /* Eo */
194 {mdoc_xx_pre, NULL}, /* Fx */
195 {mdoc_ms_pre, NULL}, /* Ms */
196 {mdoc_no_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 {mdoc_em_pre, 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 {mdoc_skip_pre, NULL}, /* Es */
239 {mdoc_quote_pre, mdoc_quote_post}, /* En */
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 {mdoc_skip_pre, NULL}, /* ll */
247 };
248
249 static const char * const lists[LIST_MAX] = {
250 NULL,
251 "list-bul",
252 "list-col",
253 "list-dash",
254 "list-diag",
255 "list-enum",
256 "list-hang",
257 "list-hyph",
258 "list-inset",
259 "list-item",
260 "list-ohang",
261 "list-tag"
262 };
263
264
265 void
266 html_mdoc(void *arg, const struct mdoc *mdoc)
267 {
268
269 print_mdoc(mdoc_meta(mdoc), mdoc_node(mdoc),
270 (struct html *)arg);
271 putchar('\n');
272 }
273
274 /*
275 * Calculate the scaling unit passed in a `-width' argument. This uses
276 * either a native scaling unit (e.g., 1i, 2m) or the string length of
277 * the value.
278 */
279 static void
280 a2width(const char *p, struct roffsu *su)
281 {
282
283 if ( ! a2roffsu(p, su, SCALE_MAX)) {
284 su->unit = SCALE_EN;
285 su->scale = html_strlen(p);
286 }
287 }
288
289 /*
290 * See the same function in mdoc_term.c for documentation.
291 */
292 static void
293 synopsis_pre(struct html *h, const struct mdoc_node *n)
294 {
295
296 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
297 return;
298
299 if (n->prev->tok == n->tok &&
300 MDOC_Fo != n->tok &&
301 MDOC_Ft != n->tok &&
302 MDOC_Fn != n->tok) {
303 print_otag(h, TAG_BR, 0, NULL);
304 return;
305 }
306
307 switch (n->prev->tok) {
308 case MDOC_Fd:
309 /* FALLTHROUGH */
310 case MDOC_Fn:
311 /* FALLTHROUGH */
312 case MDOC_Fo:
313 /* FALLTHROUGH */
314 case MDOC_In:
315 /* FALLTHROUGH */
316 case MDOC_Vt:
317 print_paragraph(h);
318 break;
319 case MDOC_Ft:
320 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
321 print_paragraph(h);
322 break;
323 }
324 /* FALLTHROUGH */
325 default:
326 print_otag(h, TAG_BR, 0, NULL);
327 break;
328 }
329 }
330
331 static void
332 print_mdoc(MDOC_ARGS)
333 {
334 struct tag *t, *tt;
335 struct htmlpair tag;
336
337 PAIR_CLASS_INIT(&tag, "mandoc");
338
339 if ( ! (HTML_FRAGMENT & h->oflags)) {
340 print_gen_decls(h);
341 t = print_otag(h, TAG_HTML, 0, NULL);
342 tt = print_otag(h, TAG_HEAD, 0, NULL);
343 print_mdoc_head(meta, n, h);
344 print_tagq(h, tt);
345 print_otag(h, TAG_BODY, 0, NULL);
346 print_otag(h, TAG_DIV, 1, &tag);
347 } else
348 t = print_otag(h, TAG_DIV, 1, &tag);
349
350 print_mdoc_nodelist(meta, n, h);
351 print_tagq(h, t);
352 }
353
354 static void
355 print_mdoc_head(MDOC_ARGS)
356 {
357
358 print_gen_head(h);
359 bufinit(h);
360 bufcat(h, meta->title);
361 if (meta->msec)
362 bufcat_fmt(h, "(%s)", meta->msec);
363 if (meta->arch)
364 bufcat_fmt(h, " (%s)", meta->arch);
365
366 print_otag(h, TAG_TITLE, 0, NULL);
367 print_text(h, h->buf);
368 }
369
370 static void
371 print_mdoc_nodelist(MDOC_ARGS)
372 {
373
374 print_mdoc_node(meta, n, h);
375 if (n->next)
376 print_mdoc_nodelist(meta, n->next, h);
377 }
378
379 static void
380 print_mdoc_node(MDOC_ARGS)
381 {
382 int child;
383 struct tag *t;
384
385 child = 1;
386 t = h->tags.head;
387
388 switch (n->type) {
389 case MDOC_ROOT:
390 child = mdoc_root_pre(meta, n, h);
391 break;
392 case MDOC_TEXT:
393 /* No tables in this mode... */
394 assert(NULL == h->tblt);
395
396 /*
397 * Make sure that if we're in a literal mode already
398 * (i.e., within a <PRE>) don't print the newline.
399 */
400 if (' ' == *n->string && MDOC_LINE & n->flags)
401 if ( ! (HTML_LITERAL & h->flags))
402 print_otag(h, TAG_BR, 0, NULL);
403 if (MDOC_DELIMC & n->flags)
404 h->flags |= HTML_NOSPACE;
405 print_text(h, n->string);
406 if (MDOC_DELIMO & n->flags)
407 h->flags |= HTML_NOSPACE;
408 return;
409 case MDOC_EQN:
410 print_eqn(h, n->eqn);
411 break;
412 case MDOC_TBL:
413 /*
414 * This will take care of initialising all of the table
415 * state data for the first table, then tearing it down
416 * for the last one.
417 */
418 print_tbl(h, n->span);
419 return;
420 default:
421 /*
422 * Close out the current table, if it's open, and unset
423 * the "meta" table state. This will be reopened on the
424 * next table element.
425 */
426 if (h->tblt) {
427 print_tblclose(h);
428 t = h->tags.head;
429 }
430
431 assert(NULL == h->tblt);
432 if (mdocs[n->tok].pre && ENDBODY_NOT == n->end)
433 child = (*mdocs[n->tok].pre)(meta, n, h);
434 break;
435 }
436
437 if (HTML_KEEP & h->flags) {
438 if (n->prev ? (n->prev->lastline != n->line) :
439 (n->parent && n->parent->line != n->line)) {
440 h->flags &= ~HTML_KEEP;
441 h->flags |= HTML_PREKEEP;
442 }
443 }
444
445 if (child && n->child)
446 print_mdoc_nodelist(meta, n->child, h);
447
448 print_stagq(h, t);
449
450 switch (n->type) {
451 case MDOC_ROOT:
452 mdoc_root_post(meta, n, h);
453 break;
454 case MDOC_EQN:
455 break;
456 default:
457 if (mdocs[n->tok].post && ENDBODY_NOT == n->end)
458 (*mdocs[n->tok].post)(meta, n, h);
459 break;
460 }
461 }
462
463 static void
464 mdoc_root_post(MDOC_ARGS)
465 {
466 struct htmlpair tag;
467 struct tag *t, *tt;
468
469 PAIR_CLASS_INIT(&tag, "foot");
470 t = print_otag(h, TAG_TABLE, 1, &tag);
471
472 print_otag(h, TAG_TBODY, 0, NULL);
473
474 tt = print_otag(h, TAG_TR, 0, NULL);
475
476 PAIR_CLASS_INIT(&tag, "foot-date");
477 print_otag(h, TAG_TD, 1, &tag);
478 print_text(h, meta->date);
479 print_stagq(h, tt);
480
481 PAIR_CLASS_INIT(&tag, "foot-os");
482 print_otag(h, TAG_TD, 1, &tag);
483 print_text(h, meta->os);
484 print_tagq(h, t);
485 }
486
487 static int
488 mdoc_root_pre(MDOC_ARGS)
489 {
490 struct htmlpair tag;
491 struct tag *t, *tt;
492 char *volume, *title;
493
494 if (NULL == meta->arch)
495 volume = mandoc_strdup(meta->vol);
496 else
497 mandoc_asprintf(&volume, "%s (%s)",
498 meta->vol, meta->arch);
499
500 if (NULL == meta->msec)
501 title = mandoc_strdup(meta->title);
502 else
503 mandoc_asprintf(&title, "%s(%s)",
504 meta->title, meta->msec);
505
506 PAIR_CLASS_INIT(&tag, "head");
507 t = print_otag(h, TAG_TABLE, 1, &tag);
508
509 print_otag(h, TAG_TBODY, 0, NULL);
510
511 tt = print_otag(h, TAG_TR, 0, NULL);
512
513 PAIR_CLASS_INIT(&tag, "head-ltitle");
514 print_otag(h, TAG_TD, 1, &tag);
515 print_text(h, title);
516 print_stagq(h, tt);
517
518 PAIR_CLASS_INIT(&tag, "head-vol");
519 print_otag(h, TAG_TD, 1, &tag);
520 print_text(h, volume);
521 print_stagq(h, tt);
522
523 PAIR_CLASS_INIT(&tag, "head-rtitle");
524 print_otag(h, TAG_TD, 1, &tag);
525 print_text(h, title);
526 print_tagq(h, t);
527
528 free(title);
529 free(volume);
530 return(1);
531 }
532
533 static int
534 mdoc_sh_pre(MDOC_ARGS)
535 {
536 struct htmlpair tag;
537
538 switch (n->type) {
539 case MDOC_BLOCK:
540 PAIR_CLASS_INIT(&tag, "section");
541 print_otag(h, TAG_DIV, 1, &tag);
542 return(1);
543 case MDOC_BODY:
544 if (n->sec == SEC_AUTHORS)
545 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
546 return(1);
547 default:
548 break;
549 }
550
551 bufinit(h);
552 bufcat(h, "x");
553
554 for (n = n->child; n && MDOC_TEXT == n->type; ) {
555 bufcat_id(h, n->string);
556 if (NULL != (n = n->next))
557 bufcat_id(h, " ");
558 }
559
560 if (NULL == n) {
561 PAIR_ID_INIT(&tag, h->buf);
562 print_otag(h, TAG_H1, 1, &tag);
563 } else
564 print_otag(h, TAG_H1, 0, NULL);
565
566 return(1);
567 }
568
569 static int
570 mdoc_ss_pre(MDOC_ARGS)
571 {
572 struct htmlpair tag;
573
574 if (MDOC_BLOCK == n->type) {
575 PAIR_CLASS_INIT(&tag, "subsection");
576 print_otag(h, TAG_DIV, 1, &tag);
577 return(1);
578 } else if (MDOC_BODY == n->type)
579 return(1);
580
581 bufinit(h);
582 bufcat(h, "x");
583
584 for (n = n->child; n && MDOC_TEXT == n->type; ) {
585 bufcat_id(h, n->string);
586 if (NULL != (n = n->next))
587 bufcat_id(h, " ");
588 }
589
590 if (NULL == n) {
591 PAIR_ID_INIT(&tag, h->buf);
592 print_otag(h, TAG_H2, 1, &tag);
593 } else
594 print_otag(h, TAG_H2, 0, NULL);
595
596 return(1);
597 }
598
599 static int
600 mdoc_fl_pre(MDOC_ARGS)
601 {
602 struct htmlpair tag;
603
604 PAIR_CLASS_INIT(&tag, "flag");
605 print_otag(h, TAG_B, 1, &tag);
606
607 /* `Cm' has no leading hyphen. */
608
609 if (MDOC_Cm == n->tok)
610 return(1);
611
612 print_text(h, "\\-");
613
614 if ( ! (n->nchild == 0 &&
615 (n->next == NULL ||
616 n->next->type == MDOC_TEXT ||
617 n->next->flags & MDOC_LINE)))
618 h->flags |= HTML_NOSPACE;
619
620 return(1);
621 }
622
623 static int
624 mdoc_nd_pre(MDOC_ARGS)
625 {
626 struct htmlpair tag;
627
628 if (MDOC_BODY != n->type)
629 return(1);
630
631 /* XXX: this tag in theory can contain block elements. */
632
633 print_text(h, "\\(em");
634 PAIR_CLASS_INIT(&tag, "desc");
635 print_otag(h, TAG_SPAN, 1, &tag);
636 return(1);
637 }
638
639 static int
640 mdoc_nm_pre(MDOC_ARGS)
641 {
642 struct htmlpair tag;
643 struct roffsu su;
644 int len;
645
646 switch (n->type) {
647 case MDOC_ELEM:
648 synopsis_pre(h, n);
649 PAIR_CLASS_INIT(&tag, "name");
650 print_otag(h, TAG_B, 1, &tag);
651 if (NULL == n->child && meta->name)
652 print_text(h, meta->name);
653 return(1);
654 case MDOC_HEAD:
655 print_otag(h, TAG_TD, 0, NULL);
656 if (NULL == n->child && meta->name)
657 print_text(h, meta->name);
658 return(1);
659 case MDOC_BODY:
660 print_otag(h, TAG_TD, 0, NULL);
661 return(1);
662 default:
663 break;
664 }
665
666 synopsis_pre(h, n);
667 PAIR_CLASS_INIT(&tag, "synopsis");
668 print_otag(h, TAG_TABLE, 1, &tag);
669
670 for (len = 0, n = n->child; n; n = n->next)
671 if (MDOC_TEXT == n->type)
672 len += html_strlen(n->string);
673
674 if (0 == len && meta->name)
675 len = html_strlen(meta->name);
676
677 SCALE_HS_INIT(&su, len);
678 bufinit(h);
679 bufcat_su(h, "width", &su);
680 PAIR_STYLE_INIT(&tag, h);
681 print_otag(h, TAG_COL, 1, &tag);
682 print_otag(h, TAG_COL, 0, NULL);
683 print_otag(h, TAG_TBODY, 0, NULL);
684 print_otag(h, TAG_TR, 0, NULL);
685 return(1);
686 }
687
688 static int
689 mdoc_xr_pre(MDOC_ARGS)
690 {
691 struct htmlpair tag[2];
692
693 if (NULL == n->child)
694 return(0);
695
696 PAIR_CLASS_INIT(&tag[0], "link-man");
697
698 if (h->base_man) {
699 buffmt_man(h, n->child->string,
700 n->child->next ?
701 n->child->next->string : NULL);
702 PAIR_HREF_INIT(&tag[1], h->buf);
703 print_otag(h, TAG_A, 2, tag);
704 } else
705 print_otag(h, TAG_A, 1, tag);
706
707 n = n->child;
708 print_text(h, n->string);
709
710 if (NULL == (n = n->next))
711 return(0);
712
713 h->flags |= HTML_NOSPACE;
714 print_text(h, "(");
715 h->flags |= HTML_NOSPACE;
716 print_text(h, n->string);
717 h->flags |= HTML_NOSPACE;
718 print_text(h, ")");
719 return(0);
720 }
721
722 static int
723 mdoc_ns_pre(MDOC_ARGS)
724 {
725
726 if ( ! (MDOC_LINE & n->flags))
727 h->flags |= HTML_NOSPACE;
728 return(1);
729 }
730
731 static int
732 mdoc_ar_pre(MDOC_ARGS)
733 {
734 struct htmlpair tag;
735
736 PAIR_CLASS_INIT(&tag, "arg");
737 print_otag(h, TAG_I, 1, &tag);
738 return(1);
739 }
740
741 static int
742 mdoc_xx_pre(MDOC_ARGS)
743 {
744 const char *pp;
745 struct htmlpair tag;
746 int flags;
747
748 switch (n->tok) {
749 case MDOC_Bsx:
750 pp = "BSD/OS";
751 break;
752 case MDOC_Dx:
753 pp = "DragonFly";
754 break;
755 case MDOC_Fx:
756 pp = "FreeBSD";
757 break;
758 case MDOC_Nx:
759 pp = "NetBSD";
760 break;
761 case MDOC_Ox:
762 pp = "OpenBSD";
763 break;
764 case MDOC_Ux:
765 pp = "UNIX";
766 break;
767 default:
768 return(1);
769 }
770
771 PAIR_CLASS_INIT(&tag, "unix");
772 print_otag(h, TAG_SPAN, 1, &tag);
773
774 print_text(h, pp);
775 if (n->child) {
776 flags = h->flags;
777 h->flags |= HTML_KEEP;
778 print_text(h, n->child->string);
779 h->flags = flags;
780 }
781 return(0);
782 }
783
784 static int
785 mdoc_bx_pre(MDOC_ARGS)
786 {
787 struct htmlpair tag;
788
789 PAIR_CLASS_INIT(&tag, "unix");
790 print_otag(h, TAG_SPAN, 1, &tag);
791
792 if (NULL != (n = n->child)) {
793 print_text(h, n->string);
794 h->flags |= HTML_NOSPACE;
795 print_text(h, "BSD");
796 } else {
797 print_text(h, "BSD");
798 return(0);
799 }
800
801 if (NULL != (n = n->next)) {
802 h->flags |= HTML_NOSPACE;
803 print_text(h, "-");
804 h->flags |= HTML_NOSPACE;
805 print_text(h, n->string);
806 }
807
808 return(0);
809 }
810
811 static int
812 mdoc_it_pre(MDOC_ARGS)
813 {
814 struct roffsu su;
815 enum mdoc_list type;
816 struct htmlpair tag[2];
817 const struct mdoc_node *bl;
818
819 bl = n->parent;
820 while (bl && MDOC_Bl != bl->tok)
821 bl = bl->parent;
822
823 assert(bl);
824
825 type = bl->norm->Bl.type;
826
827 assert(lists[type]);
828 PAIR_CLASS_INIT(&tag[0], lists[type]);
829
830 bufinit(h);
831
832 if (MDOC_HEAD == n->type) {
833 switch (type) {
834 case LIST_bullet:
835 /* FALLTHROUGH */
836 case LIST_dash:
837 /* FALLTHROUGH */
838 case LIST_item:
839 /* FALLTHROUGH */
840 case LIST_hyphen:
841 /* FALLTHROUGH */
842 case LIST_enum:
843 return(0);
844 case LIST_diag:
845 /* FALLTHROUGH */
846 case LIST_hang:
847 /* FALLTHROUGH */
848 case LIST_inset:
849 /* FALLTHROUGH */
850 case LIST_ohang:
851 /* FALLTHROUGH */
852 case LIST_tag:
853 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
854 bufcat_su(h, "margin-top", &su);
855 PAIR_STYLE_INIT(&tag[1], h);
856 print_otag(h, TAG_DT, 2, tag);
857 if (LIST_diag != type)
858 break;
859 PAIR_CLASS_INIT(&tag[0], "diag");
860 print_otag(h, TAG_B, 1, tag);
861 break;
862 case LIST_column:
863 break;
864 default:
865 break;
866 }
867 } else if (MDOC_BODY == n->type) {
868 switch (type) {
869 case LIST_bullet:
870 /* FALLTHROUGH */
871 case LIST_hyphen:
872 /* FALLTHROUGH */
873 case LIST_dash:
874 /* FALLTHROUGH */
875 case LIST_enum:
876 /* FALLTHROUGH */
877 case LIST_item:
878 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
879 bufcat_su(h, "margin-top", &su);
880 PAIR_STYLE_INIT(&tag[1], h);
881 print_otag(h, TAG_LI, 2, tag);
882 break;
883 case LIST_diag:
884 /* FALLTHROUGH */
885 case LIST_hang:
886 /* FALLTHROUGH */
887 case LIST_inset:
888 /* FALLTHROUGH */
889 case LIST_ohang:
890 /* FALLTHROUGH */
891 case LIST_tag:
892 if (NULL == bl->norm->Bl.width) {
893 print_otag(h, TAG_DD, 1, tag);
894 break;
895 }
896 a2width(bl->norm->Bl.width, &su);
897 bufcat_su(h, "margin-left", &su);
898 PAIR_STYLE_INIT(&tag[1], h);
899 print_otag(h, TAG_DD, 2, tag);
900 break;
901 case LIST_column:
902 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
903 bufcat_su(h, "margin-top", &su);
904 PAIR_STYLE_INIT(&tag[1], h);
905 print_otag(h, TAG_TD, 2, tag);
906 break;
907 default:
908 break;
909 }
910 } else {
911 switch (type) {
912 case LIST_column:
913 print_otag(h, TAG_TR, 1, tag);
914 break;
915 default:
916 break;
917 }
918 }
919
920 return(1);
921 }
922
923 static int
924 mdoc_bl_pre(MDOC_ARGS)
925 {
926 int i;
927 struct htmlpair tag[3];
928 struct roffsu su;
929 char buf[BUFSIZ];
930
931 if (MDOC_BODY == n->type) {
932 if (LIST_column == n->norm->Bl.type)
933 print_otag(h, TAG_TBODY, 0, NULL);
934 return(1);
935 }
936
937 if (MDOC_HEAD == n->type) {
938 if (LIST_column != n->norm->Bl.type)
939 return(0);
940
941 /*
942 * For each column, print out the <COL> tag with our
943 * suggested width. The last column gets min-width, as
944 * in terminal mode it auto-sizes to the width of the
945 * screen and we want to preserve that behaviour.
946 */
947
948 for (i = 0; i < (int)n->norm->Bl.ncols; i++) {
949 bufinit(h);
950 a2width(n->norm->Bl.cols[i], &su);
951 if (i < (int)n->norm->Bl.ncols - 1)
952 bufcat_su(h, "width", &su);
953 else
954 bufcat_su(h, "min-width", &su);
955 PAIR_STYLE_INIT(&tag[0], h);
956 print_otag(h, TAG_COL, 1, tag);
957 }
958
959 return(0);
960 }
961
962 SCALE_VS_INIT(&su, 0);
963 bufinit(h);
964 bufcat_su(h, "margin-top", &su);
965 bufcat_su(h, "margin-bottom", &su);
966 PAIR_STYLE_INIT(&tag[0], h);
967
968 assert(lists[n->norm->Bl.type]);
969 (void)strlcpy(buf, "list ", BUFSIZ);
970 (void)strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
971 PAIR_INIT(&tag[1], ATTR_CLASS, buf);
972
973 /* Set the block's left-hand margin. */
974
975 if (n->norm->Bl.offs) {
976 a2width(n->norm->Bl.offs, &su);
977 bufcat_su(h, "margin-left", &su);
978 }
979
980 switch (n->norm->Bl.type) {
981 case LIST_bullet:
982 /* FALLTHROUGH */
983 case LIST_dash:
984 /* FALLTHROUGH */
985 case LIST_hyphen:
986 /* FALLTHROUGH */
987 case LIST_item:
988 print_otag(h, TAG_UL, 2, tag);
989 break;
990 case LIST_enum:
991 print_otag(h, TAG_OL, 2, tag);
992 break;
993 case LIST_diag:
994 /* FALLTHROUGH */
995 case LIST_hang:
996 /* FALLTHROUGH */
997 case LIST_inset:
998 /* FALLTHROUGH */
999 case LIST_ohang:
1000 /* FALLTHROUGH */
1001 case LIST_tag:
1002 print_otag(h, TAG_DL, 2, tag);
1003 break;
1004 case LIST_column:
1005 print_otag(h, TAG_TABLE, 2, tag);
1006 break;
1007 default:
1008 abort();
1009 /* NOTREACHED */
1010 }
1011
1012 return(1);
1013 }
1014
1015 static int
1016 mdoc_ex_pre(MDOC_ARGS)
1017 {
1018 struct tag *t;
1019 struct htmlpair tag;
1020 int nchild;
1021
1022 if (n->prev)
1023 print_otag(h, TAG_BR, 0, NULL);
1024
1025 PAIR_CLASS_INIT(&tag, "utility");
1026
1027 print_text(h, "The");
1028
1029 nchild = n->nchild;
1030 for (n = n->child; n; n = n->next) {
1031 assert(MDOC_TEXT == n->type);
1032
1033 t = print_otag(h, TAG_B, 1, &tag);
1034 print_text(h, n->string);
1035 print_tagq(h, t);
1036
1037 if (nchild > 2 && n->next) {
1038 h->flags |= HTML_NOSPACE;
1039 print_text(h, ",");
1040 }
1041
1042 if (n->next && NULL == n->next->next)
1043 print_text(h, "and");
1044 }
1045
1046 if (nchild > 1)
1047 print_text(h, "utilities exit\\~0");
1048 else
1049 print_text(h, "utility exits\\~0");
1050
1051 print_text(h, "on success, and\\~>0 if an error occurs.");
1052 return(0);
1053 }
1054
1055 static int
1056 mdoc_em_pre(MDOC_ARGS)
1057 {
1058 struct htmlpair tag;
1059
1060 PAIR_CLASS_INIT(&tag, "emph");
1061 print_otag(h, TAG_SPAN, 1, &tag);
1062 return(1);
1063 }
1064
1065 static int
1066 mdoc_d1_pre(MDOC_ARGS)
1067 {
1068 struct htmlpair tag[2];
1069 struct roffsu su;
1070
1071 if (MDOC_BLOCK != n->type)
1072 return(1);
1073
1074 SCALE_VS_INIT(&su, 0);
1075 bufinit(h);
1076 bufcat_su(h, "margin-top", &su);
1077 bufcat_su(h, "margin-bottom", &su);
1078 PAIR_STYLE_INIT(&tag[0], h);
1079 print_otag(h, TAG_BLOCKQUOTE, 1, tag);
1080
1081 /* BLOCKQUOTE needs a block body. */
1082
1083 PAIR_CLASS_INIT(&tag[0], "display");
1084 print_otag(h, TAG_DIV, 1, tag);
1085
1086 if (MDOC_Dl == n->tok) {
1087 PAIR_CLASS_INIT(&tag[0], "lit");
1088 print_otag(h, TAG_CODE, 1, tag);
1089 }
1090
1091 return(1);
1092 }
1093
1094 static int
1095 mdoc_sx_pre(MDOC_ARGS)
1096 {
1097 struct htmlpair tag[2];
1098
1099 bufinit(h);
1100 bufcat(h, "#x");
1101
1102 for (n = n->child; n; ) {
1103 bufcat_id(h, n->string);
1104 if (NULL != (n = n->next))
1105 bufcat_id(h, " ");
1106 }
1107
1108 PAIR_CLASS_INIT(&tag[0], "link-sec");
1109 PAIR_HREF_INIT(&tag[1], h->buf);
1110
1111 print_otag(h, TAG_I, 1, tag);
1112 print_otag(h, TAG_A, 2, tag);
1113 return(1);
1114 }
1115
1116 static int
1117 mdoc_bd_pre(MDOC_ARGS)
1118 {
1119 struct htmlpair tag[2];
1120 int comp, sv;
1121 const struct mdoc_node *nn;
1122 struct roffsu su;
1123
1124 if (MDOC_HEAD == n->type)
1125 return(0);
1126
1127 if (MDOC_BLOCK == n->type) {
1128 comp = n->norm->Bd.comp;
1129 for (nn = n; nn && ! comp; nn = nn->parent) {
1130 if (MDOC_BLOCK != nn->type)
1131 continue;
1132 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
1133 comp = 1;
1134 if (nn->prev)
1135 break;
1136 }
1137 if ( ! comp)
1138 print_paragraph(h);
1139 return(1);
1140 }
1141
1142 /* Handle the -offset argument. */
1143
1144 if (n->norm->Bd.offs == NULL ||
1145 ! strcmp(n->norm->Bd.offs, "left"))
1146 SCALE_HS_INIT(&su, 0);
1147 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
1148 SCALE_HS_INIT(&su, INDENT);
1149 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
1150 SCALE_HS_INIT(&su, INDENT * 2);
1151 else
1152 a2width(n->norm->Bd.offs, &su);
1153
1154 bufinit(h);
1155 bufcat_su(h, "margin-left", &su);
1156 PAIR_STYLE_INIT(&tag[0], h);
1157
1158 if (DISP_unfilled != n->norm->Bd.type &&
1159 DISP_literal != n->norm->Bd.type) {
1160 PAIR_CLASS_INIT(&tag[1], "display");
1161 print_otag(h, TAG_DIV, 2, tag);
1162 return(1);
1163 }
1164
1165 PAIR_CLASS_INIT(&tag[1], "lit display");
1166 print_otag(h, TAG_PRE, 2, tag);
1167
1168 /* This can be recursive: save & set our literal state. */
1169
1170 sv = h->flags & HTML_LITERAL;
1171 h->flags |= HTML_LITERAL;
1172
1173 for (nn = n->child; nn; nn = nn->next) {
1174 print_mdoc_node(meta, nn, h);
1175 /*
1176 * If the printed node flushes its own line, then we
1177 * needn't do it here as well. This is hacky, but the
1178 * notion of selective eoln whitespace is pretty dumb
1179 * anyway, so don't sweat it.
1180 */
1181 switch (nn->tok) {
1182 case MDOC_Sm:
1183 /* FALLTHROUGH */
1184 case MDOC_br:
1185 /* FALLTHROUGH */
1186 case MDOC_sp:
1187 /* FALLTHROUGH */
1188 case MDOC_Bl:
1189 /* FALLTHROUGH */
1190 case MDOC_D1:
1191 /* FALLTHROUGH */
1192 case MDOC_Dl:
1193 /* FALLTHROUGH */
1194 case MDOC_Lp:
1195 /* FALLTHROUGH */
1196 case MDOC_Pp:
1197 continue;
1198 default:
1199 break;
1200 }
1201 if (nn->next && nn->next->line == nn->line)
1202 continue;
1203 else if (nn->next)
1204 print_text(h, "\n");
1205
1206 h->flags |= HTML_NOSPACE;
1207 }
1208
1209 if (0 == sv)
1210 h->flags &= ~HTML_LITERAL;
1211
1212 return(0);
1213 }
1214
1215 static int
1216 mdoc_pa_pre(MDOC_ARGS)
1217 {
1218 struct htmlpair tag;
1219
1220 PAIR_CLASS_INIT(&tag, "file");
1221 print_otag(h, TAG_I, 1, &tag);
1222 return(1);
1223 }
1224
1225 static int
1226 mdoc_ad_pre(MDOC_ARGS)
1227 {
1228 struct htmlpair tag;
1229
1230 PAIR_CLASS_INIT(&tag, "addr");
1231 print_otag(h, TAG_I, 1, &tag);
1232 return(1);
1233 }
1234
1235 static int
1236 mdoc_an_pre(MDOC_ARGS)
1237 {
1238 struct htmlpair tag;
1239
1240 if (n->norm->An.auth == AUTH_split) {
1241 h->flags &= ~HTML_NOSPLIT;
1242 h->flags |= HTML_SPLIT;
1243 return(0);
1244 }
1245 if (n->norm->An.auth == AUTH_nosplit) {
1246 h->flags &= ~HTML_SPLIT;
1247 h->flags |= HTML_NOSPLIT;
1248 return(0);
1249 }
1250
1251 if (n->child == NULL)
1252 return(0);
1253
1254 if (h->flags & HTML_SPLIT)
1255 print_otag(h, TAG_BR, 0, NULL);
1256
1257 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1258 h->flags |= HTML_SPLIT;
1259
1260 PAIR_CLASS_INIT(&tag, "author");
1261 print_otag(h, TAG_SPAN, 1, &tag);
1262 return(1);
1263 }
1264
1265 static int
1266 mdoc_cd_pre(MDOC_ARGS)
1267 {
1268 struct htmlpair tag;
1269
1270 synopsis_pre(h, n);
1271 PAIR_CLASS_INIT(&tag, "config");
1272 print_otag(h, TAG_B, 1, &tag);
1273 return(1);
1274 }
1275
1276 static int
1277 mdoc_dv_pre(MDOC_ARGS)
1278 {
1279 struct htmlpair tag;
1280
1281 PAIR_CLASS_INIT(&tag, "define");
1282 print_otag(h, TAG_SPAN, 1, &tag);
1283 return(1);
1284 }
1285
1286 static int
1287 mdoc_ev_pre(MDOC_ARGS)
1288 {
1289 struct htmlpair tag;
1290
1291 PAIR_CLASS_INIT(&tag, "env");
1292 print_otag(h, TAG_SPAN, 1, &tag);
1293 return(1);
1294 }
1295
1296 static int
1297 mdoc_er_pre(MDOC_ARGS)
1298 {
1299 struct htmlpair tag;
1300
1301 PAIR_CLASS_INIT(&tag, "errno");
1302 print_otag(h, TAG_SPAN, 1, &tag);
1303 return(1);
1304 }
1305
1306 static int
1307 mdoc_fa_pre(MDOC_ARGS)
1308 {
1309 const struct mdoc_node *nn;
1310 struct htmlpair tag;
1311 struct tag *t;
1312
1313 PAIR_CLASS_INIT(&tag, "farg");
1314 if (n->parent->tok != MDOC_Fo) {
1315 print_otag(h, TAG_I, 1, &tag);
1316 return(1);
1317 }
1318
1319 for (nn = n->child; nn; nn = nn->next) {
1320 t = print_otag(h, TAG_I, 1, &tag);
1321 print_text(h, nn->string);
1322 print_tagq(h, t);
1323 if (nn->next) {
1324 h->flags |= HTML_NOSPACE;
1325 print_text(h, ",");
1326 }
1327 }
1328
1329 if (n->child && n->next && n->next->tok == MDOC_Fa) {
1330 h->flags |= HTML_NOSPACE;
1331 print_text(h, ",");
1332 }
1333
1334 return(0);
1335 }
1336
1337 static int
1338 mdoc_fd_pre(MDOC_ARGS)
1339 {
1340 struct htmlpair tag[2];
1341 char buf[BUFSIZ];
1342 size_t sz;
1343 int i;
1344 struct tag *t;
1345
1346 synopsis_pre(h, n);
1347
1348 if (NULL == (n = n->child))
1349 return(0);
1350
1351 assert(MDOC_TEXT == n->type);
1352
1353 if (strcmp(n->string, "#include")) {
1354 PAIR_CLASS_INIT(&tag[0], "macro");
1355 print_otag(h, TAG_B, 1, tag);
1356 return(1);
1357 }
1358
1359 PAIR_CLASS_INIT(&tag[0], "includes");
1360 print_otag(h, TAG_B, 1, tag);
1361 print_text(h, n->string);
1362
1363 if (NULL != (n = n->next)) {
1364 assert(MDOC_TEXT == n->type);
1365
1366 /*
1367 * XXX This is broken and not easy to fix.
1368 * When using -Oincludes, truncation may occur.
1369 * Dynamic allocation wouldn't help because
1370 * passing long strings to buffmt_includes()
1371 * does not work either.
1372 */
1373
1374 strlcpy(buf, '<' == *n->string || '"' == *n->string ?
1375 n->string + 1 : n->string, BUFSIZ);
1376
1377 sz = strlen(buf);
1378 if (sz && ('>' == buf[sz - 1] || '"' == buf[sz - 1]))
1379 buf[sz - 1] = '\0';
1380
1381 PAIR_CLASS_INIT(&tag[0], "link-includes");
1382
1383 i = 1;
1384 if (h->base_includes) {
1385 buffmt_includes(h, buf);
1386 PAIR_HREF_INIT(&tag[i], h->buf);
1387 i++;
1388 }
1389
1390 t = print_otag(h, TAG_A, i, tag);
1391 print_text(h, n->string);
1392 print_tagq(h, t);
1393
1394 n = n->next;
1395 }
1396
1397 for ( ; n; n = n->next) {
1398 assert(MDOC_TEXT == n->type);
1399 print_text(h, n->string);
1400 }
1401
1402 return(0);
1403 }
1404
1405 static int
1406 mdoc_vt_pre(MDOC_ARGS)
1407 {
1408 struct htmlpair tag;
1409
1410 if (MDOC_BLOCK == n->type) {
1411 synopsis_pre(h, n);
1412 return(1);
1413 } else if (MDOC_ELEM == n->type) {
1414 synopsis_pre(h, n);
1415 } else if (MDOC_HEAD == n->type)
1416 return(0);
1417
1418 PAIR_CLASS_INIT(&tag, "type");
1419 print_otag(h, TAG_SPAN, 1, &tag);
1420 return(1);
1421 }
1422
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 static int
1435 mdoc_fn_pre(MDOC_ARGS)
1436 {
1437 struct tag *t;
1438 struct htmlpair tag[2];
1439 char nbuf[BUFSIZ];
1440 const char *sp, *ep;
1441 int sz, i, pretty;
1442
1443 pretty = MDOC_SYNPRETTY & n->flags;
1444 synopsis_pre(h, n);
1445
1446 /* Split apart into type and name. */
1447 assert(n->child->string);
1448 sp = n->child->string;
1449
1450 ep = strchr(sp, ' ');
1451 if (NULL != ep) {
1452 PAIR_CLASS_INIT(&tag[0], "ftype");
1453 t = print_otag(h, TAG_I, 1, tag);
1454
1455 while (ep) {
1456 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1457 (void)memcpy(nbuf, sp, (size_t)sz);
1458 nbuf[sz] = '\0';
1459 print_text(h, nbuf);
1460 sp = ++ep;
1461 ep = strchr(sp, ' ');
1462 }
1463 print_tagq(h, t);
1464 }
1465
1466 PAIR_CLASS_INIT(&tag[0], "fname");
1467
1468 /*
1469 * FIXME: only refer to IDs that we know exist.
1470 */
1471
1472 #if 0
1473 if (MDOC_SYNPRETTY & n->flags) {
1474 nbuf[0] = '\0';
1475 html_idcat(nbuf, sp, BUFSIZ);
1476 PAIR_ID_INIT(&tag[1], nbuf);
1477 } else {
1478 strlcpy(nbuf, "#", BUFSIZ);
1479 html_idcat(nbuf, sp, BUFSIZ);
1480 PAIR_HREF_INIT(&tag[1], nbuf);
1481 }
1482 #endif
1483
1484 t = print_otag(h, TAG_B, 1, tag);
1485
1486 if (sp)
1487 print_text(h, sp);
1488
1489 print_tagq(h, t);
1490
1491 h->flags |= HTML_NOSPACE;
1492 print_text(h, "(");
1493 h->flags |= HTML_NOSPACE;
1494
1495 PAIR_CLASS_INIT(&tag[0], "farg");
1496 bufinit(h);
1497 bufcat_style(h, "white-space", "nowrap");
1498 PAIR_STYLE_INIT(&tag[1], h);
1499
1500 for (n = n->child->next; n; n = n->next) {
1501 i = 1;
1502 if (MDOC_SYNPRETTY & n->flags)
1503 i = 2;
1504 t = print_otag(h, TAG_I, i, tag);
1505 print_text(h, n->string);
1506 print_tagq(h, t);
1507 if (n->next) {
1508 h->flags |= HTML_NOSPACE;
1509 print_text(h, ",");
1510 }
1511 }
1512
1513 h->flags |= HTML_NOSPACE;
1514 print_text(h, ")");
1515
1516 if (pretty) {
1517 h->flags |= HTML_NOSPACE;
1518 print_text(h, ";");
1519 }
1520
1521 return(0);
1522 }
1523
1524 static int
1525 mdoc_sm_pre(MDOC_ARGS)
1526 {
1527
1528 if (NULL == n->child)
1529 h->flags ^= HTML_NONOSPACE;
1530 else if (0 == strcmp("on", n->child->string))
1531 h->flags &= ~HTML_NONOSPACE;
1532 else
1533 h->flags |= HTML_NONOSPACE;
1534
1535 if ( ! (HTML_NONOSPACE & h->flags))
1536 h->flags &= ~HTML_NOSPACE;
1537
1538 return(0);
1539 }
1540
1541 static int
1542 mdoc_skip_pre(MDOC_ARGS)
1543 {
1544
1545 return(0);
1546 }
1547
1548 static int
1549 mdoc_pp_pre(MDOC_ARGS)
1550 {
1551
1552 print_paragraph(h);
1553 return(0);
1554 }
1555
1556 static int
1557 mdoc_sp_pre(MDOC_ARGS)
1558 {
1559 struct roffsu su;
1560 struct htmlpair tag;
1561
1562 SCALE_VS_INIT(&su, 1);
1563
1564 if (MDOC_sp == n->tok) {
1565 if (NULL != (n = n->child))
1566 if ( ! a2roffsu(n->string, &su, SCALE_VS))
1567 SCALE_VS_INIT(&su, atoi(n->string));
1568 } else
1569 su.scale = 0.0;
1570
1571 bufinit(h);
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 static int
1584 mdoc_lk_pre(MDOC_ARGS)
1585 {
1586 struct htmlpair tag[2];
1587
1588 if (NULL == (n = n->child))
1589 return(0);
1590
1591 assert(MDOC_TEXT == n->type);
1592
1593 PAIR_CLASS_INIT(&tag[0], "link-ext");
1594 PAIR_HREF_INIT(&tag[1], n->string);
1595
1596 print_otag(h, TAG_A, 2, tag);
1597
1598 if (NULL == n->next)
1599 print_text(h, n->string);
1600
1601 for (n = n->next; n; n = n->next)
1602 print_text(h, n->string);
1603
1604 return(0);
1605 }
1606
1607 static int
1608 mdoc_mt_pre(MDOC_ARGS)
1609 {
1610 struct htmlpair tag[2];
1611 struct tag *t;
1612
1613 PAIR_CLASS_INIT(&tag[0], "link-mail");
1614
1615 for (n = n->child; n; n = n->next) {
1616 assert(MDOC_TEXT == n->type);
1617
1618 bufinit(h);
1619 bufcat(h, "mailto:");
1620 bufcat(h, n->string);
1621
1622 PAIR_HREF_INIT(&tag[1], h->buf);
1623 t = print_otag(h, TAG_A, 2, tag);
1624 print_text(h, n->string);
1625 print_tagq(h, t);
1626 }
1627
1628 return(0);
1629 }
1630
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 static void
1660 mdoc_fo_post(MDOC_ARGS)
1661 {
1662
1663 if (MDOC_BODY != n->type)
1664 return;
1665 h->flags |= HTML_NOSPACE;
1666 print_text(h, ")");
1667 h->flags |= HTML_NOSPACE;
1668 print_text(h, ";");
1669 }
1670
1671 static int
1672 mdoc_in_pre(MDOC_ARGS)
1673 {
1674 struct tag *t;
1675 struct htmlpair tag[2];
1676 int i;
1677
1678 synopsis_pre(h, n);
1679
1680 PAIR_CLASS_INIT(&tag[0], "includes");
1681 print_otag(h, TAG_B, 1, tag);
1682
1683 /*
1684 * The first argument of the `In' gets special treatment as
1685 * being a linked value. Subsequent values are printed
1686 * afterward. groff does similarly. This also handles the case
1687 * of no children.
1688 */
1689
1690 if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags)
1691 print_text(h, "#include");
1692
1693 print_text(h, "<");
1694 h->flags |= HTML_NOSPACE;
1695
1696 if (NULL != (n = n->child)) {
1697 assert(MDOC_TEXT == n->type);
1698
1699 PAIR_CLASS_INIT(&tag[0], "link-includes");
1700
1701 i = 1;
1702 if (h->base_includes) {
1703 buffmt_includes(h, n->string);
1704 PAIR_HREF_INIT(&tag[i], h->buf);
1705 i++;
1706 }
1707
1708 t = print_otag(h, TAG_A, i, tag);
1709 print_text(h, n->string);
1710 print_tagq(h, t);
1711
1712 n = n->next;
1713 }
1714
1715 h->flags |= HTML_NOSPACE;
1716 print_text(h, ">");
1717
1718 for ( ; n; n = n->next) {
1719 assert(MDOC_TEXT == n->type);
1720 print_text(h, n->string);
1721 }
1722
1723 return(0);
1724 }
1725
1726 static int
1727 mdoc_ic_pre(MDOC_ARGS)
1728 {
1729 struct htmlpair tag;
1730
1731 PAIR_CLASS_INIT(&tag, "cmd");
1732 print_otag(h, TAG_B, 1, &tag);
1733 return(1);
1734 }
1735
1736 static int
1737 mdoc_rv_pre(MDOC_ARGS)
1738 {
1739 struct htmlpair tag;
1740 struct tag *t;
1741 int nchild;
1742
1743 if (n->prev)
1744 print_otag(h, TAG_BR, 0, NULL);
1745
1746 PAIR_CLASS_INIT(&tag, "fname");
1747
1748 nchild = n->nchild;
1749 if (nchild > 0) {
1750 print_text(h, "The");
1751
1752 for (n = n->child; n; n = n->next) {
1753 t = print_otag(h, TAG_B, 1, &tag);
1754 print_text(h, n->string);
1755 print_tagq(h, t);
1756
1757 h->flags |= HTML_NOSPACE;
1758 print_text(h, "()");
1759
1760 if (n->next == NULL)
1761 continue;
1762
1763 if (nchild > 2) {
1764 h->flags |= HTML_NOSPACE;
1765 print_text(h, ",");
1766 }
1767 if (n->next->next == NULL)
1768 print_text(h, "and");
1769 }
1770
1771 if (nchild > 1)
1772 print_text(h, "functions return");
1773 else
1774 print_text(h, "function returns");
1775
1776 print_text(h, "the value\\~0 if successful;");
1777 } else
1778 print_text(h, "Upon successful completion,"
1779 " the value\\~0 is returned;");
1780
1781 print_text(h, "otherwise the value\\~\\-1 is returned"
1782 " and the global variable");
1783
1784 PAIR_CLASS_INIT(&tag, "var");
1785 t = print_otag(h, TAG_B, 1, &tag);
1786 print_text(h, "errno");
1787 print_tagq(h, t);
1788 print_text(h, "is set to indicate the error.");
1789 return(0);
1790 }
1791
1792 static int
1793 mdoc_va_pre(MDOC_ARGS)
1794 {
1795 struct htmlpair tag;
1796
1797 PAIR_CLASS_INIT(&tag, "var");
1798 print_otag(h, TAG_B, 1, &tag);
1799 return(1);
1800 }
1801
1802 static int
1803 mdoc_ap_pre(MDOC_ARGS)
1804 {
1805
1806 h->flags |= HTML_NOSPACE;
1807 print_text(h, "\\(aq");
1808 h->flags |= HTML_NOSPACE;
1809 return(1);
1810 }
1811
1812 static int
1813 mdoc_bf_pre(MDOC_ARGS)
1814 {
1815 struct htmlpair tag[2];
1816 struct roffsu su;
1817
1818 if (MDOC_HEAD == n->type)
1819 return(0);
1820 else if (MDOC_BODY != n->type)
1821 return(1);
1822
1823 if (FONT_Em == n->norm->Bf.font)
1824 PAIR_CLASS_INIT(&tag[0], "emph");
1825 else if (FONT_Sy == n->norm->Bf.font)
1826 PAIR_CLASS_INIT(&tag[0], "symb");
1827 else if (FONT_Li == n->norm->Bf.font)
1828 PAIR_CLASS_INIT(&tag[0], "lit");
1829 else
1830 PAIR_CLASS_INIT(&tag[0], "none");
1831
1832 /*
1833 * We want this to be inline-formatted, but needs to be div to
1834 * accept block children.
1835 */
1836 bufinit(h);
1837 bufcat_style(h, "display", "inline");
1838 SCALE_HS_INIT(&su, 1);
1839 /* Needs a left-margin for spacing. */
1840 bufcat_su(h, "margin-left", &su);
1841 PAIR_STYLE_INIT(&tag[1], h);
1842 print_otag(h, TAG_DIV, 2, tag);
1843 return(1);
1844 }
1845
1846 static int
1847 mdoc_ms_pre(MDOC_ARGS)
1848 {
1849 struct htmlpair tag;
1850
1851 PAIR_CLASS_INIT(&tag, "symb");
1852 print_otag(h, TAG_SPAN, 1, &tag);
1853 return(1);
1854 }
1855
1856 static int
1857 mdoc_igndelim_pre(MDOC_ARGS)
1858 {
1859
1860 h->flags |= HTML_IGNDELIM;
1861 return(1);
1862 }
1863
1864 static void
1865 mdoc_pf_post(MDOC_ARGS)
1866 {
1867
1868 h->flags |= HTML_NOSPACE;
1869 }
1870
1871 static int
1872 mdoc_rs_pre(MDOC_ARGS)
1873 {
1874 struct htmlpair tag;
1875
1876 if (MDOC_BLOCK != n->type)
1877 return(1);
1878
1879 if (n->prev && SEC_SEE_ALSO == n->sec)
1880 print_paragraph(h);
1881
1882 PAIR_CLASS_INIT(&tag, "ref");
1883 print_otag(h, TAG_SPAN, 1, &tag);
1884 return(1);
1885 }
1886
1887 static int
1888 mdoc_no_pre(MDOC_ARGS)
1889 {
1890 struct htmlpair tag;
1891
1892 PAIR_CLASS_INIT(&tag, "none");
1893 print_otag(h, TAG_CODE, 1, &tag);
1894 return(1);
1895 }
1896
1897 static int
1898 mdoc_li_pre(MDOC_ARGS)
1899 {
1900 struct htmlpair tag;
1901
1902 PAIR_CLASS_INIT(&tag, "lit");
1903 print_otag(h, TAG_CODE, 1, &tag);
1904 return(1);
1905 }
1906
1907 static int
1908 mdoc_sy_pre(MDOC_ARGS)
1909 {
1910 struct htmlpair tag;
1911
1912 PAIR_CLASS_INIT(&tag, "symb");
1913 print_otag(h, TAG_SPAN, 1, &tag);
1914 return(1);
1915 }
1916
1917 static int
1918 mdoc_bt_pre(MDOC_ARGS)
1919 {
1920
1921 print_text(h, "is currently in beta test.");
1922 return(0);
1923 }
1924
1925 static int
1926 mdoc_ud_pre(MDOC_ARGS)
1927 {
1928
1929 print_text(h, "currently under development.");
1930 return(0);
1931 }
1932
1933 static int
1934 mdoc_lb_pre(MDOC_ARGS)
1935 {
1936 struct htmlpair tag;
1937
1938 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags && n->prev)
1939 print_otag(h, TAG_BR, 0, NULL);
1940
1941 PAIR_CLASS_INIT(&tag, "lib");
1942 print_otag(h, TAG_SPAN, 1, &tag);
1943 return(1);
1944 }
1945
1946 static int
1947 mdoc__x_pre(MDOC_ARGS)
1948 {
1949 struct htmlpair tag[2];
1950 enum htmltag t;
1951
1952 t = TAG_SPAN;
1953
1954 switch (n->tok) {
1955 case MDOC__A:
1956 PAIR_CLASS_INIT(&tag[0], "ref-auth");
1957 if (n->prev && MDOC__A == n->prev->tok)
1958 if (NULL == n->next || MDOC__A != n->next->tok)
1959 print_text(h, "and");
1960 break;
1961 case MDOC__B:
1962 PAIR_CLASS_INIT(&tag[0], "ref-book");
1963 t = TAG_I;
1964 break;
1965 case MDOC__C:
1966 PAIR_CLASS_INIT(&tag[0], "ref-city");
1967 break;
1968 case MDOC__D:
1969 PAIR_CLASS_INIT(&tag[0], "ref-date");
1970 break;
1971 case MDOC__I:
1972 PAIR_CLASS_INIT(&tag[0], "ref-issue");
1973 t = TAG_I;
1974 break;
1975 case MDOC__J:
1976 PAIR_CLASS_INIT(&tag[0], "ref-jrnl");
1977 t = TAG_I;
1978 break;
1979 case MDOC__N:
1980 PAIR_CLASS_INIT(&tag[0], "ref-num");
1981 break;
1982 case MDOC__O:
1983 PAIR_CLASS_INIT(&tag[0], "ref-opt");
1984 break;
1985 case MDOC__P:
1986 PAIR_CLASS_INIT(&tag[0], "ref-page");
1987 break;
1988 case MDOC__Q:
1989 PAIR_CLASS_INIT(&tag[0], "ref-corp");
1990 break;
1991 case MDOC__R:
1992 PAIR_CLASS_INIT(&tag[0], "ref-rep");
1993 break;
1994 case MDOC__T:
1995 PAIR_CLASS_INIT(&tag[0], "ref-title");
1996 break;
1997 case MDOC__U:
1998 PAIR_CLASS_INIT(&tag[0], "link-ref");
1999 break;
2000 case MDOC__V:
2001 PAIR_CLASS_INIT(&tag[0], "ref-vol");
2002 break;
2003 default:
2004 abort();
2005 /* NOTREACHED */
2006 }
2007
2008 if (MDOC__U != n->tok) {
2009 print_otag(h, t, 1, tag);
2010 return(1);
2011 }
2012
2013 PAIR_HREF_INIT(&tag[1], n->child->string);
2014 print_otag(h, TAG_A, 2, tag);
2015
2016 return(1);
2017 }
2018
2019 static void
2020 mdoc__x_post(MDOC_ARGS)
2021 {
2022
2023 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
2024 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
2025 if (NULL == n->prev || MDOC__A != n->prev->tok)
2026 return;
2027
2028 /* TODO: %U */
2029
2030 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
2031 return;
2032
2033 h->flags |= HTML_NOSPACE;
2034 print_text(h, n->next ? "," : ".");
2035 }
2036
2037 static int
2038 mdoc_bk_pre(MDOC_ARGS)
2039 {
2040
2041 switch (n->type) {
2042 case MDOC_BLOCK:
2043 break;
2044 case MDOC_HEAD:
2045 return(0);
2046 case MDOC_BODY:
2047 if (n->parent->args || 0 == n->prev->nchild)
2048 h->flags |= HTML_PREKEEP;
2049 break;
2050 default:
2051 abort();
2052 /* NOTREACHED */
2053 }
2054
2055 return(1);
2056 }
2057
2058 static void
2059 mdoc_bk_post(MDOC_ARGS)
2060 {
2061
2062 if (MDOC_BODY == n->type)
2063 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
2064 }
2065
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_En:
2099 if (NULL == n->norm->Es ||
2100 NULL == n->norm->Es->child)
2101 return(1);
2102 print_text(h, n->norm->Es->child->string);
2103 break;
2104 case MDOC_Eo:
2105 break;
2106 case MDOC_Do:
2107 /* FALLTHROUGH */
2108 case MDOC_Dq:
2109 /* FALLTHROUGH */
2110 case MDOC_Qo:
2111 /* FALLTHROUGH */
2112 case MDOC_Qq:
2113 print_text(h, "\\(lq");
2114 break;
2115 case MDOC_Po:
2116 /* FALLTHROUGH */
2117 case MDOC_Pq:
2118 print_text(h, "(");
2119 break;
2120 case MDOC_Ql:
2121 print_text(h, "\\(oq");
2122 h->flags |= HTML_NOSPACE;
2123 PAIR_CLASS_INIT(&tag, "lit");
2124 print_otag(h, TAG_CODE, 1, &tag);
2125 break;
2126 case MDOC_So:
2127 /* FALLTHROUGH */
2128 case MDOC_Sq:
2129 print_text(h, "\\(oq");
2130 break;
2131 default:
2132 abort();
2133 /* NOTREACHED */
2134 }
2135
2136 h->flags |= HTML_NOSPACE;
2137 return(1);
2138 }
2139
2140 static void
2141 mdoc_quote_post(MDOC_ARGS)
2142 {
2143
2144 if (MDOC_BODY != n->type)
2145 return;
2146
2147 if (MDOC_En != n->tok)
2148 h->flags |= HTML_NOSPACE;
2149
2150 switch (n->tok) {
2151 case MDOC_Ao:
2152 /* FALLTHROUGH */
2153 case MDOC_Aq:
2154 print_text(h, "\\(ra");
2155 break;
2156 case MDOC_Bro:
2157 /* FALLTHROUGH */
2158 case MDOC_Brq:
2159 print_text(h, "\\(rC");
2160 break;
2161 case MDOC_Oo:
2162 /* FALLTHROUGH */
2163 case MDOC_Op:
2164 /* FALLTHROUGH */
2165 case MDOC_Bo:
2166 /* FALLTHROUGH */
2167 case MDOC_Bq:
2168 print_text(h, "\\(rB");
2169 break;
2170 case MDOC_En:
2171 if (NULL != n->norm->Es &&
2172 NULL != n->norm->Es->child &&
2173 NULL != n->norm->Es->child->next) {
2174 h->flags |= HTML_NOSPACE;
2175 print_text(h, n->norm->Es->child->next->string);
2176 }
2177 break;
2178 case MDOC_Eo:
2179 break;
2180 case MDOC_Qo:
2181 /* FALLTHROUGH */
2182 case MDOC_Qq:
2183 /* FALLTHROUGH */
2184 case MDOC_Do:
2185 /* FALLTHROUGH */
2186 case MDOC_Dq:
2187 print_text(h, "\\(rq");
2188 break;
2189 case MDOC_Po:
2190 /* FALLTHROUGH */
2191 case MDOC_Pq:
2192 print_text(h, ")");
2193 break;
2194 case MDOC_Ql:
2195 /* FALLTHROUGH */
2196 case MDOC_So:
2197 /* FALLTHROUGH */
2198 case MDOC_Sq:
2199 print_text(h, "\\(cq");
2200 break;
2201 default:
2202 abort();
2203 /* NOTREACHED */
2204 }
2205 }