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