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