]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
Place mandoc.css into the public domain.
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.315 2018/10/04 13:23:04 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_ad_pre(MDOC_ARGS);
66 static int mdoc_an_pre(MDOC_ARGS);
67 static int mdoc_ap_pre(MDOC_ARGS);
68 static int mdoc_ar_pre(MDOC_ARGS);
69 static int mdoc_bd_pre(MDOC_ARGS);
70 static int mdoc_bf_pre(MDOC_ARGS);
71 static void mdoc_bk_post(MDOC_ARGS);
72 static int mdoc_bk_pre(MDOC_ARGS);
73 static int mdoc_bl_pre(MDOC_ARGS);
74 static int mdoc_cd_pre(MDOC_ARGS);
75 static int mdoc_cm_pre(MDOC_ARGS);
76 static int mdoc_d1_pre(MDOC_ARGS);
77 static int mdoc_dv_pre(MDOC_ARGS);
78 static int mdoc_fa_pre(MDOC_ARGS);
79 static int mdoc_fd_pre(MDOC_ARGS);
80 static int mdoc_fl_pre(MDOC_ARGS);
81 static int mdoc_fn_pre(MDOC_ARGS);
82 static int mdoc_ft_pre(MDOC_ARGS);
83 static int mdoc_em_pre(MDOC_ARGS);
84 static void mdoc_eo_post(MDOC_ARGS);
85 static int mdoc_eo_pre(MDOC_ARGS);
86 static int mdoc_er_pre(MDOC_ARGS);
87 static int mdoc_ev_pre(MDOC_ARGS);
88 static int mdoc_ex_pre(MDOC_ARGS);
89 static void mdoc_fo_post(MDOC_ARGS);
90 static int mdoc_fo_pre(MDOC_ARGS);
91 static int mdoc_ic_pre(MDOC_ARGS);
92 static int mdoc_igndelim_pre(MDOC_ARGS);
93 static int mdoc_in_pre(MDOC_ARGS);
94 static int mdoc_it_pre(MDOC_ARGS);
95 static int mdoc_lb_pre(MDOC_ARGS);
96 static int mdoc_li_pre(MDOC_ARGS);
97 static int mdoc_lk_pre(MDOC_ARGS);
98 static int mdoc_mt_pre(MDOC_ARGS);
99 static int mdoc_ms_pre(MDOC_ARGS);
100 static int mdoc_nd_pre(MDOC_ARGS);
101 static int mdoc_nm_pre(MDOC_ARGS);
102 static int mdoc_no_pre(MDOC_ARGS);
103 static int mdoc_ns_pre(MDOC_ARGS);
104 static int mdoc_pa_pre(MDOC_ARGS);
105 static void mdoc_pf_post(MDOC_ARGS);
106 static int mdoc_pp_pre(MDOC_ARGS);
107 static void mdoc_quote_post(MDOC_ARGS);
108 static int mdoc_quote_pre(MDOC_ARGS);
109 static int mdoc_rs_pre(MDOC_ARGS);
110 static int mdoc_sh_pre(MDOC_ARGS);
111 static int mdoc_skip_pre(MDOC_ARGS);
112 static int mdoc_sm_pre(MDOC_ARGS);
113 static int mdoc_ss_pre(MDOC_ARGS);
114 static int mdoc_st_pre(MDOC_ARGS);
115 static int mdoc_sx_pre(MDOC_ARGS);
116 static int mdoc_sy_pre(MDOC_ARGS);
117 static int mdoc_va_pre(MDOC_ARGS);
118 static int mdoc_vt_pre(MDOC_ARGS);
119 static int mdoc_xr_pre(MDOC_ARGS);
120 static int mdoc_xx_pre(MDOC_ARGS);
121
122 static const struct mdoc_html_act mdoc_html_acts[MDOC_MAX - MDOC_Dd] = {
123 {NULL, NULL}, /* Dd */
124 {NULL, NULL}, /* Dt */
125 {NULL, NULL}, /* Os */
126 {mdoc_sh_pre, NULL }, /* Sh */
127 {mdoc_ss_pre, NULL }, /* Ss */
128 {mdoc_pp_pre, NULL}, /* Pp */
129 {mdoc_d1_pre, NULL}, /* D1 */
130 {mdoc_d1_pre, NULL}, /* Dl */
131 {mdoc_bd_pre, NULL}, /* Bd */
132 {NULL, NULL}, /* Ed */
133 {mdoc_bl_pre, NULL}, /* Bl */
134 {NULL, NULL}, /* El */
135 {mdoc_it_pre, NULL}, /* It */
136 {mdoc_ad_pre, NULL}, /* Ad */
137 {mdoc_an_pre, NULL}, /* An */
138 {mdoc_ap_pre, NULL}, /* Ap */
139 {mdoc_ar_pre, NULL}, /* Ar */
140 {mdoc_cd_pre, NULL}, /* Cd */
141 {mdoc_cm_pre, NULL}, /* Cm */
142 {mdoc_dv_pre, NULL}, /* Dv */
143 {mdoc_er_pre, NULL}, /* Er */
144 {mdoc_ev_pre, NULL}, /* Ev */
145 {mdoc_ex_pre, NULL}, /* Ex */
146 {mdoc_fa_pre, NULL}, /* Fa */
147 {mdoc_fd_pre, NULL}, /* Fd */
148 {mdoc_fl_pre, NULL}, /* Fl */
149 {mdoc_fn_pre, NULL}, /* Fn */
150 {mdoc_ft_pre, NULL}, /* Ft */
151 {mdoc_ic_pre, NULL}, /* Ic */
152 {mdoc_in_pre, NULL}, /* In */
153 {mdoc_li_pre, NULL}, /* Li */
154 {mdoc_nd_pre, NULL}, /* Nd */
155 {mdoc_nm_pre, NULL}, /* Nm */
156 {mdoc_quote_pre, mdoc_quote_post}, /* Op */
157 {mdoc_ft_pre, NULL}, /* Ot */
158 {mdoc_pa_pre, NULL}, /* Pa */
159 {mdoc_ex_pre, NULL}, /* Rv */
160 {mdoc_st_pre, NULL}, /* St */
161 {mdoc_va_pre, NULL}, /* Va */
162 {mdoc_vt_pre, NULL}, /* Vt */
163 {mdoc_xr_pre, NULL}, /* Xr */
164 {mdoc__x_pre, mdoc__x_post}, /* %A */
165 {mdoc__x_pre, mdoc__x_post}, /* %B */
166 {mdoc__x_pre, mdoc__x_post}, /* %D */
167 {mdoc__x_pre, mdoc__x_post}, /* %I */
168 {mdoc__x_pre, mdoc__x_post}, /* %J */
169 {mdoc__x_pre, mdoc__x_post}, /* %N */
170 {mdoc__x_pre, mdoc__x_post}, /* %O */
171 {mdoc__x_pre, mdoc__x_post}, /* %P */
172 {mdoc__x_pre, mdoc__x_post}, /* %R */
173 {mdoc__x_pre, mdoc__x_post}, /* %T */
174 {mdoc__x_pre, mdoc__x_post}, /* %V */
175 {NULL, NULL}, /* Ac */
176 {mdoc_quote_pre, mdoc_quote_post}, /* Ao */
177 {mdoc_quote_pre, mdoc_quote_post}, /* Aq */
178 {mdoc_xx_pre, NULL}, /* At */
179 {NULL, NULL}, /* Bc */
180 {mdoc_bf_pre, NULL}, /* Bf */
181 {mdoc_quote_pre, mdoc_quote_post}, /* Bo */
182 {mdoc_quote_pre, mdoc_quote_post}, /* Bq */
183 {mdoc_xx_pre, NULL}, /* Bsx */
184 {mdoc_xx_pre, NULL}, /* Bx */
185 {mdoc_skip_pre, NULL}, /* Db */
186 {NULL, NULL}, /* Dc */
187 {mdoc_quote_pre, mdoc_quote_post}, /* Do */
188 {mdoc_quote_pre, mdoc_quote_post}, /* Dq */
189 {NULL, NULL}, /* Ec */ /* FIXME: no space */
190 {NULL, NULL}, /* Ef */
191 {mdoc_em_pre, NULL}, /* Em */
192 {mdoc_eo_pre, mdoc_eo_post}, /* Eo */
193 {mdoc_xx_pre, NULL}, /* Fx */
194 {mdoc_ms_pre, NULL}, /* Ms */
195 {mdoc_no_pre, NULL}, /* No */
196 {mdoc_ns_pre, NULL}, /* Ns */
197 {mdoc_xx_pre, NULL}, /* Nx */
198 {mdoc_xx_pre, NULL}, /* Ox */
199 {NULL, NULL}, /* Pc */
200 {mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
201 {mdoc_quote_pre, mdoc_quote_post}, /* Po */
202 {mdoc_quote_pre, mdoc_quote_post}, /* Pq */
203 {NULL, NULL}, /* Qc */
204 {mdoc_quote_pre, mdoc_quote_post}, /* Ql */
205 {mdoc_quote_pre, mdoc_quote_post}, /* Qo */
206 {mdoc_quote_pre, mdoc_quote_post}, /* Qq */
207 {NULL, NULL}, /* Re */
208 {mdoc_rs_pre, NULL}, /* Rs */
209 {NULL, NULL}, /* Sc */
210 {mdoc_quote_pre, mdoc_quote_post}, /* So */
211 {mdoc_quote_pre, mdoc_quote_post}, /* Sq */
212 {mdoc_sm_pre, NULL}, /* Sm */
213 {mdoc_sx_pre, NULL}, /* Sx */
214 {mdoc_sy_pre, NULL}, /* Sy */
215 {NULL, NULL}, /* Tn */
216 {mdoc_xx_pre, NULL}, /* Ux */
217 {NULL, NULL}, /* Xc */
218 {NULL, NULL}, /* Xo */
219 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
220 {NULL, NULL}, /* Fc */
221 {mdoc_quote_pre, mdoc_quote_post}, /* Oo */
222 {NULL, NULL}, /* Oc */
223 {mdoc_bk_pre, mdoc_bk_post}, /* Bk */
224 {NULL, NULL}, /* Ek */
225 {NULL, NULL}, /* Bt */
226 {NULL, NULL}, /* Hf */
227 {mdoc_em_pre, NULL}, /* Fr */
228 {NULL, NULL}, /* Ud */
229 {mdoc_lb_pre, NULL}, /* Lb */
230 {mdoc_pp_pre, NULL}, /* Lp */
231 {mdoc_lk_pre, NULL}, /* Lk */
232 {mdoc_mt_pre, NULL}, /* Mt */
233 {mdoc_quote_pre, mdoc_quote_post}, /* Brq */
234 {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
235 {NULL, NULL}, /* Brc */
236 {mdoc__x_pre, mdoc__x_post}, /* %C */
237 {mdoc_skip_pre, NULL}, /* Es */
238 {mdoc_quote_pre, mdoc_quote_post}, /* En */
239 {mdoc_xx_pre, NULL}, /* Dx */
240 {mdoc__x_pre, mdoc__x_post}, /* %Q */
241 {mdoc__x_pre, mdoc__x_post}, /* %U */
242 {NULL, NULL}, /* Ta */
243 };
244
245
246 /*
247 * See the same function in mdoc_term.c for documentation.
248 */
249 static void
250 synopsis_pre(struct html *h, const struct roff_node *n)
251 {
252
253 if (NULL == n->prev || ! (NODE_SYNPRETTY & n->flags))
254 return;
255
256 if (n->prev->tok == n->tok &&
257 MDOC_Fo != n->tok &&
258 MDOC_Ft != n->tok &&
259 MDOC_Fn != n->tok) {
260 print_otag(h, TAG_BR, "");
261 return;
262 }
263
264 switch (n->prev->tok) {
265 case MDOC_Fd:
266 case MDOC_Fn:
267 case MDOC_Fo:
268 case MDOC_In:
269 case MDOC_Vt:
270 print_paragraph(h);
271 break;
272 case MDOC_Ft:
273 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
274 print_paragraph(h);
275 break;
276 }
277 /* FALLTHROUGH */
278 default:
279 print_otag(h, TAG_BR, "");
280 break;
281 }
282 }
283
284 void
285 html_mdoc(void *arg, const struct roff_man *mdoc)
286 {
287 struct html *h;
288 struct roff_node *n;
289 struct tag *t;
290
291 h = (struct html *)arg;
292 n = mdoc->first->child;
293
294 if ((h->oflags & HTML_FRAGMENT) == 0) {
295 print_gen_decls(h);
296 print_otag(h, TAG_HTML, "");
297 if (n->type == ROFFT_COMMENT)
298 print_gen_comment(h, n);
299 t = print_otag(h, TAG_HEAD, "");
300 print_mdoc_head(&mdoc->meta, h);
301 print_tagq(h, t);
302 print_otag(h, TAG_BODY, "");
303 }
304
305 mdoc_root_pre(&mdoc->meta, h);
306 t = print_otag(h, TAG_DIV, "c", "manual-text");
307 print_mdoc_nodelist(&mdoc->meta, n, h);
308 print_tagq(h, t);
309 mdoc_root_post(&mdoc->meta, 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 int child;
349 struct tag *t;
350
351 if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
352 return;
353
354 child = 1;
355 t = h->tag;
356 n->flags &= ~NODE_ENDED;
357
358 switch (n->type) {
359 case ROFFT_TEXT:
360 /* No tables in this mode... */
361 assert(NULL == h->tblt);
362
363 /*
364 * Make sure that if we're in a literal mode already
365 * (i.e., within a <PRE>) don't print the newline.
366 */
367 if (*n->string == ' ' && n->flags & NODE_LINE &&
368 (h->flags & (HTML_LITERAL | HTML_NONEWLINE)) == 0)
369 print_otag(h, TAG_BR, "");
370 if (NODE_DELIMC & n->flags)
371 h->flags |= HTML_NOSPACE;
372 print_text(h, n->string);
373 if (NODE_DELIMO & n->flags)
374 h->flags |= HTML_NOSPACE;
375 return;
376 case ROFFT_EQN:
377 print_eqn(h, n->eqn);
378 break;
379 case ROFFT_TBL:
380 /*
381 * This will take care of initialising all of the table
382 * state data for the first table, then tearing it down
383 * for the last one.
384 */
385 print_tbl(h, n->span);
386 return;
387 default:
388 /*
389 * Close out the current table, if it's open, and unset
390 * the "meta" table state. This will be reopened on the
391 * next table element.
392 */
393 if (h->tblt != NULL) {
394 print_tblclose(h);
395 t = h->tag;
396 }
397 assert(h->tblt == NULL);
398 if (n->tok < ROFF_MAX) {
399 roff_html_pre(h, n);
400 child = 0;
401 break;
402 }
403 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
404 if (mdoc_html_acts[n->tok - MDOC_Dd].pre != NULL &&
405 (n->end == ENDBODY_NOT || n->child != NULL))
406 child = (*mdoc_html_acts[n->tok - MDOC_Dd].pre)(meta,
407 n, h);
408 break;
409 }
410
411 if (h->flags & HTML_KEEP && n->flags & NODE_LINE) {
412 h->flags &= ~HTML_KEEP;
413 h->flags |= HTML_PREKEEP;
414 }
415
416 if (child && n->child)
417 print_mdoc_nodelist(meta, n->child, h);
418
419 print_stagq(h, t);
420
421 switch (n->type) {
422 case ROFFT_EQN:
423 break;
424 default:
425 if (n->tok < ROFF_MAX ||
426 mdoc_html_acts[n->tok - MDOC_Dd].post == NULL ||
427 n->flags & NODE_ENDED)
428 break;
429 (*mdoc_html_acts[n->tok - MDOC_Dd].post)(meta, n, h);
430 if (n->end != ENDBODY_NOT)
431 n->body->flags |= NODE_ENDED;
432 break;
433 }
434 }
435
436 static void
437 mdoc_root_post(const struct roff_meta *meta, struct html *h)
438 {
439 struct tag *t, *tt;
440
441 t = print_otag(h, TAG_TABLE, "c", "foot");
442 tt = print_otag(h, TAG_TR, "");
443
444 print_otag(h, TAG_TD, "c", "foot-date");
445 print_text(h, meta->date);
446 print_stagq(h, tt);
447
448 print_otag(h, TAG_TD, "c", "foot-os");
449 print_text(h, meta->os);
450 print_tagq(h, t);
451 }
452
453 static int
454 mdoc_root_pre(const struct roff_meta *meta, struct html *h)
455 {
456 struct tag *t, *tt;
457 char *volume, *title;
458
459 if (NULL == meta->arch)
460 volume = mandoc_strdup(meta->vol);
461 else
462 mandoc_asprintf(&volume, "%s (%s)",
463 meta->vol, meta->arch);
464
465 if (NULL == meta->msec)
466 title = mandoc_strdup(meta->title);
467 else
468 mandoc_asprintf(&title, "%s(%s)",
469 meta->title, meta->msec);
470
471 t = print_otag(h, TAG_TABLE, "c", "head");
472 tt = print_otag(h, TAG_TR, "");
473
474 print_otag(h, TAG_TD, "c", "head-ltitle");
475 print_text(h, title);
476 print_stagq(h, tt);
477
478 print_otag(h, TAG_TD, "c", "head-vol");
479 print_text(h, volume);
480 print_stagq(h, tt);
481
482 print_otag(h, TAG_TD, "c", "head-rtitle");
483 print_text(h, title);
484 print_tagq(h, t);
485
486 free(title);
487 free(volume);
488 return 1;
489 }
490
491 static char *
492 cond_id(const struct roff_node *n)
493 {
494 if (n->child != NULL &&
495 n->child->type == ROFFT_TEXT &&
496 (n->prev == NULL ||
497 (n->prev->type == ROFFT_TEXT &&
498 strcmp(n->prev->string, "|") == 0)) &&
499 (n->parent->tok == MDOC_It ||
500 (n->parent->tok == MDOC_Xo &&
501 n->parent->parent->prev == NULL &&
502 n->parent->parent->parent->tok == MDOC_It)))
503 return html_make_id(n, 1);
504 return NULL;
505 }
506
507 static int
508 mdoc_sh_pre(MDOC_ARGS)
509 {
510 struct roff_node *sn, *subn;
511 struct tag *t, *tsec, *tsub;
512 char *id;
513 int sc;
514
515 switch (n->type) {
516 case ROFFT_BLOCK:
517 if ((h->oflags & HTML_TOC) == 0 ||
518 h->flags & HTML_TOCDONE ||
519 n->sec <= SEC_SYNOPSIS)
520 break;
521 h->flags |= HTML_TOCDONE;
522 sc = 0;
523 for (sn = n->next; sn != NULL; sn = sn->next)
524 if (sn->sec == SEC_CUSTOM)
525 if (++sc == 2)
526 break;
527 if (sc < 2)
528 break;
529 t = print_otag(h, TAG_H1, "c", "Sh");
530 print_text(h, "TABLE OF CONTENTS");
531 print_tagq(h, t);
532 t = print_otag(h, TAG_UL, "c", "Bl-compact");
533 for (sn = n; sn != NULL; sn = sn->next) {
534 tsec = print_otag(h, TAG_LI, "");
535 id = html_make_id(sn->head, 0);
536 print_otag(h, TAG_A, "hR", id);
537 free(id);
538 print_mdoc_nodelist(meta, sn->head->child, h);
539 tsub = NULL;
540 for (subn = sn->body->child; subn != NULL;
541 subn = subn->next) {
542 if (subn->tok != MDOC_Ss)
543 continue;
544 id = html_make_id(subn->head, 0);
545 if (id == NULL)
546 continue;
547 if (tsub == NULL)
548 print_otag(h, TAG_UL,
549 "c", "Bl-compact");
550 tsub = print_otag(h, TAG_LI, "");
551 print_otag(h, TAG_A, "hR", id);
552 free(id);
553 print_mdoc_nodelist(meta,
554 subn->head->child, h);
555 print_tagq(h, tsub);
556 }
557 print_tagq(h, tsec);
558 }
559 print_tagq(h, t);
560 break;
561 case ROFFT_HEAD:
562 id = html_make_id(n, 1);
563 print_otag(h, TAG_H1, "cTi", "Sh", id);
564 if (id != NULL)
565 print_otag(h, TAG_A, "chR", "permalink", id);
566 break;
567 case ROFFT_BODY:
568 if (n->sec == SEC_AUTHORS)
569 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
570 break;
571 default:
572 break;
573 }
574 return 1;
575 }
576
577 static int
578 mdoc_ss_pre(MDOC_ARGS)
579 {
580 char *id;
581
582 if (n->type != ROFFT_HEAD)
583 return 1;
584
585 id = html_make_id(n, 1);
586 print_otag(h, TAG_H2, "cTi", "Ss", id);
587 if (id != NULL)
588 print_otag(h, TAG_A, "chR", "permalink", id);
589 return 1;
590 }
591
592 static int
593 mdoc_fl_pre(MDOC_ARGS)
594 {
595 char *id;
596
597 if ((id = cond_id(n)) != NULL)
598 print_otag(h, TAG_A, "chR", "permalink", id);
599 print_otag(h, TAG_CODE, "cTi", "Fl", id);
600
601 print_text(h, "\\-");
602 if (!(n->child == NULL &&
603 (n->next == NULL ||
604 n->next->type == ROFFT_TEXT ||
605 n->next->flags & NODE_LINE)))
606 h->flags |= HTML_NOSPACE;
607
608 return 1;
609 }
610
611 static int
612 mdoc_cm_pre(MDOC_ARGS)
613 {
614 char *id;
615
616 if ((id = cond_id(n)) != NULL)
617 print_otag(h, TAG_A, "chR", "permalink", id);
618 print_otag(h, TAG_CODE, "cTi", "Cm", id);
619 return 1;
620 }
621
622 static int
623 mdoc_nd_pre(MDOC_ARGS)
624 {
625 if (n->type != ROFFT_BODY)
626 return 1;
627
628 print_text(h, "\\(em");
629 /* Cannot use TAG_SPAN because it may contain blocks. */
630 print_otag(h, TAG_DIV, "cT", "Nd");
631 return 1;
632 }
633
634 static int
635 mdoc_nm_pre(MDOC_ARGS)
636 {
637 switch (n->type) {
638 case ROFFT_HEAD:
639 print_otag(h, TAG_TD, "");
640 /* FALLTHROUGH */
641 case ROFFT_ELEM:
642 print_otag(h, TAG_CODE, "cT", "Nm");
643 return 1;
644 case ROFFT_BODY:
645 print_otag(h, TAG_TD, "");
646 return 1;
647 default:
648 break;
649 }
650 synopsis_pre(h, n);
651 print_otag(h, TAG_TABLE, "c", "Nm");
652 print_otag(h, TAG_TR, "");
653 return 1;
654 }
655
656 static int
657 mdoc_xr_pre(MDOC_ARGS)
658 {
659 if (NULL == n->child)
660 return 0;
661
662 if (h->base_man1)
663 print_otag(h, TAG_A, "cThM", "Xr",
664 n->child->string, n->child->next == NULL ?
665 NULL : n->child->next->string);
666 else
667 print_otag(h, TAG_A, "cT", "Xr");
668
669 n = n->child;
670 print_text(h, n->string);
671
672 if (NULL == (n = n->next))
673 return 0;
674
675 h->flags |= HTML_NOSPACE;
676 print_text(h, "(");
677 h->flags |= HTML_NOSPACE;
678 print_text(h, n->string);
679 h->flags |= HTML_NOSPACE;
680 print_text(h, ")");
681 return 0;
682 }
683
684 static int
685 mdoc_ns_pre(MDOC_ARGS)
686 {
687
688 if ( ! (NODE_LINE & n->flags))
689 h->flags |= HTML_NOSPACE;
690 return 1;
691 }
692
693 static int
694 mdoc_ar_pre(MDOC_ARGS)
695 {
696 print_otag(h, TAG_VAR, "cT", "Ar");
697 return 1;
698 }
699
700 static int
701 mdoc_xx_pre(MDOC_ARGS)
702 {
703 print_otag(h, TAG_SPAN, "c", "Ux");
704 return 1;
705 }
706
707 static int
708 mdoc_it_pre(MDOC_ARGS)
709 {
710 const struct roff_node *bl;
711 struct tag *t;
712 enum mdoc_list type;
713
714 bl = n->parent;
715 while (bl->tok != MDOC_Bl)
716 bl = bl->parent;
717 type = bl->norm->Bl.type;
718
719 switch (type) {
720 case LIST_bullet:
721 case LIST_dash:
722 case LIST_hyphen:
723 case LIST_item:
724 case LIST_enum:
725 switch (n->type) {
726 case ROFFT_HEAD:
727 return 0;
728 case ROFFT_BODY:
729 print_otag(h, TAG_LI, "");
730 break;
731 default:
732 break;
733 }
734 break;
735 case LIST_diag:
736 case LIST_hang:
737 case LIST_inset:
738 case LIST_ohang:
739 switch (n->type) {
740 case ROFFT_HEAD:
741 print_otag(h, TAG_DT, "");
742 break;
743 case ROFFT_BODY:
744 print_otag(h, TAG_DD, "");
745 break;
746 default:
747 break;
748 }
749 break;
750 case LIST_tag:
751 switch (n->type) {
752 case ROFFT_HEAD:
753 if (h->style != NULL && !bl->norm->Bl.comp &&
754 (n->parent->prev == NULL ||
755 n->parent->prev->body == NULL ||
756 n->parent->prev->body->child != NULL)) {
757 t = print_otag(h, TAG_DT, "");
758 print_text(h, "\\ ");
759 print_tagq(h, t);
760 t = print_otag(h, TAG_DD, "");
761 print_text(h, "\\ ");
762 print_tagq(h, t);
763 }
764 print_otag(h, TAG_DT, "");
765 break;
766 case ROFFT_BODY:
767 if (n->child == NULL) {
768 print_otag(h, TAG_DD, "s", "width", "auto");
769 print_text(h, "\\ ");
770 } else
771 print_otag(h, TAG_DD, "");
772 break;
773 default:
774 break;
775 }
776 break;
777 case LIST_column:
778 switch (n->type) {
779 case ROFFT_HEAD:
780 break;
781 case ROFFT_BODY:
782 print_otag(h, TAG_TD, "");
783 break;
784 default:
785 print_otag(h, TAG_TR, "");
786 }
787 default:
788 break;
789 }
790
791 return 1;
792 }
793
794 static int
795 mdoc_bl_pre(MDOC_ARGS)
796 {
797 char cattr[28];
798 struct mdoc_bl *bl;
799 enum htmltag elemtype;
800
801 switch (n->type) {
802 case ROFFT_BODY:
803 return 1;
804 case ROFFT_HEAD:
805 return 0;
806 default:
807 break;
808 }
809
810 bl = &n->norm->Bl;
811 switch (bl->type) {
812 case LIST_bullet:
813 elemtype = TAG_UL;
814 (void)strlcpy(cattr, "Bl-bullet", sizeof(cattr));
815 break;
816 case LIST_dash:
817 case LIST_hyphen:
818 elemtype = TAG_UL;
819 (void)strlcpy(cattr, "Bl-dash", sizeof(cattr));
820 break;
821 case LIST_item:
822 elemtype = TAG_UL;
823 (void)strlcpy(cattr, "Bl-item", sizeof(cattr));
824 break;
825 case LIST_enum:
826 elemtype = TAG_OL;
827 (void)strlcpy(cattr, "Bl-enum", sizeof(cattr));
828 break;
829 case LIST_diag:
830 elemtype = TAG_DL;
831 (void)strlcpy(cattr, "Bl-diag", sizeof(cattr));
832 break;
833 case LIST_hang:
834 elemtype = TAG_DL;
835 (void)strlcpy(cattr, "Bl-hang", sizeof(cattr));
836 break;
837 case LIST_inset:
838 elemtype = TAG_DL;
839 (void)strlcpy(cattr, "Bl-inset", sizeof(cattr));
840 break;
841 case LIST_ohang:
842 elemtype = TAG_DL;
843 (void)strlcpy(cattr, "Bl-ohang", sizeof(cattr));
844 break;
845 case LIST_tag:
846 if (bl->offs)
847 print_otag(h, TAG_DIV, "c", "Bd-indent");
848 print_otag(h, TAG_DL, "c", bl->comp ?
849 "Bl-tag Bl-compact" : "Bl-tag");
850 return 1;
851 case LIST_column:
852 elemtype = TAG_TABLE;
853 (void)strlcpy(cattr, "Bl-column", sizeof(cattr));
854 break;
855 default:
856 abort();
857 }
858 if (bl->offs != NULL)
859 (void)strlcat(cattr, " Bd-indent", sizeof(cattr));
860 if (bl->comp)
861 (void)strlcat(cattr, " Bl-compact", sizeof(cattr));
862 print_otag(h, elemtype, "c", cattr);
863 return 1;
864 }
865
866 static int
867 mdoc_ex_pre(MDOC_ARGS)
868 {
869 if (n->prev)
870 print_otag(h, TAG_BR, "");
871 return 1;
872 }
873
874 static int
875 mdoc_st_pre(MDOC_ARGS)
876 {
877 print_otag(h, TAG_SPAN, "cT", "St");
878 return 1;
879 }
880
881 static int
882 mdoc_em_pre(MDOC_ARGS)
883 {
884 print_otag(h, TAG_I, "cT", "Em");
885 return 1;
886 }
887
888 static int
889 mdoc_d1_pre(MDOC_ARGS)
890 {
891 if (n->type != ROFFT_BLOCK)
892 return 1;
893
894 print_otag(h, TAG_DIV, "c", "Bd Bd-indent");
895
896 if (n->tok == MDOC_Dl)
897 print_otag(h, TAG_CODE, "c", "Li");
898
899 return 1;
900 }
901
902 static int
903 mdoc_sx_pre(MDOC_ARGS)
904 {
905 char *id;
906
907 id = html_make_id(n, 0);
908 print_otag(h, TAG_A, "cThR", "Sx", id);
909 free(id);
910 return 1;
911 }
912
913 static int
914 mdoc_bd_pre(MDOC_ARGS)
915 {
916 int comp, sv;
917 struct roff_node *nn;
918
919 if (n->type == ROFFT_HEAD)
920 return 0;
921
922 if (n->type == ROFFT_BLOCK) {
923 comp = n->norm->Bd.comp;
924 for (nn = n; nn && ! comp; nn = nn->parent) {
925 if (nn->type != ROFFT_BLOCK)
926 continue;
927 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
928 comp = 1;
929 if (nn->prev)
930 break;
931 }
932 if ( ! comp)
933 print_paragraph(h);
934 return 1;
935 }
936
937 /* Handle the -offset argument. */
938
939 if (n->norm->Bd.offs == NULL ||
940 ! strcmp(n->norm->Bd.offs, "left"))
941 print_otag(h, TAG_DIV, "c", "Bd");
942 else
943 print_otag(h, TAG_DIV, "c", "Bd Bd-indent");
944
945 if (n->norm->Bd.type != DISP_unfilled &&
946 n->norm->Bd.type != DISP_literal)
947 return 1;
948
949 print_otag(h, TAG_PRE, "c", "Li");
950
951 /* This can be recursive: save & set our literal state. */
952
953 sv = h->flags & HTML_LITERAL;
954 h->flags |= HTML_LITERAL;
955
956 for (nn = n->child; nn; nn = nn->next) {
957 print_mdoc_node(meta, nn, h);
958 /*
959 * If the printed node flushes its own line, then we
960 * needn't do it here as well. This is hacky, but the
961 * notion of selective eoln whitespace is pretty dumb
962 * anyway, so don't sweat it.
963 */
964 switch (nn->tok) {
965 case ROFF_br:
966 case ROFF_sp:
967 case MDOC_Sm:
968 case MDOC_Bl:
969 case MDOC_D1:
970 case MDOC_Dl:
971 case MDOC_Lp:
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 }