]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_html.c
Improve HTML formatting of .Bl -tag.
[mandoc.git] / mdoc_html.c
1 /* $Id: mdoc_html.c,v 1.261 2017/01/25 02:14:43 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014, 2015, 2016, 2017 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 "roff.h"
31 #include "mdoc.h"
32 #include "out.h"
33 #include "html.h"
34 #include "main.h"
35
36 #define INDENT 5
37
38 #define MDOC_ARGS const struct roff_meta *meta, \
39 struct roff_node *n, \
40 struct html *h
41
42 #ifndef MIN
43 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
44 #endif
45
46 struct htmlmdoc {
47 int (*pre)(MDOC_ARGS);
48 void (*post)(MDOC_ARGS);
49 };
50
51 static char *make_id(const struct roff_node *);
52 static void print_mdoc_head(MDOC_ARGS);
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(MDOC_ARGS);
59 static int mdoc_root_pre(MDOC_ARGS);
60
61 static void mdoc__x_post(MDOC_ARGS);
62 static int mdoc__x_pre(MDOC_ARGS);
63 static int mdoc_ad_pre(MDOC_ARGS);
64 static int mdoc_an_pre(MDOC_ARGS);
65 static int mdoc_ap_pre(MDOC_ARGS);
66 static int mdoc_ar_pre(MDOC_ARGS);
67 static int mdoc_bd_pre(MDOC_ARGS);
68 static int mdoc_bf_pre(MDOC_ARGS);
69 static void mdoc_bk_post(MDOC_ARGS);
70 static int mdoc_bk_pre(MDOC_ARGS);
71 static int mdoc_bl_pre(MDOC_ARGS);
72 static int mdoc_cd_pre(MDOC_ARGS);
73 static int mdoc_cm_pre(MDOC_ARGS);
74 static int mdoc_d1_pre(MDOC_ARGS);
75 static int mdoc_dv_pre(MDOC_ARGS);
76 static int mdoc_fa_pre(MDOC_ARGS);
77 static int mdoc_fd_pre(MDOC_ARGS);
78 static int mdoc_fl_pre(MDOC_ARGS);
79 static int mdoc_fn_pre(MDOC_ARGS);
80 static int mdoc_ft_pre(MDOC_ARGS);
81 static int mdoc_em_pre(MDOC_ARGS);
82 static void mdoc_eo_post(MDOC_ARGS);
83 static int mdoc_eo_pre(MDOC_ARGS);
84 static int mdoc_er_pre(MDOC_ARGS);
85 static int mdoc_ev_pre(MDOC_ARGS);
86 static int mdoc_ex_pre(MDOC_ARGS);
87 static void mdoc_fo_post(MDOC_ARGS);
88 static int mdoc_fo_pre(MDOC_ARGS);
89 static int mdoc_ic_pre(MDOC_ARGS);
90 static int mdoc_igndelim_pre(MDOC_ARGS);
91 static int mdoc_in_pre(MDOC_ARGS);
92 static int mdoc_it_pre(MDOC_ARGS);
93 static int mdoc_lb_pre(MDOC_ARGS);
94 static int mdoc_li_pre(MDOC_ARGS);
95 static int mdoc_lk_pre(MDOC_ARGS);
96 static int mdoc_mt_pre(MDOC_ARGS);
97 static int mdoc_ms_pre(MDOC_ARGS);
98 static int mdoc_nd_pre(MDOC_ARGS);
99 static int mdoc_nm_pre(MDOC_ARGS);
100 static int mdoc_no_pre(MDOC_ARGS);
101 static int mdoc_ns_pre(MDOC_ARGS);
102 static int mdoc_pa_pre(MDOC_ARGS);
103 static void mdoc_pf_post(MDOC_ARGS);
104 static int mdoc_pp_pre(MDOC_ARGS);
105 static void mdoc_quote_post(MDOC_ARGS);
106 static int mdoc_quote_pre(MDOC_ARGS);
107 static int mdoc_rs_pre(MDOC_ARGS);
108 static int mdoc_sh_pre(MDOC_ARGS);
109 static int mdoc_skip_pre(MDOC_ARGS);
110 static int mdoc_sm_pre(MDOC_ARGS);
111 static int mdoc_sp_pre(MDOC_ARGS);
112 static int mdoc_ss_pre(MDOC_ARGS);
113 static int mdoc_sx_pre(MDOC_ARGS);
114 static int mdoc_sy_pre(MDOC_ARGS);
115 static int mdoc_va_pre(MDOC_ARGS);
116 static int mdoc_vt_pre(MDOC_ARGS);
117 static int mdoc_xr_pre(MDOC_ARGS);
118 static int mdoc_xx_pre(MDOC_ARGS);
119
120 static const struct htmlmdoc mdocs[MDOC_MAX] = {
121 {mdoc_ap_pre, NULL}, /* Ap */
122 {NULL, NULL}, /* Dd */
123 {NULL, NULL}, /* Dt */
124 {NULL, NULL}, /* Os */
125 {mdoc_sh_pre, NULL }, /* Sh */
126 {mdoc_ss_pre, NULL }, /* Ss */
127 {mdoc_pp_pre, NULL}, /* Pp */
128 {mdoc_d1_pre, NULL}, /* D1 */
129 {mdoc_d1_pre, NULL}, /* Dl */
130 {mdoc_bd_pre, NULL}, /* Bd */
131 {NULL, NULL}, /* Ed */
132 {mdoc_bl_pre, NULL}, /* Bl */
133 {NULL, NULL}, /* El */
134 {mdoc_it_pre, NULL}, /* It */
135 {mdoc_ad_pre, NULL}, /* Ad */
136 {mdoc_an_pre, NULL}, /* An */
137 {mdoc_ar_pre, NULL}, /* Ar */
138 {mdoc_cd_pre, NULL}, /* Cd */
139 {mdoc_cm_pre, NULL}, /* Cm */
140 {mdoc_dv_pre, NULL}, /* Dv */
141 {mdoc_er_pre, NULL}, /* Er */
142 {mdoc_ev_pre, NULL}, /* Ev */
143 {mdoc_ex_pre, NULL}, /* Ex */
144 {mdoc_fa_pre, NULL}, /* Fa */
145 {mdoc_fd_pre, NULL}, /* Fd */
146 {mdoc_fl_pre, NULL}, /* Fl */
147 {mdoc_fn_pre, NULL}, /* Fn */
148 {mdoc_ft_pre, NULL}, /* Ft */
149 {mdoc_ic_pre, NULL}, /* Ic */
150 {mdoc_in_pre, NULL}, /* In */
151 {mdoc_li_pre, NULL}, /* Li */
152 {mdoc_nd_pre, NULL}, /* Nd */
153 {mdoc_nm_pre, NULL}, /* Nm */
154 {mdoc_quote_pre, mdoc_quote_post}, /* Op */
155 {mdoc_ft_pre, NULL}, /* Ot */
156 {mdoc_pa_pre, NULL}, /* Pa */
157 {mdoc_ex_pre, NULL}, /* Rv */
158 {NULL, NULL}, /* St */
159 {mdoc_va_pre, NULL}, /* Va */
160 {mdoc_vt_pre, NULL}, /* Vt */
161 {mdoc_xr_pre, NULL}, /* Xr */
162 {mdoc__x_pre, mdoc__x_post}, /* %A */
163 {mdoc__x_pre, mdoc__x_post}, /* %B */
164 {mdoc__x_pre, mdoc__x_post}, /* %D */
165 {mdoc__x_pre, mdoc__x_post}, /* %I */
166 {mdoc__x_pre, mdoc__x_post}, /* %J */
167 {mdoc__x_pre, mdoc__x_post}, /* %N */
168 {mdoc__x_pre, mdoc__x_post}, /* %O */
169 {mdoc__x_pre, mdoc__x_post}, /* %P */
170 {mdoc__x_pre, mdoc__x_post}, /* %R */
171 {mdoc__x_pre, mdoc__x_post}, /* %T */
172 {mdoc__x_pre, mdoc__x_post}, /* %V */
173 {NULL, NULL}, /* Ac */
174 {mdoc_quote_pre, mdoc_quote_post}, /* Ao */
175 {mdoc_quote_pre, mdoc_quote_post}, /* Aq */
176 {NULL, NULL}, /* At */
177 {NULL, NULL}, /* Bc */
178 {mdoc_bf_pre, NULL}, /* Bf */
179 {mdoc_quote_pre, mdoc_quote_post}, /* Bo */
180 {mdoc_quote_pre, mdoc_quote_post}, /* Bq */
181 {mdoc_xx_pre, NULL}, /* Bsx */
182 {mdoc_xx_pre, NULL}, /* Bx */
183 {mdoc_skip_pre, NULL}, /* Db */
184 {NULL, NULL}, /* Dc */
185 {mdoc_quote_pre, mdoc_quote_post}, /* Do */
186 {mdoc_quote_pre, mdoc_quote_post}, /* Dq */
187 {NULL, NULL}, /* Ec */ /* FIXME: no space */
188 {NULL, NULL}, /* Ef */
189 {mdoc_em_pre, NULL}, /* Em */
190 {mdoc_eo_pre, mdoc_eo_post}, /* Eo */
191 {mdoc_xx_pre, NULL}, /* Fx */
192 {mdoc_ms_pre, NULL}, /* Ms */
193 {mdoc_no_pre, NULL}, /* No */
194 {mdoc_ns_pre, NULL}, /* Ns */
195 {mdoc_xx_pre, NULL}, /* Nx */
196 {mdoc_xx_pre, NULL}, /* Ox */
197 {NULL, NULL}, /* Pc */
198 {mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
199 {mdoc_quote_pre, mdoc_quote_post}, /* Po */
200 {mdoc_quote_pre, mdoc_quote_post}, /* Pq */
201 {NULL, NULL}, /* Qc */
202 {mdoc_quote_pre, mdoc_quote_post}, /* Ql */
203 {mdoc_quote_pre, mdoc_quote_post}, /* Qo */
204 {mdoc_quote_pre, mdoc_quote_post}, /* Qq */
205 {NULL, NULL}, /* Re */
206 {mdoc_rs_pre, NULL}, /* Rs */
207 {NULL, NULL}, /* Sc */
208 {mdoc_quote_pre, mdoc_quote_post}, /* So */
209 {mdoc_quote_pre, mdoc_quote_post}, /* Sq */
210 {mdoc_sm_pre, NULL}, /* Sm */
211 {mdoc_sx_pre, NULL}, /* Sx */
212 {mdoc_sy_pre, NULL}, /* Sy */
213 {NULL, NULL}, /* Tn */
214 {mdoc_xx_pre, NULL}, /* Ux */
215 {NULL, NULL}, /* Xc */
216 {NULL, NULL}, /* Xo */
217 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
218 {NULL, NULL}, /* Fc */
219 {mdoc_quote_pre, mdoc_quote_post}, /* Oo */
220 {NULL, NULL}, /* Oc */
221 {mdoc_bk_pre, mdoc_bk_post}, /* Bk */
222 {NULL, NULL}, /* Ek */
223 {NULL, NULL}, /* Bt */
224 {NULL, NULL}, /* Hf */
225 {mdoc_em_pre, NULL}, /* Fr */
226 {NULL, NULL}, /* Ud */
227 {mdoc_lb_pre, NULL}, /* Lb */
228 {mdoc_pp_pre, NULL}, /* Lp */
229 {mdoc_lk_pre, NULL}, /* Lk */
230 {mdoc_mt_pre, NULL}, /* Mt */
231 {mdoc_quote_pre, mdoc_quote_post}, /* Brq */
232 {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
233 {NULL, NULL}, /* Brc */
234 {mdoc__x_pre, mdoc__x_post}, /* %C */
235 {mdoc_skip_pre, NULL}, /* Es */
236 {mdoc_quote_pre, mdoc_quote_post}, /* En */
237 {mdoc_xx_pre, NULL}, /* Dx */
238 {mdoc__x_pre, mdoc__x_post}, /* %Q */
239 {mdoc_sp_pre, NULL}, /* br */
240 {mdoc_sp_pre, NULL}, /* sp */
241 {mdoc__x_pre, mdoc__x_post}, /* %U */
242 {NULL, NULL}, /* Ta */
243 {mdoc_skip_pre, NULL}, /* ll */
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 tag *t;
290
291 h = (struct html *)arg;
292
293 if ((h->oflags & HTML_FRAGMENT) == 0) {
294 print_gen_decls(h);
295 print_otag(h, TAG_HTML, "");
296 t = print_otag(h, TAG_HEAD, "");
297 print_mdoc_head(&mdoc->meta, mdoc->first->child, h);
298 print_tagq(h, t);
299 print_otag(h, TAG_BODY, "");
300 }
301
302 mdoc_root_pre(&mdoc->meta, mdoc->first->child, h);
303 t = print_otag(h, TAG_DIV, "c", "manual-text");
304 print_mdoc_nodelist(&mdoc->meta, mdoc->first->child, h);
305 print_tagq(h, t);
306 mdoc_root_post(&mdoc->meta, mdoc->first->child, h);
307 print_tagq(h, NULL);
308 }
309
310 static void
311 print_mdoc_head(MDOC_ARGS)
312 {
313 char *cp;
314
315 print_gen_head(h);
316
317 if (meta->arch != NULL && meta->msec != NULL)
318 mandoc_asprintf(&cp, "%s(%s) (%s)", meta->title,
319 meta->msec, meta->arch);
320 else if (meta->msec != NULL)
321 mandoc_asprintf(&cp, "%s(%s)", meta->title, meta->msec);
322 else if (meta->arch != NULL)
323 mandoc_asprintf(&cp, "%s (%s)", meta->title, meta->arch);
324 else
325 cp = mandoc_strdup(meta->title);
326
327 print_otag(h, TAG_TITLE, "");
328 print_text(h, cp);
329 free(cp);
330 }
331
332 static void
333 print_mdoc_nodelist(MDOC_ARGS)
334 {
335
336 while (n != NULL) {
337 print_mdoc_node(meta, n, h);
338 n = n->next;
339 }
340 }
341
342 static void
343 print_mdoc_node(MDOC_ARGS)
344 {
345 int child;
346 struct tag *t;
347
348 if (n->flags & NODE_NOPRT)
349 return;
350
351 child = 1;
352 t = h->tags.head;
353 n->flags &= ~NODE_ENDED;
354
355 switch (n->type) {
356 case ROFFT_TEXT:
357 /* No tables in this mode... */
358 assert(NULL == h->tblt);
359
360 /*
361 * Make sure that if we're in a literal mode already
362 * (i.e., within a <PRE>) don't print the newline.
363 */
364 if (' ' == *n->string && NODE_LINE & n->flags)
365 if ( ! (HTML_LITERAL & h->flags))
366 print_otag(h, TAG_BR, "");
367 if (NODE_DELIMC & n->flags)
368 h->flags |= HTML_NOSPACE;
369 print_text(h, n->string);
370 if (NODE_DELIMO & n->flags)
371 h->flags |= HTML_NOSPACE;
372 return;
373 case ROFFT_EQN:
374 print_eqn(h, n->eqn);
375 break;
376 case ROFFT_TBL:
377 /*
378 * This will take care of initialising all of the table
379 * state data for the first table, then tearing it down
380 * for the last one.
381 */
382 print_tbl(h, n->span);
383 return;
384 default:
385 /*
386 * Close out the current table, if it's open, and unset
387 * the "meta" table state. This will be reopened on the
388 * next table element.
389 */
390 if (h->tblt != NULL) {
391 print_tblclose(h);
392 t = h->tags.head;
393 }
394 assert(h->tblt == NULL);
395 if (mdocs[n->tok].pre && (n->end == ENDBODY_NOT || n->child))
396 child = (*mdocs[n->tok].pre)(meta, n, h);
397 break;
398 }
399
400 if (h->flags & HTML_KEEP && n->flags & NODE_LINE) {
401 h->flags &= ~HTML_KEEP;
402 h->flags |= HTML_PREKEEP;
403 }
404
405 if (child && n->child)
406 print_mdoc_nodelist(meta, n->child, h);
407
408 print_stagq(h, t);
409
410 switch (n->type) {
411 case ROFFT_EQN:
412 break;
413 default:
414 if ( ! mdocs[n->tok].post || n->flags & NODE_ENDED)
415 break;
416 (*mdocs[n->tok].post)(meta, n, h);
417 if (n->end != ENDBODY_NOT)
418 n->body->flags |= NODE_ENDED;
419 if (n->end == ENDBODY_NOSPACE)
420 h->flags |= HTML_NOSPACE;
421 break;
422 }
423 }
424
425 static void
426 mdoc_root_post(MDOC_ARGS)
427 {
428 struct tag *t, *tt;
429
430 t = print_otag(h, TAG_TABLE, "c", "foot");
431 print_otag(h, TAG_TBODY, "");
432 tt = print_otag(h, TAG_TR, "");
433
434 print_otag(h, TAG_TD, "c", "foot-date");
435 print_text(h, meta->date);
436 print_stagq(h, tt);
437
438 print_otag(h, TAG_TD, "c", "foot-os");
439 print_text(h, meta->os);
440 print_tagq(h, t);
441 }
442
443 static int
444 mdoc_root_pre(MDOC_ARGS)
445 {
446 struct tag *t, *tt;
447 char *volume, *title;
448
449 if (NULL == meta->arch)
450 volume = mandoc_strdup(meta->vol);
451 else
452 mandoc_asprintf(&volume, "%s (%s)",
453 meta->vol, meta->arch);
454
455 if (NULL == meta->msec)
456 title = mandoc_strdup(meta->title);
457 else
458 mandoc_asprintf(&title, "%s(%s)",
459 meta->title, meta->msec);
460
461 t = print_otag(h, TAG_TABLE, "c", "head");
462 print_otag(h, TAG_TBODY, "");
463 tt = print_otag(h, TAG_TR, "");
464
465 print_otag(h, TAG_TD, "c", "head-ltitle");
466 print_text(h, title);
467 print_stagq(h, tt);
468
469 print_otag(h, TAG_TD, "c", "head-vol");
470 print_text(h, volume);
471 print_stagq(h, tt);
472
473 print_otag(h, TAG_TD, "c", "head-rtitle");
474 print_text(h, title);
475 print_tagq(h, t);
476
477 free(title);
478 free(volume);
479 return 1;
480 }
481
482 static char *
483 make_id(const struct roff_node *n)
484 {
485 const struct roff_node *nch;
486 char *buf, *cp;
487
488 for (nch = n->child; nch != NULL; nch = nch->next)
489 if (nch->type != ROFFT_TEXT)
490 return NULL;
491
492 buf = NULL;
493 deroff(&buf, n);
494
495 /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
496
497 for (cp = buf; *cp != '\0'; cp++)
498 if (*cp == ' ')
499 *cp = '_';
500
501 return buf;
502 }
503
504 static int
505 mdoc_sh_pre(MDOC_ARGS)
506 {
507 char *id;
508
509 switch (n->type) {
510 case ROFFT_BLOCK:
511 return 1;
512 case ROFFT_BODY:
513 if (n->sec == SEC_AUTHORS)
514 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
515 return 1;
516 default:
517 break;
518 }
519
520 if ((id = make_id(n)) != NULL) {
521 print_otag(h, TAG_H1, "ci", "Sh", id);
522 free(id);
523 } else
524 print_otag(h, TAG_H1, "c", "Sh");
525
526 return 1;
527 }
528
529 static int
530 mdoc_ss_pre(MDOC_ARGS)
531 {
532 char *id;
533
534 if (n->type != ROFFT_HEAD)
535 return 1;
536
537 if ((id = make_id(n)) != NULL) {
538 print_otag(h, TAG_H2, "ci", "Ss", id);
539 free(id);
540 } else
541 print_otag(h, TAG_H2, "c", "Ss");
542
543 return 1;
544 }
545
546 static int
547 mdoc_fl_pre(MDOC_ARGS)
548 {
549 print_otag(h, TAG_B, "c", "Fl");
550 print_text(h, "\\-");
551
552 if (!(n->child == NULL &&
553 (n->next == NULL ||
554 n->next->type == ROFFT_TEXT ||
555 n->next->flags & NODE_LINE)))
556 h->flags |= HTML_NOSPACE;
557
558 return 1;
559 }
560
561 static int
562 mdoc_cm_pre(MDOC_ARGS)
563 {
564 print_otag(h, TAG_B, "c", "Cm");
565 return 1;
566 }
567
568 static int
569 mdoc_nd_pre(MDOC_ARGS)
570 {
571 if (n->type != ROFFT_BODY)
572 return 1;
573
574 /* XXX: this tag in theory can contain block elements. */
575
576 print_text(h, "\\(em");
577 print_otag(h, TAG_SPAN, "c", "Nd");
578 return 1;
579 }
580
581 static int
582 mdoc_nm_pre(MDOC_ARGS)
583 {
584 int len;
585
586 switch (n->type) {
587 case ROFFT_HEAD:
588 print_otag(h, TAG_TD, "");
589 /* FALLTHROUGH */
590 case ROFFT_ELEM:
591 print_otag(h, TAG_B, "c", "Nm");
592 if (n->child == NULL && meta->name != NULL)
593 print_text(h, meta->name);
594 return 1;
595 case ROFFT_BODY:
596 print_otag(h, TAG_TD, "");
597 return 1;
598 default:
599 break;
600 }
601
602 synopsis_pre(h, n);
603 print_otag(h, TAG_TABLE, "c", "Nm");
604
605 for (len = 0, n = n->head->child; n; n = n->next)
606 if (n->type == ROFFT_TEXT)
607 len += html_strlen(n->string);
608
609 if (len == 0 && meta->name != NULL)
610 len = html_strlen(meta->name);
611
612 print_otag(h, TAG_COL, "shw", len);
613 print_otag(h, TAG_COL, "");
614 print_otag(h, TAG_TBODY, "");
615 print_otag(h, TAG_TR, "");
616 return 1;
617 }
618
619 static int
620 mdoc_xr_pre(MDOC_ARGS)
621 {
622 if (NULL == n->child)
623 return 0;
624
625 if (h->base_man)
626 print_otag(h, TAG_A, "chM", "Xr",
627 n->child->string, n->child->next == NULL ?
628 NULL : n->child->next->string);
629 else
630 print_otag(h, TAG_A, "c", "Xr");
631
632 n = n->child;
633 print_text(h, n->string);
634
635 if (NULL == (n = n->next))
636 return 0;
637
638 h->flags |= HTML_NOSPACE;
639 print_text(h, "(");
640 h->flags |= HTML_NOSPACE;
641 print_text(h, n->string);
642 h->flags |= HTML_NOSPACE;
643 print_text(h, ")");
644 return 0;
645 }
646
647 static int
648 mdoc_ns_pre(MDOC_ARGS)
649 {
650
651 if ( ! (NODE_LINE & n->flags))
652 h->flags |= HTML_NOSPACE;
653 return 1;
654 }
655
656 static int
657 mdoc_ar_pre(MDOC_ARGS)
658 {
659 print_otag(h, TAG_I, "c", "Ar");
660 return 1;
661 }
662
663 static int
664 mdoc_xx_pre(MDOC_ARGS)
665 {
666 print_otag(h, TAG_SPAN, "c", "Ux");
667 return 1;
668 }
669
670 static int
671 mdoc_it_pre(MDOC_ARGS)
672 {
673 const struct roff_node *bl;
674 struct tag *t;
675 const char *cattr;
676 enum mdoc_list type;
677
678 bl = n->parent;
679 while (bl != NULL && bl->tok != MDOC_Bl)
680 bl = bl->parent;
681 type = bl->norm->Bl.type;
682
683 switch (type) {
684 case LIST_bullet:
685 cattr = "It-bullet";
686 break;
687 case LIST_dash:
688 case LIST_hyphen:
689 cattr = "It-dash";
690 break;
691 case LIST_item:
692 cattr = "It-item";
693 break;
694 case LIST_enum:
695 cattr = "It-enum";
696 break;
697 case LIST_diag:
698 cattr = "It-diag";
699 break;
700 case LIST_hang:
701 cattr = "It-hang";
702 break;
703 case LIST_inset:
704 cattr = "It-inset";
705 break;
706 case LIST_ohang:
707 cattr = "It-ohang";
708 break;
709 case LIST_tag:
710 cattr = "It-tag";
711 break;
712 case LIST_column:
713 cattr = "It-column";
714 break;
715 default:
716 break;
717 }
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 if (bl->norm->Bl.comp)
730 print_otag(h, TAG_LI, "csvt", cattr, 0);
731 else
732 print_otag(h, TAG_LI, "c", cattr);
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 if (bl->norm->Bl.comp)
745 print_otag(h, TAG_DT, "csvt", cattr, 0);
746 else
747 print_otag(h, TAG_DT, "c", cattr);
748 if (type == LIST_diag)
749 print_otag(h, TAG_B, "c", cattr);
750 break;
751 case ROFFT_BODY:
752 if (bl->norm->Bl.width == NULL)
753 print_otag(h, TAG_DD, "c", cattr);
754 else
755 print_otag(h, TAG_DD, "cswl", cattr,
756 bl->norm->Bl.width);
757 break;
758 default:
759 break;
760 }
761 break;
762 case LIST_tag:
763 switch (n->type) {
764 case ROFFT_HEAD:
765 if (h->style != NULL && !bl->norm->Bl.comp &&
766 (n->parent->prev == NULL ||
767 n->parent->prev->body->child != NULL)) {
768 if (bl->norm->Bl.width == NULL)
769 t = print_otag(h, TAG_DT, "c", cattr);
770 else
771 t = print_otag(h, TAG_DT, "csWl",
772 cattr, bl->norm->Bl.width);
773 print_text(h, "\\ ");
774 print_tagq(h, t);
775 t = print_otag(h, TAG_DD, "c", cattr);
776 print_text(h, "\\ ");
777 print_tagq(h, t);
778 }
779 if (bl->norm->Bl.width == NULL)
780 print_otag(h, TAG_DT, "c", cattr);
781 else
782 print_otag(h, TAG_DT, "csWl", cattr,
783 bl->norm->Bl.width);
784 break;
785 case ROFFT_BODY:
786 if (n->child == NULL) {
787 print_otag(h, TAG_DD, "css?", cattr,
788 "width", "auto");
789 print_text(h, "\\ ");
790 } else
791 print_otag(h, TAG_DD, "c", cattr);
792 break;
793 default:
794 break;
795 }
796 break;
797 case LIST_column:
798 switch (n->type) {
799 case ROFFT_HEAD:
800 break;
801 case ROFFT_BODY:
802 if (bl->norm->Bl.comp)
803 print_otag(h, TAG_TD, "csvt", cattr, 0);
804 else
805 print_otag(h, TAG_TD, "c", cattr);
806 break;
807 default:
808 print_otag(h, TAG_TR, "c", cattr);
809 }
810 default:
811 break;
812 }
813
814 return 1;
815 }
816
817 static int
818 mdoc_bl_pre(MDOC_ARGS)
819 {
820 const char *cattr;
821 int i;
822 enum htmltag elemtype;
823
824 if (n->type == ROFFT_BODY) {
825 if (LIST_column == n->norm->Bl.type)
826 print_otag(h, TAG_TBODY, "");
827 return 1;
828 }
829
830 if (n->type == ROFFT_HEAD) {
831 if (LIST_column != n->norm->Bl.type)
832 return 0;
833
834 /*
835 * For each column, print out the <COL> tag with our
836 * suggested width. The last column gets min-width, as
837 * in terminal mode it auto-sizes to the width of the
838 * screen and we want to preserve that behaviour.
839 */
840
841 for (i = 0; i < (int)n->norm->Bl.ncols - 1; i++)
842 print_otag(h, TAG_COL, "sww", n->norm->Bl.cols[i]);
843 print_otag(h, TAG_COL, "swW", n->norm->Bl.cols[i]);
844
845 return 0;
846 }
847
848 switch (n->norm->Bl.type) {
849 case LIST_bullet:
850 elemtype = TAG_UL;
851 cattr = "Bl-bullet";
852 break;
853 case LIST_dash:
854 case LIST_hyphen:
855 elemtype = TAG_UL;
856 cattr = "Bl-dash";
857 break;
858 case LIST_item:
859 elemtype = TAG_UL;
860 cattr = "Bl-item";
861 break;
862 case LIST_enum:
863 elemtype = TAG_OL;
864 cattr = "Bl-enum";
865 break;
866 case LIST_diag:
867 elemtype = TAG_DL;
868 cattr = "Bl-diag";
869 break;
870 case LIST_hang:
871 elemtype = TAG_DL;
872 cattr = "Bl-hang";
873 break;
874 case LIST_inset:
875 elemtype = TAG_DL;
876 cattr = "Bl-inset";
877 break;
878 case LIST_ohang:
879 elemtype = TAG_DL;
880 cattr = "Bl-ohang";
881 break;
882 case LIST_tag:
883 cattr = "Bl-tag";
884 if (n->norm->Bl.offs)
885 print_otag(h, TAG_DIV, "cswl", cattr,
886 n->norm->Bl.offs);
887 if (n->norm->Bl.width == NULL)
888 print_otag(h, TAG_DL, "c", cattr);
889 else
890 print_otag(h, TAG_DL, "cswl", cattr,
891 n->norm->Bl.width);
892 return 1;
893 case LIST_column:
894 elemtype = TAG_TABLE;
895 cattr = "Bl-column";
896 break;
897 default:
898 abort();
899 }
900
901 if (n->norm->Bl.offs)
902 print_otag(h, elemtype, "cswl", cattr, n->norm->Bl.offs);
903 else
904 print_otag(h, elemtype, "c", cattr);
905
906 return 1;
907 }
908
909 static int
910 mdoc_ex_pre(MDOC_ARGS)
911 {
912 if (n->prev)
913 print_otag(h, TAG_BR, "");
914 return 1;
915 }
916
917 static int
918 mdoc_em_pre(MDOC_ARGS)
919 {
920 print_otag(h, TAG_I, "c", "Em");
921 return 1;
922 }
923
924 static int
925 mdoc_d1_pre(MDOC_ARGS)
926 {
927 if (n->type != ROFFT_BLOCK)
928 return 1;
929
930 print_otag(h, TAG_DIV, "c", "D1");
931
932 if (n->tok == MDOC_Dl)
933 print_otag(h, TAG_CODE, "c", "Li");
934
935 return 1;
936 }
937
938 static int
939 mdoc_sx_pre(MDOC_ARGS)
940 {
941 char *id;
942
943 if ((id = make_id(n)) != NULL) {
944 print_otag(h, TAG_A, "chR", "Sx", id);
945 free(id);
946 } else
947 print_otag(h, TAG_A, "c", "Sx");
948
949 return 1;
950 }
951
952 static int
953 mdoc_bd_pre(MDOC_ARGS)
954 {
955 int comp, offs, sv;
956 struct roff_node *nn;
957
958 if (n->type == ROFFT_HEAD)
959 return 0;
960
961 if (n->type == ROFFT_BLOCK) {
962 comp = n->norm->Bd.comp;
963 for (nn = n; nn && ! comp; nn = nn->parent) {
964 if (nn->type != ROFFT_BLOCK)
965 continue;
966 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
967 comp = 1;
968 if (nn->prev)
969 break;
970 }
971 if ( ! comp)
972 print_paragraph(h);
973 return 1;
974 }
975
976 /* Handle the -offset argument. */
977
978 if (n->norm->Bd.offs == NULL ||
979 ! strcmp(n->norm->Bd.offs, "left"))
980 offs = 0;
981 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
982 offs = INDENT;
983 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
984 offs = INDENT * 2;
985 else
986 offs = -1;
987
988 if (offs == -1)
989 print_otag(h, TAG_DIV, "cswl", "Bd", n->norm->Bd.offs);
990 else
991 print_otag(h, TAG_DIV, "cshl", "Bd", offs);
992
993 if (n->norm->Bd.type != DISP_unfilled &&
994 n->norm->Bd.type != DISP_literal)
995 return 1;
996
997 print_otag(h, TAG_PRE, "c", "Li");
998
999 /* This can be recursive: save & set our literal state. */
1000
1001 sv = h->flags & HTML_LITERAL;
1002 h->flags |= HTML_LITERAL;
1003
1004 for (nn = n->child; nn; nn = nn->next) {
1005 print_mdoc_node(meta, nn, h);
1006 /*
1007 * If the printed node flushes its own line, then we
1008 * needn't do it here as well. This is hacky, but the
1009 * notion of selective eoln whitespace is pretty dumb
1010 * anyway, so don't sweat it.
1011 */
1012 switch (nn->tok) {
1013 case MDOC_Sm:
1014 case MDOC_br:
1015 case MDOC_sp:
1016 case MDOC_Bl:
1017 case MDOC_D1:
1018 case MDOC_Dl:
1019 case MDOC_Lp:
1020 case MDOC_Pp:
1021 continue;
1022 default:
1023 break;
1024 }
1025 if (h->flags & HTML_NONEWLINE ||
1026 (nn->next && ! (nn->next->flags & NODE_LINE)))
1027 continue;
1028 else if (nn->next)
1029 print_text(h, "\n");
1030
1031 h->flags |= HTML_NOSPACE;
1032 }
1033
1034 if (0 == sv)
1035 h->flags &= ~HTML_LITERAL;
1036
1037 return 0;
1038 }
1039
1040 static int
1041 mdoc_pa_pre(MDOC_ARGS)
1042 {
1043 print_otag(h, TAG_I, "c", "Pa");
1044 return 1;
1045 }
1046
1047 static int
1048 mdoc_ad_pre(MDOC_ARGS)
1049 {
1050 print_otag(h, TAG_I, "c", "Ad");
1051 return 1;
1052 }
1053
1054 static int
1055 mdoc_an_pre(MDOC_ARGS)
1056 {
1057 if (n->norm->An.auth == AUTH_split) {
1058 h->flags &= ~HTML_NOSPLIT;
1059 h->flags |= HTML_SPLIT;
1060 return 0;
1061 }
1062 if (n->norm->An.auth == AUTH_nosplit) {
1063 h->flags &= ~HTML_SPLIT;
1064 h->flags |= HTML_NOSPLIT;
1065 return 0;
1066 }
1067
1068 if (h->flags & HTML_SPLIT)
1069 print_otag(h, TAG_BR, "");
1070
1071 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1072 h->flags |= HTML_SPLIT;
1073
1074 print_otag(h, TAG_SPAN, "c", "An");
1075 return 1;
1076 }
1077
1078 static int
1079 mdoc_cd_pre(MDOC_ARGS)
1080 {
1081 synopsis_pre(h, n);
1082 print_otag(h, TAG_B, "c", "Cd");
1083 return 1;
1084 }
1085
1086 static int
1087 mdoc_dv_pre(MDOC_ARGS)
1088 {
1089 print_otag(h, TAG_CODE, "c", "Dv");
1090 return 1;
1091 }
1092
1093 static int
1094 mdoc_ev_pre(MDOC_ARGS)
1095 {
1096 print_otag(h, TAG_CODE, "c", "Ev");
1097 return 1;
1098 }
1099
1100 static int
1101 mdoc_er_pre(MDOC_ARGS)
1102 {
1103 print_otag(h, TAG_CODE, "c", "Er");
1104 return 1;
1105 }
1106
1107 static int
1108 mdoc_fa_pre(MDOC_ARGS)
1109 {
1110 const struct roff_node *nn;
1111 struct tag *t;
1112
1113 if (n->parent->tok != MDOC_Fo) {
1114 print_otag(h, TAG_I, "c", "Fa");
1115 return 1;
1116 }
1117
1118 for (nn = n->child; nn; nn = nn->next) {
1119 t = print_otag(h, TAG_I, "c", "Fa");
1120 print_text(h, nn->string);
1121 print_tagq(h, t);
1122 if (nn->next) {
1123 h->flags |= HTML_NOSPACE;
1124 print_text(h, ",");
1125 }
1126 }
1127
1128 if (n->child && n->next && n->next->tok == MDOC_Fa) {
1129 h->flags |= HTML_NOSPACE;
1130 print_text(h, ",");
1131 }
1132
1133 return 0;
1134 }
1135
1136 static int
1137 mdoc_fd_pre(MDOC_ARGS)
1138 {
1139 struct tag *t;
1140 char *buf, *cp;
1141
1142 synopsis_pre(h, n);
1143
1144 if (NULL == (n = n->child))
1145 return 0;
1146
1147 assert(n->type == ROFFT_TEXT);
1148
1149 if (strcmp(n->string, "#include")) {
1150 print_otag(h, TAG_B, "c", "Fd");
1151 return 1;
1152 }
1153
1154 print_otag(h, TAG_B, "c", "In");
1155 print_text(h, n->string);
1156
1157 if (NULL != (n = n->next)) {
1158 assert(n->type == ROFFT_TEXT);
1159
1160 if (h->base_includes) {
1161 cp = n->string;
1162 if (*cp == '<' || *cp == '"')
1163 cp++;
1164 buf = mandoc_strdup(cp);
1165 cp = strchr(buf, '\0') - 1;
1166 if (cp >= buf && (*cp == '>' || *cp == '"'))
1167 *cp = '\0';
1168 t = print_otag(h, TAG_A, "chI", "In", buf);
1169 free(buf);
1170 } else
1171 t = print_otag(h, TAG_A, "c", "In");
1172
1173 print_text(h, n->string);
1174 print_tagq(h, t);
1175
1176 n = n->next;
1177 }
1178
1179 for ( ; n; n = n->next) {
1180 assert(n->type == ROFFT_TEXT);
1181 print_text(h, n->string);
1182 }
1183
1184 return 0;
1185 }
1186
1187 static int
1188 mdoc_vt_pre(MDOC_ARGS)
1189 {
1190 if (n->type == ROFFT_BLOCK) {
1191 synopsis_pre(h, n);
1192 return 1;
1193 } else if (n->type == ROFFT_ELEM) {
1194 synopsis_pre(h, n);
1195 } else if (n->type == ROFFT_HEAD)
1196 return 0;
1197
1198 print_otag(h, TAG_I, "c", "Vt");
1199 return 1;
1200 }
1201
1202 static int
1203 mdoc_ft_pre(MDOC_ARGS)
1204 {
1205 synopsis_pre(h, n);
1206 print_otag(h, TAG_I, "c", "Ft");
1207 return 1;
1208 }
1209
1210 static int
1211 mdoc_fn_pre(MDOC_ARGS)
1212 {
1213 struct tag *t;
1214 char nbuf[BUFSIZ];
1215 const char *sp, *ep;
1216 int sz, pretty;
1217
1218 pretty = NODE_SYNPRETTY & n->flags;
1219 synopsis_pre(h, n);
1220
1221 /* Split apart into type and name. */
1222 assert(n->child->string);
1223 sp = n->child->string;
1224
1225 ep = strchr(sp, ' ');
1226 if (NULL != ep) {
1227 t = print_otag(h, TAG_I, "c", "Ft");
1228
1229 while (ep) {
1230 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1231 (void)memcpy(nbuf, sp, (size_t)sz);
1232 nbuf[sz] = '\0';
1233 print_text(h, nbuf);
1234 sp = ++ep;
1235 ep = strchr(sp, ' ');
1236 }
1237 print_tagq(h, t);
1238 }
1239
1240 t = print_otag(h, TAG_B, "c", "Fn");
1241
1242 if (sp)
1243 print_text(h, sp);
1244
1245 print_tagq(h, t);
1246
1247 h->flags |= HTML_NOSPACE;
1248 print_text(h, "(");
1249 h->flags |= HTML_NOSPACE;
1250
1251 for (n = n->child->next; n; n = n->next) {
1252 if (NODE_SYNPRETTY & n->flags)
1253 t = print_otag(h, TAG_I, "css?", "Fa",
1254 "white-space", "nowrap");
1255 else
1256 t = print_otag(h, TAG_I, "c", "Fa");
1257 print_text(h, n->string);
1258 print_tagq(h, t);
1259 if (n->next) {
1260 h->flags |= HTML_NOSPACE;
1261 print_text(h, ",");
1262 }
1263 }
1264
1265 h->flags |= HTML_NOSPACE;
1266 print_text(h, ")");
1267
1268 if (pretty) {
1269 h->flags |= HTML_NOSPACE;
1270 print_text(h, ";");
1271 }
1272
1273 return 0;
1274 }
1275
1276 static int
1277 mdoc_sm_pre(MDOC_ARGS)
1278 {
1279
1280 if (NULL == n->child)
1281 h->flags ^= HTML_NONOSPACE;
1282 else if (0 == strcmp("on", n->child->string))
1283 h->flags &= ~HTML_NONOSPACE;
1284 else
1285 h->flags |= HTML_NONOSPACE;
1286
1287 if ( ! (HTML_NONOSPACE & h->flags))
1288 h->flags &= ~HTML_NOSPACE;
1289
1290 return 0;
1291 }
1292
1293 static int
1294 mdoc_skip_pre(MDOC_ARGS)
1295 {
1296
1297 return 0;
1298 }
1299
1300 static int
1301 mdoc_pp_pre(MDOC_ARGS)
1302 {
1303
1304 print_paragraph(h);
1305 return 0;
1306 }
1307
1308 static int
1309 mdoc_sp_pre(MDOC_ARGS)
1310 {
1311 struct roffsu su;
1312
1313 SCALE_VS_INIT(&su, 1);
1314
1315 if (MDOC_sp == n->tok) {
1316 if (NULL != (n = n->child)) {
1317 if ( ! a2roffsu(n->string, &su, SCALE_VS))
1318 su.scale = 1.0;
1319 else if (su.scale < 0.0)
1320 su.scale = 0.0;
1321 }
1322 } else
1323 su.scale = 0.0;
1324
1325 print_otag(h, TAG_DIV, "suh", &su);
1326
1327 /* So the div isn't empty: */
1328 print_text(h, "\\~");
1329
1330 return 0;
1331
1332 }
1333
1334 static int
1335 mdoc_lk_pre(MDOC_ARGS)
1336 {
1337 if (NULL == (n = n->child))
1338 return 0;
1339
1340 assert(n->type == ROFFT_TEXT);
1341
1342 print_otag(h, TAG_A, "ch", "Lk", n->string);
1343
1344 if (NULL == n->next)
1345 print_text(h, n->string);
1346
1347 for (n = n->next; n; n = n->next)
1348 print_text(h, n->string);
1349
1350 return 0;
1351 }
1352
1353 static int
1354 mdoc_mt_pre(MDOC_ARGS)
1355 {
1356 struct tag *t;
1357 char *cp;
1358
1359 for (n = n->child; n; n = n->next) {
1360 assert(n->type == ROFFT_TEXT);
1361
1362 mandoc_asprintf(&cp, "mailto:%s", n->string);
1363 t = print_otag(h, TAG_A, "ch", "Mt", cp);
1364 print_text(h, n->string);
1365 print_tagq(h, t);
1366 free(cp);
1367 }
1368
1369 return 0;
1370 }
1371
1372 static int
1373 mdoc_fo_pre(MDOC_ARGS)
1374 {
1375 struct tag *t;
1376
1377 if (n->type == ROFFT_BODY) {
1378 h->flags |= HTML_NOSPACE;
1379 print_text(h, "(");
1380 h->flags |= HTML_NOSPACE;
1381 return 1;
1382 } else if (n->type == ROFFT_BLOCK) {
1383 synopsis_pre(h, n);
1384 return 1;
1385 }
1386
1387 if (n->child == NULL)
1388 return 0;
1389
1390 assert(n->child->string);
1391 t = print_otag(h, TAG_B, "c", "Fn");
1392 print_text(h, n->child->string);
1393 print_tagq(h, t);
1394 return 0;
1395 }
1396
1397 static void
1398 mdoc_fo_post(MDOC_ARGS)
1399 {
1400
1401 if (n->type != ROFFT_BODY)
1402 return;
1403 h->flags |= HTML_NOSPACE;
1404 print_text(h, ")");
1405 h->flags |= HTML_NOSPACE;
1406 print_text(h, ";");
1407 }
1408
1409 static int
1410 mdoc_in_pre(MDOC_ARGS)
1411 {
1412 struct tag *t;
1413
1414 synopsis_pre(h, n);
1415 print_otag(h, TAG_B, "c", "In");
1416
1417 /*
1418 * The first argument of the `In' gets special treatment as
1419 * being a linked value. Subsequent values are printed
1420 * afterward. groff does similarly. This also handles the case
1421 * of no children.
1422 */
1423
1424 if (NODE_SYNPRETTY & n->flags && NODE_LINE & n->flags)
1425 print_text(h, "#include");
1426
1427 print_text(h, "<");
1428 h->flags |= HTML_NOSPACE;
1429
1430 if (NULL != (n = n->child)) {
1431 assert(n->type == ROFFT_TEXT);
1432
1433 if (h->base_includes)
1434 t = print_otag(h, TAG_A, "chI", "In", n->string);
1435 else
1436 t = print_otag(h, TAG_A, "c", "In");
1437 print_text(h, n->string);
1438 print_tagq(h, t);
1439
1440 n = n->next;
1441 }
1442
1443 h->flags |= HTML_NOSPACE;
1444 print_text(h, ">");
1445
1446 for ( ; n; n = n->next) {
1447 assert(n->type == ROFFT_TEXT);
1448 print_text(h, n->string);
1449 }
1450
1451 return 0;
1452 }
1453
1454 static int
1455 mdoc_ic_pre(MDOC_ARGS)
1456 {
1457 print_otag(h, TAG_B, "c", "Ic");
1458 return 1;
1459 }
1460
1461 static int
1462 mdoc_va_pre(MDOC_ARGS)
1463 {
1464 print_otag(h, TAG_I, "c", "Va");
1465 return 1;
1466 }
1467
1468 static int
1469 mdoc_ap_pre(MDOC_ARGS)
1470 {
1471
1472 h->flags |= HTML_NOSPACE;
1473 print_text(h, "\\(aq");
1474 h->flags |= HTML_NOSPACE;
1475 return 1;
1476 }
1477
1478 static int
1479 mdoc_bf_pre(MDOC_ARGS)
1480 {
1481 const char *cattr;
1482
1483 if (n->type == ROFFT_HEAD)
1484 return 0;
1485 else if (n->type != ROFFT_BODY)
1486 return 1;
1487
1488 if (FONT_Em == n->norm->Bf.font)
1489 cattr = "Em";
1490 else if (FONT_Sy == n->norm->Bf.font)
1491 cattr = "Sy";
1492 else if (FONT_Li == n->norm->Bf.font)
1493 cattr = "Li";
1494 else
1495 cattr = "none";
1496
1497 /*
1498 * We want this to be inline-formatted, but needs to be div to
1499 * accept block children.
1500 */
1501
1502 print_otag(h, TAG_DIV, "css?hl", cattr, "display", "inline", 1);
1503 return 1;
1504 }
1505
1506 static int
1507 mdoc_ms_pre(MDOC_ARGS)
1508 {
1509 print_otag(h, TAG_B, "c", "Ms");
1510 return 1;
1511 }
1512
1513 static int
1514 mdoc_igndelim_pre(MDOC_ARGS)
1515 {
1516
1517 h->flags |= HTML_IGNDELIM;
1518 return 1;
1519 }
1520
1521 static void
1522 mdoc_pf_post(MDOC_ARGS)
1523 {
1524
1525 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1526 h->flags |= HTML_NOSPACE;
1527 }
1528
1529 static int
1530 mdoc_rs_pre(MDOC_ARGS)
1531 {
1532 if (n->type != ROFFT_BLOCK)
1533 return 1;
1534
1535 if (n->prev && SEC_SEE_ALSO == n->sec)
1536 print_paragraph(h);
1537
1538 print_otag(h, TAG_SPAN, "c", "Rs");
1539 return 1;
1540 }
1541
1542 static int
1543 mdoc_no_pre(MDOC_ARGS)
1544 {
1545 print_otag(h, TAG_SPAN, "c", "No");
1546 return 1;
1547 }
1548
1549 static int
1550 mdoc_li_pre(MDOC_ARGS)
1551 {
1552 print_otag(h, TAG_CODE, "c", "Li");
1553 return 1;
1554 }
1555
1556 static int
1557 mdoc_sy_pre(MDOC_ARGS)
1558 {
1559 print_otag(h, TAG_B, "c", "Sy");
1560 return 1;
1561 }
1562
1563 static int
1564 mdoc_lb_pre(MDOC_ARGS)
1565 {
1566 if (SEC_LIBRARY == n->sec && NODE_LINE & n->flags && n->prev)
1567 print_otag(h, TAG_BR, "");
1568
1569 print_otag(h, TAG_SPAN, "c", "Lb");
1570 return 1;
1571 }
1572
1573 static int
1574 mdoc__x_pre(MDOC_ARGS)
1575 {
1576 const char *cattr;
1577 enum htmltag t;
1578
1579 t = TAG_SPAN;
1580
1581 switch (n->tok) {
1582 case MDOC__A:
1583 cattr = "RsA";
1584 if (n->prev && MDOC__A == n->prev->tok)
1585 if (NULL == n->next || MDOC__A != n->next->tok)
1586 print_text(h, "and");
1587 break;
1588 case MDOC__B:
1589 t = TAG_I;
1590 cattr = "RsB";
1591 break;
1592 case MDOC__C:
1593 cattr = "RsC";
1594 break;
1595 case MDOC__D:
1596 cattr = "RsD";
1597 break;
1598 case MDOC__I:
1599 t = TAG_I;
1600 cattr = "RsI";
1601 break;
1602 case MDOC__J:
1603 t = TAG_I;
1604 cattr = "RsJ";
1605 break;
1606 case MDOC__N:
1607 cattr = "RsN";
1608 break;
1609 case MDOC__O:
1610 cattr = "RsO";
1611 break;
1612 case MDOC__P:
1613 cattr = "RsP";
1614 break;
1615 case MDOC__Q:
1616 cattr = "RsQ";
1617 break;
1618 case MDOC__R:
1619 cattr = "RsR";
1620 break;
1621 case MDOC__T:
1622 cattr = "RsT";
1623 break;
1624 case MDOC__U:
1625 print_otag(h, TAG_A, "ch", "RsU", n->child->string);
1626 return 1;
1627 case MDOC__V:
1628 cattr = "RsV";
1629 break;
1630 default:
1631 abort();
1632 }
1633
1634 print_otag(h, t, "c", cattr);
1635 return 1;
1636 }
1637
1638 static void
1639 mdoc__x_post(MDOC_ARGS)
1640 {
1641
1642 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
1643 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
1644 if (NULL == n->prev || MDOC__A != n->prev->tok)
1645 return;
1646
1647 /* TODO: %U */
1648
1649 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
1650 return;
1651
1652 h->flags |= HTML_NOSPACE;
1653 print_text(h, n->next ? "," : ".");
1654 }
1655
1656 static int
1657 mdoc_bk_pre(MDOC_ARGS)
1658 {
1659
1660 switch (n->type) {
1661 case ROFFT_BLOCK:
1662 break;
1663 case ROFFT_HEAD:
1664 return 0;
1665 case ROFFT_BODY:
1666 if (n->parent->args != NULL || n->prev->child == NULL)
1667 h->flags |= HTML_PREKEEP;
1668 break;
1669 default:
1670 abort();
1671 }
1672
1673 return 1;
1674 }
1675
1676 static void
1677 mdoc_bk_post(MDOC_ARGS)
1678 {
1679
1680 if (n->type == ROFFT_BODY)
1681 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
1682 }
1683
1684 static int
1685 mdoc_quote_pre(MDOC_ARGS)
1686 {
1687 if (n->type != ROFFT_BODY)
1688 return 1;
1689
1690 switch (n->tok) {
1691 case MDOC_Ao:
1692 case MDOC_Aq:
1693 print_text(h, n->child != NULL && n->child->next == NULL &&
1694 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1695 break;
1696 case MDOC_Bro:
1697 case MDOC_Brq:
1698 print_text(h, "\\(lC");
1699 break;
1700 case MDOC_Bo:
1701 case MDOC_Bq:
1702 print_text(h, "\\(lB");
1703 break;
1704 case MDOC_Oo:
1705 case MDOC_Op:
1706 print_text(h, "\\(lB");
1707 h->flags |= HTML_NOSPACE;
1708 print_otag(h, TAG_SPAN, "c", "Op");
1709 break;
1710 case MDOC_En:
1711 if (NULL == n->norm->Es ||
1712 NULL == n->norm->Es->child)
1713 return 1;
1714 print_text(h, n->norm->Es->child->string);
1715 break;
1716 case MDOC_Do:
1717 case MDOC_Dq:
1718 case MDOC_Qo:
1719 case MDOC_Qq:
1720 print_text(h, "\\(lq");
1721 break;
1722 case MDOC_Po:
1723 case MDOC_Pq:
1724 print_text(h, "(");
1725 break;
1726 case MDOC_Ql:
1727 print_text(h, "\\(oq");
1728 h->flags |= HTML_NOSPACE;
1729 print_otag(h, TAG_CODE, "c", "Li");
1730 break;
1731 case MDOC_So:
1732 case MDOC_Sq:
1733 print_text(h, "\\(oq");
1734 break;
1735 default:
1736 abort();
1737 }
1738
1739 h->flags |= HTML_NOSPACE;
1740 return 1;
1741 }
1742
1743 static void
1744 mdoc_quote_post(MDOC_ARGS)
1745 {
1746
1747 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1748 return;
1749
1750 h->flags |= HTML_NOSPACE;
1751
1752 switch (n->tok) {
1753 case MDOC_Ao:
1754 case MDOC_Aq:
1755 print_text(h, n->child != NULL && n->child->next == NULL &&
1756 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1757 break;
1758 case MDOC_Bro:
1759 case MDOC_Brq:
1760 print_text(h, "\\(rC");
1761 break;
1762 case MDOC_Oo:
1763 case MDOC_Op:
1764 case MDOC_Bo:
1765 case MDOC_Bq:
1766 print_text(h, "\\(rB");
1767 break;
1768 case MDOC_En:
1769 if (n->norm->Es == NULL ||
1770 n->norm->Es->child == NULL ||
1771 n->norm->Es->child->next == NULL)
1772 h->flags &= ~HTML_NOSPACE;
1773 else
1774 print_text(h, n->norm->Es->child->next->string);
1775 break;
1776 case MDOC_Qo:
1777 case MDOC_Qq:
1778 case MDOC_Do:
1779 case MDOC_Dq:
1780 print_text(h, "\\(rq");
1781 break;
1782 case MDOC_Po:
1783 case MDOC_Pq:
1784 print_text(h, ")");
1785 break;
1786 case MDOC_Ql:
1787 case MDOC_So:
1788 case MDOC_Sq:
1789 print_text(h, "\\(cq");
1790 break;
1791 default:
1792 abort();
1793 }
1794 }
1795
1796 static int
1797 mdoc_eo_pre(MDOC_ARGS)
1798 {
1799
1800 if (n->type != ROFFT_BODY)
1801 return 1;
1802
1803 if (n->end == ENDBODY_NOT &&
1804 n->parent->head->child == NULL &&
1805 n->child != NULL &&
1806 n->child->end != ENDBODY_NOT)
1807 print_text(h, "\\&");
1808 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1809 n->parent->head->child != NULL && (n->child != NULL ||
1810 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1811 h->flags |= HTML_NOSPACE;
1812 return 1;
1813 }
1814
1815 static void
1816 mdoc_eo_post(MDOC_ARGS)
1817 {
1818 int body, tail;
1819
1820 if (n->type != ROFFT_BODY)
1821 return;
1822
1823 if (n->end != ENDBODY_NOT) {
1824 h->flags &= ~HTML_NOSPACE;
1825 return;
1826 }
1827
1828 body = n->child != NULL || n->parent->head->child != NULL;
1829 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1830
1831 if (body && tail)
1832 h->flags |= HTML_NOSPACE;
1833 else if ( ! tail)
1834 h->flags &= ~HTML_NOSPACE;
1835 }