]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
delete some TODO entries that were already fixed
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.117 2015/04/18 17:53:21 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015 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
28 #include "mandoc_aux.h"
29 #include "roff.h"
30 #include "man.h"
31 #include "out.h"
32 #include "html.h"
33 #include "main.h"
34
35 /* TODO: preserve ident widths. */
36 /* FIXME: have PD set the default vspace width. */
37
38 #define INDENT 5
39
40 #define MAN_ARGS const struct roff_meta *man, \
41 const struct roff_node *n, \
42 struct mhtml *mh, \
43 struct html *h
44
45 struct mhtml {
46 int fl;
47 #define MANH_LITERAL (1 << 0) /* literal context */
48 };
49
50 struct htmlman {
51 int (*pre)(MAN_ARGS);
52 int (*post)(MAN_ARGS);
53 };
54
55 static void print_bvspace(struct html *,
56 const struct roff_node *);
57 static void print_man_head(MAN_ARGS);
58 static void print_man_nodelist(MAN_ARGS);
59 static void print_man_node(MAN_ARGS);
60 static int a2width(const struct roff_node *,
61 struct roffsu *);
62 static int man_B_pre(MAN_ARGS);
63 static int man_HP_pre(MAN_ARGS);
64 static int man_IP_pre(MAN_ARGS);
65 static int man_I_pre(MAN_ARGS);
66 static int man_OP_pre(MAN_ARGS);
67 static int man_PP_pre(MAN_ARGS);
68 static int man_RS_pre(MAN_ARGS);
69 static int man_SH_pre(MAN_ARGS);
70 static int man_SM_pre(MAN_ARGS);
71 static int man_SS_pre(MAN_ARGS);
72 static int man_UR_pre(MAN_ARGS);
73 static int man_alt_pre(MAN_ARGS);
74 static int man_br_pre(MAN_ARGS);
75 static int man_ign_pre(MAN_ARGS);
76 static int man_in_pre(MAN_ARGS);
77 static int man_literal_pre(MAN_ARGS);
78 static void man_root_post(MAN_ARGS);
79 static void man_root_pre(MAN_ARGS);
80
81 static const struct htmlman mans[MAN_MAX] = {
82 { man_br_pre, NULL }, /* br */
83 { NULL, NULL }, /* TH */
84 { man_SH_pre, NULL }, /* SH */
85 { man_SS_pre, NULL }, /* SS */
86 { man_IP_pre, NULL }, /* TP */
87 { man_PP_pre, NULL }, /* LP */
88 { man_PP_pre, NULL }, /* PP */
89 { man_PP_pre, NULL }, /* P */
90 { man_IP_pre, NULL }, /* IP */
91 { man_HP_pre, NULL }, /* HP */
92 { man_SM_pre, NULL }, /* SM */
93 { man_SM_pre, NULL }, /* SB */
94 { man_alt_pre, NULL }, /* BI */
95 { man_alt_pre, NULL }, /* IB */
96 { man_alt_pre, NULL }, /* BR */
97 { man_alt_pre, NULL }, /* RB */
98 { NULL, NULL }, /* R */
99 { man_B_pre, NULL }, /* B */
100 { man_I_pre, NULL }, /* I */
101 { man_alt_pre, NULL }, /* IR */
102 { man_alt_pre, NULL }, /* RI */
103 { man_br_pre, NULL }, /* sp */
104 { man_literal_pre, NULL }, /* nf */
105 { man_literal_pre, NULL }, /* fi */
106 { NULL, NULL }, /* RE */
107 { man_RS_pre, NULL }, /* RS */
108 { man_ign_pre, NULL }, /* DT */
109 { man_ign_pre, NULL }, /* UC */
110 { man_ign_pre, NULL }, /* PD */
111 { man_ign_pre, NULL }, /* AT */
112 { man_in_pre, NULL }, /* in */
113 { man_ign_pre, NULL }, /* ft */
114 { man_OP_pre, NULL }, /* OP */
115 { man_literal_pre, NULL }, /* EX */
116 { man_literal_pre, NULL }, /* EE */
117 { man_UR_pre, NULL }, /* UR */
118 { NULL, NULL }, /* UE */
119 { man_ign_pre, NULL }, /* ll */
120 };
121
122
123 /*
124 * Printing leading vertical space before a block.
125 * This is used for the paragraph macros.
126 * The rules are pretty simple, since there's very little nesting going
127 * on here. Basically, if we're the first within another block (SS/SH),
128 * then don't emit vertical space. If we are (RS), then do. If not the
129 * first, print it.
130 */
131 static void
132 print_bvspace(struct html *h, const struct roff_node *n)
133 {
134
135 if (n->body && n->body->child)
136 if (n->body->child->type == ROFFT_TBL)
137 return;
138
139 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
140 if (NULL == n->prev)
141 return;
142
143 print_paragraph(h);
144 }
145
146 void
147 html_man(void *arg, const struct roff_man *man)
148 {
149 struct mhtml mh;
150 struct htmlpair tag;
151 struct html *h;
152 struct tag *t, *tt;
153
154 memset(&mh, 0, sizeof(mh));
155 PAIR_CLASS_INIT(&tag, "mandoc");
156 h = (struct html *)arg;
157
158 if ( ! (HTML_FRAGMENT & h->oflags)) {
159 print_gen_decls(h);
160 t = print_otag(h, TAG_HTML, 0, NULL);
161 tt = print_otag(h, TAG_HEAD, 0, NULL);
162 print_man_head(&man->meta, man->first, &mh, h);
163 print_tagq(h, tt);
164 print_otag(h, TAG_BODY, 0, NULL);
165 print_otag(h, TAG_DIV, 1, &tag);
166 } else
167 t = print_otag(h, TAG_DIV, 1, &tag);
168
169 print_man_nodelist(&man->meta, man->first, &mh, h);
170 print_tagq(h, t);
171 putchar('\n');
172 }
173
174 static void
175 print_man_head(MAN_ARGS)
176 {
177
178 print_gen_head(h);
179 assert(man->title);
180 assert(man->msec);
181 bufcat_fmt(h, "%s(%s)", man->title, man->msec);
182 print_otag(h, TAG_TITLE, 0, NULL);
183 print_text(h, h->buf);
184 }
185
186 static void
187 print_man_nodelist(MAN_ARGS)
188 {
189
190 while (n != NULL) {
191 print_man_node(man, n, mh, h);
192 n = n->next;
193 }
194 }
195
196 static void
197 print_man_node(MAN_ARGS)
198 {
199 int child;
200 struct tag *t;
201
202 child = 1;
203 t = h->tags.head;
204
205 switch (n->type) {
206 case ROFFT_ROOT:
207 man_root_pre(man, n, mh, h);
208 break;
209 case ROFFT_TEXT:
210 if ('\0' == *n->string) {
211 print_paragraph(h);
212 return;
213 }
214 if (n->flags & MAN_LINE && (*n->string == ' ' ||
215 (n->prev != NULL && mh->fl & MANH_LITERAL &&
216 ! (h->flags & HTML_NONEWLINE))))
217 print_otag(h, TAG_BR, 0, NULL);
218 print_text(h, n->string);
219 return;
220 case ROFFT_EQN:
221 if (n->flags & MAN_LINE)
222 putchar('\n');
223 print_eqn(h, n->eqn);
224 break;
225 case ROFFT_TBL:
226 /*
227 * This will take care of initialising all of the table
228 * state data for the first table, then tearing it down
229 * for the last one.
230 */
231 print_tbl(h, n->span);
232 return;
233 default:
234 /*
235 * Close out scope of font prior to opening a macro
236 * scope.
237 */
238 if (HTMLFONT_NONE != h->metac) {
239 h->metal = h->metac;
240 h->metac = HTMLFONT_NONE;
241 }
242
243 /*
244 * Close out the current table, if it's open, and unset
245 * the "meta" table state. This will be reopened on the
246 * next table element.
247 */
248 if (h->tblt) {
249 print_tblclose(h);
250 t = h->tags.head;
251 }
252 if (mans[n->tok].pre)
253 child = (*mans[n->tok].pre)(man, n, mh, h);
254 break;
255 }
256
257 if (child && n->child)
258 print_man_nodelist(man, n->child, mh, h);
259
260 /* This will automatically close out any font scope. */
261 print_stagq(h, t);
262
263 switch (n->type) {
264 case ROFFT_ROOT:
265 man_root_post(man, n, mh, h);
266 break;
267 case ROFFT_EQN:
268 break;
269 default:
270 if (mans[n->tok].post)
271 (*mans[n->tok].post)(man, n, mh, h);
272 break;
273 }
274 }
275
276 static int
277 a2width(const struct roff_node *n, struct roffsu *su)
278 {
279
280 if (n->type != ROFFT_TEXT)
281 return(0);
282 if (a2roffsu(n->string, su, SCALE_EN))
283 return(1);
284
285 return(0);
286 }
287
288 static void
289 man_root_pre(MAN_ARGS)
290 {
291 struct htmlpair tag;
292 struct tag *t, *tt;
293 char *title;
294
295 assert(man->title);
296 assert(man->msec);
297 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
298
299 PAIR_CLASS_INIT(&tag, "head");
300 t = print_otag(h, TAG_TABLE, 1, &tag);
301
302 print_otag(h, TAG_TBODY, 0, NULL);
303
304 tt = print_otag(h, TAG_TR, 0, NULL);
305
306 PAIR_CLASS_INIT(&tag, "head-ltitle");
307 print_otag(h, TAG_TD, 1, &tag);
308 print_text(h, title);
309 print_stagq(h, tt);
310
311 PAIR_CLASS_INIT(&tag, "head-vol");
312 print_otag(h, TAG_TD, 1, &tag);
313 if (NULL != man->vol)
314 print_text(h, man->vol);
315 print_stagq(h, tt);
316
317 PAIR_CLASS_INIT(&tag, "head-rtitle");
318 print_otag(h, TAG_TD, 1, &tag);
319 print_text(h, title);
320 print_tagq(h, t);
321 free(title);
322 }
323
324 static void
325 man_root_post(MAN_ARGS)
326 {
327 struct htmlpair tag;
328 struct tag *t, *tt;
329
330 PAIR_CLASS_INIT(&tag, "foot");
331 t = print_otag(h, TAG_TABLE, 1, &tag);
332
333 tt = print_otag(h, TAG_TR, 0, NULL);
334
335 PAIR_CLASS_INIT(&tag, "foot-date");
336 print_otag(h, TAG_TD, 1, &tag);
337
338 assert(man->date);
339 print_text(h, man->date);
340 print_stagq(h, tt);
341
342 PAIR_CLASS_INIT(&tag, "foot-os");
343 print_otag(h, TAG_TD, 1, &tag);
344
345 if (man->os)
346 print_text(h, man->os);
347 print_tagq(h, t);
348 }
349
350
351 static int
352 man_br_pre(MAN_ARGS)
353 {
354 struct roffsu su;
355 struct htmlpair tag;
356
357 SCALE_VS_INIT(&su, 1);
358
359 if (MAN_sp == n->tok) {
360 if (NULL != (n = n->child))
361 if ( ! a2roffsu(n->string, &su, SCALE_VS))
362 su.scale = 1.0;
363 } else
364 su.scale = 0.0;
365
366 bufinit(h);
367 bufcat_su(h, "height", &su);
368 PAIR_STYLE_INIT(&tag, h);
369 print_otag(h, TAG_DIV, 1, &tag);
370
371 /* So the div isn't empty: */
372 print_text(h, "\\~");
373
374 return(0);
375 }
376
377 static int
378 man_SH_pre(MAN_ARGS)
379 {
380 struct htmlpair tag;
381
382 if (n->type == ROFFT_BLOCK) {
383 mh->fl &= ~MANH_LITERAL;
384 PAIR_CLASS_INIT(&tag, "section");
385 print_otag(h, TAG_DIV, 1, &tag);
386 return(1);
387 } else if (n->type == ROFFT_BODY)
388 return(1);
389
390 print_otag(h, TAG_H1, 0, NULL);
391 return(1);
392 }
393
394 static int
395 man_alt_pre(MAN_ARGS)
396 {
397 const struct roff_node *nn;
398 int i, savelit;
399 enum htmltag fp;
400 struct tag *t;
401
402 if ((savelit = mh->fl & MANH_LITERAL))
403 print_otag(h, TAG_BR, 0, NULL);
404
405 mh->fl &= ~MANH_LITERAL;
406
407 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
408 t = NULL;
409 switch (n->tok) {
410 case MAN_BI:
411 fp = i % 2 ? TAG_I : TAG_B;
412 break;
413 case MAN_IB:
414 fp = i % 2 ? TAG_B : TAG_I;
415 break;
416 case MAN_RI:
417 fp = i % 2 ? TAG_I : TAG_MAX;
418 break;
419 case MAN_IR:
420 fp = i % 2 ? TAG_MAX : TAG_I;
421 break;
422 case MAN_BR:
423 fp = i % 2 ? TAG_MAX : TAG_B;
424 break;
425 case MAN_RB:
426 fp = i % 2 ? TAG_B : TAG_MAX;
427 break;
428 default:
429 abort();
430 /* NOTREACHED */
431 }
432
433 if (i)
434 h->flags |= HTML_NOSPACE;
435
436 if (TAG_MAX != fp)
437 t = print_otag(h, fp, 0, NULL);
438
439 print_man_node(man, nn, mh, h);
440
441 if (t)
442 print_tagq(h, t);
443 }
444
445 if (savelit)
446 mh->fl |= MANH_LITERAL;
447
448 return(0);
449 }
450
451 static int
452 man_SM_pre(MAN_ARGS)
453 {
454
455 print_otag(h, TAG_SMALL, 0, NULL);
456 if (MAN_SB == n->tok)
457 print_otag(h, TAG_B, 0, NULL);
458 return(1);
459 }
460
461 static int
462 man_SS_pre(MAN_ARGS)
463 {
464 struct htmlpair tag;
465
466 if (n->type == ROFFT_BLOCK) {
467 mh->fl &= ~MANH_LITERAL;
468 PAIR_CLASS_INIT(&tag, "subsection");
469 print_otag(h, TAG_DIV, 1, &tag);
470 return(1);
471 } else if (n->type == ROFFT_BODY)
472 return(1);
473
474 print_otag(h, TAG_H2, 0, NULL);
475 return(1);
476 }
477
478 static int
479 man_PP_pre(MAN_ARGS)
480 {
481
482 if (n->type == ROFFT_HEAD)
483 return(0);
484 else if (n->type == ROFFT_BLOCK)
485 print_bvspace(h, n);
486
487 return(1);
488 }
489
490 static int
491 man_IP_pre(MAN_ARGS)
492 {
493 const struct roff_node *nn;
494
495 if (n->type == ROFFT_BODY) {
496 print_otag(h, TAG_DD, 0, NULL);
497 return(1);
498 } else if (n->type != ROFFT_HEAD) {
499 print_otag(h, TAG_DL, 0, NULL);
500 return(1);
501 }
502
503 /* FIXME: width specification. */
504
505 print_otag(h, TAG_DT, 0, NULL);
506
507 /* For IP, only print the first header element. */
508
509 if (MAN_IP == n->tok && n->child)
510 print_man_node(man, n->child, mh, h);
511
512 /* For TP, only print next-line header elements. */
513
514 if (MAN_TP == n->tok) {
515 nn = n->child;
516 while (NULL != nn && 0 == (MAN_LINE & nn->flags))
517 nn = nn->next;
518 while (NULL != nn) {
519 print_man_node(man, nn, mh, h);
520 nn = nn->next;
521 }
522 }
523
524 return(0);
525 }
526
527 static int
528 man_HP_pre(MAN_ARGS)
529 {
530 struct htmlpair tag[2];
531 struct roffsu su;
532 const struct roff_node *np;
533
534 if (n->type == ROFFT_HEAD)
535 return(0);
536 else if (n->type != ROFFT_BLOCK)
537 return(1);
538
539 np = n->head->child;
540
541 if (NULL == np || ! a2width(np, &su))
542 SCALE_HS_INIT(&su, INDENT);
543
544 bufinit(h);
545
546 print_bvspace(h, n);
547 bufcat_su(h, "margin-left", &su);
548 su.scale = -su.scale;
549 bufcat_su(h, "text-indent", &su);
550 PAIR_STYLE_INIT(&tag[0], h);
551 PAIR_CLASS_INIT(&tag[1], "spacer");
552 print_otag(h, TAG_DIV, 2, tag);
553 return(1);
554 }
555
556 static int
557 man_OP_pre(MAN_ARGS)
558 {
559 struct tag *tt;
560 struct htmlpair tag;
561
562 print_text(h, "[");
563 h->flags |= HTML_NOSPACE;
564 PAIR_CLASS_INIT(&tag, "opt");
565 tt = print_otag(h, TAG_SPAN, 1, &tag);
566
567 if (NULL != (n = n->child)) {
568 print_otag(h, TAG_B, 0, NULL);
569 print_text(h, n->string);
570 }
571
572 print_stagq(h, tt);
573
574 if (NULL != n && NULL != n->next) {
575 print_otag(h, TAG_I, 0, NULL);
576 print_text(h, n->next->string);
577 }
578
579 print_stagq(h, tt);
580 h->flags |= HTML_NOSPACE;
581 print_text(h, "]");
582 return(0);
583 }
584
585 static int
586 man_B_pre(MAN_ARGS)
587 {
588
589 print_otag(h, TAG_B, 0, NULL);
590 return(1);
591 }
592
593 static int
594 man_I_pre(MAN_ARGS)
595 {
596
597 print_otag(h, TAG_I, 0, NULL);
598 return(1);
599 }
600
601 static int
602 man_literal_pre(MAN_ARGS)
603 {
604
605 if (MAN_fi == n->tok || MAN_EE == n->tok) {
606 print_otag(h, TAG_BR, 0, NULL);
607 mh->fl &= ~MANH_LITERAL;
608 } else
609 mh->fl |= MANH_LITERAL;
610
611 return(0);
612 }
613
614 static int
615 man_in_pre(MAN_ARGS)
616 {
617
618 print_otag(h, TAG_BR, 0, NULL);
619 return(0);
620 }
621
622 static int
623 man_ign_pre(MAN_ARGS)
624 {
625
626 return(0);
627 }
628
629 static int
630 man_RS_pre(MAN_ARGS)
631 {
632 struct htmlpair tag;
633 struct roffsu su;
634
635 if (n->type == ROFFT_HEAD)
636 return(0);
637 else if (n->type == ROFFT_BODY)
638 return(1);
639
640 SCALE_HS_INIT(&su, INDENT);
641 if (n->head->child)
642 a2width(n->head->child, &su);
643
644 bufinit(h);
645 bufcat_su(h, "margin-left", &su);
646 PAIR_STYLE_INIT(&tag, h);
647 print_otag(h, TAG_DIV, 1, &tag);
648 return(1);
649 }
650
651 static int
652 man_UR_pre(MAN_ARGS)
653 {
654 struct htmlpair tag[2];
655
656 n = n->child;
657 assert(n->type == ROFFT_HEAD);
658 if (n->nchild) {
659 assert(n->child->type == ROFFT_TEXT);
660 PAIR_CLASS_INIT(&tag[0], "link-ext");
661 PAIR_HREF_INIT(&tag[1], n->child->string);
662 print_otag(h, TAG_A, 2, tag);
663 }
664
665 assert(n->next->type == ROFFT_BODY);
666 if (n->next->nchild)
667 n = n->next;
668
669 print_man_nodelist(man, n->child, mh, h);
670
671 return(0);
672 }