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