]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
47726a32b9a478f5dbfe6b4e579a8bcda2548f4a
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.20 2009/11/14 19:23:58 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "out.h"
26 #include "html.h"
27 #include "man.h"
28 #include "main.h"
29
30 /* TODO: preserve ident widths. */
31 /* FIXME: have PD set the default vspace width. */
32
33 #define INDENT 5
34 #define HALFINDENT 3
35
36 #define MAN_ARGS const struct man_meta *m, \
37 const struct man_node *n, \
38 struct html *h
39
40 struct htmlman {
41 int (*pre)(MAN_ARGS);
42 int (*post)(MAN_ARGS);
43 };
44
45 static void print_man(MAN_ARGS);
46 static void print_man_head(MAN_ARGS);
47 static void print_man_nodelist(MAN_ARGS);
48 static void print_man_node(MAN_ARGS);
49
50 static int a2width(const struct man_node *,
51 struct roffsu *);
52
53 static int man_alt_pre(MAN_ARGS);
54 static int man_br_pre(MAN_ARGS);
55 static int man_ign_pre(MAN_ARGS);
56 static void man_root_post(MAN_ARGS);
57 static int man_root_pre(MAN_ARGS);
58 static int man_B_pre(MAN_ARGS);
59 static int man_HP_pre(MAN_ARGS);
60 static int man_I_pre(MAN_ARGS);
61 static int man_IP_pre(MAN_ARGS);
62 static int man_PP_pre(MAN_ARGS);
63 static int man_RS_pre(MAN_ARGS);
64 static int man_SB_pre(MAN_ARGS);
65 static int man_SH_pre(MAN_ARGS);
66 static int man_SM_pre(MAN_ARGS);
67 static int man_SS_pre(MAN_ARGS);
68
69 #ifdef __linux__
70 extern size_t strlcpy(char *, const char *, size_t);
71 extern size_t strlcat(char *, const char *, size_t);
72 #endif
73
74 static const struct htmlman mans[MAN_MAX] = {
75 { man_br_pre, NULL }, /* br */
76 { NULL, NULL }, /* TH */
77 { man_SH_pre, NULL }, /* SH */
78 { man_SS_pre, NULL }, /* SS */
79 { man_IP_pre, NULL }, /* TP */
80 { man_PP_pre, NULL }, /* LP */
81 { man_PP_pre, NULL }, /* PP */
82 { man_PP_pre, NULL }, /* P */
83 { man_IP_pre, NULL }, /* IP */
84 { man_HP_pre, NULL }, /* HP */
85 { man_SM_pre, NULL }, /* SM */
86 { man_SB_pre, NULL }, /* SB */
87 { man_alt_pre, NULL }, /* BI */
88 { man_alt_pre, NULL }, /* IB */
89 { man_alt_pre, NULL }, /* BR */
90 { man_alt_pre, NULL }, /* RB */
91 { NULL, NULL }, /* R */
92 { man_B_pre, NULL }, /* B */
93 { man_I_pre, NULL }, /* I */
94 { man_alt_pre, NULL }, /* IR */
95 { man_alt_pre, NULL }, /* RI */
96 { NULL, NULL }, /* na */
97 { NULL, NULL }, /* i */
98 { man_br_pre, NULL }, /* sp */
99 { NULL, NULL }, /* nf */
100 { NULL, NULL }, /* fi */
101 { NULL, NULL }, /* r */
102 { NULL, NULL }, /* RE */
103 { man_RS_pre, NULL }, /* RS */
104 { man_ign_pre, NULL }, /* DT */
105 { man_ign_pre, NULL }, /* UC */
106 { man_ign_pre, NULL }, /* PD */
107 };
108
109
110 void
111 html_man(void *arg, const struct man *m)
112 {
113 struct html *h;
114 struct tag *t;
115
116 h = (struct html *)arg;
117
118 print_gen_doctype(h);
119
120 t = print_otag(h, TAG_HTML, 0, NULL);
121 print_man(man_meta(m), man_node(m), h);
122 print_tagq(h, t);
123
124 printf("\n");
125 }
126
127
128 static void
129 print_man(MAN_ARGS)
130 {
131 struct tag *t;
132 struct htmlpair tag;
133
134 t = print_otag(h, TAG_HEAD, 0, NULL);
135
136 print_man_head(m, n, h);
137 print_tagq(h, t);
138 t = print_otag(h, TAG_BODY, 0, NULL);
139
140 tag.key = ATTR_CLASS;
141 tag.val = "body";
142 print_otag(h, TAG_DIV, 1, &tag);
143
144 print_man_nodelist(m, n, h);
145
146 print_tagq(h, t);
147 }
148
149
150 /* ARGSUSED */
151 static void
152 print_man_head(MAN_ARGS)
153 {
154
155 print_gen_head(h);
156 bufinit(h);
157 buffmt(h, "%s(%d)", m->title, m->msec);
158
159 print_otag(h, TAG_TITLE, 0, NULL);
160 print_text(h, h->buf);
161 }
162
163
164 static void
165 print_man_nodelist(MAN_ARGS)
166 {
167
168 print_man_node(m, n, h);
169 if (n->next)
170 print_man_nodelist(m, n->next, h);
171 }
172
173
174 static void
175 print_man_node(MAN_ARGS)
176 {
177 int child;
178 struct tag *t;
179
180 child = 1;
181 t = h->tags.head;
182
183 bufinit(h);
184
185 switch (n->type) {
186 case (MAN_ROOT):
187 child = man_root_pre(m, n, h);
188 break;
189 case (MAN_TEXT):
190 print_text(h, n->string);
191 return;
192 default:
193 if (h->metaf) {
194 assert(h->metaf == t);
195 print_tagq(h, h->metaf);
196 t = h->tags.head;
197 }
198 if (mans[n->tok].pre)
199 child = (*mans[n->tok].pre)(m, n, h);
200 break;
201 }
202
203 print_stagq(h, t);
204
205 bufinit(h);
206
207 switch (n->type) {
208 case (MAN_ROOT):
209 man_root_post(m, n, h);
210 break;
211 case (MAN_TEXT):
212 break;
213 default:
214 if (mans[n->tok].post)
215 (*mans[n->tok].post)(m, n, h);
216 break;
217 }
218 }
219
220
221 static int
222 a2width(const struct man_node *n, struct roffsu *su)
223 {
224
225 if (MAN_TEXT != n->type)
226 return(0);
227 if (a2roffsu(n->string, su, SCALE_BU))
228 return(1);
229
230 return(0);
231 }
232
233
234 /* ARGSUSED */
235 static int
236 man_root_pre(MAN_ARGS)
237 {
238 struct htmlpair tag[3];
239 struct tag *t, *tt;
240 char b[BUFSIZ], title[BUFSIZ];
241
242 b[0] = 0;
243 if (m->vol)
244 (void)strlcat(b, m->vol, BUFSIZ);
245
246 (void)snprintf(title, BUFSIZ - 1,
247 "%s(%d)", m->title, m->msec);
248
249 PAIR_CLASS_INIT(&tag[0], "header");
250 bufcat_style(h, "width", "100%");
251 PAIR_STYLE_INIT(&tag[1], h);
252 PAIR_SUMMARY_INIT(&tag[2], "header");
253
254 t = print_otag(h, TAG_TABLE, 3, tag);
255 tt = print_otag(h, TAG_TR, 0, NULL);
256
257 bufinit(h);
258 bufcat_style(h, "width", "10%");
259 PAIR_STYLE_INIT(&tag[0], h);
260 print_otag(h, TAG_TD, 1, tag);
261 print_text(h, title);
262 print_stagq(h, tt);
263
264 bufinit(h);
265 bufcat_style(h, "width", "80%");
266 bufcat_style(h, "white-space", "nowrap");
267 bufcat_style(h, "text-align", "center");
268 PAIR_STYLE_INIT(&tag[0], h);
269 print_otag(h, TAG_TD, 1, tag);
270 print_text(h, b);
271 print_stagq(h, tt);
272
273 bufinit(h);
274 bufcat_style(h, "width", "10%");
275 bufcat_style(h, "text-align", "right");
276 PAIR_STYLE_INIT(&tag[0], h);
277 print_otag(h, TAG_TD, 1, tag);
278 print_text(h, title);
279 print_tagq(h, t);
280 return(1);
281 }
282
283
284 /* ARGSUSED */
285 static void
286 man_root_post(MAN_ARGS)
287 {
288 struct htmlpair tag[3];
289 struct tag *t, *tt;
290 char b[DATESIZ];
291
292 time2a(m->date, b, DATESIZ);
293
294 PAIR_CLASS_INIT(&tag[0], "footer");
295 bufcat_style(h, "width", "100%");
296 PAIR_STYLE_INIT(&tag[1], h);
297 PAIR_SUMMARY_INIT(&tag[2], "footer");
298
299 t = print_otag(h, TAG_TABLE, 3, tag);
300 tt = print_otag(h, TAG_TR, 0, NULL);
301
302 bufinit(h);
303 bufcat_style(h, "width", "50%");
304 PAIR_STYLE_INIT(&tag[0], h);
305 print_otag(h, TAG_TD, 1, tag);
306 print_text(h, b);
307 print_stagq(h, tt);
308
309 bufinit(h);
310 bufcat_style(h, "width", "50%");
311 bufcat_style(h, "text-align", "right");
312 PAIR_STYLE_INIT(&tag[0], h);
313 print_otag(h, TAG_TD, 1, tag);
314 if (m->source)
315 print_text(h, m->source);
316 print_tagq(h, t);
317 }
318
319
320
321 /* ARGSUSED */
322 static int
323 man_br_pre(MAN_ARGS)
324 {
325 struct roffsu su;
326 struct htmlpair tag;
327
328 SCALE_VS_INIT(&su, 1);
329
330 if (MAN_sp == n->tok && n->child)
331 a2roffsu(n->child->string, &su, SCALE_VS);
332 else if (MAN_br == n->tok)
333 su.scale = 0;
334
335 bufcat_su(h, "height", &su);
336 PAIR_STYLE_INIT(&tag, h);
337 print_otag(h, TAG_DIV, 1, &tag);
338 /* So the div isn't empty: */
339 print_text(h, "\\~");
340
341 return(0);
342 }
343
344
345 /* ARGSUSED */
346 static int
347 man_SH_pre(MAN_ARGS)
348 {
349 struct htmlpair tag[2];
350 struct roffsu su;
351
352 if (MAN_BODY == n->type) {
353 SCALE_HS_INIT(&su, INDENT);
354 bufcat_su(h, "margin-left", &su);
355 PAIR_CLASS_INIT(&tag[0], "sec-body");
356 PAIR_STYLE_INIT(&tag[1], h);
357 print_otag(h, TAG_DIV, 2, tag);
358 return(1);
359 } else if (MAN_BLOCK == n->type) {
360 PAIR_CLASS_INIT(&tag[0], "sec-block");
361 if (n->prev && MAN_SH == n->prev->tok)
362 if (NULL == n->prev->body->child) {
363 print_otag(h, TAG_DIV, 1, tag);
364 return(1);
365 }
366
367 SCALE_VS_INIT(&su, 1);
368 bufcat_su(h, "margin-top", &su);
369 if (NULL == n->next)
370 bufcat_su(h, "margin-bottom", &su);
371 PAIR_STYLE_INIT(&tag[1], h);
372 print_otag(h, TAG_DIV, 2, tag);
373 return(1);
374 }
375
376 PAIR_CLASS_INIT(&tag[0], "sec-head");
377 print_otag(h, TAG_DIV, 1, tag);
378 return(1);
379 }
380
381
382 /* ARGSUSED */
383 static int
384 man_alt_pre(MAN_ARGS)
385 {
386 const struct man_node *nn;
387 struct tag *t;
388 int i;
389 struct htmlpair tagi, tagb, *tagp;
390
391 PAIR_CLASS_INIT(&tagi, "italic");
392 PAIR_CLASS_INIT(&tagb, "bold");
393
394 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
395 switch (n->tok) {
396 case (MAN_BI):
397 tagp = i % 2 ? &tagi : &tagb;
398 break;
399 case (MAN_IB):
400 tagp = i % 2 ? &tagb : &tagi;
401 break;
402 case (MAN_RI):
403 tagp = i % 2 ? &tagi : NULL;
404 break;
405 case (MAN_IR):
406 tagp = i % 2 ? NULL : &tagi;
407 break;
408 case (MAN_BR):
409 tagp = i % 2 ? NULL : &tagb;
410 break;
411 case (MAN_RB):
412 tagp = i % 2 ? &tagb : NULL;
413 break;
414 default:
415 abort();
416 /* NOTREACHED */
417 }
418
419 if (i)
420 h->flags |= HTML_NOSPACE;
421
422 if (tagp) {
423 t = print_otag(h, TAG_SPAN, 1, tagp);
424 print_man_node(m, nn, h);
425 print_tagq(h, t);
426 } else
427 print_man_node(m, nn, h);
428 }
429
430 return(0);
431 }
432
433
434 /* ARGSUSED */
435 static int
436 man_SB_pre(MAN_ARGS)
437 {
438 struct htmlpair tag;
439
440 PAIR_CLASS_INIT(&tag, "small bold");
441 print_otag(h, TAG_SPAN, 1, &tag);
442 return(1);
443 }
444
445
446 /* ARGSUSED */
447 static int
448 man_SM_pre(MAN_ARGS)
449 {
450 struct htmlpair tag;
451
452 PAIR_CLASS_INIT(&tag, "small");
453 print_otag(h, TAG_SPAN, 1, &tag);
454 return(1);
455 }
456
457
458 /* ARGSUSED */
459 static int
460 man_SS_pre(MAN_ARGS)
461 {
462 struct htmlpair tag[3];
463 struct roffsu su;
464
465 SCALE_VS_INIT(&su, 1);
466
467 if (MAN_BODY == n->type) {
468 PAIR_CLASS_INIT(&tag[0], "ssec-body");
469 if (n->parent->next && n->child) {
470 bufcat_su(h, "margin-bottom", &su);
471 PAIR_STYLE_INIT(&tag[1], h);
472 print_otag(h, TAG_DIV, 2, tag);
473 return(1);
474 }
475
476 print_otag(h, TAG_DIV, 1, tag);
477 return(1);
478 } else if (MAN_BLOCK == n->type) {
479 PAIR_CLASS_INIT(&tag[0], "ssec-block");
480 if (n->prev && MAN_SS == n->prev->tok)
481 if (n->prev->body->child) {
482 bufcat_su(h, "margin-top", &su);
483 PAIR_STYLE_INIT(&tag[1], h);
484 print_otag(h, TAG_DIV, 2, tag);
485 return(1);
486 }
487
488 print_otag(h, TAG_DIV, 1, tag);
489 return(1);
490 }
491
492 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
493 bufcat_su(h, "margin-left", &su);
494 PAIR_CLASS_INIT(&tag[0], "ssec-head");
495 PAIR_STYLE_INIT(&tag[1], h);
496 print_otag(h, TAG_DIV, 2, tag);
497 return(1);
498 }
499
500
501 /* ARGSUSED */
502 static int
503 man_PP_pre(MAN_ARGS)
504 {
505 struct htmlpair tag;
506 struct roffsu su;
507 int i;
508
509 if (MAN_BLOCK != n->type)
510 return(1);
511
512 i = 0;
513
514 if (MAN_ROOT == n->parent->tok) {
515 SCALE_HS_INIT(&su, INDENT);
516 bufcat_su(h, "margin-left", &su);
517 i++;
518 }
519 if (n->next && n->next->child) {
520 SCALE_VS_INIT(&su, 1);
521 bufcat_su(h, "margin-bottom", &su);
522 i++;
523 }
524
525 PAIR_STYLE_INIT(&tag, h);
526 print_otag(h, TAG_DIV, i ? 1 : 0, &tag);
527 return(1);
528 }
529
530
531 /* ARGSUSED */
532 static int
533 man_IP_pre(MAN_ARGS)
534 {
535 struct roffsu su;
536 struct htmlpair tag;
537 const struct man_node *nn;
538 int width;
539
540 /*
541 * This scattering of 1-BU margins and pads is to make sure that
542 * when text overruns its box, the subsequent text isn't flush
543 * up against it. However, the rest of the right-hand box must
544 * also be adjusted in consideration of this 1-BU space.
545 */
546
547 if (MAN_BODY == n->type) {
548 SCALE_HS_INIT(&su, INDENT);
549 bufcat_su(h, "margin-left", &su);
550 PAIR_STYLE_INIT(&tag, h);
551 print_otag(h, TAG_DIV, 1, &tag);
552 return(1);
553 }
554
555 nn = MAN_BLOCK == n->type ?
556 n->head->child : n->parent->head->child;
557
558 SCALE_HS_INIT(&su, INDENT);
559 width = 0;
560
561 if (MAN_IP == n->tok && NULL != nn)
562 if (NULL != (nn = nn->next)) {
563 for ( ; nn->next; nn = nn->next)
564 /* Do nothing. */ ;
565 width = a2width(nn, &su);
566 }
567
568 if (MAN_TP == n->tok && NULL != nn)
569 width = a2width(nn, &su);
570
571 if (MAN_BLOCK == n->type) {
572 bufcat_su(h, "margin-left", &su);
573 SCALE_VS_INIT(&su, 1);
574 bufcat_su(h, "margin-top", &su);
575 bufcat_style(h, "clear", "both");
576 PAIR_STYLE_INIT(&tag, h);
577 print_otag(h, TAG_DIV, 1, &tag);
578 return(1);
579 }
580
581 bufcat_su(h, "min-width", &su);
582 SCALE_INVERT(&su);
583 bufcat_su(h, "margin-left", &su);
584 SCALE_HS_INIT(&su, 1);
585 bufcat_su(h, "margin-right", &su);
586 bufcat_style(h, "clear", "left");
587
588 if (n->next && n->next->child)
589 bufcat_style(h, "float", "left");
590
591 PAIR_STYLE_INIT(&tag, h);
592 print_otag(h, TAG_DIV, 1, &tag);
593
594 /* With a length string, manually omit the last child. */
595
596 if ( ! width)
597 return(1);
598
599 if (MAN_IP == n->tok)
600 for (nn = n->child; nn->next; nn = nn->next)
601 print_man_node(m, nn, h);
602 if (MAN_TP == n->tok)
603 for (nn = n->child->next; nn; nn = nn->next)
604 print_man_node(m, nn, h);
605
606 return(0);
607 }
608
609
610 /* ARGSUSED */
611 static int
612 man_HP_pre(MAN_ARGS)
613 {
614 const struct man_node *nn;
615 struct htmlpair tag;
616 struct roffsu su;
617
618 if (MAN_HEAD == n->type)
619 return(0);
620
621 nn = MAN_BLOCK == n->type ?
622 n->head->child : n->parent->head->child;
623
624 SCALE_HS_INIT(&su, INDENT);
625
626 if (NULL != nn)
627 (void)a2width(nn, &su);
628
629 if (MAN_BLOCK == n->type) {
630 bufcat_su(h, "margin-left", &su);
631 SCALE_VS_INIT(&su, 1);
632 bufcat_su(h, "margin-top", &su);
633 bufcat_style(h, "clear", "both");
634 PAIR_STYLE_INIT(&tag, h);
635 print_otag(h, TAG_DIV, 1, &tag);
636 return(1);
637 }
638
639 bufcat_su(h, "margin-left", &su);
640 SCALE_INVERT(&su);
641 bufcat_su(h, "text-indent", &su);
642
643 PAIR_STYLE_INIT(&tag, h);
644 print_otag(h, TAG_DIV, 1, &tag);
645 return(1);
646 }
647
648
649 /* ARGSUSED */
650 static int
651 man_B_pre(MAN_ARGS)
652 {
653 struct htmlpair tag;
654
655 PAIR_CLASS_INIT(&tag, "bold");
656 print_otag(h, TAG_SPAN, 1, &tag);
657 return(1);
658 }
659
660
661 /* ARGSUSED */
662 static int
663 man_I_pre(MAN_ARGS)
664 {
665 struct htmlpair tag;
666
667 PAIR_CLASS_INIT(&tag, "italic");
668 print_otag(h, TAG_SPAN, 1, &tag);
669 return(1);
670 }
671
672
673 /* ARGSUSED */
674 static int
675 man_ign_pre(MAN_ARGS)
676 {
677
678 return(0);
679 }
680
681
682 /* ARGSUSED */
683 static int
684 man_RS_pre(MAN_ARGS)
685 {
686 struct htmlpair tag;
687 struct roffsu su;
688
689 if (MAN_HEAD == n->type)
690 return(0);
691 else if (MAN_BODY == n->type)
692 return(1);
693
694 SCALE_HS_INIT(&su, INDENT);
695 bufcat_su(h, "margin-left", &su);
696
697 if (n->head->child) {
698 SCALE_VS_INIT(&su, 1);
699 a2width(n->head->child, &su);
700 bufcat_su(h, "margin-top", &su);
701 }
702
703 PAIR_STYLE_INIT(&tag, h);
704 print_otag(h, TAG_DIV, 1, &tag);
705 return(1);
706 }