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