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