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