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