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