]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
While we do not recommend the idiom ".Fl Fl long" for long options
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.340 2020/04/19 15:16:56 schwarze Exp $ */
2 /*
3 * Copyright (c) 2014-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 * HTML formatter for mdoc(7) used by mandoc(1).
19 */
20 #include "config.h"
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "mandoc_aux.h"
32 #include "mandoc.h"
33 #include "roff.h"
34 #include "mdoc.h"
35 #include "out.h"
36 #include "html.h"
37 #include "main.h"
38
39 #define MDOC_ARGS const struct roff_meta *meta, \
40 struct roff_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 mdoc_html_act {
48 int (*pre)(MDOC_ARGS);
49 void (*post)(MDOC_ARGS);
50 };
51
52 static void print_mdoc_head(const struct roff_meta *,
53 struct html *);
54 static void print_mdoc_node(MDOC_ARGS);
55 static void print_mdoc_nodelist(MDOC_ARGS);
56 static void synopsis_pre(struct html *, struct roff_node *);
57
58 static void mdoc_root_post(const struct roff_meta *,
59 struct html *);
60 static int mdoc_root_pre(const struct roff_meta *,
61 struct html *);
62
63 static void mdoc__x_post(MDOC_ARGS);
64 static int mdoc__x_pre(MDOC_ARGS);
65 static int mdoc_abort_pre(MDOC_ARGS);
66 static int mdoc_ad_pre(MDOC_ARGS);
67 static int mdoc_an_pre(MDOC_ARGS);
68 static int mdoc_ap_pre(MDOC_ARGS);
69 static int mdoc_ar_pre(MDOC_ARGS);
70 static int mdoc_bd_pre(MDOC_ARGS);
71 static int mdoc_bf_pre(MDOC_ARGS);
72 static void mdoc_bk_post(MDOC_ARGS);
73 static int mdoc_bk_pre(MDOC_ARGS);
74 static int mdoc_bl_pre(MDOC_ARGS);
75 static int mdoc_cd_pre(MDOC_ARGS);
76 static int mdoc_code_pre(MDOC_ARGS);
77 static int mdoc_d1_pre(MDOC_ARGS);
78 static int mdoc_fa_pre(MDOC_ARGS);
79 static int mdoc_fd_pre(MDOC_ARGS);
80 static int mdoc_fl_pre(MDOC_ARGS);
81 static int mdoc_fn_pre(MDOC_ARGS);
82 static int mdoc_ft_pre(MDOC_ARGS);
83 static int mdoc_em_pre(MDOC_ARGS);
84 static void mdoc_eo_post(MDOC_ARGS);
85 static int mdoc_eo_pre(MDOC_ARGS);
86 static int mdoc_ex_pre(MDOC_ARGS);
87 static void mdoc_fo_post(MDOC_ARGS);
88 static int mdoc_fo_pre(MDOC_ARGS);
89 static int mdoc_igndelim_pre(MDOC_ARGS);
90 static int mdoc_in_pre(MDOC_ARGS);
91 static int mdoc_it_pre(MDOC_ARGS);
92 static int mdoc_lb_pre(MDOC_ARGS);
93 static int mdoc_lk_pre(MDOC_ARGS);
94 static int mdoc_mt_pre(MDOC_ARGS);
95 static int mdoc_nd_pre(MDOC_ARGS);
96 static int mdoc_nm_pre(MDOC_ARGS);
97 static int mdoc_no_pre(MDOC_ARGS);
98 static int mdoc_ns_pre(MDOC_ARGS);
99 static int mdoc_pa_pre(MDOC_ARGS);
100 static void mdoc_pf_post(MDOC_ARGS);
101 static int mdoc_pp_pre(MDOC_ARGS);
102 static void mdoc_quote_post(MDOC_ARGS);
103 static int mdoc_quote_pre(MDOC_ARGS);
104 static int mdoc_rs_pre(MDOC_ARGS);
105 static int mdoc_sh_pre(MDOC_ARGS);
106 static int mdoc_skip_pre(MDOC_ARGS);
107 static int mdoc_sm_pre(MDOC_ARGS);
108 static int mdoc_ss_pre(MDOC_ARGS);
109 static int mdoc_st_pre(MDOC_ARGS);
110 static int mdoc_sx_pre(MDOC_ARGS);
111 static int mdoc_sy_pre(MDOC_ARGS);
112 static int mdoc_tg_pre(MDOC_ARGS);
113 static int mdoc_va_pre(MDOC_ARGS);
114 static int mdoc_vt_pre(MDOC_ARGS);
115 static int mdoc_xr_pre(MDOC_ARGS);
116 static int mdoc_xx_pre(MDOC_ARGS);
117
118 static const struct mdoc_html_act mdoc_html_acts[MDOC_MAX - MDOC_Dd] = {
119 {NULL, NULL}, /* Dd */
120 {NULL, NULL}, /* Dt */
121 {NULL, NULL}, /* Os */
122 {mdoc_sh_pre, NULL }, /* Sh */
123 {mdoc_ss_pre, NULL }, /* Ss */
124 {mdoc_pp_pre, NULL}, /* Pp */
125 {mdoc_d1_pre, NULL}, /* D1 */
126 {mdoc_d1_pre, NULL}, /* Dl */
127 {mdoc_bd_pre, NULL}, /* Bd */
128 {NULL, NULL}, /* Ed */
129 {mdoc_bl_pre, NULL}, /* Bl */
130 {NULL, NULL}, /* El */
131 {mdoc_it_pre, NULL}, /* It */
132 {mdoc_ad_pre, NULL}, /* Ad */
133 {mdoc_an_pre, NULL}, /* An */
134 {mdoc_ap_pre, NULL}, /* Ap */
135 {mdoc_ar_pre, NULL}, /* Ar */
136 {mdoc_cd_pre, NULL}, /* Cd */
137 {mdoc_code_pre, NULL}, /* Cm */
138 {mdoc_code_pre, NULL}, /* Dv */
139 {mdoc_code_pre, NULL}, /* Er */
140 {mdoc_code_pre, NULL}, /* Ev */
141 {mdoc_ex_pre, NULL}, /* Ex */
142 {mdoc_fa_pre, NULL}, /* Fa */
143 {mdoc_fd_pre, NULL}, /* Fd */
144 {mdoc_fl_pre, NULL}, /* Fl */
145 {mdoc_fn_pre, NULL}, /* Fn */
146 {mdoc_ft_pre, NULL}, /* Ft */
147 {mdoc_code_pre, NULL}, /* Ic */
148 {mdoc_in_pre, NULL}, /* In */
149 {mdoc_code_pre, NULL}, /* Li */
150 {mdoc_nd_pre, NULL}, /* Nd */
151 {mdoc_nm_pre, NULL}, /* Nm */
152 {mdoc_quote_pre, mdoc_quote_post}, /* Op */
153 {mdoc_abort_pre, NULL}, /* Ot */
154 {mdoc_pa_pre, NULL}, /* Pa */
155 {mdoc_ex_pre, NULL}, /* Rv */
156 {mdoc_st_pre, NULL}, /* St */
157 {mdoc_va_pre, NULL}, /* Va */
158 {mdoc_vt_pre, NULL}, /* Vt */
159 {mdoc_xr_pre, NULL}, /* Xr */
160 {mdoc__x_pre, mdoc__x_post}, /* %A */
161 {mdoc__x_pre, mdoc__x_post}, /* %B */
162 {mdoc__x_pre, mdoc__x_post}, /* %D */
163 {mdoc__x_pre, mdoc__x_post}, /* %I */
164 {mdoc__x_pre, mdoc__x_post}, /* %J */
165 {mdoc__x_pre, mdoc__x_post}, /* %N */
166 {mdoc__x_pre, mdoc__x_post}, /* %O */
167 {mdoc__x_pre, mdoc__x_post}, /* %P */
168 {mdoc__x_pre, mdoc__x_post}, /* %R */
169 {mdoc__x_pre, mdoc__x_post}, /* %T */
170 {mdoc__x_pre, mdoc__x_post}, /* %V */
171 {NULL, NULL}, /* Ac */
172 {mdoc_quote_pre, mdoc_quote_post}, /* Ao */
173 {mdoc_quote_pre, mdoc_quote_post}, /* Aq */
174 {mdoc_xx_pre, NULL}, /* At */
175 {NULL, NULL}, /* Bc */
176 {mdoc_bf_pre, NULL}, /* Bf */
177 {mdoc_quote_pre, mdoc_quote_post}, /* Bo */
178 {mdoc_quote_pre, mdoc_quote_post}, /* Bq */
179 {mdoc_xx_pre, NULL}, /* Bsx */
180 {mdoc_xx_pre, NULL}, /* Bx */
181 {mdoc_skip_pre, NULL}, /* Db */
182 {NULL, NULL}, /* Dc */
183 {mdoc_quote_pre, mdoc_quote_post}, /* Do */
184 {mdoc_quote_pre, mdoc_quote_post}, /* Dq */
185 {NULL, NULL}, /* Ec */ /* FIXME: no space */
186 {NULL, NULL}, /* Ef */
187 {mdoc_em_pre, NULL}, /* Em */
188 {mdoc_eo_pre, mdoc_eo_post}, /* Eo */
189 {mdoc_xx_pre, NULL}, /* Fx */
190 {mdoc_no_pre, NULL}, /* Ms */
191 {mdoc_no_pre, NULL}, /* No */
192 {mdoc_ns_pre, NULL}, /* Ns */
193 {mdoc_xx_pre, NULL}, /* Nx */
194 {mdoc_xx_pre, NULL}, /* Ox */
195 {NULL, NULL}, /* Pc */
196 {mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
197 {mdoc_quote_pre, mdoc_quote_post}, /* Po */
198 {mdoc_quote_pre, mdoc_quote_post}, /* Pq */
199 {NULL, NULL}, /* Qc */
200 {mdoc_quote_pre, mdoc_quote_post}, /* Ql */
201 {mdoc_quote_pre, mdoc_quote_post}, /* Qo */
202 {mdoc_quote_pre, mdoc_quote_post}, /* Qq */
203 {NULL, NULL}, /* Re */
204 {mdoc_rs_pre, NULL}, /* Rs */
205 {NULL, NULL}, /* Sc */
206 {mdoc_quote_pre, mdoc_quote_post}, /* So */
207 {mdoc_quote_pre, mdoc_quote_post}, /* Sq */
208 {mdoc_sm_pre, NULL}, /* Sm */
209 {mdoc_sx_pre, NULL}, /* Sx */
210 {mdoc_sy_pre, NULL}, /* Sy */
211 {NULL, NULL}, /* Tn */
212 {mdoc_xx_pre, NULL}, /* Ux */
213 {NULL, NULL}, /* Xc */
214 {NULL, NULL}, /* Xo */
215 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
216 {NULL, NULL}, /* Fc */
217 {mdoc_quote_pre, mdoc_quote_post}, /* Oo */
218 {NULL, NULL}, /* Oc */
219 {mdoc_bk_pre, mdoc_bk_post}, /* Bk */
220 {NULL, NULL}, /* Ek */
221 {NULL, NULL}, /* Bt */
222 {NULL, NULL}, /* Hf */
223 {mdoc_em_pre, NULL}, /* Fr */
224 {NULL, NULL}, /* Ud */
225 {mdoc_lb_pre, NULL}, /* Lb */
226 {mdoc_abort_pre, NULL}, /* Lp */
227 {mdoc_lk_pre, NULL}, /* Lk */
228 {mdoc_mt_pre, NULL}, /* Mt */
229 {mdoc_quote_pre, mdoc_quote_post}, /* Brq */
230 {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
231 {NULL, NULL}, /* Brc */
232 {mdoc__x_pre, mdoc__x_post}, /* %C */
233 {mdoc_skip_pre, NULL}, /* Es */
234 {mdoc_quote_pre, mdoc_quote_post}, /* En */
235 {mdoc_xx_pre, NULL}, /* Dx */
236 {mdoc__x_pre, mdoc__x_post}, /* %Q */
237 {mdoc__x_pre, mdoc__x_post}, /* %U */
238 {NULL, NULL}, /* Ta */
239 {mdoc_tg_pre, NULL}, /* Tg */
240 };
241
242
243 /*
244 * See the same function in mdoc_term.c for documentation.
245 */
246 static void
247 synopsis_pre(struct html *h, struct roff_node *n)
248 {
249 struct roff_node *np;
250
251 if ((n->flags & NODE_SYNPRETTY) == 0 ||
252 (np = roff_node_prev(n)) == NULL)
253 return;
254
255 if (np->tok == n->tok &&
256 MDOC_Fo != n->tok &&
257 MDOC_Ft != n->tok &&
258 MDOC_Fn != n->tok) {
259 print_otag(h, TAG_BR, "");
260 return;
261 }
262
263 switch (np->tok) {
264 case MDOC_Fd:
265 case MDOC_Fn:
266 case MDOC_Fo:
267 case MDOC_In:
268 case MDOC_Vt:
269 break;
270 case MDOC_Ft:
271 if (n->tok != MDOC_Fn && n->tok != MDOC_Fo)
272 break;
273 /* FALLTHROUGH */
274 default:
275 print_otag(h, TAG_BR, "");
276 return;
277 }
278 html_close_paragraph(h);
279 print_otag(h, TAG_P, "c", "Pp");
280 }
281
282 void
283 html_mdoc(void *arg, const struct roff_meta *mdoc)
284 {
285 struct html *h;
286 struct roff_node *n;
287 struct tag *t;
288
289 h = (struct html *)arg;
290 n = mdoc->first->child;
291
292 if ((h->oflags & HTML_FRAGMENT) == 0) {
293 print_gen_decls(h);
294 print_otag(h, TAG_HTML, "");
295 if (n != NULL && n->type == ROFFT_COMMENT)
296 print_gen_comment(h, n);
297 t = print_otag(h, TAG_HEAD, "");
298 print_mdoc_head(mdoc, h);
299 print_tagq(h, t);
300 print_otag(h, TAG_BODY, "");
301 }
302
303 mdoc_root_pre(mdoc, h);
304 t = print_otag(h, TAG_DIV, "c", "manual-text");
305 print_mdoc_nodelist(mdoc, n, h);
306 print_tagq(h, t);
307 mdoc_root_post(mdoc, h);
308 print_tagq(h, NULL);
309 }
310
311 static void
312 print_mdoc_head(const struct roff_meta *meta, struct html *h)
313 {
314 char *cp;
315
316 print_gen_head(h);
317
318 if (meta->arch != NULL && meta->msec != NULL)
319 mandoc_asprintf(&cp, "%s(%s) (%s)", meta->title,
320 meta->msec, meta->arch);
321 else if (meta->msec != NULL)
322 mandoc_asprintf(&cp, "%s(%s)", meta->title, meta->msec);
323 else if (meta->arch != NULL)
324 mandoc_asprintf(&cp, "%s (%s)", meta->title, meta->arch);
325 else
326 cp = mandoc_strdup(meta->title);
327
328 print_otag(h, TAG_TITLE, "");
329 print_text(h, cp);
330 free(cp);
331 }
332
333 static void
334 print_mdoc_nodelist(MDOC_ARGS)
335 {
336
337 while (n != NULL) {
338 print_mdoc_node(meta, n, h);
339 n = n->next;
340 }
341 }
342
343 static void
344 print_mdoc_node(MDOC_ARGS)
345 {
346 struct tag *t;
347 int child;
348
349 if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
350 return;
351
352 if (n->flags & NODE_NOFILL) {
353 html_fillmode(h, ROFF_nf);
354 if (n->flags & NODE_LINE)
355 print_endline(h);
356 } else
357 html_fillmode(h, ROFF_fi);
358
359 child = 1;
360 n->flags &= ~NODE_ENDED;
361 switch (n->type) {
362 case ROFFT_TEXT:
363 if (n->flags & NODE_LINE) {
364 switch (*n->string) {
365 case '\0':
366 h->col = 1;
367 print_endline(h);
368 return;
369 case ' ':
370 if ((h->flags & HTML_NONEWLINE) == 0 &&
371 (n->flags & NODE_NOFILL) == 0)
372 print_otag(h, TAG_BR, "");
373 break;
374 default:
375 break;
376 }
377 }
378 t = h->tag;
379 t->refcnt++;
380 if (n->flags & NODE_DELIMC)
381 h->flags |= HTML_NOSPACE;
382 if (n->flags & NODE_HREF)
383 print_tagged_text(h, n->string, n);
384 else
385 print_text(h, n->string);
386 if (n->flags & NODE_DELIMO)
387 h->flags |= HTML_NOSPACE;
388 break;
389 case ROFFT_EQN:
390 t = h->tag;
391 t->refcnt++;
392 print_eqn(h, n->eqn);
393 break;
394 case ROFFT_TBL:
395 /*
396 * This will take care of initialising all of the table
397 * state data for the first table, then tearing it down
398 * for the last one.
399 */
400 print_tbl(h, n->span);
401 return;
402 default:
403 /*
404 * Close out the current table, if it's open, and unset
405 * the "meta" table state. This will be reopened on the
406 * next table element.
407 */
408 if (h->tblt != NULL)
409 print_tblclose(h);
410 assert(h->tblt == NULL);
411 t = h->tag;
412 t->refcnt++;
413 if (n->tok < ROFF_MAX) {
414 roff_html_pre(h, n);
415 t->refcnt--;
416 print_stagq(h, t);
417 return;
418 }
419 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
420 if (mdoc_html_acts[n->tok - MDOC_Dd].pre != NULL &&
421 (n->end == ENDBODY_NOT || n->child != NULL))
422 child = (*mdoc_html_acts[n->tok - MDOC_Dd].pre)(meta,
423 n, h);
424 break;
425 }
426
427 if (h->flags & HTML_KEEP && n->flags & NODE_LINE) {
428 h->flags &= ~HTML_KEEP;
429 h->flags |= HTML_PREKEEP;
430 }
431
432 if (child && n->child != NULL)
433 print_mdoc_nodelist(meta, n->child, h);
434
435 t->refcnt--;
436 print_stagq(h, t);
437
438 switch (n->type) {
439 case ROFFT_TEXT:
440 case ROFFT_EQN:
441 break;
442 default:
443 if (mdoc_html_acts[n->tok - MDOC_Dd].post == NULL ||
444 n->flags & NODE_ENDED)
445 break;
446 (*mdoc_html_acts[n->tok - MDOC_Dd].post)(meta, n, h);
447 if (n->end != ENDBODY_NOT)
448 n->body->flags |= NODE_ENDED;
449 break;
450 }
451 }
452
453 static void
454 mdoc_root_post(const struct roff_meta *meta, struct html *h)
455 {
456 struct tag *t, *tt;
457
458 t = print_otag(h, TAG_TABLE, "c", "foot");
459 tt = print_otag(h, TAG_TR, "");
460
461 print_otag(h, TAG_TD, "c", "foot-date");
462 print_text(h, meta->date);
463 print_stagq(h, tt);
464
465 print_otag(h, TAG_TD, "c", "foot-os");
466 print_text(h, meta->os);
467 print_tagq(h, t);
468 }
469
470 static int
471 mdoc_root_pre(const struct roff_meta *meta, struct html *h)
472 {
473 struct tag *t, *tt;
474 char *volume, *title;
475
476 if (NULL == meta->arch)
477 volume = mandoc_strdup(meta->vol);
478 else
479 mandoc_asprintf(&volume, "%s (%s)",
480 meta->vol, meta->arch);
481
482 if (NULL == meta->msec)
483 title = mandoc_strdup(meta->title);
484 else
485 mandoc_asprintf(&title, "%s(%s)",
486 meta->title, meta->msec);
487
488 t = print_otag(h, TAG_TABLE, "c", "head");
489 tt = print_otag(h, TAG_TR, "");
490
491 print_otag(h, TAG_TD, "c", "head-ltitle");
492 print_text(h, title);
493 print_stagq(h, tt);
494
495 print_otag(h, TAG_TD, "c", "head-vol");
496 print_text(h, volume);
497 print_stagq(h, tt);
498
499 print_otag(h, TAG_TD, "c", "head-rtitle");
500 print_text(h, title);
501 print_tagq(h, t);
502
503 free(title);
504 free(volume);
505 return 1;
506 }
507
508 static int
509 mdoc_code_pre(MDOC_ARGS)
510 {
511 print_otag_id(h, TAG_CODE, roff_name[n->tok], n);
512 return 1;
513 }
514
515 static int
516 mdoc_sh_pre(MDOC_ARGS)
517 {
518 struct roff_node *sn, *subn;
519 struct tag *t, *tsec, *tsub;
520 char *id;
521 int sc;
522
523 switch (n->type) {
524 case ROFFT_BLOCK:
525 html_close_paragraph(h);
526 if ((h->oflags & HTML_TOC) == 0 ||
527 h->flags & HTML_TOCDONE ||
528 n->sec <= SEC_SYNOPSIS) {
529 print_otag(h, TAG_SECTION, "c", "Sh");
530 break;
531 }
532 h->flags |= HTML_TOCDONE;
533 sc = 0;
534 for (sn = n->next; sn != NULL; sn = sn->next)
535 if (sn->sec == SEC_CUSTOM)
536 if (++sc == 2)
537 break;
538 if (sc < 2)
539 break;
540 t = print_otag(h, TAG_H1, "c", "Sh");
541 print_text(h, "TABLE OF CONTENTS");
542 print_tagq(h, t);
543 t = print_otag(h, TAG_UL, "c", "Bl-compact");
544 for (sn = n; sn != NULL; sn = sn->next) {
545 tsec = print_otag(h, TAG_LI, "");
546 id = html_make_id(sn->head, 0);
547 tsub = print_otag(h, TAG_A, "hR", id);
548 free(id);
549 print_mdoc_nodelist(meta, sn->head->child, h);
550 print_tagq(h, tsub);
551 tsub = NULL;
552 for (subn = sn->body->child; subn != NULL;
553 subn = subn->next) {
554 if (subn->tok != MDOC_Ss)
555 continue;
556 id = html_make_id(subn->head, 0);
557 if (id == NULL)
558 continue;
559 if (tsub == NULL)
560 print_otag(h, TAG_UL,
561 "c", "Bl-compact");
562 tsub = print_otag(h, TAG_LI, "");
563 print_otag(h, TAG_A, "hR", id);
564 free(id);
565 print_mdoc_nodelist(meta,
566 subn->head->child, h);
567 print_tagq(h, tsub);
568 }
569 print_tagq(h, tsec);
570 }
571 print_tagq(h, t);
572 print_otag(h, TAG_SECTION, "c", "Sh");
573 break;
574 case ROFFT_HEAD:
575 print_otag_id(h, TAG_H1, "Sh", n);
576 break;
577 case ROFFT_BODY:
578 if (n->sec == SEC_AUTHORS)
579 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
580 break;
581 default:
582 break;
583 }
584 return 1;
585 }
586
587 static int
588 mdoc_ss_pre(MDOC_ARGS)
589 {
590 switch (n->type) {
591 case ROFFT_BLOCK:
592 html_close_paragraph(h);
593 print_otag(h, TAG_SECTION, "c", "Ss");
594 break;
595 case ROFFT_HEAD:
596 print_otag_id(h, TAG_H2, "Ss", n);
597 break;
598 case ROFFT_BODY:
599 break;
600 default:
601 abort();
602 }
603 return 1;
604 }
605
606 static int
607 mdoc_fl_pre(MDOC_ARGS)
608 {
609 struct roff_node *nn;
610
611 print_otag_id(h, TAG_CODE, "Fl", n);
612 print_text(h, "\\-");
613 if (n->child != NULL ||
614 ((nn = roff_node_next(n)) != NULL &&
615 nn->type != ROFFT_TEXT &&
616 (nn->flags & NODE_LINE) == 0))
617 h->flags |= HTML_NOSPACE;
618
619 return 1;
620 }
621
622 static int
623 mdoc_nd_pre(MDOC_ARGS)
624 {
625 switch (n->type) {
626 case ROFFT_BLOCK:
627 return 1;
628 case ROFFT_HEAD:
629 return 0;
630 case ROFFT_BODY:
631 break;
632 default:
633 abort();
634 }
635 print_text(h, "\\(em");
636 print_otag(h, TAG_SPAN, "c", "Nd");
637 return 1;
638 }
639
640 static int
641 mdoc_nm_pre(MDOC_ARGS)
642 {
643 switch (n->type) {
644 case ROFFT_BLOCK:
645 break;
646 case ROFFT_HEAD:
647 print_otag(h, TAG_TD, "");
648 /* FALLTHROUGH */
649 case ROFFT_ELEM:
650 print_otag(h, TAG_CODE, "c", "Nm");
651 return 1;
652 case ROFFT_BODY:
653 print_otag(h, TAG_TD, "");
654 return 1;
655 default:
656 abort();
657 }
658 html_close_paragraph(h);
659 synopsis_pre(h, n);
660 print_otag(h, TAG_TABLE, "c", "Nm");
661 print_otag(h, TAG_TR, "");
662 return 1;
663 }
664
665 static int
666 mdoc_xr_pre(MDOC_ARGS)
667 {
668 if (NULL == n->child)
669 return 0;
670
671 if (h->base_man1)
672 print_otag(h, TAG_A, "chM", "Xr",
673 n->child->string, n->child->next == NULL ?
674 NULL : n->child->next->string);
675 else
676 print_otag(h, TAG_A, "c", "Xr");
677
678 n = n->child;
679 print_text(h, n->string);
680
681 if (NULL == (n = n->next))
682 return 0;
683
684 h->flags |= HTML_NOSPACE;
685 print_text(h, "(");
686 h->flags |= HTML_NOSPACE;
687 print_text(h, n->string);
688 h->flags |= HTML_NOSPACE;
689 print_text(h, ")");
690 return 0;
691 }
692
693 static int
694 mdoc_tg_pre(MDOC_ARGS)
695 {
696 char *id;
697
698 if ((id = html_make_id(n, 1)) != NULL) {
699 print_tagq(h, print_otag(h, TAG_MARK, "i", id));
700 free(id);
701 }
702 return 0;
703 }
704
705 static int
706 mdoc_ns_pre(MDOC_ARGS)
707 {
708
709 if ( ! (NODE_LINE & n->flags))
710 h->flags |= HTML_NOSPACE;
711 return 1;
712 }
713
714 static int
715 mdoc_ar_pre(MDOC_ARGS)
716 {
717 print_otag(h, TAG_VAR, "c", "Ar");
718 return 1;
719 }
720
721 static int
722 mdoc_xx_pre(MDOC_ARGS)
723 {
724 print_otag(h, TAG_SPAN, "c", "Ux");
725 return 1;
726 }
727
728 static int
729 mdoc_it_pre(MDOC_ARGS)
730 {
731 const struct roff_node *bl;
732 enum mdoc_list type;
733
734 bl = n->parent;
735 while (bl->tok != MDOC_Bl)
736 bl = bl->parent;
737 type = bl->norm->Bl.type;
738
739 switch (type) {
740 case LIST_bullet:
741 case LIST_dash:
742 case LIST_hyphen:
743 case LIST_item:
744 case LIST_enum:
745 switch (n->type) {
746 case ROFFT_HEAD:
747 return 0;
748 case ROFFT_BODY:
749 print_otag_id(h, TAG_LI, NULL, n);
750 break;
751 default:
752 break;
753 }
754 break;
755 case LIST_diag:
756 case LIST_hang:
757 case LIST_inset:
758 case LIST_ohang:
759 switch (n->type) {
760 case ROFFT_HEAD:
761 print_otag_id(h, TAG_DT, NULL, n);
762 break;
763 case ROFFT_BODY:
764 print_otag(h, TAG_DD, "");
765 break;
766 default:
767 break;
768 }
769 break;
770 case LIST_tag:
771 switch (n->type) {
772 case ROFFT_HEAD:
773 print_otag_id(h, TAG_DT, NULL, n);
774 break;
775 case ROFFT_BODY:
776 if (n->child == NULL) {
777 print_otag(h, TAG_DD, "s", "width", "auto");
778 print_text(h, "\\ ");
779 } else
780 print_otag(h, TAG_DD, "");
781 break;
782 default:
783 break;
784 }
785 break;
786 case LIST_column:
787 switch (n->type) {
788 case ROFFT_HEAD:
789 break;
790 case ROFFT_BODY:
791 print_otag(h, TAG_TD, "");
792 break;
793 default:
794 print_otag_id(h, TAG_TR, NULL, n);
795 }
796 default:
797 break;
798 }
799
800 return 1;
801 }
802
803 static int
804 mdoc_bl_pre(MDOC_ARGS)
805 {
806 char cattr[32];
807 struct mdoc_bl *bl;
808 enum htmltag elemtype;
809
810 switch (n->type) {
811 case ROFFT_BLOCK:
812 html_close_paragraph(h);
813 break;
814 case ROFFT_HEAD:
815 return 0;
816 case ROFFT_BODY:
817 return 1;
818 default:
819 abort();
820 }
821
822 bl = &n->norm->Bl;
823 switch (bl->type) {
824 case LIST_bullet:
825 elemtype = TAG_UL;
826 (void)strlcpy(cattr, "Bl-bullet", sizeof(cattr));
827 break;
828 case LIST_dash:
829 case LIST_hyphen:
830 elemtype = TAG_UL;
831 (void)strlcpy(cattr, "Bl-dash", sizeof(cattr));
832 break;
833 case LIST_item:
834 elemtype = TAG_UL;
835 (void)strlcpy(cattr, "Bl-item", sizeof(cattr));
836 break;
837 case LIST_enum:
838 elemtype = TAG_OL;
839 (void)strlcpy(cattr, "Bl-enum", sizeof(cattr));
840 break;
841 case LIST_diag:
842 elemtype = TAG_DL;
843 (void)strlcpy(cattr, "Bl-diag", sizeof(cattr));
844 break;
845 case LIST_hang:
846 elemtype = TAG_DL;
847 (void)strlcpy(cattr, "Bl-hang", sizeof(cattr));
848 break;
849 case LIST_inset:
850 elemtype = TAG_DL;
851 (void)strlcpy(cattr, "Bl-inset", sizeof(cattr));
852 break;
853 case LIST_ohang:
854 elemtype = TAG_DL;
855 (void)strlcpy(cattr, "Bl-ohang", sizeof(cattr));
856 break;
857 case LIST_tag:
858 if (bl->offs)
859 print_otag(h, TAG_DIV, "c", "Bd-indent");
860 print_otag_id(h, TAG_DL,
861 bl->comp ? "Bl-tag Bl-compact" : "Bl-tag", n->body);
862 return 1;
863 case LIST_column:
864 elemtype = TAG_TABLE;
865 (void)strlcpy(cattr, "Bl-column", sizeof(cattr));
866 break;
867 default:
868 abort();
869 }
870 if (bl->offs != NULL)
871 (void)strlcat(cattr, " Bd-indent", sizeof(cattr));
872 if (bl->comp)
873 (void)strlcat(cattr, " Bl-compact", sizeof(cattr));
874 print_otag_id(h, elemtype, cattr, n->body);
875 return 1;
876 }
877
878 static int
879 mdoc_ex_pre(MDOC_ARGS)
880 {
881 if (roff_node_prev(n) != NULL)
882 print_otag(h, TAG_BR, "");
883 return 1;
884 }
885
886 static int
887 mdoc_st_pre(MDOC_ARGS)
888 {
889 print_otag(h, TAG_SPAN, "c", "St");
890 return 1;
891 }
892
893 static int
894 mdoc_em_pre(MDOC_ARGS)
895 {
896 print_otag_id(h, TAG_I, "Em", n);
897 return 1;
898 }
899
900 static int
901 mdoc_d1_pre(MDOC_ARGS)
902 {
903 switch (n->type) {
904 case ROFFT_BLOCK:
905 html_close_paragraph(h);
906 return 1;
907 case ROFFT_HEAD:
908 return 0;
909 case ROFFT_BODY:
910 break;
911 default:
912 abort();
913 }
914 print_otag_id(h, TAG_DIV, "Bd Bd-indent", n);
915 if (n->tok == MDOC_Dl)
916 print_otag(h, TAG_CODE, "c", "Li");
917 return 1;
918 }
919
920 static int
921 mdoc_sx_pre(MDOC_ARGS)
922 {
923 char *id;
924
925 id = html_make_id(n, 0);
926 print_otag(h, TAG_A, "chR", "Sx", id);
927 free(id);
928 return 1;
929 }
930
931 static int
932 mdoc_bd_pre(MDOC_ARGS)
933 {
934 char buf[16];
935 struct roff_node *nn;
936 int comp;
937
938 switch (n->type) {
939 case ROFFT_BLOCK:
940 html_close_paragraph(h);
941 return 1;
942 case ROFFT_HEAD:
943 return 0;
944 case ROFFT_BODY:
945 break;
946 default:
947 abort();
948 }
949
950 /* Handle preceding whitespace. */
951
952 comp = n->norm->Bd.comp;
953 for (nn = n; nn != NULL && comp == 0; nn = nn->parent) {
954 if (nn->type != ROFFT_BLOCK)
955 continue;
956 if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
957 comp = 1;
958 if (roff_node_prev(nn) != NULL)
959 break;
960 }
961 (void)strlcpy(buf, "Bd", sizeof(buf));
962 if (comp == 0)
963 (void)strlcat(buf, " Pp", sizeof(buf));
964
965 /* Handle the -offset argument. */
966
967 if (n->norm->Bd.offs != NULL &&
968 strcmp(n->norm->Bd.offs, "left") != 0)
969 (void)strlcat(buf, " Bd-indent", sizeof(buf));
970
971 print_otag_id(h, TAG_DIV, buf, n);
972 return 1;
973 }
974
975 static int
976 mdoc_pa_pre(MDOC_ARGS)
977 {
978 print_otag(h, TAG_SPAN, "c", "Pa");
979 return 1;
980 }
981
982 static int
983 mdoc_ad_pre(MDOC_ARGS)
984 {
985 print_otag(h, TAG_SPAN, "c", "Ad");
986 return 1;
987 }
988
989 static int
990 mdoc_an_pre(MDOC_ARGS)
991 {
992 if (n->norm->An.auth == AUTH_split) {
993 h->flags &= ~HTML_NOSPLIT;
994 h->flags |= HTML_SPLIT;
995 return 0;
996 }
997 if (n->norm->An.auth == AUTH_nosplit) {
998 h->flags &= ~HTML_SPLIT;
999 h->flags |= HTML_NOSPLIT;
1000 return 0;
1001 }
1002
1003 if (h->flags & HTML_SPLIT)
1004 print_otag(h, TAG_BR, "");
1005
1006 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1007 h->flags |= HTML_SPLIT;
1008
1009 print_otag(h, TAG_SPAN, "c", "An");
1010 return 1;
1011 }
1012
1013 static int
1014 mdoc_cd_pre(MDOC_ARGS)
1015 {
1016 synopsis_pre(h, n);
1017 print_otag(h, TAG_CODE, "c", "Cd");
1018 return 1;
1019 }
1020
1021 static int
1022 mdoc_fa_pre(MDOC_ARGS)
1023 {
1024 const struct roff_node *nn;
1025 struct tag *t;
1026
1027 if (n->parent->tok != MDOC_Fo) {
1028 print_otag(h, TAG_VAR, "c", "Fa");
1029 return 1;
1030 }
1031 for (nn = n->child; nn != NULL; nn = nn->next) {
1032 t = print_otag(h, TAG_VAR, "c", "Fa");
1033 print_text(h, nn->string);
1034 print_tagq(h, t);
1035 if (nn->next != NULL) {
1036 h->flags |= HTML_NOSPACE;
1037 print_text(h, ",");
1038 }
1039 }
1040 if (n->child != NULL &&
1041 (nn = roff_node_next(n)) != NULL &&
1042 nn->tok == MDOC_Fa) {
1043 h->flags |= HTML_NOSPACE;
1044 print_text(h, ",");
1045 }
1046 return 0;
1047 }
1048
1049 static int
1050 mdoc_fd_pre(MDOC_ARGS)
1051 {
1052 struct tag *t;
1053 char *buf, *cp;
1054
1055 synopsis_pre(h, n);
1056
1057 if (NULL == (n = n->child))
1058 return 0;
1059
1060 assert(n->type == ROFFT_TEXT);
1061
1062 if (strcmp(n->string, "#include")) {
1063 print_otag(h, TAG_CODE, "c", "Fd");
1064 return 1;
1065 }
1066
1067 print_otag(h, TAG_CODE, "c", "In");
1068 print_text(h, n->string);
1069
1070 if (NULL != (n = n->next)) {
1071 assert(n->type == ROFFT_TEXT);
1072
1073 if (h->base_includes) {
1074 cp = n->string;
1075 if (*cp == '<' || *cp == '"')
1076 cp++;
1077 buf = mandoc_strdup(cp);
1078 cp = strchr(buf, '\0') - 1;
1079 if (cp >= buf && (*cp == '>' || *cp == '"'))
1080 *cp = '\0';
1081 t = print_otag(h, TAG_A, "chI", "In", buf);
1082 free(buf);
1083 } else
1084 t = print_otag(h, TAG_A, "c", "In");
1085
1086 print_text(h, n->string);
1087 print_tagq(h, t);
1088
1089 n = n->next;
1090 }
1091
1092 for ( ; n; n = n->next) {
1093 assert(n->type == ROFFT_TEXT);
1094 print_text(h, n->string);
1095 }
1096
1097 return 0;
1098 }
1099
1100 static int
1101 mdoc_vt_pre(MDOC_ARGS)
1102 {
1103 if (n->type == ROFFT_BLOCK) {
1104 synopsis_pre(h, n);
1105 return 1;
1106 } else if (n->type == ROFFT_ELEM) {
1107 synopsis_pre(h, n);
1108 } else if (n->type == ROFFT_HEAD)
1109 return 0;
1110
1111 print_otag(h, TAG_VAR, "c", "Vt");
1112 return 1;
1113 }
1114
1115 static int
1116 mdoc_ft_pre(MDOC_ARGS)
1117 {
1118 synopsis_pre(h, n);
1119 print_otag(h, TAG_VAR, "c", "Ft");
1120 return 1;
1121 }
1122
1123 static int
1124 mdoc_fn_pre(MDOC_ARGS)
1125 {
1126 struct tag *t;
1127 char nbuf[BUFSIZ];
1128 const char *sp, *ep;
1129 int sz, pretty;
1130
1131 pretty = NODE_SYNPRETTY & n->flags;
1132 synopsis_pre(h, n);
1133
1134 /* Split apart into type and name. */
1135 assert(n->child->string);
1136 sp = n->child->string;
1137
1138 ep = strchr(sp, ' ');
1139 if (NULL != ep) {
1140 t = print_otag(h, TAG_VAR, "c", "Ft");
1141
1142 while (ep) {
1143 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1144 (void)memcpy(nbuf, sp, (size_t)sz);
1145 nbuf[sz] = '\0';
1146 print_text(h, nbuf);
1147 sp = ++ep;
1148 ep = strchr(sp, ' ');
1149 }
1150 print_tagq(h, t);
1151 }
1152
1153 t = print_otag_id(h, TAG_CODE, "Fn", n);
1154
1155 if (sp)
1156 print_text(h, sp);
1157
1158 print_tagq(h, t);
1159
1160 h->flags |= HTML_NOSPACE;
1161 print_text(h, "(");
1162 h->flags |= HTML_NOSPACE;
1163
1164 for (n = n->child->next; n; n = n->next) {
1165 if (NODE_SYNPRETTY & n->flags)
1166 t = print_otag(h, TAG_VAR, "cs", "Fa",
1167 "white-space", "nowrap");
1168 else
1169 t = print_otag(h, TAG_VAR, "c", "Fa");
1170 print_text(h, n->string);
1171 print_tagq(h, t);
1172 if (n->next) {
1173 h->flags |= HTML_NOSPACE;
1174 print_text(h, ",");
1175 }
1176 }
1177
1178 h->flags |= HTML_NOSPACE;
1179 print_text(h, ")");
1180
1181 if (pretty) {
1182 h->flags |= HTML_NOSPACE;
1183 print_text(h, ";");
1184 }
1185
1186 return 0;
1187 }
1188
1189 static int
1190 mdoc_sm_pre(MDOC_ARGS)
1191 {
1192
1193 if (NULL == n->child)
1194 h->flags ^= HTML_NONOSPACE;
1195 else if (0 == strcmp("on", n->child->string))
1196 h->flags &= ~HTML_NONOSPACE;
1197 else
1198 h->flags |= HTML_NONOSPACE;
1199
1200 if ( ! (HTML_NONOSPACE & h->flags))
1201 h->flags &= ~HTML_NOSPACE;
1202
1203 return 0;
1204 }
1205
1206 static int
1207 mdoc_skip_pre(MDOC_ARGS)
1208 {
1209
1210 return 0;
1211 }
1212
1213 static int
1214 mdoc_pp_pre(MDOC_ARGS)
1215 {
1216 char *id;
1217
1218 if (n->flags & NODE_NOFILL) {
1219 print_endline(h);
1220 if (n->flags & NODE_ID)
1221 mdoc_tg_pre(meta, n, h);
1222 else {
1223 h->col = 1;
1224 print_endline(h);
1225 }
1226 } else {
1227 html_close_paragraph(h);
1228 id = n->flags & NODE_ID ? html_make_id(n, 1) : NULL;
1229 print_otag(h, TAG_P, "ci", "Pp", id);
1230 free(id);
1231 }
1232 return 0;
1233 }
1234
1235 static int
1236 mdoc_lk_pre(MDOC_ARGS)
1237 {
1238 const struct roff_node *link, *descr, *punct;
1239 struct tag *t;
1240
1241 if ((link = n->child) == NULL)
1242 return 0;
1243
1244 /* Find beginning of trailing punctuation. */
1245 punct = n->last;
1246 while (punct != link && punct->flags & NODE_DELIMC)
1247 punct = punct->prev;
1248 punct = punct->next;
1249
1250 /* Link target and link text. */
1251 descr = link->next;
1252 if (descr == punct)
1253 descr = link; /* no text */
1254 t = print_otag(h, TAG_A, "ch", "Lk", link->string);
1255 do {
1256 if (descr->flags & (NODE_DELIMC | NODE_DELIMO))
1257 h->flags |= HTML_NOSPACE;
1258 print_text(h, descr->string);
1259 descr = descr->next;
1260 } while (descr != punct);
1261 print_tagq(h, t);
1262
1263 /* Trailing punctuation. */
1264 while (punct != NULL) {
1265 h->flags |= HTML_NOSPACE;
1266 print_text(h, punct->string);
1267 punct = punct->next;
1268 }
1269 return 0;
1270 }
1271
1272 static int
1273 mdoc_mt_pre(MDOC_ARGS)
1274 {
1275 struct tag *t;
1276 char *cp;
1277
1278 for (n = n->child; n; n = n->next) {
1279 assert(n->type == ROFFT_TEXT);
1280 mandoc_asprintf(&cp, "mailto:%s", n->string);
1281 t = print_otag(h, TAG_A, "ch", "Mt", cp);
1282 print_text(h, n->string);
1283 print_tagq(h, t);
1284 free(cp);
1285 }
1286 return 0;
1287 }
1288
1289 static int
1290 mdoc_fo_pre(MDOC_ARGS)
1291 {
1292 struct tag *t;
1293
1294 switch (n->type) {
1295 case ROFFT_BLOCK:
1296 synopsis_pre(h, n);
1297 return 1;
1298 case ROFFT_HEAD:
1299 if (n->child != NULL) {
1300 t = print_otag_id(h, TAG_CODE, "Fn", n);
1301 print_text(h, n->child->string);
1302 print_tagq(h, t);
1303 }
1304 return 0;
1305 case ROFFT_BODY:
1306 h->flags |= HTML_NOSPACE;
1307 print_text(h, "(");
1308 h->flags |= HTML_NOSPACE;
1309 return 1;
1310 default:
1311 abort();
1312 }
1313 }
1314
1315 static void
1316 mdoc_fo_post(MDOC_ARGS)
1317 {
1318 if (n->type != ROFFT_BODY)
1319 return;
1320 h->flags |= HTML_NOSPACE;
1321 print_text(h, ")");
1322 h->flags |= HTML_NOSPACE;
1323 print_text(h, ";");
1324 }
1325
1326 static int
1327 mdoc_in_pre(MDOC_ARGS)
1328 {
1329 struct tag *t;
1330
1331 synopsis_pre(h, n);
1332 print_otag(h, TAG_CODE, "c", "In");
1333
1334 /*
1335 * The first argument of the `In' gets special treatment as
1336 * being a linked value. Subsequent values are printed
1337 * afterward. groff does similarly. This also handles the case
1338 * of no children.
1339 */
1340
1341 if (NODE_SYNPRETTY & n->flags && NODE_LINE & n->flags)
1342 print_text(h, "#include");
1343
1344 print_text(h, "<");
1345 h->flags |= HTML_NOSPACE;
1346
1347 if (NULL != (n = n->child)) {
1348 assert(n->type == ROFFT_TEXT);
1349
1350 if (h->base_includes)
1351 t = print_otag(h, TAG_A, "chI", "In", n->string);
1352 else
1353 t = print_otag(h, TAG_A, "c", "In");
1354 print_text(h, n->string);
1355 print_tagq(h, t);
1356
1357 n = n->next;
1358 }
1359
1360 h->flags |= HTML_NOSPACE;
1361 print_text(h, ">");
1362
1363 for ( ; n; n = n->next) {
1364 assert(n->type == ROFFT_TEXT);
1365 print_text(h, n->string);
1366 }
1367 return 0;
1368 }
1369
1370 static int
1371 mdoc_va_pre(MDOC_ARGS)
1372 {
1373 print_otag(h, TAG_VAR, "c", "Va");
1374 return 1;
1375 }
1376
1377 static int
1378 mdoc_ap_pre(MDOC_ARGS)
1379 {
1380 h->flags |= HTML_NOSPACE;
1381 print_text(h, "\\(aq");
1382 h->flags |= HTML_NOSPACE;
1383 return 1;
1384 }
1385
1386 static int
1387 mdoc_bf_pre(MDOC_ARGS)
1388 {
1389 const char *cattr;
1390
1391 switch (n->type) {
1392 case ROFFT_BLOCK:
1393 html_close_paragraph(h);
1394 return 1;
1395 case ROFFT_HEAD:
1396 return 0;
1397 case ROFFT_BODY:
1398 break;
1399 default:
1400 abort();
1401 }
1402
1403 if (FONT_Em == n->norm->Bf.font)
1404 cattr = "Bf Em";
1405 else if (FONT_Sy == n->norm->Bf.font)
1406 cattr = "Bf Sy";
1407 else if (FONT_Li == n->norm->Bf.font)
1408 cattr = "Bf Li";
1409 else
1410 cattr = "Bf No";
1411
1412 /* Cannot use TAG_SPAN because it may contain blocks. */
1413 print_otag(h, TAG_DIV, "c", cattr);
1414 return 1;
1415 }
1416
1417 static int
1418 mdoc_igndelim_pre(MDOC_ARGS)
1419 {
1420 h->flags |= HTML_IGNDELIM;
1421 return 1;
1422 }
1423
1424 static void
1425 mdoc_pf_post(MDOC_ARGS)
1426 {
1427 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1428 h->flags |= HTML_NOSPACE;
1429 }
1430
1431 static int
1432 mdoc_rs_pre(MDOC_ARGS)
1433 {
1434 switch (n->type) {
1435 case ROFFT_BLOCK:
1436 if (n->sec == SEC_SEE_ALSO)
1437 html_close_paragraph(h);
1438 break;
1439 case ROFFT_HEAD:
1440 return 0;
1441 case ROFFT_BODY:
1442 if (n->sec == SEC_SEE_ALSO)
1443 print_otag(h, TAG_P, "c", "Pp");
1444 print_otag(h, TAG_CITE, "c", "Rs");
1445 break;
1446 default:
1447 abort();
1448 }
1449 return 1;
1450 }
1451
1452 static int
1453 mdoc_no_pre(MDOC_ARGS)
1454 {
1455 print_otag_id(h, TAG_SPAN, roff_name[n->tok], n);
1456 return 1;
1457 }
1458
1459 static int
1460 mdoc_sy_pre(MDOC_ARGS)
1461 {
1462 print_otag_id(h, TAG_B, "Sy", n);
1463 return 1;
1464 }
1465
1466 static int
1467 mdoc_lb_pre(MDOC_ARGS)
1468 {
1469 if (n->sec == SEC_LIBRARY &&
1470 n->flags & NODE_LINE &&
1471 roff_node_prev(n) != NULL)
1472 print_otag(h, TAG_BR, "");
1473
1474 print_otag(h, TAG_SPAN, "c", "Lb");
1475 return 1;
1476 }
1477
1478 static int
1479 mdoc__x_pre(MDOC_ARGS)
1480 {
1481 struct roff_node *nn;
1482 const char *cattr;
1483 enum htmltag t;
1484
1485 t = TAG_SPAN;
1486
1487 switch (n->tok) {
1488 case MDOC__A:
1489 cattr = "RsA";
1490 if ((nn = roff_node_prev(n)) != NULL && nn->tok == MDOC__A &&
1491 ((nn = roff_node_next(n)) == NULL || nn->tok != MDOC__A))
1492 print_text(h, "and");
1493 break;
1494 case MDOC__B:
1495 t = TAG_I;
1496 cattr = "RsB";
1497 break;
1498 case MDOC__C:
1499 cattr = "RsC";
1500 break;
1501 case MDOC__D:
1502 cattr = "RsD";
1503 break;
1504 case MDOC__I:
1505 t = TAG_I;
1506 cattr = "RsI";
1507 break;
1508 case MDOC__J:
1509 t = TAG_I;
1510 cattr = "RsJ";
1511 break;
1512 case MDOC__N:
1513 cattr = "RsN";
1514 break;
1515 case MDOC__O:
1516 cattr = "RsO";
1517 break;
1518 case MDOC__P:
1519 cattr = "RsP";
1520 break;
1521 case MDOC__Q:
1522 cattr = "RsQ";
1523 break;
1524 case MDOC__R:
1525 cattr = "RsR";
1526 break;
1527 case MDOC__T:
1528 cattr = "RsT";
1529 break;
1530 case MDOC__U:
1531 print_otag(h, TAG_A, "ch", "RsU", n->child->string);
1532 return 1;
1533 case MDOC__V:
1534 cattr = "RsV";
1535 break;
1536 default:
1537 abort();
1538 }
1539
1540 print_otag(h, t, "c", cattr);
1541 return 1;
1542 }
1543
1544 static void
1545 mdoc__x_post(MDOC_ARGS)
1546 {
1547 struct roff_node *nn;
1548
1549 if (n->tok == MDOC__A &&
1550 (nn = roff_node_next(n)) != NULL && nn->tok == MDOC__A &&
1551 ((nn = roff_node_next(nn)) == NULL || nn->tok != MDOC__A) &&
1552 ((nn = roff_node_prev(n)) == NULL || nn->tok != MDOC__A))
1553 return;
1554
1555 /* TODO: %U */
1556
1557 if (n->parent == NULL || n->parent->tok != MDOC_Rs)
1558 return;
1559
1560 h->flags |= HTML_NOSPACE;
1561 print_text(h, roff_node_next(n) ? "," : ".");
1562 }
1563
1564 static int
1565 mdoc_bk_pre(MDOC_ARGS)
1566 {
1567
1568 switch (n->type) {
1569 case ROFFT_BLOCK:
1570 break;
1571 case ROFFT_HEAD:
1572 return 0;
1573 case ROFFT_BODY:
1574 if (n->parent->args != NULL || n->prev->child == NULL)
1575 h->flags |= HTML_PREKEEP;
1576 break;
1577 default:
1578 abort();
1579 }
1580
1581 return 1;
1582 }
1583
1584 static void
1585 mdoc_bk_post(MDOC_ARGS)
1586 {
1587
1588 if (n->type == ROFFT_BODY)
1589 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
1590 }
1591
1592 static int
1593 mdoc_quote_pre(MDOC_ARGS)
1594 {
1595 if (n->type != ROFFT_BODY)
1596 return 1;
1597
1598 switch (n->tok) {
1599 case MDOC_Ao:
1600 case MDOC_Aq:
1601 print_text(h, n->child != NULL && n->child->next == NULL &&
1602 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1603 break;
1604 case MDOC_Bro:
1605 case MDOC_Brq:
1606 print_text(h, "\\(lC");
1607 break;
1608 case MDOC_Bo:
1609 case MDOC_Bq:
1610 print_text(h, "\\(lB");
1611 break;
1612 case MDOC_Oo:
1613 case MDOC_Op:
1614 print_text(h, "\\(lB");
1615 /*
1616 * Give up on semantic markup for now.
1617 * We cannot use TAG_SPAN because .Oo may contain blocks.
1618 * We cannot use TAG_DIV because we might be in a
1619 * phrasing context (like .Dl or .Pp); we cannot
1620 * close out a .Pp at this point either because
1621 * that would break the line.
1622 */
1623 /* XXX print_otag(h, TAG_???, "c", "Op"); */
1624 break;
1625 case MDOC_En:
1626 if (NULL == n->norm->Es ||
1627 NULL == n->norm->Es->child)
1628 return 1;
1629 print_text(h, n->norm->Es->child->string);
1630 break;
1631 case MDOC_Do:
1632 case MDOC_Dq:
1633 print_text(h, "\\(lq");
1634 break;
1635 case MDOC_Qo:
1636 case MDOC_Qq:
1637 print_text(h, "\"");
1638 break;
1639 case MDOC_Po:
1640 case MDOC_Pq:
1641 print_text(h, "(");
1642 break;
1643 case MDOC_Ql:
1644 print_text(h, "\\(oq");
1645 h->flags |= HTML_NOSPACE;
1646 print_otag(h, TAG_CODE, "c", "Li");
1647 break;
1648 case MDOC_So:
1649 case MDOC_Sq:
1650 print_text(h, "\\(oq");
1651 break;
1652 default:
1653 abort();
1654 }
1655
1656 h->flags |= HTML_NOSPACE;
1657 return 1;
1658 }
1659
1660 static void
1661 mdoc_quote_post(MDOC_ARGS)
1662 {
1663
1664 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1665 return;
1666
1667 h->flags |= HTML_NOSPACE;
1668
1669 switch (n->tok) {
1670 case MDOC_Ao:
1671 case MDOC_Aq:
1672 print_text(h, n->child != NULL && n->child->next == NULL &&
1673 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1674 break;
1675 case MDOC_Bro:
1676 case MDOC_Brq:
1677 print_text(h, "\\(rC");
1678 break;
1679 case MDOC_Oo:
1680 case MDOC_Op:
1681 case MDOC_Bo:
1682 case MDOC_Bq:
1683 print_text(h, "\\(rB");
1684 break;
1685 case MDOC_En:
1686 if (n->norm->Es == NULL ||
1687 n->norm->Es->child == NULL ||
1688 n->norm->Es->child->next == NULL)
1689 h->flags &= ~HTML_NOSPACE;
1690 else
1691 print_text(h, n->norm->Es->child->next->string);
1692 break;
1693 case MDOC_Do:
1694 case MDOC_Dq:
1695 print_text(h, "\\(rq");
1696 break;
1697 case MDOC_Qo:
1698 case MDOC_Qq:
1699 print_text(h, "\"");
1700 break;
1701 case MDOC_Po:
1702 case MDOC_Pq:
1703 print_text(h, ")");
1704 break;
1705 case MDOC_Ql:
1706 case MDOC_So:
1707 case MDOC_Sq:
1708 print_text(h, "\\(cq");
1709 break;
1710 default:
1711 abort();
1712 }
1713 }
1714
1715 static int
1716 mdoc_eo_pre(MDOC_ARGS)
1717 {
1718
1719 if (n->type != ROFFT_BODY)
1720 return 1;
1721
1722 if (n->end == ENDBODY_NOT &&
1723 n->parent->head->child == NULL &&
1724 n->child != NULL &&
1725 n->child->end != ENDBODY_NOT)
1726 print_text(h, "\\&");
1727 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1728 n->parent->head->child != NULL && (n->child != NULL ||
1729 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1730 h->flags |= HTML_NOSPACE;
1731 return 1;
1732 }
1733
1734 static void
1735 mdoc_eo_post(MDOC_ARGS)
1736 {
1737 int body, tail;
1738
1739 if (n->type != ROFFT_BODY)
1740 return;
1741
1742 if (n->end != ENDBODY_NOT) {
1743 h->flags &= ~HTML_NOSPACE;
1744 return;
1745 }
1746
1747 body = n->child != NULL || n->parent->head->child != NULL;
1748 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1749
1750 if (body && tail)
1751 h->flags |= HTML_NOSPACE;
1752 else if ( ! tail)
1753 h->flags &= ~HTML_NOSPACE;
1754 }
1755
1756 static int
1757 mdoc_abort_pre(MDOC_ARGS)
1758 {
1759 abort();
1760 }