]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
During validation, drop .br before a text line starting with a
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.316 2018/12/03 21:00:10 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 print_paragraph(h);
272 break;
273 case MDOC_Ft:
274 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
275 print_paragraph(h);
276 break;
277 }
278 /* FALLTHROUGH */
279 default:
280 print_otag(h, TAG_BR, "");
281 break;
282 }
283 }
284
285 void
286 html_mdoc(void *arg, const struct roff_man *mdoc)
287 {
288 struct html *h;
289 struct roff_node *n;
290 struct tag *t;
291
292 h = (struct html *)arg;
293 n = mdoc->first->child;
294
295 if ((h->oflags & HTML_FRAGMENT) == 0) {
296 print_gen_decls(h);
297 print_otag(h, TAG_HTML, "");
298 if (n->type == ROFFT_COMMENT)
299 print_gen_comment(h, n);
300 t = print_otag(h, TAG_HEAD, "");
301 print_mdoc_head(&mdoc->meta, h);
302 print_tagq(h, t);
303 print_otag(h, TAG_BODY, "");
304 }
305
306 mdoc_root_pre(&mdoc->meta, h);
307 t = print_otag(h, TAG_DIV, "c", "manual-text");
308 print_mdoc_nodelist(&mdoc->meta, n, h);
309 print_tagq(h, t);
310 mdoc_root_post(&mdoc->meta, h);
311 print_tagq(h, NULL);
312 }
313
314 static void
315 print_mdoc_head(const struct roff_meta *meta, struct html *h)
316 {
317 char *cp;
318
319 print_gen_head(h);
320
321 if (meta->arch != NULL && meta->msec != NULL)
322 mandoc_asprintf(&cp, "%s(%s) (%s)", meta->title,
323 meta->msec, meta->arch);
324 else if (meta->msec != NULL)
325 mandoc_asprintf(&cp, "%s(%s)", meta->title, meta->msec);
326 else if (meta->arch != NULL)
327 mandoc_asprintf(&cp, "%s (%s)", meta->title, meta->arch);
328 else
329 cp = mandoc_strdup(meta->title);
330
331 print_otag(h, TAG_TITLE, "");
332 print_text(h, cp);
333 free(cp);
334 }
335
336 static void
337 print_mdoc_nodelist(MDOC_ARGS)
338 {
339
340 while (n != NULL) {
341 print_mdoc_node(meta, n, h);
342 n = n->next;
343 }
344 }
345
346 static void
347 print_mdoc_node(MDOC_ARGS)
348 {
349 int child;
350 struct tag *t;
351
352 if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
353 return;
354
355 child = 1;
356 t = h->tag;
357 n->flags &= ~NODE_ENDED;
358
359 switch (n->type) {
360 case ROFFT_TEXT:
361 /* No tables in this mode... */
362 assert(NULL == h->tblt);
363
364 /*
365 * Make sure that if we're in a literal mode already
366 * (i.e., within a <PRE>) don't print the newline.
367 */
368 if (*n->string == ' ' && n->flags & NODE_LINE &&
369 (h->flags & (HTML_LITERAL | HTML_NONEWLINE)) == 0)
370 print_otag(h, TAG_BR, "");
371 if (NODE_DELIMC & n->flags)
372 h->flags |= HTML_NOSPACE;
373 print_text(h, n->string);
374 if (NODE_DELIMO & n->flags)
375 h->flags |= HTML_NOSPACE;
376 return;
377 case ROFFT_EQN:
378 print_eqn(h, n->eqn);
379 break;
380 case ROFFT_TBL:
381 /*
382 * This will take care of initialising all of the table
383 * state data for the first table, then tearing it down
384 * for the last one.
385 */
386 print_tbl(h, n->span);
387 return;
388 default:
389 /*
390 * Close out the current table, if it's open, and unset
391 * the "meta" table state. This will be reopened on the
392 * next table element.
393 */
394 if (h->tblt != NULL) {
395 print_tblclose(h);
396 t = h->tag;
397 }
398 assert(h->tblt == NULL);
399 if (n->tok < ROFF_MAX) {
400 roff_html_pre(h, n);
401 child = 0;
402 break;
403 }
404 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
405 if (mdoc_html_acts[n->tok - MDOC_Dd].pre != NULL &&
406 (n->end == ENDBODY_NOT || n->child != NULL))
407 child = (*mdoc_html_acts[n->tok - MDOC_Dd].pre)(meta,
408 n, h);
409 break;
410 }
411
412 if (h->flags & HTML_KEEP && n->flags & NODE_LINE) {
413 h->flags &= ~HTML_KEEP;
414 h->flags |= HTML_PREKEEP;
415 }
416
417 if (child && n->child)
418 print_mdoc_nodelist(meta, n->child, h);
419
420 print_stagq(h, t);
421
422 switch (n->type) {
423 case ROFFT_EQN:
424 break;
425 default:
426 if (n->tok < ROFF_MAX ||
427 mdoc_html_acts[n->tok - MDOC_Dd].post == NULL ||
428 n->flags & NODE_ENDED)
429 break;
430 (*mdoc_html_acts[n->tok - MDOC_Dd].post)(meta, n, h);
431 if (n->end != ENDBODY_NOT)
432 n->body->flags |= NODE_ENDED;
433 break;
434 }
435 }
436
437 static void
438 mdoc_root_post(const struct roff_meta *meta, struct html *h)
439 {
440 struct tag *t, *tt;
441
442 t = print_otag(h, TAG_TABLE, "c", "foot");
443 tt = print_otag(h, TAG_TR, "");
444
445 print_otag(h, TAG_TD, "c", "foot-date");
446 print_text(h, meta->date);
447 print_stagq(h, tt);
448
449 print_otag(h, TAG_TD, "c", "foot-os");
450 print_text(h, meta->os);
451 print_tagq(h, t);
452 }
453
454 static int
455 mdoc_root_pre(const struct roff_meta *meta, struct html *h)
456 {
457 struct tag *t, *tt;
458 char *volume, *title;
459
460 if (NULL == meta->arch)
461 volume = mandoc_strdup(meta->vol);
462 else
463 mandoc_asprintf(&volume, "%s (%s)",
464 meta->vol, meta->arch);
465
466 if (NULL == meta->msec)
467 title = mandoc_strdup(meta->title);
468 else
469 mandoc_asprintf(&title, "%s(%s)",
470 meta->title, meta->msec);
471
472 t = print_otag(h, TAG_TABLE, "c", "head");
473 tt = print_otag(h, TAG_TR, "");
474
475 print_otag(h, TAG_TD, "c", "head-ltitle");
476 print_text(h, title);
477 print_stagq(h, tt);
478
479 print_otag(h, TAG_TD, "c", "head-vol");
480 print_text(h, volume);
481 print_stagq(h, tt);
482
483 print_otag(h, TAG_TD, "c", "head-rtitle");
484 print_text(h, title);
485 print_tagq(h, t);
486
487 free(title);
488 free(volume);
489 return 1;
490 }
491
492 static char *
493 cond_id(const struct roff_node *n)
494 {
495 if (n->child != NULL &&
496 n->child->type == ROFFT_TEXT &&
497 (n->prev == NULL ||
498 (n->prev->type == ROFFT_TEXT &&
499 strcmp(n->prev->string, "|") == 0)) &&
500 (n->parent->tok == MDOC_It ||
501 (n->parent->tok == MDOC_Xo &&
502 n->parent->parent->prev == NULL &&
503 n->parent->parent->parent->tok == MDOC_It)))
504 return html_make_id(n, 1);
505 return NULL;
506 }
507
508 static int
509 mdoc_sh_pre(MDOC_ARGS)
510 {
511 struct roff_node *sn, *subn;
512 struct tag *t, *tsec, *tsub;
513 char *id;
514 int sc;
515
516 switch (n->type) {
517 case ROFFT_BLOCK:
518 if ((h->oflags & HTML_TOC) == 0 ||
519 h->flags & HTML_TOCDONE ||
520 n->sec <= SEC_SYNOPSIS)
521 break;
522 h->flags |= HTML_TOCDONE;
523 sc = 0;
524 for (sn = n->next; sn != NULL; sn = sn->next)
525 if (sn->sec == SEC_CUSTOM)
526 if (++sc == 2)
527 break;
528 if (sc < 2)
529 break;
530 t = print_otag(h, TAG_H1, "c", "Sh");
531 print_text(h, "TABLE OF CONTENTS");
532 print_tagq(h, t);
533 t = print_otag(h, TAG_UL, "c", "Bl-compact");
534 for (sn = n; sn != NULL; sn = sn->next) {
535 tsec = print_otag(h, TAG_LI, "");
536 id = html_make_id(sn->head, 0);
537 print_otag(h, TAG_A, "hR", id);
538 free(id);
539 print_mdoc_nodelist(meta, sn->head->child, h);
540 tsub = NULL;
541 for (subn = sn->body->child; subn != NULL;
542 subn = subn->next) {
543 if (subn->tok != MDOC_Ss)
544 continue;
545 id = html_make_id(subn->head, 0);
546 if (id == NULL)
547 continue;
548 if (tsub == NULL)
549 print_otag(h, TAG_UL,
550 "c", "Bl-compact");
551 tsub = print_otag(h, TAG_LI, "");
552 print_otag(h, TAG_A, "hR", id);
553 free(id);
554 print_mdoc_nodelist(meta,
555 subn->head->child, h);
556 print_tagq(h, tsub);
557 }
558 print_tagq(h, tsec);
559 }
560 print_tagq(h, t);
561 break;
562 case ROFFT_HEAD:
563 id = html_make_id(n, 1);
564 print_otag(h, TAG_H1, "cTi", "Sh", id);
565 if (id != NULL)
566 print_otag(h, TAG_A, "chR", "permalink", id);
567 break;
568 case ROFFT_BODY:
569 if (n->sec == SEC_AUTHORS)
570 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
571 break;
572 default:
573 break;
574 }
575 return 1;
576 }
577
578 static int
579 mdoc_ss_pre(MDOC_ARGS)
580 {
581 char *id;
582
583 if (n->type != ROFFT_HEAD)
584 return 1;
585
586 id = html_make_id(n, 1);
587 print_otag(h, TAG_H2, "cTi", "Ss", id);
588 if (id != NULL)
589 print_otag(h, TAG_A, "chR", "permalink", id);
590 return 1;
591 }
592
593 static int
594 mdoc_fl_pre(MDOC_ARGS)
595 {
596 char *id;
597
598 if ((id = cond_id(n)) != NULL)
599 print_otag(h, TAG_A, "chR", "permalink", id);
600 print_otag(h, TAG_CODE, "cTi", "Fl", id);
601
602 print_text(h, "\\-");
603 if (!(n->child == NULL &&
604 (n->next == NULL ||
605 n->next->type == ROFFT_TEXT ||
606 n->next->flags & NODE_LINE)))
607 h->flags |= HTML_NOSPACE;
608
609 return 1;
610 }
611
612 static int
613 mdoc_cm_pre(MDOC_ARGS)
614 {
615 char *id;
616
617 if ((id = cond_id(n)) != NULL)
618 print_otag(h, TAG_A, "chR", "permalink", id);
619 print_otag(h, TAG_CODE, "cTi", "Cm", id);
620 return 1;
621 }
622
623 static int
624 mdoc_nd_pre(MDOC_ARGS)
625 {
626 if (n->type != ROFFT_BODY)
627 return 1;
628
629 print_text(h, "\\(em");
630 /* Cannot use TAG_SPAN because it may contain blocks. */
631 print_otag(h, TAG_DIV, "cT", "Nd");
632 return 1;
633 }
634
635 static int
636 mdoc_nm_pre(MDOC_ARGS)
637 {
638 switch (n->type) {
639 case ROFFT_HEAD:
640 print_otag(h, TAG_TD, "");
641 /* FALLTHROUGH */
642 case ROFFT_ELEM:
643 print_otag(h, TAG_CODE, "cT", "Nm");
644 return 1;
645 case ROFFT_BODY:
646 print_otag(h, TAG_TD, "");
647 return 1;
648 default:
649 break;
650 }
651 synopsis_pre(h, n);
652 print_otag(h, TAG_TABLE, "c", "Nm");
653 print_otag(h, TAG_TR, "");
654 return 1;
655 }
656
657 static int
658 mdoc_xr_pre(MDOC_ARGS)
659 {
660 if (NULL == n->child)
661 return 0;
662
663 if (h->base_man1)
664 print_otag(h, TAG_A, "cThM", "Xr",
665 n->child->string, n->child->next == NULL ?
666 NULL : n->child->next->string);
667 else
668 print_otag(h, TAG_A, "cT", "Xr");
669
670 n = n->child;
671 print_text(h, n->string);
672
673 if (NULL == (n = n->next))
674 return 0;
675
676 h->flags |= HTML_NOSPACE;
677 print_text(h, "(");
678 h->flags |= HTML_NOSPACE;
679 print_text(h, n->string);
680 h->flags |= HTML_NOSPACE;
681 print_text(h, ")");
682 return 0;
683 }
684
685 static int
686 mdoc_ns_pre(MDOC_ARGS)
687 {
688
689 if ( ! (NODE_LINE & n->flags))
690 h->flags |= HTML_NOSPACE;
691 return 1;
692 }
693
694 static int
695 mdoc_ar_pre(MDOC_ARGS)
696 {
697 print_otag(h, TAG_VAR, "cT", "Ar");
698 return 1;
699 }
700
701 static int
702 mdoc_xx_pre(MDOC_ARGS)
703 {
704 print_otag(h, TAG_SPAN, "c", "Ux");
705 return 1;
706 }
707
708 static int
709 mdoc_it_pre(MDOC_ARGS)
710 {
711 const struct roff_node *bl;
712 struct tag *t;
713 enum mdoc_list type;
714
715 bl = n->parent;
716 while (bl->tok != MDOC_Bl)
717 bl = bl->parent;
718 type = bl->norm->Bl.type;
719
720 switch (type) {
721 case LIST_bullet:
722 case LIST_dash:
723 case LIST_hyphen:
724 case LIST_item:
725 case LIST_enum:
726 switch (n->type) {
727 case ROFFT_HEAD:
728 return 0;
729 case ROFFT_BODY:
730 print_otag(h, TAG_LI, "");
731 break;
732 default:
733 break;
734 }
735 break;
736 case LIST_diag:
737 case LIST_hang:
738 case LIST_inset:
739 case LIST_ohang:
740 switch (n->type) {
741 case ROFFT_HEAD:
742 print_otag(h, TAG_DT, "");
743 break;
744 case ROFFT_BODY:
745 print_otag(h, TAG_DD, "");
746 break;
747 default:
748 break;
749 }
750 break;
751 case LIST_tag:
752 switch (n->type) {
753 case ROFFT_HEAD:
754 if (h->style != NULL && !bl->norm->Bl.comp &&
755 (n->parent->prev == NULL ||
756 n->parent->prev->body == NULL ||
757 n->parent->prev->body->child != NULL)) {
758 t = print_otag(h, TAG_DT, "");
759 print_text(h, "\\ ");
760 print_tagq(h, t);
761 t = print_otag(h, TAG_DD, "");
762 print_text(h, "\\ ");
763 print_tagq(h, t);
764 }
765 print_otag(h, TAG_DT, "");
766 break;
767 case ROFFT_BODY:
768 if (n->child == NULL) {
769 print_otag(h, TAG_DD, "s", "width", "auto");
770 print_text(h, "\\ ");
771 } else
772 print_otag(h, TAG_DD, "");
773 break;
774 default:
775 break;
776 }
777 break;
778 case LIST_column:
779 switch (n->type) {
780 case ROFFT_HEAD:
781 break;
782 case ROFFT_BODY:
783 print_otag(h, TAG_TD, "");
784 break;
785 default:
786 print_otag(h, TAG_TR, "");
787 }
788 default:
789 break;
790 }
791
792 return 1;
793 }
794
795 static int
796 mdoc_bl_pre(MDOC_ARGS)
797 {
798 char cattr[28];
799 struct mdoc_bl *bl;
800 enum htmltag elemtype;
801
802 switch (n->type) {
803 case ROFFT_BODY:
804 return 1;
805 case ROFFT_HEAD:
806 return 0;
807 default:
808 break;
809 }
810
811 bl = &n->norm->Bl;
812 switch (bl->type) {
813 case LIST_bullet:
814 elemtype = TAG_UL;
815 (void)strlcpy(cattr, "Bl-bullet", sizeof(cattr));
816 break;
817 case LIST_dash:
818 case LIST_hyphen:
819 elemtype = TAG_UL;
820 (void)strlcpy(cattr, "Bl-dash", sizeof(cattr));
821 break;
822 case LIST_item:
823 elemtype = TAG_UL;
824 (void)strlcpy(cattr, "Bl-item", sizeof(cattr));
825 break;
826 case LIST_enum:
827 elemtype = TAG_OL;
828 (void)strlcpy(cattr, "Bl-enum", sizeof(cattr));
829 break;
830 case LIST_diag:
831 elemtype = TAG_DL;
832 (void)strlcpy(cattr, "Bl-diag", sizeof(cattr));
833 break;
834 case LIST_hang:
835 elemtype = TAG_DL;
836 (void)strlcpy(cattr, "Bl-hang", sizeof(cattr));
837 break;
838 case LIST_inset:
839 elemtype = TAG_DL;
840 (void)strlcpy(cattr, "Bl-inset", sizeof(cattr));
841 break;
842 case LIST_ohang:
843 elemtype = TAG_DL;
844 (void)strlcpy(cattr, "Bl-ohang", sizeof(cattr));
845 break;
846 case LIST_tag:
847 if (bl->offs)
848 print_otag(h, TAG_DIV, "c", "Bd-indent");
849 print_otag(h, TAG_DL, "c", bl->comp ?
850 "Bl-tag Bl-compact" : "Bl-tag");
851 return 1;
852 case LIST_column:
853 elemtype = TAG_TABLE;
854 (void)strlcpy(cattr, "Bl-column", sizeof(cattr));
855 break;
856 default:
857 abort();
858 }
859 if (bl->offs != NULL)
860 (void)strlcat(cattr, " Bd-indent", sizeof(cattr));
861 if (bl->comp)
862 (void)strlcat(cattr, " Bl-compact", sizeof(cattr));
863 print_otag(h, elemtype, "c", cattr);
864 return 1;
865 }
866
867 static int
868 mdoc_ex_pre(MDOC_ARGS)
869 {
870 if (n->prev)
871 print_otag(h, TAG_BR, "");
872 return 1;
873 }
874
875 static int
876 mdoc_st_pre(MDOC_ARGS)
877 {
878 print_otag(h, TAG_SPAN, "cT", "St");
879 return 1;
880 }
881
882 static int
883 mdoc_em_pre(MDOC_ARGS)
884 {
885 print_otag(h, TAG_I, "cT", "Em");
886 return 1;
887 }
888
889 static int
890 mdoc_d1_pre(MDOC_ARGS)
891 {
892 if (n->type != ROFFT_BLOCK)
893 return 1;
894
895 print_otag(h, TAG_DIV, "c", "Bd Bd-indent");
896
897 if (n->tok == MDOC_Dl)
898 print_otag(h, TAG_CODE, "c", "Li");
899
900 return 1;
901 }
902
903 static int
904 mdoc_sx_pre(MDOC_ARGS)
905 {
906 char *id;
907
908 id = html_make_id(n, 0);
909 print_otag(h, TAG_A, "cThR", "Sx", id);
910 free(id);
911 return 1;
912 }
913
914 static int
915 mdoc_bd_pre(MDOC_ARGS)
916 {
917 int comp, sv;
918 struct roff_node *nn;
919
920 if (n->type == ROFFT_HEAD)
921 return 0;
922
923 if (n->type == ROFFT_BLOCK) {
924 comp = n->norm->Bd.comp;
925 for (nn = n; nn && ! comp; nn = nn->parent) {
926 if (nn->type != ROFFT_BLOCK)
927 continue;
928 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
929 comp = 1;
930 if (nn->prev)
931 break;
932 }
933 if ( ! comp)
934 print_paragraph(h);
935 return 1;
936 }
937
938 /* Handle the -offset argument. */
939
940 if (n->norm->Bd.offs == NULL ||
941 ! strcmp(n->norm->Bd.offs, "left"))
942 print_otag(h, TAG_DIV, "c", "Bd");
943 else
944 print_otag(h, TAG_DIV, "c", "Bd Bd-indent");
945
946 if (n->norm->Bd.type != DISP_unfilled &&
947 n->norm->Bd.type != DISP_literal)
948 return 1;
949
950 print_otag(h, TAG_PRE, "c", "Li");
951
952 /* This can be recursive: save & set our literal state. */
953
954 sv = h->flags & HTML_LITERAL;
955 h->flags |= HTML_LITERAL;
956
957 for (nn = n->child; nn; nn = nn->next) {
958 print_mdoc_node(meta, nn, h);
959 /*
960 * If the printed node flushes its own line, then we
961 * needn't do it here as well. This is hacky, but the
962 * notion of selective eoln whitespace is pretty dumb
963 * anyway, so don't sweat it.
964 */
965 switch (nn->tok) {
966 case ROFF_br:
967 case ROFF_sp:
968 case MDOC_Sm:
969 case MDOC_Bl:
970 case MDOC_D1:
971 case MDOC_Dl:
972 case MDOC_Pp:
973 continue;
974 default:
975 break;
976 }
977 if (h->flags & HTML_NONEWLINE ||
978 (nn->next && ! (nn->next->flags & NODE_LINE)))
979 continue;
980 else if (nn->next)
981 print_text(h, "\n");
982
983 h->flags |= HTML_NOSPACE;
984 }
985
986 if (0 == sv)
987 h->flags &= ~HTML_LITERAL;
988
989 return 0;
990 }
991
992 static int
993 mdoc_pa_pre(MDOC_ARGS)
994 {
995 print_otag(h, TAG_SPAN, "cT", "Pa");
996 return 1;
997 }
998
999 static int
1000 mdoc_ad_pre(MDOC_ARGS)
1001 {
1002 print_otag(h, TAG_SPAN, "c", "Ad");
1003 return 1;
1004 }
1005
1006 static int
1007 mdoc_an_pre(MDOC_ARGS)
1008 {
1009 if (n->norm->An.auth == AUTH_split) {
1010 h->flags &= ~HTML_NOSPLIT;
1011 h->flags |= HTML_SPLIT;
1012 return 0;
1013 }
1014 if (n->norm->An.auth == AUTH_nosplit) {
1015 h->flags &= ~HTML_SPLIT;
1016 h->flags |= HTML_NOSPLIT;
1017 return 0;
1018 }
1019
1020 if (h->flags & HTML_SPLIT)
1021 print_otag(h, TAG_BR, "");
1022
1023 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1024 h->flags |= HTML_SPLIT;
1025
1026 print_otag(h, TAG_SPAN, "cT", "An");
1027 return 1;
1028 }
1029
1030 static int
1031 mdoc_cd_pre(MDOC_ARGS)
1032 {
1033 synopsis_pre(h, n);
1034 print_otag(h, TAG_CODE, "cT", "Cd");
1035 return 1;
1036 }
1037
1038 static int
1039 mdoc_dv_pre(MDOC_ARGS)
1040 {
1041 char *id;
1042
1043 if ((id = cond_id(n)) != NULL)
1044 print_otag(h, TAG_A, "chR", "permalink", id);
1045 print_otag(h, TAG_CODE, "cTi", "Dv", id);
1046 return 1;
1047 }
1048
1049 static int
1050 mdoc_ev_pre(MDOC_ARGS)
1051 {
1052 char *id;
1053
1054 if ((id = cond_id(n)) != NULL)
1055 print_otag(h, TAG_A, "chR", "permalink", id);
1056 print_otag(h, TAG_CODE, "cTi", "Ev", id);
1057 return 1;
1058 }
1059
1060 static int
1061 mdoc_er_pre(MDOC_ARGS)
1062 {
1063 char *id;
1064
1065 id = n->sec == SEC_ERRORS &&
1066 (n->parent->tok == MDOC_It ||
1067 (n->parent->tok == MDOC_Bq &&
1068 n->parent->parent->parent->tok == MDOC_It)) ?
1069 html_make_id(n, 1) : NULL;
1070
1071 if (id != NULL)
1072 print_otag(h, TAG_A, "chR", "permalink", id);
1073 print_otag(h, TAG_CODE, "cTi", "Er", id);
1074 return 1;
1075 }
1076
1077 static int
1078 mdoc_fa_pre(MDOC_ARGS)
1079 {
1080 const struct roff_node *nn;
1081 struct tag *t;
1082
1083 if (n->parent->tok != MDOC_Fo) {
1084 print_otag(h, TAG_VAR, "cT", "Fa");
1085 return 1;
1086 }
1087
1088 for (nn = n->child; nn; nn = nn->next) {
1089 t = print_otag(h, TAG_VAR, "cT", "Fa");
1090 print_text(h, nn->string);
1091 print_tagq(h, t);
1092 if (nn->next) {
1093 h->flags |= HTML_NOSPACE;
1094 print_text(h, ",");
1095 }
1096 }
1097
1098 if (n->child && n->next && n->next->tok == MDOC_Fa) {
1099 h->flags |= HTML_NOSPACE;
1100 print_text(h, ",");
1101 }
1102
1103 return 0;
1104 }
1105
1106 static int
1107 mdoc_fd_pre(MDOC_ARGS)
1108 {
1109 struct tag *t;
1110 char *buf, *cp;
1111
1112 synopsis_pre(h, n);
1113
1114 if (NULL == (n = n->child))
1115 return 0;
1116
1117 assert(n->type == ROFFT_TEXT);
1118
1119 if (strcmp(n->string, "#include")) {
1120 print_otag(h, TAG_CODE, "cT", "Fd");
1121 return 1;
1122 }
1123
1124 print_otag(h, TAG_CODE, "cT", "In");
1125 print_text(h, n->string);
1126
1127 if (NULL != (n = n->next)) {
1128 assert(n->type == ROFFT_TEXT);
1129
1130 if (h->base_includes) {
1131 cp = n->string;
1132 if (*cp == '<' || *cp == '"')
1133 cp++;
1134 buf = mandoc_strdup(cp);
1135 cp = strchr(buf, '\0') - 1;
1136 if (cp >= buf && (*cp == '>' || *cp == '"'))
1137 *cp = '\0';
1138 t = print_otag(h, TAG_A, "cThI", "In", buf);
1139 free(buf);
1140 } else
1141 t = print_otag(h, TAG_A, "cT", "In");
1142
1143 print_text(h, n->string);
1144 print_tagq(h, t);
1145
1146 n = n->next;
1147 }
1148
1149 for ( ; n; n = n->next) {
1150 assert(n->type == ROFFT_TEXT);
1151 print_text(h, n->string);
1152 }
1153
1154 return 0;
1155 }
1156
1157 static int
1158 mdoc_vt_pre(MDOC_ARGS)
1159 {
1160 if (n->type == ROFFT_BLOCK) {
1161 synopsis_pre(h, n);
1162 return 1;
1163 } else if (n->type == ROFFT_ELEM) {
1164 synopsis_pre(h, n);
1165 } else if (n->type == ROFFT_HEAD)
1166 return 0;
1167
1168 print_otag(h, TAG_VAR, "cT", "Vt");
1169 return 1;
1170 }
1171
1172 static int
1173 mdoc_ft_pre(MDOC_ARGS)
1174 {
1175 synopsis_pre(h, n);
1176 print_otag(h, TAG_VAR, "cT", "Ft");
1177 return 1;
1178 }
1179
1180 static int
1181 mdoc_fn_pre(MDOC_ARGS)
1182 {
1183 struct tag *t;
1184 char nbuf[BUFSIZ];
1185 const char *sp, *ep;
1186 int sz, pretty;
1187
1188 pretty = NODE_SYNPRETTY & n->flags;
1189 synopsis_pre(h, n);
1190
1191 /* Split apart into type and name. */
1192 assert(n->child->string);
1193 sp = n->child->string;
1194
1195 ep = strchr(sp, ' ');
1196 if (NULL != ep) {
1197 t = print_otag(h, TAG_VAR, "cT", "Ft");
1198
1199 while (ep) {
1200 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1201 (void)memcpy(nbuf, sp, (size_t)sz);
1202 nbuf[sz] = '\0';
1203 print_text(h, nbuf);
1204 sp = ++ep;
1205 ep = strchr(sp, ' ');
1206 }
1207 print_tagq(h, t);
1208 }
1209
1210 t = print_otag(h, TAG_CODE, "cT", "Fn");
1211
1212 if (sp)
1213 print_text(h, sp);
1214
1215 print_tagq(h, t);
1216
1217 h->flags |= HTML_NOSPACE;
1218 print_text(h, "(");
1219 h->flags |= HTML_NOSPACE;
1220
1221 for (n = n->child->next; n; n = n->next) {
1222 if (NODE_SYNPRETTY & n->flags)
1223 t = print_otag(h, TAG_VAR, "cTs", "Fa",
1224 "white-space", "nowrap");
1225 else
1226 t = print_otag(h, TAG_VAR, "cT", "Fa");
1227 print_text(h, n->string);
1228 print_tagq(h, t);
1229 if (n->next) {
1230 h->flags |= HTML_NOSPACE;
1231 print_text(h, ",");
1232 }
1233 }
1234
1235 h->flags |= HTML_NOSPACE;
1236 print_text(h, ")");
1237
1238 if (pretty) {
1239 h->flags |= HTML_NOSPACE;
1240 print_text(h, ";");
1241 }
1242
1243 return 0;
1244 }
1245
1246 static int
1247 mdoc_sm_pre(MDOC_ARGS)
1248 {
1249
1250 if (NULL == n->child)
1251 h->flags ^= HTML_NONOSPACE;
1252 else if (0 == strcmp("on", n->child->string))
1253 h->flags &= ~HTML_NONOSPACE;
1254 else
1255 h->flags |= HTML_NONOSPACE;
1256
1257 if ( ! (HTML_NONOSPACE & h->flags))
1258 h->flags &= ~HTML_NOSPACE;
1259
1260 return 0;
1261 }
1262
1263 static int
1264 mdoc_skip_pre(MDOC_ARGS)
1265 {
1266
1267 return 0;
1268 }
1269
1270 static int
1271 mdoc_pp_pre(MDOC_ARGS)
1272 {
1273
1274 print_paragraph(h);
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, "cTh", "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, "cTh", "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, "cT", "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, "cT", "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, "cThI", "In", n->string);
1397 else
1398 t = print_otag(h, TAG_A, "cT", "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, "cTi", "Ic", id);
1424 return 1;
1425 }
1426
1427 static int
1428 mdoc_va_pre(MDOC_ARGS)
1429 {
1430 print_otag(h, TAG_VAR, "cT", "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 if (n->type == ROFFT_HEAD)
1450 return 0;
1451 else if (n->type != ROFFT_BODY)
1452 return 1;
1453
1454 if (FONT_Em == n->norm->Bf.font)
1455 cattr = "Bf Em";
1456 else if (FONT_Sy == n->norm->Bf.font)
1457 cattr = "Bf Sy";
1458 else if (FONT_Li == n->norm->Bf.font)
1459 cattr = "Bf Li";
1460 else
1461 cattr = "Bf No";
1462
1463 /* Cannot use TAG_SPAN because it may contain blocks. */
1464 print_otag(h, TAG_DIV, "c", cattr);
1465 return 1;
1466 }
1467
1468 static int
1469 mdoc_ms_pre(MDOC_ARGS)
1470 {
1471 char *id;
1472
1473 if ((id = cond_id(n)) != NULL)
1474 print_otag(h, TAG_A, "chR", "permalink", id);
1475 print_otag(h, TAG_SPAN, "cTi", "Ms", id);
1476 return 1;
1477 }
1478
1479 static int
1480 mdoc_igndelim_pre(MDOC_ARGS)
1481 {
1482
1483 h->flags |= HTML_IGNDELIM;
1484 return 1;
1485 }
1486
1487 static void
1488 mdoc_pf_post(MDOC_ARGS)
1489 {
1490
1491 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1492 h->flags |= HTML_NOSPACE;
1493 }
1494
1495 static int
1496 mdoc_rs_pre(MDOC_ARGS)
1497 {
1498 if (n->type != ROFFT_BLOCK)
1499 return 1;
1500
1501 if (n->prev && SEC_SEE_ALSO == n->sec)
1502 print_paragraph(h);
1503
1504 print_otag(h, TAG_CITE, "cT", "Rs");
1505 return 1;
1506 }
1507
1508 static int
1509 mdoc_no_pre(MDOC_ARGS)
1510 {
1511 char *id;
1512
1513 if ((id = cond_id(n)) != NULL)
1514 print_otag(h, TAG_A, "chR", "permalink", id);
1515 print_otag(h, TAG_SPAN, "ci", "No", id);
1516 return 1;
1517 }
1518
1519 static int
1520 mdoc_li_pre(MDOC_ARGS)
1521 {
1522 char *id;
1523
1524 if ((id = cond_id(n)) != NULL)
1525 print_otag(h, TAG_A, "chR", "permalink", id);
1526 print_otag(h, TAG_CODE, "ci", "Li", id);
1527 return 1;
1528 }
1529
1530 static int
1531 mdoc_sy_pre(MDOC_ARGS)
1532 {
1533 print_otag(h, TAG_B, "cT", "Sy");
1534 return 1;
1535 }
1536
1537 static int
1538 mdoc_lb_pre(MDOC_ARGS)
1539 {
1540 if (SEC_LIBRARY == n->sec && NODE_LINE & n->flags && n->prev)
1541 print_otag(h, TAG_BR, "");
1542
1543 print_otag(h, TAG_SPAN, "cT", "Lb");
1544 return 1;
1545 }
1546
1547 static int
1548 mdoc__x_pre(MDOC_ARGS)
1549 {
1550 const char *cattr;
1551 enum htmltag t;
1552
1553 t = TAG_SPAN;
1554
1555 switch (n->tok) {
1556 case MDOC__A:
1557 cattr = "RsA";
1558 if (n->prev && MDOC__A == n->prev->tok)
1559 if (NULL == n->next || MDOC__A != n->next->tok)
1560 print_text(h, "and");
1561 break;
1562 case MDOC__B:
1563 t = TAG_I;
1564 cattr = "RsB";
1565 break;
1566 case MDOC__C:
1567 cattr = "RsC";
1568 break;
1569 case MDOC__D:
1570 cattr = "RsD";
1571 break;
1572 case MDOC__I:
1573 t = TAG_I;
1574 cattr = "RsI";
1575 break;
1576 case MDOC__J:
1577 t = TAG_I;
1578 cattr = "RsJ";
1579 break;
1580 case MDOC__N:
1581 cattr = "RsN";
1582 break;
1583 case MDOC__O:
1584 cattr = "RsO";
1585 break;
1586 case MDOC__P:
1587 cattr = "RsP";
1588 break;
1589 case MDOC__Q:
1590 cattr = "RsQ";
1591 break;
1592 case MDOC__R:
1593 cattr = "RsR";
1594 break;
1595 case MDOC__T:
1596 cattr = "RsT";
1597 break;
1598 case MDOC__U:
1599 print_otag(h, TAG_A, "ch", "RsU", n->child->string);
1600 return 1;
1601 case MDOC__V:
1602 cattr = "RsV";
1603 break;
1604 default:
1605 abort();
1606 }
1607
1608 print_otag(h, t, "c", cattr);
1609 return 1;
1610 }
1611
1612 static void
1613 mdoc__x_post(MDOC_ARGS)
1614 {
1615
1616 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
1617 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
1618 if (NULL == n->prev || MDOC__A != n->prev->tok)
1619 return;
1620
1621 /* TODO: %U */
1622
1623 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
1624 return;
1625
1626 h->flags |= HTML_NOSPACE;
1627 print_text(h, n->next ? "," : ".");
1628 }
1629
1630 static int
1631 mdoc_bk_pre(MDOC_ARGS)
1632 {
1633
1634 switch (n->type) {
1635 case ROFFT_BLOCK:
1636 break;
1637 case ROFFT_HEAD:
1638 return 0;
1639 case ROFFT_BODY:
1640 if (n->parent->args != NULL || n->prev->child == NULL)
1641 h->flags |= HTML_PREKEEP;
1642 break;
1643 default:
1644 abort();
1645 }
1646
1647 return 1;
1648 }
1649
1650 static void
1651 mdoc_bk_post(MDOC_ARGS)
1652 {
1653
1654 if (n->type == ROFFT_BODY)
1655 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
1656 }
1657
1658 static int
1659 mdoc_quote_pre(MDOC_ARGS)
1660 {
1661 if (n->type != ROFFT_BODY)
1662 return 1;
1663
1664 switch (n->tok) {
1665 case MDOC_Ao:
1666 case MDOC_Aq:
1667 print_text(h, n->child != NULL && n->child->next == NULL &&
1668 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1669 break;
1670 case MDOC_Bro:
1671 case MDOC_Brq:
1672 print_text(h, "\\(lC");
1673 break;
1674 case MDOC_Bo:
1675 case MDOC_Bq:
1676 print_text(h, "\\(lB");
1677 break;
1678 case MDOC_Oo:
1679 case MDOC_Op:
1680 print_text(h, "\\(lB");
1681 h->flags |= HTML_NOSPACE;
1682 /* Cannot use TAG_SPAN because it may contain blocks. */
1683 print_otag(h, TAG_IDIV, "c", "Op");
1684 break;
1685 case MDOC_En:
1686 if (NULL == n->norm->Es ||
1687 NULL == n->norm->Es->child)
1688 return 1;
1689 print_text(h, n->norm->Es->child->string);
1690 break;
1691 case MDOC_Do:
1692 case MDOC_Dq:
1693 case MDOC_Qo:
1694 case MDOC_Qq:
1695 print_text(h, "\\(lq");
1696 break;
1697 case MDOC_Po:
1698 case MDOC_Pq:
1699 print_text(h, "(");
1700 break;
1701 case MDOC_Ql:
1702 print_text(h, "\\(oq");
1703 h->flags |= HTML_NOSPACE;
1704 print_otag(h, TAG_CODE, "c", "Li");
1705 break;
1706 case MDOC_So:
1707 case MDOC_Sq:
1708 print_text(h, "\\(oq");
1709 break;
1710 default:
1711 abort();
1712 }
1713
1714 h->flags |= HTML_NOSPACE;
1715 return 1;
1716 }
1717
1718 static void
1719 mdoc_quote_post(MDOC_ARGS)
1720 {
1721
1722 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1723 return;
1724
1725 h->flags |= HTML_NOSPACE;
1726
1727 switch (n->tok) {
1728 case MDOC_Ao:
1729 case MDOC_Aq:
1730 print_text(h, n->child != NULL && n->child->next == NULL &&
1731 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1732 break;
1733 case MDOC_Bro:
1734 case MDOC_Brq:
1735 print_text(h, "\\(rC");
1736 break;
1737 case MDOC_Oo:
1738 case MDOC_Op:
1739 case MDOC_Bo:
1740 case MDOC_Bq:
1741 print_text(h, "\\(rB");
1742 break;
1743 case MDOC_En:
1744 if (n->norm->Es == NULL ||
1745 n->norm->Es->child == NULL ||
1746 n->norm->Es->child->next == NULL)
1747 h->flags &= ~HTML_NOSPACE;
1748 else
1749 print_text(h, n->norm->Es->child->next->string);
1750 break;
1751 case MDOC_Qo:
1752 case MDOC_Qq:
1753 case MDOC_Do:
1754 case MDOC_Dq:
1755 print_text(h, "\\(rq");
1756 break;
1757 case MDOC_Po:
1758 case MDOC_Pq:
1759 print_text(h, ")");
1760 break;
1761 case MDOC_Ql:
1762 case MDOC_So:
1763 case MDOC_Sq:
1764 print_text(h, "\\(cq");
1765 break;
1766 default:
1767 abort();
1768 }
1769 }
1770
1771 static int
1772 mdoc_eo_pre(MDOC_ARGS)
1773 {
1774
1775 if (n->type != ROFFT_BODY)
1776 return 1;
1777
1778 if (n->end == ENDBODY_NOT &&
1779 n->parent->head->child == NULL &&
1780 n->child != NULL &&
1781 n->child->end != ENDBODY_NOT)
1782 print_text(h, "\\&");
1783 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1784 n->parent->head->child != NULL && (n->child != NULL ||
1785 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1786 h->flags |= HTML_NOSPACE;
1787 return 1;
1788 }
1789
1790 static void
1791 mdoc_eo_post(MDOC_ARGS)
1792 {
1793 int body, tail;
1794
1795 if (n->type != ROFFT_BODY)
1796 return;
1797
1798 if (n->end != ENDBODY_NOT) {
1799 h->flags &= ~HTML_NOSPACE;
1800 return;
1801 }
1802
1803 body = n->child != NULL || n->parent->head->child != NULL;
1804 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1805
1806 if (body && tail)
1807 h->flags |= HTML_NOSPACE;
1808 else if ( ! tail)
1809 h->flags &= ~HTML_NOSPACE;
1810 }
1811
1812 static int
1813 mdoc_abort_pre(MDOC_ARGS)
1814 {
1815 abort();
1816 }