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