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