]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
Made sure devices and formats recognise that -man and -mdoc have different syntax...
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.11 2009/10/18 19:03:36 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 #include <sys/queue.h>
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <err.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "out.h"
28 #include "html.h"
29 #include "man.h"
30 #include "main.h"
31
32 /* TODO: preserve ident widths. */
33
34 #define INDENT 5
35 #define HALFINDENT 3
36
37 #define MAN_ARGS const struct man_meta *m, \
38 const struct man_node *n, \
39 struct html *h
40
41 struct htmlman {
42 int (*pre)(MAN_ARGS);
43 int (*post)(MAN_ARGS);
44 };
45
46 static void print_man(MAN_ARGS);
47 static void print_man_head(MAN_ARGS);
48 static void print_man_nodelist(MAN_ARGS);
49 static void print_man_node(MAN_ARGS);
50
51 static int a2width(const struct man_node *,
52 struct roffsu *);
53
54 static int man_alt_pre(MAN_ARGS);
55 static int man_br_pre(MAN_ARGS);
56 static int man_ign_pre(MAN_ARGS);
57 static void man_root_post(MAN_ARGS);
58 static int man_root_pre(MAN_ARGS);
59 static int man_B_pre(MAN_ARGS);
60 static int man_HP_pre(MAN_ARGS);
61 static int man_I_pre(MAN_ARGS);
62 static int man_IP_pre(MAN_ARGS);
63 static int man_PP_pre(MAN_ARGS);
64 static int man_RS_pre(MAN_ARGS);
65 static int man_SB_pre(MAN_ARGS);
66 static int man_SH_pre(MAN_ARGS);
67 static int man_SM_pre(MAN_ARGS);
68 static int man_SS_pre(MAN_ARGS);
69
70 #ifdef __linux__
71 extern size_t strlcpy(char *, const char *, size_t);
72 extern size_t strlcat(char *, const char *, size_t);
73 #endif
74
75 static const struct htmlman mans[MAN_MAX] = {
76 { man_br_pre, NULL }, /* br */
77 { NULL, NULL }, /* TH */
78 { man_SH_pre, NULL }, /* SH */
79 { man_SS_pre, NULL }, /* SS */
80 { man_IP_pre, NULL }, /* TP */
81 { man_PP_pre, NULL }, /* LP */
82 { man_PP_pre, NULL }, /* PP */
83 { man_PP_pre, NULL }, /* P */
84 { man_IP_pre, NULL }, /* IP */
85 { man_HP_pre, NULL }, /* HP */
86 { man_SM_pre, NULL }, /* SM */
87 { man_SB_pre, NULL }, /* SB */
88 { man_alt_pre, NULL }, /* BI */
89 { man_alt_pre, NULL }, /* IB */
90 { man_alt_pre, NULL }, /* BR */
91 { man_alt_pre, NULL }, /* RB */
92 { NULL, NULL }, /* R */
93 { man_B_pre, NULL }, /* B */
94 { man_I_pre, NULL }, /* I */
95 { man_alt_pre, NULL }, /* IR */
96 { man_alt_pre, NULL }, /* RI */
97 { NULL, NULL }, /* na */
98 { NULL, NULL }, /* i */
99 { man_br_pre, NULL }, /* sp */
100 { NULL, NULL }, /* nf */
101 { NULL, NULL }, /* fi */
102 { NULL, NULL }, /* r */
103 { NULL, NULL }, /* RE */
104 { man_RS_pre, NULL }, /* RS */
105 { man_ign_pre, NULL }, /* DT */
106 { man_ign_pre, NULL }, /* UC */
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 = SLIST_FIRST(&h->tags);
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 break;
192 default:
193 if (mans[n->tok].pre)
194 child = (*mans[n->tok].pre)(m, n, h);
195 break;
196 }
197
198 if (child && n->child)
199 print_man_nodelist(m, n->child, h);
200
201 print_stagq(h, t);
202
203 bufinit(h);
204
205 switch (n->type) {
206 case (MAN_ROOT):
207 man_root_post(m, n, h);
208 break;
209 case (MAN_TEXT):
210 break;
211 default:
212 if (mans[n->tok].post)
213 (*mans[n->tok].post)(m, n, h);
214 break;
215 }
216 }
217
218
219 static int
220 a2width(const struct man_node *n, struct roffsu *su)
221 {
222
223 if (MAN_TEXT != n->type)
224 return(0);
225 if (a2roffsu(n->string, su, SCALE_BU))
226 return(1);
227
228 return(0);
229 }
230
231
232 /* ARGSUSED */
233 static int
234 man_root_pre(MAN_ARGS)
235 {
236 struct htmlpair tag[2];
237 struct tag *t, *tt;
238 char b[BUFSIZ], title[BUFSIZ];
239
240 b[0] = 0;
241 if (m->vol)
242 (void)strlcat(b, m->vol, BUFSIZ);
243
244 (void)snprintf(title, BUFSIZ - 1,
245 "%s(%d)", m->title, m->msec);
246
247 PAIR_CLASS_INIT(&tag[0], "header");
248 bufcat_style(h, "width", "100%");
249 PAIR_STYLE_INIT(&tag[1], h);
250 t = print_otag(h, TAG_TABLE, 2, tag);
251 tt = print_otag(h, TAG_TR, 0, NULL);
252
253 bufinit(h);
254 bufcat_style(h, "width", "10%");
255 PAIR_STYLE_INIT(&tag[0], h);
256 print_otag(h, TAG_TD, 1, tag);
257 print_text(h, title);
258 print_stagq(h, tt);
259
260 bufinit(h);
261 bufcat_style(h, "width", "80%");
262 bufcat_style(h, "white-space", "nowrap");
263 bufcat_style(h, "text-align", "center");
264 PAIR_STYLE_INIT(&tag[0], h);
265 print_otag(h, TAG_TD, 1, tag);
266 print_text(h, b);
267 print_stagq(h, tt);
268
269 bufinit(h);
270 bufcat_style(h, "width", "10%");
271 bufcat_style(h, "text-align", "right");
272 PAIR_STYLE_INIT(&tag[0], h);
273 print_otag(h, TAG_TD, 1, tag);
274 print_text(h, title);
275 print_tagq(h, t);
276 return(1);
277 }
278
279
280 /* ARGSUSED */
281 static void
282 man_root_post(MAN_ARGS)
283 {
284 struct tm tm;
285 struct htmlpair tag[2];
286 struct tag *t, *tt;
287 char b[BUFSIZ];
288
289 (void)localtime_r(&m->date, &tm);
290
291 if (0 == strftime(b, BUFSIZ - 1, "%B %e, %Y", &tm))
292 err(EXIT_FAILURE, "strftime");
293
294 PAIR_CLASS_INIT(&tag[0], "footer");
295 bufcat_style(h, "width", "100%");
296 PAIR_STYLE_INIT(&tag[1], h);
297 t = print_otag(h, TAG_TABLE, 2, tag);
298 tt = print_otag(h, TAG_TR, 0, NULL);
299
300 bufinit(h);
301 bufcat_style(h, "width", "50%");
302 PAIR_STYLE_INIT(&tag[0], h);
303 print_otag(h, TAG_TD, 1, tag);
304 print_text(h, b);
305 print_stagq(h, tt);
306
307 bufinit(h);
308 bufcat_style(h, "width", "50%");
309 bufcat_style(h, "text-align", "right");
310 PAIR_STYLE_INIT(&tag[0], h);
311 print_otag(h, TAG_TD, 1, tag);
312 if (m->source)
313 print_text(h, m->source);
314 print_tagq(h, t);
315 }
316
317
318
319 /* ARGSUSED */
320 static int
321 man_br_pre(MAN_ARGS)
322 {
323 struct roffsu su;
324 struct htmlpair tag;
325
326 SCALE_VS_INIT(&su, 1);
327
328 if (MAN_sp == n->tok && n->child)
329 a2roffsu(n->child->string, &su, SCALE_VS);
330 else if (MAN_br == n->tok)
331 su.scale = 0;
332
333 bufcat_su(h, "height", &su);
334 PAIR_STYLE_INIT(&tag, h);
335 print_otag(h, TAG_DIV, 1, &tag);
336 return(0);
337 }
338
339
340 /* ARGSUSED */
341 static int
342 man_SH_pre(MAN_ARGS)
343 {
344 struct htmlpair tag[2];
345 struct roffsu su;
346
347 if (MAN_BODY == n->type) {
348 SCALE_HS_INIT(&su, INDENT);
349 bufcat_su(h, "margin-left", &su);
350 PAIR_CLASS_INIT(&tag[0], "sec-body");
351 PAIR_STYLE_INIT(&tag[1], h);
352 print_otag(h, TAG_DIV, 2, tag);
353 return(1);
354 } else if (MAN_BLOCK == n->type) {
355 PAIR_CLASS_INIT(&tag[0], "sec-block");
356 if (n->prev && MAN_SH == n->prev->tok)
357 if (NULL == n->prev->body->child) {
358 print_otag(h, TAG_DIV, 1, tag);
359 return(1);
360 }
361
362 SCALE_VS_INIT(&su, 1);
363 bufcat_su(h, "margin-top", &su);
364 if (NULL == n->next)
365 bufcat_su(h, "margin-bottom", &su);
366 PAIR_STYLE_INIT(&tag[1], h);
367 print_otag(h, TAG_DIV, 2, tag);
368 return(1);
369 }
370
371 PAIR_CLASS_INIT(&tag[0], "sec-head");
372 print_otag(h, TAG_DIV, 1, tag);
373 return(1);
374 }
375
376
377 /* ARGSUSED */
378 static int
379 man_alt_pre(MAN_ARGS)
380 {
381 const struct man_node *nn;
382 struct tag *t;
383 int i;
384 struct htmlpair tagi, tagb, *tagp;
385
386 PAIR_CLASS_INIT(&tagi, "italic");
387 PAIR_CLASS_INIT(&tagb, "bold");
388
389 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
390 switch (n->tok) {
391 case (MAN_BI):
392 tagp = i % 2 ? &tagi : &tagb;
393 break;
394 case (MAN_IB):
395 tagp = i % 2 ? &tagb : &tagi;
396 break;
397 case (MAN_RI):
398 tagp = i % 2 ? &tagi : NULL;
399 break;
400 case (MAN_IR):
401 tagp = i % 2 ? NULL : &tagi;
402 break;
403 case (MAN_BR):
404 tagp = i % 2 ? NULL : &tagb;
405 break;
406 case (MAN_RB):
407 tagp = i % 2 ? &tagb : NULL;
408 break;
409 default:
410 abort();
411 /* NOTREACHED */
412 }
413
414 if (i)
415 h->flags |= HTML_NOSPACE;
416
417 if (tagp) {
418 t = print_otag(h, TAG_SPAN, 1, tagp);
419 print_man_node(m, nn, h);
420 print_tagq(h, t);
421 } else
422 print_man_node(m, nn, h);
423 }
424
425 return(0);
426 }
427
428
429 /* ARGSUSED */
430 static int
431 man_SB_pre(MAN_ARGS)
432 {
433 struct htmlpair tag;
434
435 PAIR_CLASS_INIT(&tag, "small bold");
436 print_otag(h, TAG_SPAN, 1, &tag);
437 return(1);
438 }
439
440
441 /* ARGSUSED */
442 static int
443 man_SM_pre(MAN_ARGS)
444 {
445 struct htmlpair tag;
446
447 PAIR_CLASS_INIT(&tag, "small");
448 print_otag(h, TAG_SPAN, 1, &tag);
449 return(1);
450 }
451
452
453 /* ARGSUSED */
454 static int
455 man_SS_pre(MAN_ARGS)
456 {
457 struct htmlpair tag[3];
458 struct roffsu su;
459
460 SCALE_VS_INIT(&su, 1);
461
462 if (MAN_BODY == n->type) {
463 PAIR_CLASS_INIT(&tag[0], "ssec-body");
464 if (n->parent->next && n->child) {
465 bufcat_su(h, "margin-bottom", &su);
466 PAIR_STYLE_INIT(&tag[1], h);
467 print_otag(h, TAG_DIV, 2, tag);
468 return(1);
469 }
470
471 print_otag(h, TAG_DIV, 1, tag);
472 return(1);
473 } else if (MAN_BLOCK == n->type) {
474 PAIR_CLASS_INIT(&tag[0], "ssec-block");
475 if (n->prev && MAN_SS == n->prev->tok)
476 if (n->prev->body->child) {
477 bufcat_su(h, "margin-top", &su);
478 PAIR_STYLE_INIT(&tag[1], h);
479 print_otag(h, TAG_DIV, 2, tag);
480 return(1);
481 }
482
483 print_otag(h, TAG_DIV, 1, tag);
484 return(1);
485 }
486
487 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
488 bufcat_su(h, "margin-left", &su);
489 PAIR_CLASS_INIT(&tag[0], "ssec-head");
490 PAIR_STYLE_INIT(&tag[1], h);
491 print_otag(h, TAG_DIV, 2, tag);
492 return(1);
493 }
494
495
496 /* ARGSUSED */
497 static int
498 man_PP_pre(MAN_ARGS)
499 {
500 struct htmlpair tag;
501 struct roffsu su;
502 int i;
503
504 if (MAN_BLOCK != n->type)
505 return(1);
506
507 i = 0;
508
509 if (MAN_ROOT == n->parent->tok) {
510 SCALE_HS_INIT(&su, INDENT);
511 bufcat_su(h, "margin-left", &su);
512 i++;
513 }
514 if (n->next && n->next->child) {
515 SCALE_VS_INIT(&su, 1);
516 bufcat_su(h, "margin-bottom", &su);
517 i++;
518 }
519
520 PAIR_STYLE_INIT(&tag, h);
521 print_otag(h, TAG_DIV, i ? 1 : 0, &tag);
522 return(1);
523 }
524
525
526 /* ARGSUSED */
527 static int
528 man_IP_pre(MAN_ARGS)
529 {
530 struct roffsu su;
531 struct htmlpair tag;
532 const struct man_node *nn;
533 int width;
534
535 /*
536 * This scattering of 1-BU margins and pads is to make sure that
537 * when text overruns its box, the subsequent text isn't flush
538 * up against it. However, the rest of the right-hand box must
539 * also be adjusted in consideration of this 1-BU space.
540 */
541
542 if (MAN_BODY == n->type) {
543 SCALE_HS_INIT(&su, INDENT);
544 bufcat_su(h, "margin-left", &su);
545 PAIR_STYLE_INIT(&tag, h);
546 print_otag(h, TAG_DIV, 1, &tag);
547 return(1);
548 }
549
550 nn = MAN_BLOCK == n->type ?
551 n->head->child : n->parent->head->child;
552
553 SCALE_HS_INIT(&su, INDENT);
554 width = 0;
555
556 if (MAN_IP == n->tok && NULL != nn)
557 if (NULL != (nn = nn->next)) {
558 for ( ; nn->next; nn = nn->next)
559 /* Do nothing. */ ;
560 width = a2width(nn, &su);
561 }
562
563 if (MAN_TP == n->tok && NULL != nn)
564 width = a2width(nn, &su);
565
566 if (MAN_BLOCK == n->type) {
567 bufcat_su(h, "margin-left", &su);
568 SCALE_VS_INIT(&su, 1);
569 bufcat_su(h, "margin-top", &su);
570 bufcat_style(h, "clear", "both");
571 PAIR_STYLE_INIT(&tag, h);
572 print_otag(h, TAG_DIV, 1, &tag);
573 return(1);
574 }
575
576 bufcat_su(h, "min-width", &su);
577 SCALE_INVERT(&su);
578 bufcat_su(h, "margin-left", &su);
579 SCALE_HS_INIT(&su, 1);
580 bufcat_su(h, "margin-right", &su);
581 bufcat_style(h, "clear", "left");
582
583 if (n->next && n->next->child)
584 bufcat_style(h, "float", "left");
585
586 PAIR_STYLE_INIT(&tag, h);
587 print_otag(h, TAG_DIV, 1, &tag);
588
589 /* With a length string, manually omit the last child. */
590
591 if ( ! width)
592 return(1);
593
594 if (MAN_IP == n->tok)
595 for (nn = n->child; nn->next; nn = nn->next)
596 print_man_node(m, nn, h);
597 if (MAN_TP == n->tok)
598 for (nn = n->child->next; nn; nn = nn->next)
599 print_man_node(m, nn, h);
600
601 return(0);
602 }
603
604
605 /* ARGSUSED */
606 static int
607 man_HP_pre(MAN_ARGS)
608 {
609 const struct man_node *nn;
610 struct htmlpair tag;
611 struct roffsu su;
612
613 if (MAN_HEAD == n->type)
614 return(0);
615
616 nn = MAN_BLOCK == n->type ?
617 n->head->child : n->parent->head->child;
618
619 SCALE_HS_INIT(&su, INDENT);
620
621 if (NULL != nn)
622 (void)a2width(nn, &su);
623
624 if (MAN_BLOCK == n->type) {
625 bufcat_su(h, "margin-left", &su);
626 SCALE_VS_INIT(&su, 1);
627 bufcat_su(h, "margin-top", &su);
628 bufcat_style(h, "clear", "both");
629 PAIR_STYLE_INIT(&tag, h);
630 print_otag(h, TAG_DIV, 1, &tag);
631 return(1);
632 }
633
634 bufcat_su(h, "margin-left", &su);
635 SCALE_INVERT(&su);
636 bufcat_su(h, "text-indent", &su);
637
638 PAIR_STYLE_INIT(&tag, h);
639 print_otag(h, TAG_DIV, 1, &tag);
640 return(1);
641 }
642
643
644 /* ARGSUSED */
645 static int
646 man_B_pre(MAN_ARGS)
647 {
648 struct htmlpair tag;
649
650 PAIR_CLASS_INIT(&tag, "bold");
651 print_otag(h, TAG_SPAN, 1, &tag);
652 return(1);
653 }
654
655
656 /* ARGSUSED */
657 static int
658 man_I_pre(MAN_ARGS)
659 {
660 struct htmlpair tag;
661
662 PAIR_CLASS_INIT(&tag, "italic");
663 print_otag(h, TAG_SPAN, 1, &tag);
664 return(1);
665 }
666
667
668 /* ARGSUSED */
669 static int
670 man_ign_pre(MAN_ARGS)
671 {
672
673 return(0);
674 }
675
676
677 /* ARGSUSED */
678 static int
679 man_RS_pre(MAN_ARGS)
680 {
681 struct htmlpair tag;
682 struct roffsu su;
683
684 if (MAN_HEAD == n->type)
685 return(0);
686 else if (MAN_BODY == n->type)
687 return(1);
688
689 SCALE_HS_INIT(&su, INDENT);
690 bufcat_su(h, "margin-left", &su);
691
692 if (n->head->child) {
693 SCALE_VS_INIT(&su, 1);
694 a2width(n->head->child, &su);
695 bufcat_su(h, "margin-top", &su);
696 }
697
698 PAIR_STYLE_INIT(&tag, h);
699 print_otag(h, TAG_DIV, 1, &tag);
700 return(1);
701 }