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