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