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