]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
\f escapes appear to work properly for -Thtml -man. Needs more testing.
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.21 2009/11/15 06:45:31 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 /*
194 * Close out scope of font prior to opening a macro
195 * scope. Assert that the metafont is on the top of the
196 * stack (it's never nested).
197 */
198 if (h->metaf) {
199 assert(h->metaf == t);
200 print_tagq(h, h->metaf);
201 assert(NULL == h->metaf);
202 t = h->tags.head;
203 }
204 if (mans[n->tok].pre)
205 child = (*mans[n->tok].pre)(m, n, h);
206 break;
207 }
208
209 if (child && n->child)
210 print_man_nodelist(m, n->child, h);
211
212 print_stagq(h, t);
213
214 bufinit(h);
215
216 switch (n->type) {
217 case (MAN_ROOT):
218 man_root_post(m, n, h);
219 break;
220 case (MAN_TEXT):
221 break;
222 default:
223 if (mans[n->tok].post)
224 (*mans[n->tok].post)(m, n, h);
225 break;
226 }
227 }
228
229
230 static int
231 a2width(const struct man_node *n, struct roffsu *su)
232 {
233
234 if (MAN_TEXT != n->type)
235 return(0);
236 if (a2roffsu(n->string, su, SCALE_BU))
237 return(1);
238
239 return(0);
240 }
241
242
243 /* ARGSUSED */
244 static int
245 man_root_pre(MAN_ARGS)
246 {
247 struct htmlpair tag[3];
248 struct tag *t, *tt;
249 char b[BUFSIZ], title[BUFSIZ];
250
251 b[0] = 0;
252 if (m->vol)
253 (void)strlcat(b, m->vol, BUFSIZ);
254
255 (void)snprintf(title, BUFSIZ - 1,
256 "%s(%d)", m->title, m->msec);
257
258 PAIR_CLASS_INIT(&tag[0], "header");
259 bufcat_style(h, "width", "100%");
260 PAIR_STYLE_INIT(&tag[1], h);
261 PAIR_SUMMARY_INIT(&tag[2], "header");
262
263 t = print_otag(h, TAG_TABLE, 3, tag);
264 tt = print_otag(h, TAG_TR, 0, NULL);
265
266 bufinit(h);
267 bufcat_style(h, "width", "10%");
268 PAIR_STYLE_INIT(&tag[0], h);
269 print_otag(h, TAG_TD, 1, tag);
270 print_text(h, title);
271 print_stagq(h, tt);
272
273 bufinit(h);
274 bufcat_style(h, "width", "80%");
275 bufcat_style(h, "white-space", "nowrap");
276 bufcat_style(h, "text-align", "center");
277 PAIR_STYLE_INIT(&tag[0], h);
278 print_otag(h, TAG_TD, 1, tag);
279 print_text(h, b);
280 print_stagq(h, tt);
281
282 bufinit(h);
283 bufcat_style(h, "width", "10%");
284 bufcat_style(h, "text-align", "right");
285 PAIR_STYLE_INIT(&tag[0], h);
286 print_otag(h, TAG_TD, 1, tag);
287 print_text(h, title);
288 print_tagq(h, t);
289 return(1);
290 }
291
292
293 /* ARGSUSED */
294 static void
295 man_root_post(MAN_ARGS)
296 {
297 struct htmlpair tag[3];
298 struct tag *t, *tt;
299 char b[DATESIZ];
300
301 time2a(m->date, b, DATESIZ);
302
303 PAIR_CLASS_INIT(&tag[0], "footer");
304 bufcat_style(h, "width", "100%");
305 PAIR_STYLE_INIT(&tag[1], h);
306 PAIR_SUMMARY_INIT(&tag[2], "footer");
307
308 t = print_otag(h, TAG_TABLE, 3, tag);
309 tt = print_otag(h, TAG_TR, 0, NULL);
310
311 bufinit(h);
312 bufcat_style(h, "width", "50%");
313 PAIR_STYLE_INIT(&tag[0], h);
314 print_otag(h, TAG_TD, 1, tag);
315 print_text(h, b);
316 print_stagq(h, tt);
317
318 bufinit(h);
319 bufcat_style(h, "width", "50%");
320 bufcat_style(h, "text-align", "right");
321 PAIR_STYLE_INIT(&tag[0], h);
322 print_otag(h, TAG_TD, 1, tag);
323 if (m->source)
324 print_text(h, m->source);
325 print_tagq(h, t);
326 }
327
328
329
330 /* ARGSUSED */
331 static int
332 man_br_pre(MAN_ARGS)
333 {
334 struct roffsu su;
335 struct htmlpair tag;
336
337 SCALE_VS_INIT(&su, 1);
338
339 if (MAN_sp == n->tok && n->child)
340 a2roffsu(n->child->string, &su, SCALE_VS);
341 else if (MAN_br == n->tok)
342 su.scale = 0;
343
344 bufcat_su(h, "height", &su);
345 PAIR_STYLE_INIT(&tag, h);
346 print_otag(h, TAG_DIV, 1, &tag);
347 /* So the div isn't empty: */
348 print_text(h, "\\~");
349
350 return(0);
351 }
352
353
354 /* ARGSUSED */
355 static int
356 man_SH_pre(MAN_ARGS)
357 {
358 struct htmlpair tag[2];
359 struct roffsu su;
360
361 if (MAN_BODY == n->type) {
362 SCALE_HS_INIT(&su, INDENT);
363 bufcat_su(h, "margin-left", &su);
364 PAIR_CLASS_INIT(&tag[0], "sec-body");
365 PAIR_STYLE_INIT(&tag[1], h);
366 print_otag(h, TAG_DIV, 2, tag);
367 return(1);
368 } else if (MAN_BLOCK == n->type) {
369 PAIR_CLASS_INIT(&tag[0], "sec-block");
370 if (n->prev && MAN_SH == n->prev->tok)
371 if (NULL == n->prev->body->child) {
372 print_otag(h, TAG_DIV, 1, tag);
373 return(1);
374 }
375
376 SCALE_VS_INIT(&su, 1);
377 bufcat_su(h, "margin-top", &su);
378 if (NULL == n->next)
379 bufcat_su(h, "margin-bottom", &su);
380 PAIR_STYLE_INIT(&tag[1], h);
381 print_otag(h, TAG_DIV, 2, tag);
382 return(1);
383 }
384
385 PAIR_CLASS_INIT(&tag[0], "sec-head");
386 print_otag(h, TAG_DIV, 1, tag);
387 return(1);
388 }
389
390
391 /* ARGSUSED */
392 static int
393 man_alt_pre(MAN_ARGS)
394 {
395 const struct man_node *nn;
396 struct tag *t;
397 int i;
398 struct htmlpair tagi, tagb, tagr, *tagp;
399
400 PAIR_CLASS_INIT(&tagi, "italic");
401 PAIR_CLASS_INIT(&tagb, "bold");
402 PAIR_CLASS_INIT(&tagr, "roman");
403
404 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
405 switch (n->tok) {
406 case (MAN_BI):
407 tagp = i % 2 ? &tagi : &tagb;
408 break;
409 case (MAN_IB):
410 tagp = i % 2 ? &tagb : &tagi;
411 break;
412 case (MAN_RI):
413 tagp = i % 2 ? &tagi : &tagr;
414 break;
415 case (MAN_IR):
416 tagp = i % 2 ? &tagr : &tagi;
417 break;
418 case (MAN_BR):
419 tagp = i % 2 ? &tagr : &tagb;
420 break;
421 case (MAN_RB):
422 tagp = i % 2 ? &tagb : &tagr;
423 break;
424 default:
425 abort();
426 /* NOTREACHED */
427 }
428
429 if (i)
430 h->flags |= HTML_NOSPACE;
431
432 t = print_otag(h, TAG_SPAN, 1, tagp);
433 print_man_node(m, nn, h);
434 print_tagq(h, t);
435 }
436
437 return(0);
438 }
439
440
441 /* ARGSUSED */
442 static int
443 man_SB_pre(MAN_ARGS)
444 {
445 struct htmlpair tag;
446
447 PAIR_CLASS_INIT(&tag, "small bold");
448 print_otag(h, TAG_SPAN, 1, &tag);
449 return(1);
450 }
451
452
453 /* ARGSUSED */
454 static int
455 man_SM_pre(MAN_ARGS)
456 {
457 struct htmlpair tag;
458
459 PAIR_CLASS_INIT(&tag, "small");
460 print_otag(h, TAG_SPAN, 1, &tag);
461 return(1);
462 }
463
464
465 /* ARGSUSED */
466 static int
467 man_SS_pre(MAN_ARGS)
468 {
469 struct htmlpair tag[3];
470 struct roffsu su;
471
472 SCALE_VS_INIT(&su, 1);
473
474 if (MAN_BODY == n->type) {
475 PAIR_CLASS_INIT(&tag[0], "ssec-body");
476 if (n->parent->next && n->child) {
477 bufcat_su(h, "margin-bottom", &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 } else if (MAN_BLOCK == n->type) {
486 PAIR_CLASS_INIT(&tag[0], "ssec-block");
487 if (n->prev && MAN_SS == n->prev->tok)
488 if (n->prev->body->child) {
489 bufcat_su(h, "margin-top", &su);
490 PAIR_STYLE_INIT(&tag[1], h);
491 print_otag(h, TAG_DIV, 2, tag);
492 return(1);
493 }
494
495 print_otag(h, TAG_DIV, 1, tag);
496 return(1);
497 }
498
499 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
500 bufcat_su(h, "margin-left", &su);
501 PAIR_CLASS_INIT(&tag[0], "ssec-head");
502 PAIR_STYLE_INIT(&tag[1], h);
503 print_otag(h, TAG_DIV, 2, tag);
504 return(1);
505 }
506
507
508 /* ARGSUSED */
509 static int
510 man_PP_pre(MAN_ARGS)
511 {
512 struct htmlpair tag;
513 struct roffsu su;
514 int i;
515
516 if (MAN_BLOCK != n->type)
517 return(1);
518
519 i = 0;
520
521 if (MAN_ROOT == n->parent->tok) {
522 SCALE_HS_INIT(&su, INDENT);
523 bufcat_su(h, "margin-left", &su);
524 i++;
525 }
526 if (n->next && n->next->child) {
527 SCALE_VS_INIT(&su, 1);
528 bufcat_su(h, "margin-bottom", &su);
529 i++;
530 }
531
532 PAIR_STYLE_INIT(&tag, h);
533 print_otag(h, TAG_DIV, i ? 1 : 0, &tag);
534 return(1);
535 }
536
537
538 /* ARGSUSED */
539 static int
540 man_IP_pre(MAN_ARGS)
541 {
542 struct roffsu su;
543 struct htmlpair tag;
544 const struct man_node *nn;
545 int width;
546
547 /*
548 * This scattering of 1-BU margins and pads is to make sure that
549 * when text overruns its box, the subsequent text isn't flush
550 * up against it. However, the rest of the right-hand box must
551 * also be adjusted in consideration of this 1-BU space.
552 */
553
554 if (MAN_BODY == n->type) {
555 SCALE_HS_INIT(&su, INDENT);
556 bufcat_su(h, "margin-left", &su);
557 PAIR_STYLE_INIT(&tag, h);
558 print_otag(h, TAG_DIV, 1, &tag);
559 return(1);
560 }
561
562 nn = MAN_BLOCK == n->type ?
563 n->head->child : n->parent->head->child;
564
565 SCALE_HS_INIT(&su, INDENT);
566 width = 0;
567
568 if (MAN_IP == n->tok && NULL != nn)
569 if (NULL != (nn = nn->next)) {
570 for ( ; nn->next; nn = nn->next)
571 /* Do nothing. */ ;
572 width = a2width(nn, &su);
573 }
574
575 if (MAN_TP == n->tok && NULL != nn)
576 width = a2width(nn, &su);
577
578 if (MAN_BLOCK == n->type) {
579 bufcat_su(h, "margin-left", &su);
580 SCALE_VS_INIT(&su, 1);
581 bufcat_su(h, "margin-top", &su);
582 bufcat_style(h, "clear", "both");
583 PAIR_STYLE_INIT(&tag, h);
584 print_otag(h, TAG_DIV, 1, &tag);
585 return(1);
586 }
587
588 bufcat_su(h, "min-width", &su);
589 SCALE_INVERT(&su);
590 bufcat_su(h, "margin-left", &su);
591 SCALE_HS_INIT(&su, 1);
592 bufcat_su(h, "margin-right", &su);
593 bufcat_style(h, "clear", "left");
594
595 if (n->next && n->next->child)
596 bufcat_style(h, "float", "left");
597
598 PAIR_STYLE_INIT(&tag, h);
599 print_otag(h, TAG_DIV, 1, &tag);
600
601 /* With a length string, manually omit the last child. */
602
603 if ( ! width)
604 return(1);
605
606 if (MAN_IP == n->tok)
607 for (nn = n->child; nn->next; nn = nn->next)
608 print_man_node(m, nn, h);
609 if (MAN_TP == n->tok)
610 for (nn = n->child->next; nn; nn = nn->next)
611 print_man_node(m, nn, h);
612
613 return(0);
614 }
615
616
617 /* ARGSUSED */
618 static int
619 man_HP_pre(MAN_ARGS)
620 {
621 const struct man_node *nn;
622 struct htmlpair tag;
623 struct roffsu su;
624
625 if (MAN_HEAD == n->type)
626 return(0);
627
628 nn = MAN_BLOCK == n->type ?
629 n->head->child : n->parent->head->child;
630
631 SCALE_HS_INIT(&su, INDENT);
632
633 if (NULL != nn)
634 (void)a2width(nn, &su);
635
636 if (MAN_BLOCK == n->type) {
637 bufcat_su(h, "margin-left", &su);
638 SCALE_VS_INIT(&su, 1);
639 bufcat_su(h, "margin-top", &su);
640 bufcat_style(h, "clear", "both");
641 PAIR_STYLE_INIT(&tag, h);
642 print_otag(h, TAG_DIV, 1, &tag);
643 return(1);
644 }
645
646 bufcat_su(h, "margin-left", &su);
647 SCALE_INVERT(&su);
648 bufcat_su(h, "text-indent", &su);
649
650 PAIR_STYLE_INIT(&tag, h);
651 print_otag(h, TAG_DIV, 1, &tag);
652 return(1);
653 }
654
655
656 /* ARGSUSED */
657 static int
658 man_B_pre(MAN_ARGS)
659 {
660 struct htmlpair tag;
661
662 PAIR_CLASS_INIT(&tag, "bold");
663 print_otag(h, TAG_SPAN, 1, &tag);
664 return(1);
665 }
666
667
668 /* ARGSUSED */
669 static int
670 man_I_pre(MAN_ARGS)
671 {
672 struct htmlpair tag;
673
674 PAIR_CLASS_INIT(&tag, "italic");
675 print_otag(h, TAG_SPAN, 1, &tag);
676 return(1);
677 }
678
679
680 /* ARGSUSED */
681 static int
682 man_ign_pre(MAN_ARGS)
683 {
684
685 return(0);
686 }
687
688
689 /* ARGSUSED */
690 static int
691 man_RS_pre(MAN_ARGS)
692 {
693 struct htmlpair tag;
694 struct roffsu su;
695
696 if (MAN_HEAD == n->type)
697 return(0);
698 else if (MAN_BODY == n->type)
699 return(1);
700
701 SCALE_HS_INIT(&su, INDENT);
702 bufcat_su(h, "margin-left", &su);
703
704 if (n->head->child) {
705 SCALE_VS_INIT(&su, 1);
706 a2width(n->head->child, &su);
707 bufcat_su(h, "margin-top", &su);
708 }
709
710 PAIR_STYLE_INIT(&tag, h);
711 print_otag(h, TAG_DIV, 1, &tag);
712 return(1);
713 }