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