]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
MANWIDTH
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.341 2020/10/16 17:22:43 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) == 0)
353 html_fillmode(h, ROFF_fi);
354 else if (html_fillmode(h, ROFF_nf) == ROFF_nf &&
355 n->tok != ROFF_fi && n->flags & NODE_LINE)
356 print_endline(h);
357
358 child = 1;
359 n->flags &= ~NODE_ENDED;
360 switch (n->type) {
361 case ROFFT_TEXT:
362 if (n->flags & NODE_LINE) {
363 switch (*n->string) {
364 case '\0':
365 h->col = 1;
366 print_endline(h);
367 return;
368 case ' ':
369 if ((h->flags & HTML_NONEWLINE) == 0 &&
370 (n->flags & NODE_NOFILL) == 0)
371 print_otag(h, TAG_BR, "");
372 break;
373 default:
374 break;
375 }
376 }
377 t = h->tag;
378 t->refcnt++;
379 if (n->flags & NODE_DELIMC)
380 h->flags |= HTML_NOSPACE;
381 if (n->flags & NODE_HREF)
382 print_tagged_text(h, n->string, n);
383 else
384 print_text(h, n->string);
385 if (n->flags & NODE_DELIMO)
386 h->flags |= HTML_NOSPACE;
387 break;
388 case ROFFT_EQN:
389 t = h->tag;
390 t->refcnt++;
391 print_eqn(h, n->eqn);
392 break;
393 case ROFFT_TBL:
394 /*
395 * This will take care of initialising all of the table
396 * state data for the first table, then tearing it down
397 * for the last one.
398 */
399 print_tbl(h, n->span);
400 return;
401 default:
402 /*
403 * Close out the current table, if it's open, and unset
404 * the "meta" table state. This will be reopened on the
405 * next table element.
406 */
407 if (h->tblt != NULL)
408 print_tblclose(h);
409 assert(h->tblt == NULL);
410 t = h->tag;
411 t->refcnt++;
412 if (n->tok < ROFF_MAX) {
413 roff_html_pre(h, n);
414 t->refcnt--;
415 print_stagq(h, t);
416 return;
417 }
418 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
419 if (mdoc_html_acts[n->tok - MDOC_Dd].pre != NULL &&
420 (n->end == ENDBODY_NOT || n->child != NULL))
421 child = (*mdoc_html_acts[n->tok - MDOC_Dd].pre)(meta,
422 n, h);
423 break;
424 }
425
426 if (h->flags & HTML_KEEP && n->flags & NODE_LINE) {
427 h->flags &= ~HTML_KEEP;
428 h->flags |= HTML_PREKEEP;
429 }
430
431 if (child && n->child != NULL)
432 print_mdoc_nodelist(meta, n->child, h);
433
434 t->refcnt--;
435 print_stagq(h, t);
436
437 switch (n->type) {
438 case ROFFT_TEXT:
439 case ROFFT_EQN:
440 break;
441 default:
442 if (mdoc_html_acts[n->tok - MDOC_Dd].post == NULL ||
443 n->flags & NODE_ENDED)
444 break;
445 (*mdoc_html_acts[n->tok - MDOC_Dd].post)(meta, n, h);
446 if (n->end != ENDBODY_NOT)
447 n->body->flags |= NODE_ENDED;
448 break;
449 }
450 }
451
452 static void
453 mdoc_root_post(const struct roff_meta *meta, struct html *h)
454 {
455 struct tag *t, *tt;
456
457 t = print_otag(h, TAG_TABLE, "c", "foot");
458 tt = print_otag(h, TAG_TR, "");
459
460 print_otag(h, TAG_TD, "c", "foot-date");
461 print_text(h, meta->date);
462 print_stagq(h, tt);
463
464 print_otag(h, TAG_TD, "c", "foot-os");
465 print_text(h, meta->os);
466 print_tagq(h, t);
467 }
468
469 static int
470 mdoc_root_pre(const struct roff_meta *meta, struct html *h)
471 {
472 struct tag *t, *tt;
473 char *volume, *title;
474
475 if (NULL == meta->arch)
476 volume = mandoc_strdup(meta->vol);
477 else
478 mandoc_asprintf(&volume, "%s (%s)",
479 meta->vol, meta->arch);
480
481 if (NULL == meta->msec)
482 title = mandoc_strdup(meta->title);
483 else
484 mandoc_asprintf(&title, "%s(%s)",
485 meta->title, meta->msec);
486
487 t = print_otag(h, TAG_TABLE, "c", "head");
488 tt = print_otag(h, TAG_TR, "");
489
490 print_otag(h, TAG_TD, "c", "head-ltitle");
491 print_text(h, title);
492 print_stagq(h, tt);
493
494 print_otag(h, TAG_TD, "c", "head-vol");
495 print_text(h, volume);
496 print_stagq(h, tt);
497
498 print_otag(h, TAG_TD, "c", "head-rtitle");
499 print_text(h, title);
500 print_tagq(h, t);
501
502 free(title);
503 free(volume);
504 return 1;
505 }
506
507 static int
508 mdoc_code_pre(MDOC_ARGS)
509 {
510 print_otag_id(h, TAG_CODE, roff_name[n->tok], n);
511 return 1;
512 }
513
514 static int
515 mdoc_sh_pre(MDOC_ARGS)
516 {
517 struct roff_node *sn, *subn;
518 struct tag *t, *tsec, *tsub;
519 char *id;
520 int sc;
521
522 switch (n->type) {
523 case ROFFT_BLOCK:
524 html_close_paragraph(h);
525 if ((h->oflags & HTML_TOC) == 0 ||
526 h->flags & HTML_TOCDONE ||
527 n->sec <= SEC_SYNOPSIS) {
528 print_otag(h, TAG_SECTION, "c", "Sh");
529 break;
530 }
531 h->flags |= HTML_TOCDONE;
532 sc = 0;
533 for (sn = n->next; sn != NULL; sn = sn->next)
534 if (sn->sec == SEC_CUSTOM)
535 if (++sc == 2)
536 break;
537 if (sc < 2)
538 break;
539 t = print_otag(h, TAG_H1, "c", "Sh");
540 print_text(h, "TABLE OF CONTENTS");
541 print_tagq(h, t);
542 t = print_otag(h, TAG_UL, "c", "Bl-compact");
543 for (sn = n; sn != NULL; sn = sn->next) {
544 tsec = print_otag(h, TAG_LI, "");
545 id = html_make_id(sn->head, 0);
546 tsub = print_otag(h, TAG_A, "hR", id);
547 free(id);
548 print_mdoc_nodelist(meta, sn->head->child, h);
549 print_tagq(h, tsub);
550 tsub = NULL;
551 for (subn = sn->body->child; subn != NULL;
552 subn = subn->next) {
553 if (subn->tok != MDOC_Ss)
554 continue;
555 id = html_make_id(subn->head, 0);
556 if (id == NULL)
557 continue;
558 if (tsub == NULL)
559 print_otag(h, TAG_UL,
560 "c", "Bl-compact");
561 tsub = print_otag(h, TAG_LI, "");
562 print_otag(h, TAG_A, "hR", id);
563 free(id);
564 print_mdoc_nodelist(meta,
565 subn->head->child, h);
566 print_tagq(h, tsub);
567 }
568 print_tagq(h, tsec);
569 }
570 print_tagq(h, t);
571 print_otag(h, TAG_SECTION, "c", "Sh");
572 break;
573 case ROFFT_HEAD:
574 print_otag_id(h, TAG_H1, "Sh", n);
575 break;
576 case ROFFT_BODY:
577 if (n->sec == SEC_AUTHORS)
578 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
579 break;
580 default:
581 break;
582 }
583 return 1;
584 }
585
586 static int
587 mdoc_ss_pre(MDOC_ARGS)
588 {
589 switch (n->type) {
590 case ROFFT_BLOCK:
591 html_close_paragraph(h);
592 print_otag(h, TAG_SECTION, "c", "Ss");
593 break;
594 case ROFFT_HEAD:
595 print_otag_id(h, TAG_H2, "Ss", n);
596 break;
597 case ROFFT_BODY:
598 break;
599 default:
600 abort();
601 }
602 return 1;
603 }
604
605 static int
606 mdoc_fl_pre(MDOC_ARGS)
607 {
608 struct roff_node *nn;
609
610 print_otag_id(h, TAG_CODE, "Fl", n);
611 print_text(h, "\\-");
612 if (n->child != NULL ||
613 ((nn = roff_node_next(n)) != NULL &&
614 nn->type != ROFFT_TEXT &&
615 (nn->flags & NODE_LINE) == 0))
616 h->flags |= HTML_NOSPACE;
617
618 return 1;
619 }
620
621 static int
622 mdoc_nd_pre(MDOC_ARGS)
623 {
624 switch (n->type) {
625 case ROFFT_BLOCK:
626 return 1;
627 case ROFFT_HEAD:
628 return 0;
629 case ROFFT_BODY:
630 break;
631 default:
632 abort();
633 }
634 print_text(h, "\\(em");
635 print_otag(h, TAG_SPAN, "c", "Nd");
636 return 1;
637 }
638
639 static int
640 mdoc_nm_pre(MDOC_ARGS)
641 {
642 switch (n->type) {
643 case ROFFT_BLOCK:
644 break;
645 case ROFFT_HEAD:
646 print_otag(h, TAG_TD, "");
647 /* FALLTHROUGH */
648 case ROFFT_ELEM:
649 print_otag(h, TAG_CODE, "c", "Nm");
650 return 1;
651 case ROFFT_BODY:
652 print_otag(h, TAG_TD, "");
653 return 1;
654 default:
655 abort();
656 }
657 html_close_paragraph(h);
658 synopsis_pre(h, n);
659 print_otag(h, TAG_TABLE, "c", "Nm");
660 print_otag(h, TAG_TR, "");
661 return 1;
662 }
663
664 static int
665 mdoc_xr_pre(MDOC_ARGS)
666 {
667 if (NULL == n->child)
668 return 0;
669
670 if (h->base_man1)
671 print_otag(h, TAG_A, "chM", "Xr",
672 n->child->string, n->child->next == NULL ?
673 NULL : n->child->next->string);
674 else
675 print_otag(h, TAG_A, "c", "Xr");
676
677 n = n->child;
678 print_text(h, n->string);
679
680 if (NULL == (n = n->next))
681 return 0;
682
683 h->flags |= HTML_NOSPACE;
684 print_text(h, "(");
685 h->flags |= HTML_NOSPACE;
686 print_text(h, n->string);
687 h->flags |= HTML_NOSPACE;
688 print_text(h, ")");
689 return 0;
690 }
691
692 static int
693 mdoc_tg_pre(MDOC_ARGS)
694 {
695 char *id;
696
697 if ((id = html_make_id(n, 1)) != NULL) {
698 print_tagq(h, print_otag(h, TAG_MARK, "i", id));
699 free(id);
700 }
701 return 0;
702 }
703
704 static int
705 mdoc_ns_pre(MDOC_ARGS)
706 {
707
708 if ( ! (NODE_LINE & n->flags))
709 h->flags |= HTML_NOSPACE;
710 return 1;
711 }
712
713 static int
714 mdoc_ar_pre(MDOC_ARGS)
715 {
716 print_otag(h, TAG_VAR, "c", "Ar");
717 return 1;
718 }
719
720 static int
721 mdoc_xx_pre(MDOC_ARGS)
722 {
723 print_otag(h, TAG_SPAN, "c", "Ux");
724 return 1;
725 }
726
727 static int
728 mdoc_it_pre(MDOC_ARGS)
729 {
730 const struct roff_node *bl;
731 enum mdoc_list type;
732
733 bl = n->parent;
734 while (bl->tok != MDOC_Bl)
735 bl = bl->parent;
736 type = bl->norm->Bl.type;
737
738 switch (type) {
739 case LIST_bullet:
740 case LIST_dash:
741 case LIST_hyphen:
742 case LIST_item:
743 case LIST_enum:
744 switch (n->type) {
745 case ROFFT_HEAD:
746 return 0;
747 case ROFFT_BODY:
748 print_otag_id(h, TAG_LI, NULL, n);
749 break;
750 default:
751 break;
752 }
753 break;
754 case LIST_diag:
755 case LIST_hang:
756 case LIST_inset:
757 case LIST_ohang:
758 switch (n->type) {
759 case ROFFT_HEAD:
760 print_otag_id(h, TAG_DT, NULL, n);
761 break;
762 case ROFFT_BODY:
763 print_otag(h, TAG_DD, "");
764 break;
765 default:
766 break;
767 }
768 break;
769 case LIST_tag:
770 switch (n->type) {
771 case ROFFT_HEAD:
772 print_otag_id(h, TAG_DT, NULL, n);
773 break;
774 case ROFFT_BODY:
775 if (n->child == NULL) {
776 print_otag(h, TAG_DD, "s", "width", "auto");
777 print_text(h, "\\ ");
778 } else
779 print_otag(h, TAG_DD, "");
780 break;
781 default:
782 break;
783 }
784 break;
785 case LIST_column:
786 switch (n->type) {
787 case ROFFT_HEAD:
788 break;
789 case ROFFT_BODY:
790 print_otag(h, TAG_TD, "");
791 break;
792 default:
793 print_otag_id(h, TAG_TR, NULL, n);
794 }
795 default:
796 break;
797 }
798
799 return 1;
800 }
801
802 static int
803 mdoc_bl_pre(MDOC_ARGS)
804 {
805 char cattr[32];
806 struct mdoc_bl *bl;
807 enum htmltag elemtype;
808
809 switch (n->type) {
810 case ROFFT_BLOCK:
811 html_close_paragraph(h);
812 break;
813 case ROFFT_HEAD:
814 return 0;
815 case ROFFT_BODY:
816 return 1;
817 default:
818 abort();
819 }
820
821 bl = &n->norm->Bl;
822 switch (bl->type) {
823 case LIST_bullet:
824 elemtype = TAG_UL;
825 (void)strlcpy(cattr, "Bl-bullet", sizeof(cattr));
826 break;
827 case LIST_dash:
828 case LIST_hyphen:
829 elemtype = TAG_UL;
830 (void)strlcpy(cattr, "Bl-dash", sizeof(cattr));
831 break;
832 case LIST_item:
833 elemtype = TAG_UL;
834 (void)strlcpy(cattr, "Bl-item", sizeof(cattr));
835 break;
836 case LIST_enum:
837 elemtype = TAG_OL;
838 (void)strlcpy(cattr, "Bl-enum", sizeof(cattr));
839 break;
840 case LIST_diag:
841 elemtype = TAG_DL;
842 (void)strlcpy(cattr, "Bl-diag", sizeof(cattr));
843 break;
844 case LIST_hang:
845 elemtype = TAG_DL;
846 (void)strlcpy(cattr, "Bl-hang", sizeof(cattr));
847 break;
848 case LIST_inset:
849 elemtype = TAG_DL;
850 (void)strlcpy(cattr, "Bl-inset", sizeof(cattr));
851 break;
852 case LIST_ohang:
853 elemtype = TAG_DL;
854 (void)strlcpy(cattr, "Bl-ohang", sizeof(cattr));
855 break;
856 case LIST_tag:
857 if (bl->offs)
858 print_otag(h, TAG_DIV, "c", "Bd-indent");
859 print_otag_id(h, TAG_DL,
860 bl->comp ? "Bl-tag Bl-compact" : "Bl-tag", n->body);
861 return 1;
862 case LIST_column:
863 elemtype = TAG_TABLE;
864 (void)strlcpy(cattr, "Bl-column", sizeof(cattr));
865 break;
866 default:
867 abort();
868 }
869 if (bl->offs != NULL)
870 (void)strlcat(cattr, " Bd-indent", sizeof(cattr));
871 if (bl->comp)
872 (void)strlcat(cattr, " Bl-compact", sizeof(cattr));
873 print_otag_id(h, elemtype, cattr, n->body);
874 return 1;
875 }
876
877 static int
878 mdoc_ex_pre(MDOC_ARGS)
879 {
880 if (roff_node_prev(n) != NULL)
881 print_otag(h, TAG_BR, "");
882 return 1;
883 }
884
885 static int
886 mdoc_st_pre(MDOC_ARGS)
887 {
888 print_otag(h, TAG_SPAN, "c", "St");
889 return 1;
890 }
891
892 static int
893 mdoc_em_pre(MDOC_ARGS)
894 {
895 print_otag_id(h, TAG_I, "Em", n);
896 return 1;
897 }
898
899 static int
900 mdoc_d1_pre(MDOC_ARGS)
901 {
902 switch (n->type) {
903 case ROFFT_BLOCK:
904 html_close_paragraph(h);
905 return 1;
906 case ROFFT_HEAD:
907 return 0;
908 case ROFFT_BODY:
909 break;
910 default:
911 abort();
912 }
913 print_otag_id(h, TAG_DIV, "Bd Bd-indent", n);
914 if (n->tok == MDOC_Dl)
915 print_otag(h, TAG_CODE, "c", "Li");
916 return 1;
917 }
918
919 static int
920 mdoc_sx_pre(MDOC_ARGS)
921 {
922 char *id;
923
924 id = html_make_id(n, 0);
925 print_otag(h, TAG_A, "chR", "Sx", id);
926 free(id);
927 return 1;
928 }
929
930 static int
931 mdoc_bd_pre(MDOC_ARGS)
932 {
933 char buf[16];
934 struct roff_node *nn;
935 int comp;
936
937 switch (n->type) {
938 case ROFFT_BLOCK:
939 html_close_paragraph(h);
940 return 1;
941 case ROFFT_HEAD:
942 return 0;
943 case ROFFT_BODY:
944 break;
945 default:
946 abort();
947 }
948
949 /* Handle preceding whitespace. */
950
951 comp = n->norm->Bd.comp;
952 for (nn = n; nn != NULL && comp == 0; nn = nn->parent) {
953 if (nn->type != ROFFT_BLOCK)
954 continue;
955 if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
956 comp = 1;
957 if (roff_node_prev(nn) != NULL)
958 break;
959 }
960 (void)strlcpy(buf, "Bd", sizeof(buf));
961 if (comp == 0)
962 (void)strlcat(buf, " Pp", sizeof(buf));
963
964 /* Handle the -offset argument. */
965
966 if (n->norm->Bd.offs != NULL &&
967 strcmp(n->norm->Bd.offs, "left") != 0)
968 (void)strlcat(buf, " Bd-indent", sizeof(buf));
969
970 print_otag_id(h, TAG_DIV, buf, n);
971 return 1;
972 }
973
974 static int
975 mdoc_pa_pre(MDOC_ARGS)
976 {
977 print_otag(h, TAG_SPAN, "c", "Pa");
978 return 1;
979 }
980
981 static int
982 mdoc_ad_pre(MDOC_ARGS)
983 {
984 print_otag(h, TAG_SPAN, "c", "Ad");
985 return 1;
986 }
987
988 static int
989 mdoc_an_pre(MDOC_ARGS)
990 {
991 if (n->norm->An.auth == AUTH_split) {
992 h->flags &= ~HTML_NOSPLIT;
993 h->flags |= HTML_SPLIT;
994 return 0;
995 }
996 if (n->norm->An.auth == AUTH_nosplit) {
997 h->flags &= ~HTML_SPLIT;
998 h->flags |= HTML_NOSPLIT;
999 return 0;
1000 }
1001
1002 if (h->flags & HTML_SPLIT)
1003 print_otag(h, TAG_BR, "");
1004
1005 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1006 h->flags |= HTML_SPLIT;
1007
1008 print_otag(h, TAG_SPAN, "c", "An");
1009 return 1;
1010 }
1011
1012 static int
1013 mdoc_cd_pre(MDOC_ARGS)
1014 {
1015 synopsis_pre(h, n);
1016 print_otag(h, TAG_CODE, "c", "Cd");
1017 return 1;
1018 }
1019
1020 static int
1021 mdoc_fa_pre(MDOC_ARGS)
1022 {
1023 const struct roff_node *nn;
1024 struct tag *t;
1025
1026 if (n->parent->tok != MDOC_Fo) {
1027 print_otag(h, TAG_VAR, "c", "Fa");
1028 return 1;
1029 }
1030 for (nn = n->child; nn != NULL; nn = nn->next) {
1031 t = print_otag(h, TAG_VAR, "c", "Fa");
1032 print_text(h, nn->string);
1033 print_tagq(h, t);
1034 if (nn->next != NULL) {
1035 h->flags |= HTML_NOSPACE;
1036 print_text(h, ",");
1037 }
1038 }
1039 if (n->child != NULL &&
1040 (nn = roff_node_next(n)) != NULL &&
1041 nn->tok == MDOC_Fa) {
1042 h->flags |= HTML_NOSPACE;
1043 print_text(h, ",");
1044 }
1045 return 0;
1046 }
1047
1048 static int
1049 mdoc_fd_pre(MDOC_ARGS)
1050 {
1051 struct tag *t;
1052 char *buf, *cp;
1053
1054 synopsis_pre(h, n);
1055
1056 if (NULL == (n = n->child))
1057 return 0;
1058
1059 assert(n->type == ROFFT_TEXT);
1060
1061 if (strcmp(n->string, "#include")) {
1062 print_otag(h, TAG_CODE, "c", "Fd");
1063 return 1;
1064 }
1065
1066 print_otag(h, TAG_CODE, "c", "In");
1067 print_text(h, n->string);
1068
1069 if (NULL != (n = n->next)) {
1070 assert(n->type == ROFFT_TEXT);
1071
1072 if (h->base_includes) {
1073 cp = n->string;
1074 if (*cp == '<' || *cp == '"')
1075 cp++;
1076 buf = mandoc_strdup(cp);
1077 cp = strchr(buf, '\0') - 1;
1078 if (cp >= buf && (*cp == '>' || *cp == '"'))
1079 *cp = '\0';
1080 t = print_otag(h, TAG_A, "chI", "In", buf);
1081 free(buf);
1082 } else
1083 t = print_otag(h, TAG_A, "c", "In");
1084
1085 print_text(h, n->string);
1086 print_tagq(h, t);
1087
1088 n = n->next;
1089 }
1090
1091 for ( ; n; n = n->next) {
1092 assert(n->type == ROFFT_TEXT);
1093 print_text(h, n->string);
1094 }
1095
1096 return 0;
1097 }
1098
1099 static int
1100 mdoc_vt_pre(MDOC_ARGS)
1101 {
1102 if (n->type == ROFFT_BLOCK) {
1103 synopsis_pre(h, n);
1104 return 1;
1105 } else if (n->type == ROFFT_ELEM) {
1106 synopsis_pre(h, n);
1107 } else if (n->type == ROFFT_HEAD)
1108 return 0;
1109
1110 print_otag(h, TAG_VAR, "c", "Vt");
1111 return 1;
1112 }
1113
1114 static int
1115 mdoc_ft_pre(MDOC_ARGS)
1116 {
1117 synopsis_pre(h, n);
1118 print_otag(h, TAG_VAR, "c", "Ft");
1119 return 1;
1120 }
1121
1122 static int
1123 mdoc_fn_pre(MDOC_ARGS)
1124 {
1125 struct tag *t;
1126 char nbuf[BUFSIZ];
1127 const char *sp, *ep;
1128 int sz, pretty;
1129
1130 pretty = NODE_SYNPRETTY & n->flags;
1131 synopsis_pre(h, n);
1132
1133 /* Split apart into type and name. */
1134 assert(n->child->string);
1135 sp = n->child->string;
1136
1137 ep = strchr(sp, ' ');
1138 if (NULL != ep) {
1139 t = print_otag(h, TAG_VAR, "c", "Ft");
1140
1141 while (ep) {
1142 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1143 (void)memcpy(nbuf, sp, (size_t)sz);
1144 nbuf[sz] = '\0';
1145 print_text(h, nbuf);
1146 sp = ++ep;
1147 ep = strchr(sp, ' ');
1148 }
1149 print_tagq(h, t);
1150 }
1151
1152 t = print_otag_id(h, TAG_CODE, "Fn", n);
1153
1154 if (sp)
1155 print_text(h, sp);
1156
1157 print_tagq(h, t);
1158
1159 h->flags |= HTML_NOSPACE;
1160 print_text(h, "(");
1161 h->flags |= HTML_NOSPACE;
1162
1163 for (n = n->child->next; n; n = n->next) {
1164 if (NODE_SYNPRETTY & n->flags)
1165 t = print_otag(h, TAG_VAR, "cs", "Fa",
1166 "white-space", "nowrap");
1167 else
1168 t = print_otag(h, TAG_VAR, "c", "Fa");
1169 print_text(h, n->string);
1170 print_tagq(h, t);
1171 if (n->next) {
1172 h->flags |= HTML_NOSPACE;
1173 print_text(h, ",");
1174 }
1175 }
1176
1177 h->flags |= HTML_NOSPACE;
1178 print_text(h, ")");
1179
1180 if (pretty) {
1181 h->flags |= HTML_NOSPACE;
1182 print_text(h, ";");
1183 }
1184
1185 return 0;
1186 }
1187
1188 static int
1189 mdoc_sm_pre(MDOC_ARGS)
1190 {
1191
1192 if (NULL == n->child)
1193 h->flags ^= HTML_NONOSPACE;
1194 else if (0 == strcmp("on", n->child->string))
1195 h->flags &= ~HTML_NONOSPACE;
1196 else
1197 h->flags |= HTML_NONOSPACE;
1198
1199 if ( ! (HTML_NONOSPACE & h->flags))
1200 h->flags &= ~HTML_NOSPACE;
1201
1202 return 0;
1203 }
1204
1205 static int
1206 mdoc_skip_pre(MDOC_ARGS)
1207 {
1208
1209 return 0;
1210 }
1211
1212 static int
1213 mdoc_pp_pre(MDOC_ARGS)
1214 {
1215 char *id;
1216
1217 if (n->flags & NODE_NOFILL) {
1218 print_endline(h);
1219 if (n->flags & NODE_ID)
1220 mdoc_tg_pre(meta, n, h);
1221 else {
1222 h->col = 1;
1223 print_endline(h);
1224 }
1225 } else {
1226 html_close_paragraph(h);
1227 id = n->flags & NODE_ID ? html_make_id(n, 1) : NULL;
1228 print_otag(h, TAG_P, "ci", "Pp", id);
1229 free(id);
1230 }
1231 return 0;
1232 }
1233
1234 static int
1235 mdoc_lk_pre(MDOC_ARGS)
1236 {
1237 const struct roff_node *link, *descr, *punct;
1238 struct tag *t;
1239
1240 if ((link = n->child) == NULL)
1241 return 0;
1242
1243 /* Find beginning of trailing punctuation. */
1244 punct = n->last;
1245 while (punct != link && punct->flags & NODE_DELIMC)
1246 punct = punct->prev;
1247 punct = punct->next;
1248
1249 /* Link target and link text. */
1250 descr = link->next;
1251 if (descr == punct)
1252 descr = link; /* no text */
1253 t = print_otag(h, TAG_A, "ch", "Lk", link->string);
1254 do {
1255 if (descr->flags & (NODE_DELIMC | NODE_DELIMO))
1256 h->flags |= HTML_NOSPACE;
1257 print_text(h, descr->string);
1258 descr = descr->next;
1259 } while (descr != punct);
1260 print_tagq(h, t);
1261
1262 /* Trailing punctuation. */
1263 while (punct != NULL) {
1264 h->flags |= HTML_NOSPACE;
1265 print_text(h, punct->string);
1266 punct = punct->next;
1267 }
1268 return 0;
1269 }
1270
1271 static int
1272 mdoc_mt_pre(MDOC_ARGS)
1273 {
1274 struct tag *t;
1275 char *cp;
1276
1277 for (n = n->child; n; n = n->next) {
1278 assert(n->type == ROFFT_TEXT);
1279 mandoc_asprintf(&cp, "mailto:%s", n->string);
1280 t = print_otag(h, TAG_A, "ch", "Mt", cp);
1281 print_text(h, n->string);
1282 print_tagq(h, t);
1283 free(cp);
1284 }
1285 return 0;
1286 }
1287
1288 static int
1289 mdoc_fo_pre(MDOC_ARGS)
1290 {
1291 struct tag *t;
1292
1293 switch (n->type) {
1294 case ROFFT_BLOCK:
1295 synopsis_pre(h, n);
1296 return 1;
1297 case ROFFT_HEAD:
1298 if (n->child != NULL) {
1299 t = print_otag_id(h, TAG_CODE, "Fn", n);
1300 print_text(h, n->child->string);
1301 print_tagq(h, t);
1302 }
1303 return 0;
1304 case ROFFT_BODY:
1305 h->flags |= HTML_NOSPACE;
1306 print_text(h, "(");
1307 h->flags |= HTML_NOSPACE;
1308 return 1;
1309 default:
1310 abort();
1311 }
1312 }
1313
1314 static void
1315 mdoc_fo_post(MDOC_ARGS)
1316 {
1317 if (n->type != ROFFT_BODY)
1318 return;
1319 h->flags |= HTML_NOSPACE;
1320 print_text(h, ")");
1321 h->flags |= HTML_NOSPACE;
1322 print_text(h, ";");
1323 }
1324
1325 static int
1326 mdoc_in_pre(MDOC_ARGS)
1327 {
1328 struct tag *t;
1329
1330 synopsis_pre(h, n);
1331 print_otag(h, TAG_CODE, "c", "In");
1332
1333 /*
1334 * The first argument of the `In' gets special treatment as
1335 * being a linked value. Subsequent values are printed
1336 * afterward. groff does similarly. This also handles the case
1337 * of no children.
1338 */
1339
1340 if (NODE_SYNPRETTY & n->flags && NODE_LINE & n->flags)
1341 print_text(h, "#include");
1342
1343 print_text(h, "<");
1344 h->flags |= HTML_NOSPACE;
1345
1346 if (NULL != (n = n->child)) {
1347 assert(n->type == ROFFT_TEXT);
1348
1349 if (h->base_includes)
1350 t = print_otag(h, TAG_A, "chI", "In", n->string);
1351 else
1352 t = print_otag(h, TAG_A, "c", "In");
1353 print_text(h, n->string);
1354 print_tagq(h, t);
1355
1356 n = n->next;
1357 }
1358
1359 h->flags |= HTML_NOSPACE;
1360 print_text(h, ">");
1361
1362 for ( ; n; n = n->next) {
1363 assert(n->type == ROFFT_TEXT);
1364 print_text(h, n->string);
1365 }
1366 return 0;
1367 }
1368
1369 static int
1370 mdoc_va_pre(MDOC_ARGS)
1371 {
1372 print_otag(h, TAG_VAR, "c", "Va");
1373 return 1;
1374 }
1375
1376 static int
1377 mdoc_ap_pre(MDOC_ARGS)
1378 {
1379 h->flags |= HTML_NOSPACE;
1380 print_text(h, "\\(aq");
1381 h->flags |= HTML_NOSPACE;
1382 return 1;
1383 }
1384
1385 static int
1386 mdoc_bf_pre(MDOC_ARGS)
1387 {
1388 const char *cattr;
1389
1390 switch (n->type) {
1391 case ROFFT_BLOCK:
1392 html_close_paragraph(h);
1393 return 1;
1394 case ROFFT_HEAD:
1395 return 0;
1396 case ROFFT_BODY:
1397 break;
1398 default:
1399 abort();
1400 }
1401
1402 if (FONT_Em == n->norm->Bf.font)
1403 cattr = "Bf Em";
1404 else if (FONT_Sy == n->norm->Bf.font)
1405 cattr = "Bf Sy";
1406 else if (FONT_Li == n->norm->Bf.font)
1407 cattr = "Bf Li";
1408 else
1409 cattr = "Bf No";
1410
1411 /* Cannot use TAG_SPAN because it may contain blocks. */
1412 print_otag(h, TAG_DIV, "c", cattr);
1413 return 1;
1414 }
1415
1416 static int
1417 mdoc_igndelim_pre(MDOC_ARGS)
1418 {
1419 h->flags |= HTML_IGNDELIM;
1420 return 1;
1421 }
1422
1423 static void
1424 mdoc_pf_post(MDOC_ARGS)
1425 {
1426 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1427 h->flags |= HTML_NOSPACE;
1428 }
1429
1430 static int
1431 mdoc_rs_pre(MDOC_ARGS)
1432 {
1433 switch (n->type) {
1434 case ROFFT_BLOCK:
1435 if (n->sec == SEC_SEE_ALSO)
1436 html_close_paragraph(h);
1437 break;
1438 case ROFFT_HEAD:
1439 return 0;
1440 case ROFFT_BODY:
1441 if (n->sec == SEC_SEE_ALSO)
1442 print_otag(h, TAG_P, "c", "Pp");
1443 print_otag(h, TAG_CITE, "c", "Rs");
1444 break;
1445 default:
1446 abort();
1447 }
1448 return 1;
1449 }
1450
1451 static int
1452 mdoc_no_pre(MDOC_ARGS)
1453 {
1454 print_otag_id(h, TAG_SPAN, roff_name[n->tok], n);
1455 return 1;
1456 }
1457
1458 static int
1459 mdoc_sy_pre(MDOC_ARGS)
1460 {
1461 print_otag_id(h, TAG_B, "Sy", n);
1462 return 1;
1463 }
1464
1465 static int
1466 mdoc_lb_pre(MDOC_ARGS)
1467 {
1468 if (n->sec == SEC_LIBRARY &&
1469 n->flags & NODE_LINE &&
1470 roff_node_prev(n) != NULL)
1471 print_otag(h, TAG_BR, "");
1472
1473 print_otag(h, TAG_SPAN, "c", "Lb");
1474 return 1;
1475 }
1476
1477 static int
1478 mdoc__x_pre(MDOC_ARGS)
1479 {
1480 struct roff_node *nn;
1481 const char *cattr;
1482 enum htmltag t;
1483
1484 t = TAG_SPAN;
1485
1486 switch (n->tok) {
1487 case MDOC__A:
1488 cattr = "RsA";
1489 if ((nn = roff_node_prev(n)) != NULL && nn->tok == MDOC__A &&
1490 ((nn = roff_node_next(n)) == NULL || nn->tok != MDOC__A))
1491 print_text(h, "and");
1492 break;
1493 case MDOC__B:
1494 t = TAG_I;
1495 cattr = "RsB";
1496 break;
1497 case MDOC__C:
1498 cattr = "RsC";
1499 break;
1500 case MDOC__D:
1501 cattr = "RsD";
1502 break;
1503 case MDOC__I:
1504 t = TAG_I;
1505 cattr = "RsI";
1506 break;
1507 case MDOC__J:
1508 t = TAG_I;
1509 cattr = "RsJ";
1510 break;
1511 case MDOC__N:
1512 cattr = "RsN";
1513 break;
1514 case MDOC__O:
1515 cattr = "RsO";
1516 break;
1517 case MDOC__P:
1518 cattr = "RsP";
1519 break;
1520 case MDOC__Q:
1521 cattr = "RsQ";
1522 break;
1523 case MDOC__R:
1524 cattr = "RsR";
1525 break;
1526 case MDOC__T:
1527 cattr = "RsT";
1528 break;
1529 case MDOC__U:
1530 print_otag(h, TAG_A, "ch", "RsU", n->child->string);
1531 return 1;
1532 case MDOC__V:
1533 cattr = "RsV";
1534 break;
1535 default:
1536 abort();
1537 }
1538
1539 print_otag(h, t, "c", cattr);
1540 return 1;
1541 }
1542
1543 static void
1544 mdoc__x_post(MDOC_ARGS)
1545 {
1546 struct roff_node *nn;
1547
1548 if (n->tok == MDOC__A &&
1549 (nn = roff_node_next(n)) != NULL && nn->tok == MDOC__A &&
1550 ((nn = roff_node_next(nn)) == NULL || nn->tok != MDOC__A) &&
1551 ((nn = roff_node_prev(n)) == NULL || nn->tok != MDOC__A))
1552 return;
1553
1554 /* TODO: %U */
1555
1556 if (n->parent == NULL || n->parent->tok != MDOC_Rs)
1557 return;
1558
1559 h->flags |= HTML_NOSPACE;
1560 print_text(h, roff_node_next(n) ? "," : ".");
1561 }
1562
1563 static int
1564 mdoc_bk_pre(MDOC_ARGS)
1565 {
1566
1567 switch (n->type) {
1568 case ROFFT_BLOCK:
1569 break;
1570 case ROFFT_HEAD:
1571 return 0;
1572 case ROFFT_BODY:
1573 if (n->parent->args != NULL || n->prev->child == NULL)
1574 h->flags |= HTML_PREKEEP;
1575 break;
1576 default:
1577 abort();
1578 }
1579
1580 return 1;
1581 }
1582
1583 static void
1584 mdoc_bk_post(MDOC_ARGS)
1585 {
1586
1587 if (n->type == ROFFT_BODY)
1588 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
1589 }
1590
1591 static int
1592 mdoc_quote_pre(MDOC_ARGS)
1593 {
1594 if (n->type != ROFFT_BODY)
1595 return 1;
1596
1597 switch (n->tok) {
1598 case MDOC_Ao:
1599 case MDOC_Aq:
1600 print_text(h, n->child != NULL && n->child->next == NULL &&
1601 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1602 break;
1603 case MDOC_Bro:
1604 case MDOC_Brq:
1605 print_text(h, "\\(lC");
1606 break;
1607 case MDOC_Bo:
1608 case MDOC_Bq:
1609 print_text(h, "\\(lB");
1610 break;
1611 case MDOC_Oo:
1612 case MDOC_Op:
1613 print_text(h, "\\(lB");
1614 /*
1615 * Give up on semantic markup for now.
1616 * We cannot use TAG_SPAN because .Oo may contain blocks.
1617 * We cannot use TAG_DIV because we might be in a
1618 * phrasing context (like .Dl or .Pp); we cannot
1619 * close out a .Pp at this point either because
1620 * that would break the line.
1621 */
1622 /* XXX print_otag(h, TAG_???, "c", "Op"); */
1623 break;
1624 case MDOC_En:
1625 if (NULL == n->norm->Es ||
1626 NULL == n->norm->Es->child)
1627 return 1;
1628 print_text(h, n->norm->Es->child->string);
1629 break;
1630 case MDOC_Do:
1631 case MDOC_Dq:
1632 print_text(h, "\\(lq");
1633 break;
1634 case MDOC_Qo:
1635 case MDOC_Qq:
1636 print_text(h, "\"");
1637 break;
1638 case MDOC_Po:
1639 case MDOC_Pq:
1640 print_text(h, "(");
1641 break;
1642 case MDOC_Ql:
1643 print_text(h, "\\(oq");
1644 h->flags |= HTML_NOSPACE;
1645 print_otag(h, TAG_CODE, "c", "Li");
1646 break;
1647 case MDOC_So:
1648 case MDOC_Sq:
1649 print_text(h, "\\(oq");
1650 break;
1651 default:
1652 abort();
1653 }
1654
1655 h->flags |= HTML_NOSPACE;
1656 return 1;
1657 }
1658
1659 static void
1660 mdoc_quote_post(MDOC_ARGS)
1661 {
1662
1663 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1664 return;
1665
1666 h->flags |= HTML_NOSPACE;
1667
1668 switch (n->tok) {
1669 case MDOC_Ao:
1670 case MDOC_Aq:
1671 print_text(h, n->child != NULL && n->child->next == NULL &&
1672 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1673 break;
1674 case MDOC_Bro:
1675 case MDOC_Brq:
1676 print_text(h, "\\(rC");
1677 break;
1678 case MDOC_Oo:
1679 case MDOC_Op:
1680 case MDOC_Bo:
1681 case MDOC_Bq:
1682 print_text(h, "\\(rB");
1683 break;
1684 case MDOC_En:
1685 if (n->norm->Es == NULL ||
1686 n->norm->Es->child == NULL ||
1687 n->norm->Es->child->next == NULL)
1688 h->flags &= ~HTML_NOSPACE;
1689 else
1690 print_text(h, n->norm->Es->child->next->string);
1691 break;
1692 case MDOC_Do:
1693 case MDOC_Dq:
1694 print_text(h, "\\(rq");
1695 break;
1696 case MDOC_Qo:
1697 case MDOC_Qq:
1698 print_text(h, "\"");
1699 break;
1700 case MDOC_Po:
1701 case MDOC_Pq:
1702 print_text(h, ")");
1703 break;
1704 case MDOC_Ql:
1705 case MDOC_So:
1706 case MDOC_Sq:
1707 print_text(h, "\\(cq");
1708 break;
1709 default:
1710 abort();
1711 }
1712 }
1713
1714 static int
1715 mdoc_eo_pre(MDOC_ARGS)
1716 {
1717
1718 if (n->type != ROFFT_BODY)
1719 return 1;
1720
1721 if (n->end == ENDBODY_NOT &&
1722 n->parent->head->child == NULL &&
1723 n->child != NULL &&
1724 n->child->end != ENDBODY_NOT)
1725 print_text(h, "\\&");
1726 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1727 n->parent->head->child != NULL && (n->child != NULL ||
1728 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1729 h->flags |= HTML_NOSPACE;
1730 return 1;
1731 }
1732
1733 static void
1734 mdoc_eo_post(MDOC_ARGS)
1735 {
1736 int body, tail;
1737
1738 if (n->type != ROFFT_BODY)
1739 return;
1740
1741 if (n->end != ENDBODY_NOT) {
1742 h->flags &= ~HTML_NOSPACE;
1743 return;
1744 }
1745
1746 body = n->child != NULL || n->parent->head->child != NULL;
1747 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1748
1749 if (body && tail)
1750 h->flags |= HTML_NOSPACE;
1751 else if ( ! tail)
1752 h->flags &= ~HTML_NOSPACE;
1753 }
1754
1755 static int
1756 mdoc_abort_pre(MDOC_ARGS)
1757 {
1758 abort();
1759 }