]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
Do not indent by SIZE_MAX/2 when .ce occurs inside explicit no-fill mode.
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.178 2020/04/04 20:33:33 schwarze Exp $ */
2 /*
3 * Copyright (c) 2013-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
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 * HTML formatter for man(7) used by mandoc(1).
19 */
20 #include "config.h"
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "mandoc_aux.h"
31 #include "mandoc.h"
32 #include "roff.h"
33 #include "man.h"
34 #include "out.h"
35 #include "html.h"
36 #include "main.h"
37
38 #define MAN_ARGS const struct roff_meta *man, \
39 struct roff_node *n, \
40 struct html *h
41
42 struct man_html_act {
43 int (*pre)(MAN_ARGS);
44 int (*post)(MAN_ARGS);
45 };
46
47 static void print_man_head(const struct roff_meta *,
48 struct html *);
49 static void print_man_nodelist(MAN_ARGS);
50 static void print_man_node(MAN_ARGS);
51 static char list_continues(const struct roff_node *,
52 const struct roff_node *);
53 static int man_B_pre(MAN_ARGS);
54 static int man_IP_pre(MAN_ARGS);
55 static int man_I_pre(MAN_ARGS);
56 static int man_OP_pre(MAN_ARGS);
57 static int man_PP_pre(MAN_ARGS);
58 static int man_RS_pre(MAN_ARGS);
59 static int man_SH_pre(MAN_ARGS);
60 static int man_SM_pre(MAN_ARGS);
61 static int man_SY_pre(MAN_ARGS);
62 static int man_UR_pre(MAN_ARGS);
63 static int man_abort_pre(MAN_ARGS);
64 static int man_alt_pre(MAN_ARGS);
65 static int man_ign_pre(MAN_ARGS);
66 static int man_in_pre(MAN_ARGS);
67 static void man_root_post(const struct roff_meta *,
68 struct html *);
69 static void man_root_pre(const struct roff_meta *,
70 struct html *);
71
72 static const struct man_html_act man_html_acts[MAN_MAX - MAN_TH] = {
73 { NULL, NULL }, /* TH */
74 { man_SH_pre, NULL }, /* SH */
75 { man_SH_pre, NULL }, /* SS */
76 { man_IP_pre, NULL }, /* TP */
77 { man_IP_pre, NULL }, /* TQ */
78 { man_abort_pre, NULL }, /* LP */
79 { man_PP_pre, NULL }, /* PP */
80 { man_abort_pre, NULL }, /* P */
81 { man_IP_pre, NULL }, /* IP */
82 { man_PP_pre, NULL }, /* HP */
83 { man_SM_pre, NULL }, /* SM */
84 { man_SM_pre, NULL }, /* SB */
85 { man_alt_pre, NULL }, /* BI */
86 { man_alt_pre, NULL }, /* IB */
87 { man_alt_pre, NULL }, /* BR */
88 { man_alt_pre, NULL }, /* RB */
89 { NULL, NULL }, /* R */
90 { man_B_pre, NULL }, /* B */
91 { man_I_pre, NULL }, /* I */
92 { man_alt_pre, NULL }, /* IR */
93 { man_alt_pre, NULL }, /* RI */
94 { NULL, NULL }, /* RE */
95 { man_RS_pre, NULL }, /* RS */
96 { man_ign_pre, NULL }, /* DT */
97 { man_ign_pre, NULL }, /* UC */
98 { man_ign_pre, NULL }, /* PD */
99 { man_ign_pre, NULL }, /* AT */
100 { man_in_pre, NULL }, /* in */
101 { man_SY_pre, NULL }, /* SY */
102 { NULL, NULL }, /* YS */
103 { man_OP_pre, NULL }, /* OP */
104 { NULL, NULL }, /* EX */
105 { NULL, NULL }, /* EE */
106 { man_UR_pre, NULL }, /* UR */
107 { NULL, NULL }, /* UE */
108 { man_UR_pre, NULL }, /* MT */
109 { NULL, NULL }, /* ME */
110 };
111
112
113 void
114 html_man(void *arg, const struct roff_meta *man)
115 {
116 struct html *h;
117 struct roff_node *n;
118 struct tag *t;
119
120 h = (struct html *)arg;
121 n = man->first->child;
122
123 if ((h->oflags & HTML_FRAGMENT) == 0) {
124 print_gen_decls(h);
125 print_otag(h, TAG_HTML, "");
126 if (n != NULL && n->type == ROFFT_COMMENT)
127 print_gen_comment(h, n);
128 t = print_otag(h, TAG_HEAD, "");
129 print_man_head(man, h);
130 print_tagq(h, t);
131 print_otag(h, TAG_BODY, "");
132 }
133
134 man_root_pre(man, h);
135 t = print_otag(h, TAG_DIV, "c", "manual-text");
136 print_man_nodelist(man, n, h);
137 print_tagq(h, t);
138 man_root_post(man, h);
139 print_tagq(h, NULL);
140 }
141
142 static void
143 print_man_head(const struct roff_meta *man, struct html *h)
144 {
145 char *cp;
146
147 print_gen_head(h);
148 mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
149 print_otag(h, TAG_TITLE, "");
150 print_text(h, cp);
151 free(cp);
152 }
153
154 static void
155 print_man_nodelist(MAN_ARGS)
156 {
157 while (n != NULL) {
158 print_man_node(man, n, h);
159 n = n->next;
160 }
161 }
162
163 static void
164 print_man_node(MAN_ARGS)
165 {
166 struct tag *t;
167 int child;
168
169 if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
170 return;
171
172 html_fillmode(h, n->flags & NODE_NOFILL ? ROFF_nf : ROFF_fi);
173
174 child = 1;
175 switch (n->type) {
176 case ROFFT_TEXT:
177 if (*n->string == '\0') {
178 print_endline(h);
179 return;
180 }
181 if (*n->string == ' ' && n->flags & NODE_LINE &&
182 (h->flags & HTML_NONEWLINE) == 0)
183 print_otag(h, TAG_BR, "");
184 else if (n->flags & NODE_DELIMC)
185 h->flags |= HTML_NOSPACE;
186 t = h->tag;
187 t->refcnt++;
188 print_text(h, n->string);
189 break;
190 case ROFFT_EQN:
191 t = h->tag;
192 t->refcnt++;
193 print_eqn(h, n->eqn);
194 break;
195 case ROFFT_TBL:
196 /*
197 * This will take care of initialising all of the table
198 * state data for the first table, then tearing it down
199 * for the last one.
200 */
201 print_tbl(h, n->span);
202 return;
203 default:
204 /*
205 * Close out scope of font prior to opening a macro
206 * scope.
207 */
208 if (h->metac != ESCAPE_FONTROMAN) {
209 h->metal = h->metac;
210 h->metac = ESCAPE_FONTROMAN;
211 }
212
213 /*
214 * Close out the current table, if it's open, and unset
215 * the "meta" table state. This will be reopened on the
216 * next table element.
217 */
218 if (h->tblt != NULL)
219 print_tblclose(h);
220 t = h->tag;
221 t->refcnt++;
222 if (n->tok < ROFF_MAX) {
223 roff_html_pre(h, n);
224 t->refcnt--;
225 print_stagq(h, t);
226 return;
227 }
228 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
229 if (man_html_acts[n->tok - MAN_TH].pre != NULL)
230 child = (*man_html_acts[n->tok - MAN_TH].pre)(man,
231 n, h);
232 break;
233 }
234
235 if (child && n->child != NULL)
236 print_man_nodelist(man, n->child, h);
237
238 /* This will automatically close out any font scope. */
239 t->refcnt--;
240 if (n->type == ROFFT_BLOCK &&
241 (n->tok == MAN_IP || n->tok == MAN_TP || n->tok == MAN_TQ)) {
242 t = h->tag;
243 while (t->tag != TAG_DL && t->tag != TAG_UL)
244 t = t->next;
245 /*
246 * Close the list if no further item of the same type
247 * follows; otherwise, close the item only.
248 */
249 if (list_continues(n, roff_node_next(n)) == '\0') {
250 print_tagq(h, t);
251 t = NULL;
252 }
253 }
254 if (t != NULL)
255 print_stagq(h, t);
256
257 if (n->flags & NODE_NOFILL && n->tok != MAN_YS &&
258 (n->next != NULL && n->next->flags & NODE_LINE)) {
259 /* In .nf = <pre>, print even empty lines. */
260 h->col++;
261 print_endline(h);
262 }
263 }
264
265 static void
266 man_root_pre(const struct roff_meta *man, struct html *h)
267 {
268 struct tag *t, *tt;
269 char *title;
270
271 assert(man->title);
272 assert(man->msec);
273 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
274
275 t = print_otag(h, TAG_TABLE, "c", "head");
276 tt = print_otag(h, TAG_TR, "");
277
278 print_otag(h, TAG_TD, "c", "head-ltitle");
279 print_text(h, title);
280 print_stagq(h, tt);
281
282 print_otag(h, TAG_TD, "c", "head-vol");
283 if (man->vol != NULL)
284 print_text(h, man->vol);
285 print_stagq(h, tt);
286
287 print_otag(h, TAG_TD, "c", "head-rtitle");
288 print_text(h, title);
289 print_tagq(h, t);
290 free(title);
291 }
292
293 static void
294 man_root_post(const struct roff_meta *man, struct html *h)
295 {
296 struct tag *t, *tt;
297
298 t = print_otag(h, TAG_TABLE, "c", "foot");
299 tt = print_otag(h, TAG_TR, "");
300
301 print_otag(h, TAG_TD, "c", "foot-date");
302 print_text(h, man->date);
303 print_stagq(h, tt);
304
305 print_otag(h, TAG_TD, "c", "foot-os");
306 if (man->os != NULL)
307 print_text(h, man->os);
308 print_tagq(h, t);
309 }
310
311 static int
312 man_SH_pre(MAN_ARGS)
313 {
314 const char *class;
315 enum htmltag tag;
316
317 if (n->tok == MAN_SH) {
318 tag = TAG_H1;
319 class = "Sh";
320 } else {
321 tag = TAG_H2;
322 class = "Ss";
323 }
324 switch (n->type) {
325 case ROFFT_BLOCK:
326 html_close_paragraph(h);
327 print_otag(h, TAG_SECTION, "c", class);
328 break;
329 case ROFFT_HEAD:
330 print_otag_id(h, tag, class, n);
331 break;
332 case ROFFT_BODY:
333 break;
334 default:
335 abort();
336 }
337 return 1;
338 }
339
340 static int
341 man_alt_pre(MAN_ARGS)
342 {
343 const struct roff_node *nn;
344 struct tag *t;
345 int i;
346 enum htmltag fp;
347
348 for (i = 0, nn = n->child; nn != NULL; nn = nn->next, i++) {
349 switch (n->tok) {
350 case MAN_BI:
351 fp = i % 2 ? TAG_I : TAG_B;
352 break;
353 case MAN_IB:
354 fp = i % 2 ? TAG_B : TAG_I;
355 break;
356 case MAN_RI:
357 fp = i % 2 ? TAG_I : TAG_MAX;
358 break;
359 case MAN_IR:
360 fp = i % 2 ? TAG_MAX : TAG_I;
361 break;
362 case MAN_BR:
363 fp = i % 2 ? TAG_MAX : TAG_B;
364 break;
365 case MAN_RB:
366 fp = i % 2 ? TAG_B : TAG_MAX;
367 break;
368 default:
369 abort();
370 }
371
372 if (i)
373 h->flags |= HTML_NOSPACE;
374
375 if (fp != TAG_MAX)
376 t = print_otag(h, fp, "");
377
378 print_text(h, nn->string);
379
380 if (fp != TAG_MAX)
381 print_tagq(h, t);
382 }
383 return 0;
384 }
385
386 static int
387 man_SM_pre(MAN_ARGS)
388 {
389 print_otag(h, TAG_SMALL, "");
390 if (n->tok == MAN_SB)
391 print_otag(h, TAG_B, "");
392 return 1;
393 }
394
395 static int
396 man_PP_pre(MAN_ARGS)
397 {
398 switch (n->type) {
399 case ROFFT_BLOCK:
400 html_close_paragraph(h);
401 break;
402 case ROFFT_HEAD:
403 return 0;
404 case ROFFT_BODY:
405 if (n->child != NULL &&
406 (n->child->flags & NODE_NOFILL) == 0)
407 print_otag(h, TAG_P, "c",
408 n->tok == MAN_PP ? "Pp" : "Pp HP");
409 break;
410 default:
411 abort();
412 }
413 return 1;
414 }
415
416 static char
417 list_continues(const struct roff_node *n1, const struct roff_node *n2)
418 {
419 const char *s1, *s2;
420 char c1, c2;
421
422 if (n1 == NULL || n1->type != ROFFT_BLOCK ||
423 n2 == NULL || n2->type != ROFFT_BLOCK)
424 return '\0';
425 if ((n1->tok == MAN_TP || n1->tok == MAN_TQ) &&
426 (n2->tok == MAN_TP || n2->tok == MAN_TQ))
427 return ' ';
428 if (n1->tok != MAN_IP || n2->tok != MAN_IP)
429 return '\0';
430 n1 = n1->head->child;
431 n2 = n2->head->child;
432 s1 = n1 == NULL ? "" : n1->string;
433 s2 = n2 == NULL ? "" : n2->string;
434 c1 = strcmp(s1, "*") == 0 ? '*' :
435 strcmp(s1, "\\-") == 0 ? '-' :
436 strcmp(s1, "\\(bu") == 0 ? 'b' : ' ';
437 c2 = strcmp(s2, "*") == 0 ? '*' :
438 strcmp(s2, "\\-") == 0 ? '-' :
439 strcmp(s2, "\\(bu") == 0 ? 'b' : ' ';
440 return c1 != c2 ? '\0' : c1 == 'b' ? '*' : c1;
441 }
442
443 static int
444 man_IP_pre(MAN_ARGS)
445 {
446 struct roff_node *nn;
447 const char *list_class;
448 enum htmltag list_elem, body_elem;
449 char list_type;
450
451 nn = n->type == ROFFT_BLOCK ? n : n->parent;
452 list_type = list_continues(roff_node_prev(nn), nn);
453 if (list_type == '\0') {
454 /* Start a new list. */
455 list_type = list_continues(nn, roff_node_next(nn));
456 if (list_type == '\0')
457 list_type = ' ';
458 switch (list_type) {
459 case ' ':
460 list_class = "Bl-tag";
461 list_elem = TAG_DL;
462 break;
463 case '*':
464 list_class = "Bl-bullet";
465 list_elem = TAG_UL;
466 break;
467 case '-':
468 list_class = "Bl-dash";
469 list_elem = TAG_UL;
470 break;
471 default:
472 abort();
473 }
474 } else {
475 /* Continue a list that was started earlier. */
476 list_class = NULL;
477 list_elem = TAG_MAX;
478 }
479 body_elem = list_type == ' ' ? TAG_DD : TAG_LI;
480
481 switch (n->type) {
482 case ROFFT_BLOCK:
483 html_close_paragraph(h);
484 if (list_elem != TAG_MAX)
485 print_otag(h, list_elem, "c", list_class);
486 return 1;
487 case ROFFT_HEAD:
488 if (body_elem == TAG_LI)
489 return 0;
490 print_otag_id(h, TAG_DT, NULL, n);
491 break;
492 case ROFFT_BODY:
493 print_otag(h, body_elem, "");
494 return 1;
495 default:
496 abort();
497 }
498 switch(n->tok) {
499 case MAN_IP: /* Only print the first header element. */
500 if (n->child != NULL)
501 print_man_node(man, n->child, h);
502 break;
503 case MAN_TP: /* Only print next-line header elements. */
504 case MAN_TQ:
505 nn = n->child;
506 while (nn != NULL && (NODE_LINE & nn->flags) == 0)
507 nn = nn->next;
508 while (nn != NULL) {
509 print_man_node(man, nn, h);
510 nn = nn->next;
511 }
512 break;
513 default:
514 abort();
515 }
516 return 0;
517 }
518
519 static int
520 man_OP_pre(MAN_ARGS)
521 {
522 struct tag *tt;
523
524 print_text(h, "[");
525 h->flags |= HTML_NOSPACE;
526 tt = print_otag(h, TAG_SPAN, "c", "Op");
527
528 if ((n = n->child) != NULL) {
529 print_otag(h, TAG_B, "");
530 print_text(h, n->string);
531 }
532
533 print_stagq(h, tt);
534
535 if (n != NULL && n->next != NULL) {
536 print_otag(h, TAG_I, "");
537 print_text(h, n->next->string);
538 }
539
540 print_stagq(h, tt);
541 h->flags |= HTML_NOSPACE;
542 print_text(h, "]");
543 return 0;
544 }
545
546 static int
547 man_B_pre(MAN_ARGS)
548 {
549 print_otag(h, TAG_B, "");
550 return 1;
551 }
552
553 static int
554 man_I_pre(MAN_ARGS)
555 {
556 print_otag(h, TAG_I, "");
557 return 1;
558 }
559
560 static int
561 man_in_pre(MAN_ARGS)
562 {
563 print_otag(h, TAG_BR, "");
564 return 0;
565 }
566
567 static int
568 man_ign_pre(MAN_ARGS)
569 {
570 return 0;
571 }
572
573 static int
574 man_RS_pre(MAN_ARGS)
575 {
576 switch (n->type) {
577 case ROFFT_BLOCK:
578 html_close_paragraph(h);
579 break;
580 case ROFFT_HEAD:
581 return 0;
582 case ROFFT_BODY:
583 print_otag(h, TAG_DIV, "c", "Bd-indent");
584 break;
585 default:
586 abort();
587 }
588 return 1;
589 }
590
591 static int
592 man_SY_pre(MAN_ARGS)
593 {
594 switch (n->type) {
595 case ROFFT_BLOCK:
596 html_close_paragraph(h);
597 print_otag(h, TAG_TABLE, "c", "Nm");
598 print_otag(h, TAG_TR, "");
599 break;
600 case ROFFT_HEAD:
601 print_otag(h, TAG_TD, "");
602 print_otag(h, TAG_CODE, "c", "Nm");
603 break;
604 case ROFFT_BODY:
605 print_otag(h, TAG_TD, "");
606 break;
607 default:
608 abort();
609 }
610 return 1;
611 }
612
613 static int
614 man_UR_pre(MAN_ARGS)
615 {
616 char *cp;
617
618 n = n->child;
619 assert(n->type == ROFFT_HEAD);
620 if (n->child != NULL) {
621 assert(n->child->type == ROFFT_TEXT);
622 if (n->tok == MAN_MT) {
623 mandoc_asprintf(&cp, "mailto:%s", n->child->string);
624 print_otag(h, TAG_A, "ch", "Mt", cp);
625 free(cp);
626 } else
627 print_otag(h, TAG_A, "ch", "Lk", n->child->string);
628 }
629
630 assert(n->next->type == ROFFT_BODY);
631 if (n->next->child != NULL)
632 n = n->next;
633
634 print_man_nodelist(man, n->child, h);
635 return 0;
636 }
637
638 static int
639 man_abort_pre(MAN_ARGS)
640 {
641 abort();
642 }