]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
f3aab2e2030c4e6723b784cfc1dfd697c4f59932
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.75 2011/06/29 15:38:09 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "mandoc.h"
30 #include "out.h"
31 #include "html.h"
32 #include "man.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 #define HALFINDENT 3
40
41 #define MAN_ARGS const struct man_meta *m, \
42 const struct man_node *n, \
43 struct mhtml *mh, \
44 struct html *h
45
46 struct mhtml {
47 int fl;
48 #define MANH_LITERAL (1 << 0) /* literal context */
49 };
50
51 struct htmlman {
52 int (*pre)(MAN_ARGS);
53 int (*post)(MAN_ARGS);
54 };
55
56 static void print_bvspace(struct html *,
57 const struct man_node *);
58 static void print_man(MAN_ARGS);
59 static void print_man_head(MAN_ARGS);
60 static void print_man_nodelist(MAN_ARGS);
61 static void print_man_node(MAN_ARGS);
62
63 static int a2width(const struct man_node *,
64 struct roffsu *);
65
66 static int man_alt_pre(MAN_ARGS);
67 static int man_br_pre(MAN_ARGS);
68 static int man_ign_pre(MAN_ARGS);
69 static int man_in_pre(MAN_ARGS);
70 static int man_literal_pre(MAN_ARGS);
71 static void man_root_post(MAN_ARGS);
72 static void man_root_pre(MAN_ARGS);
73 static int man_B_pre(MAN_ARGS);
74 static int man_HP_pre(MAN_ARGS);
75 static int man_I_pre(MAN_ARGS);
76 static int man_IP_pre(MAN_ARGS);
77 static int man_PP_pre(MAN_ARGS);
78 static int man_RS_pre(MAN_ARGS);
79 static int man_SH_pre(MAN_ARGS);
80 static int man_SM_pre(MAN_ARGS);
81 static int man_SS_pre(MAN_ARGS);
82
83 static const struct htmlman mans[MAN_MAX] = {
84 { man_br_pre, NULL }, /* br */
85 { NULL, NULL }, /* TH */
86 { man_SH_pre, NULL }, /* SH */
87 { man_SS_pre, NULL }, /* SS */
88 { man_IP_pre, NULL }, /* TP */
89 { man_PP_pre, NULL }, /* LP */
90 { man_PP_pre, NULL }, /* PP */
91 { man_PP_pre, NULL }, /* P */
92 { man_IP_pre, NULL }, /* IP */
93 { man_HP_pre, NULL }, /* HP */
94 { man_SM_pre, NULL }, /* SM */
95 { man_SM_pre, NULL }, /* SB */
96 { man_alt_pre, NULL }, /* BI */
97 { man_alt_pre, NULL }, /* IB */
98 { man_alt_pre, NULL }, /* BR */
99 { man_alt_pre, NULL }, /* RB */
100 { NULL, NULL }, /* R */
101 { man_B_pre, NULL }, /* B */
102 { man_I_pre, NULL }, /* I */
103 { man_alt_pre, NULL }, /* IR */
104 { man_alt_pre, NULL }, /* RI */
105 { man_ign_pre, NULL }, /* na */
106 { man_br_pre, NULL }, /* sp */
107 { man_literal_pre, NULL }, /* nf */
108 { man_literal_pre, NULL }, /* fi */
109 { NULL, NULL }, /* RE */
110 { man_RS_pre, NULL }, /* RS */
111 { man_ign_pre, NULL }, /* DT */
112 { man_ign_pre, NULL }, /* UC */
113 { man_ign_pre, NULL }, /* PD */
114 { man_ign_pre, NULL }, /* AT */
115 { man_in_pre, NULL }, /* in */
116 { man_ign_pre, NULL }, /* ft */
117 };
118
119 /*
120 * Printing leading vertical space before a block.
121 * This is used for the paragraph macros.
122 * The rules are pretty simple, since there's very little nesting going
123 * on here. Basically, if we're the first within another block (SS/SH),
124 * then don't emit vertical space. If we are (RS), then do. If not the
125 * first, print it.
126 */
127 static void
128 print_bvspace(struct html *h, const struct man_node *n)
129 {
130
131 if (n->body && n->body->child)
132 if (MAN_TBL == n->body->child->type)
133 return;
134
135 if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
136 if (NULL == n->prev)
137 return;
138
139 print_otag(h, TAG_P, 0, NULL);
140 }
141
142 void
143 html_man(void *arg, const struct man *m)
144 {
145 struct html *h;
146 struct tag *t;
147 struct mhtml mh;
148
149 h = (struct html *)arg;
150
151 print_gen_decls(h);
152
153 memset(&mh, 0, sizeof(struct mhtml));
154
155 t = print_otag(h, TAG_HTML, 0, NULL);
156 print_man(man_meta(m), man_node(m), &mh, h);
157 print_tagq(h, t);
158
159 printf("\n");
160 }
161
162 static void
163 print_man(MAN_ARGS)
164 {
165 struct tag *t;
166
167 t = print_otag(h, TAG_HEAD, 0, NULL);
168 print_man_head(m, n, mh, h);
169 print_tagq(h, t);
170
171 t = print_otag(h, TAG_BODY, 0, NULL);
172 print_man_nodelist(m, n, mh, h);
173 print_tagq(h, t);
174 }
175
176
177 /* ARGSUSED */
178 static void
179 print_man_head(MAN_ARGS)
180 {
181
182 print_gen_head(h);
183 bufcat_fmt(h, "%s(%s)", m->title, m->msec);
184 print_otag(h, TAG_TITLE, 0, NULL);
185 print_text(h, h->buf);
186 }
187
188
189 static void
190 print_man_nodelist(MAN_ARGS)
191 {
192
193 print_man_node(m, n, mh, h);
194 if (n->next)
195 print_man_nodelist(m, n->next, mh, h);
196 }
197
198
199 static void
200 print_man_node(MAN_ARGS)
201 {
202 int child;
203 struct tag *t;
204 struct htmlpair tag;
205
206 child = 1;
207 t = h->tags.head;
208
209 switch (n->type) {
210 case (MAN_ROOT):
211 man_root_pre(m, n, mh, h);
212 break;
213 case (MAN_TEXT):
214 /*
215 * If we have a blank line, output a vertical space.
216 * If we have a space as the first character, break
217 * before printing the line's data.
218 */
219 if ('\0' == *n->string) {
220 print_otag(h, TAG_P, 0, NULL);
221 return;
222 } else if (' ' == *n->string && MAN_LINE & n->flags)
223 print_otag(h, TAG_BR, 0, NULL);
224
225 print_text(h, n->string);
226
227 /*
228 * If we're in a literal context, make sure that words
229 * togehter on the same line stay together. This is a
230 * POST-printing call, so we check the NEXT word. Since
231 * -man doesn't have nested macros, we don't need to be
232 * more specific than this.
233 */
234 if (MANH_LITERAL & mh->fl &&
235 (NULL == n->next ||
236 n->next->line > n->line))
237 print_otag(h, TAG_BR, 0, NULL);
238 return;
239 case (MAN_EQN):
240 PAIR_CLASS_INIT(&tag, "eqn");
241 print_otag(h, TAG_SPAN, 1, &tag);
242 print_text(h, n->eqn->data);
243 break;
244 case (MAN_TBL):
245 /*
246 * This will take care of initialising all of the table
247 * state data for the first table, then tearing it down
248 * for the last one.
249 */
250 print_tbl(h, n->span);
251 return;
252 default:
253 /*
254 * Close out scope of font prior to opening a macro
255 * scope.
256 */
257 if (HTMLFONT_NONE != h->metac) {
258 h->metal = h->metac;
259 h->metac = HTMLFONT_NONE;
260 }
261
262 /*
263 * Close out the current table, if it's open, and unset
264 * the "meta" table state. This will be reopened on the
265 * next table element.
266 */
267 if (h->tblt) {
268 print_tblclose(h);
269 t = h->tags.head;
270 }
271 if (mans[n->tok].pre)
272 child = (*mans[n->tok].pre)(m, n, mh, h);
273 break;
274 }
275
276 if (child && n->child)
277 print_man_nodelist(m, n->child, mh, h);
278
279 /* This will automatically close out any font scope. */
280 print_stagq(h, t);
281
282 switch (n->type) {
283 case (MAN_ROOT):
284 man_root_post(m, n, mh, h);
285 break;
286 case (MAN_EQN):
287 break;
288 default:
289 if (mans[n->tok].post)
290 (*mans[n->tok].post)(m, n, mh, h);
291 break;
292 }
293 }
294
295
296 static int
297 a2width(const struct man_node *n, struct roffsu *su)
298 {
299
300 if (MAN_TEXT != n->type)
301 return(0);
302 if (a2roffsu(n->string, su, SCALE_BU))
303 return(1);
304
305 return(0);
306 }
307
308
309 /* ARGSUSED */
310 static void
311 man_root_pre(MAN_ARGS)
312 {
313 struct htmlpair tag[3];
314 struct tag *t, *tt;
315 char b[BUFSIZ], title[BUFSIZ];
316
317 b[0] = 0;
318 if (m->vol)
319 (void)strlcat(b, m->vol, BUFSIZ);
320
321 snprintf(title, BUFSIZ - 1, "%s(%s)", m->title, m->msec);
322
323 PAIR_SUMMARY_INIT(&tag[0], "Document Header");
324 PAIR_CLASS_INIT(&tag[1], "head");
325 if (NULL == h->style) {
326 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
327 t = print_otag(h, TAG_TABLE, 3, tag);
328 PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
329 print_otag(h, TAG_COL, 1, tag);
330 print_otag(h, TAG_COL, 1, tag);
331 print_otag(h, TAG_COL, 1, tag);
332 } else
333 t = print_otag(h, TAG_TABLE, 2, tag);
334
335 print_otag(h, TAG_TBODY, 0, NULL);
336
337 tt = print_otag(h, TAG_TR, 0, NULL);
338
339 PAIR_CLASS_INIT(&tag[0], "head-ltitle");
340 print_otag(h, TAG_TD, 1, tag);
341
342 print_text(h, title);
343 print_stagq(h, tt);
344
345 PAIR_CLASS_INIT(&tag[0], "head-vol");
346 if (NULL == h->style) {
347 PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
348 print_otag(h, TAG_TD, 2, tag);
349 } else
350 print_otag(h, TAG_TD, 1, tag);
351
352 print_text(h, b);
353 print_stagq(h, tt);
354
355 PAIR_CLASS_INIT(&tag[0], "head-rtitle");
356 if (NULL == h->style) {
357 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
358 print_otag(h, TAG_TD, 2, tag);
359 } else
360 print_otag(h, TAG_TD, 1, tag);
361
362 print_text(h, title);
363 print_tagq(h, t);
364 }
365
366
367 /* ARGSUSED */
368 static void
369 man_root_post(MAN_ARGS)
370 {
371 struct htmlpair tag[3];
372 struct tag *t, *tt;
373
374 PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
375 PAIR_CLASS_INIT(&tag[1], "foot");
376 if (NULL == h->style) {
377 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
378 t = print_otag(h, TAG_TABLE, 3, tag);
379 PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
380 print_otag(h, TAG_COL, 1, tag);
381 print_otag(h, TAG_COL, 1, tag);
382 } else
383 t = print_otag(h, TAG_TABLE, 2, tag);
384
385 tt = print_otag(h, TAG_TR, 0, NULL);
386
387 PAIR_CLASS_INIT(&tag[0], "foot-date");
388 print_otag(h, TAG_TD, 1, tag);
389
390 print_text(h, m->date);
391 print_stagq(h, tt);
392
393 PAIR_CLASS_INIT(&tag[0], "foot-os");
394 if (NULL == h->style) {
395 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
396 print_otag(h, TAG_TD, 2, tag);
397 } else
398 print_otag(h, TAG_TD, 1, tag);
399
400 if (m->source)
401 print_text(h, m->source);
402 print_tagq(h, t);
403 }
404
405
406
407 /* ARGSUSED */
408 static int
409 man_br_pre(MAN_ARGS)
410 {
411 struct roffsu su;
412 struct htmlpair tag;
413
414 SCALE_VS_INIT(&su, 1);
415
416 if (MAN_sp == n->tok) {
417 if (NULL != (n = n->child))
418 if ( ! a2roffsu(n->string, &su, SCALE_VS))
419 SCALE_VS_INIT(&su, atoi(n->string));
420 } else
421 su.scale = 0;
422
423 bufinit(h);
424 bufcat_su(h, "height", &su);
425 PAIR_STYLE_INIT(&tag, h);
426 print_otag(h, TAG_DIV, 1, &tag);
427
428 /* So the div isn't empty: */
429 print_text(h, "\\~");
430
431 return(0);
432 }
433
434
435 /* ARGSUSED */
436 static int
437 man_SH_pre(MAN_ARGS)
438 {
439 struct htmlpair tag;
440
441 if (MAN_BLOCK == n->type) {
442 PAIR_CLASS_INIT(&tag, "section");
443 print_otag(h, TAG_DIV, 1, &tag);
444 return(1);
445 } else if (MAN_BODY == n->type)
446 return(1);
447
448 print_otag(h, TAG_H1, 0, NULL);
449 return(1);
450 }
451
452
453 /* ARGSUSED */
454 static int
455 man_alt_pre(MAN_ARGS)
456 {
457 const struct man_node *nn;
458 int i;
459 enum htmltag fp;
460 struct tag *t;
461
462 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
463 t = NULL;
464 switch (n->tok) {
465 case (MAN_BI):
466 fp = i % 2 ? TAG_I : TAG_B;
467 break;
468 case (MAN_IB):
469 fp = i % 2 ? TAG_B : TAG_I;
470 break;
471 case (MAN_RI):
472 fp = i % 2 ? TAG_I : TAG_MAX;
473 break;
474 case (MAN_IR):
475 fp = i % 2 ? TAG_MAX : TAG_I;
476 break;
477 case (MAN_BR):
478 fp = i % 2 ? TAG_MAX : TAG_B;
479 break;
480 case (MAN_RB):
481 fp = i % 2 ? TAG_B : TAG_MAX;
482 break;
483 default:
484 abort();
485 /* NOTREACHED */
486 }
487
488 if (i)
489 h->flags |= HTML_NOSPACE;
490
491 if (TAG_MAX != fp)
492 t = print_otag(h, fp, 0, NULL);
493
494 print_man_node(m, nn, mh, h);
495
496 if (t)
497 print_tagq(h, t);
498 }
499
500 return(0);
501 }
502
503
504 /* ARGSUSED */
505 static int
506 man_SM_pre(MAN_ARGS)
507 {
508
509 print_otag(h, TAG_SMALL, 0, NULL);
510 if (MAN_SB == n->tok)
511 print_otag(h, TAG_B, 0, NULL);
512 return(1);
513 }
514
515
516 /* ARGSUSED */
517 static int
518 man_SS_pre(MAN_ARGS)
519 {
520 struct htmlpair tag;
521
522 if (MAN_BLOCK == n->type) {
523 PAIR_CLASS_INIT(&tag, "subsection");
524 print_otag(h, TAG_DIV, 1, &tag);
525 return(1);
526 } else if (MAN_BODY == n->type)
527 return(1);
528
529 print_otag(h, TAG_H2, 0, NULL);
530 return(1);
531 }
532
533
534 /* ARGSUSED */
535 static int
536 man_PP_pre(MAN_ARGS)
537 {
538
539 if (MAN_HEAD == n->type)
540 return(0);
541 else if (MAN_BLOCK == n->type)
542 print_bvspace(h, n);
543
544 return(1);
545 }
546
547
548 /* ARGSUSED */
549 static int
550 man_IP_pre(MAN_ARGS)
551 {
552 struct roffsu su;
553 struct htmlpair tag;
554 const struct man_node *nn;
555
556 /*
557 * This scattering of 1-BU margins and pads is to make sure that
558 * when text overruns its box, the subsequent text isn't flush
559 * up against it. However, the rest of the right-hand box must
560 * also be adjusted in consideration of this 1-BU space.
561 */
562
563 if (MAN_BODY == n->type) {
564 print_otag(h, TAG_TD, 0, NULL);
565 return(1);
566 }
567
568 nn = MAN_BLOCK == n->type ?
569 n->head->child : n->parent->head->child;
570
571 SCALE_HS_INIT(&su, INDENT);
572
573 /* Width is the second token. */
574
575 if (MAN_IP == n->tok && NULL != nn)
576 if (NULL != (nn = nn->next))
577 a2width(nn, &su);
578
579 /* Width is the first token. */
580
581 if (MAN_TP == n->tok && NULL != nn) {
582 /* Skip past non-text children. */
583 while (nn && MAN_TEXT != nn->type)
584 nn = nn->next;
585 if (nn)
586 a2width(nn, &su);
587 }
588
589 if (MAN_BLOCK == n->type) {
590 print_bvspace(h, n);
591 print_otag(h, TAG_TABLE, 0, NULL);
592 bufinit(h);
593 bufcat_su(h, "width", &su);
594 PAIR_STYLE_INIT(&tag, h);
595 print_otag(h, TAG_COL, 1, &tag);
596 print_otag(h, TAG_COL, 0, NULL);
597 print_otag(h, TAG_TBODY, 0, NULL);
598 print_otag(h, TAG_TR, 0, NULL);
599 return(1);
600 }
601
602 print_otag(h, TAG_TD, 0, NULL);
603
604 /* For IP, only print the first header element. */
605
606 if (MAN_IP == n->tok && n->child)
607 print_man_node(m, n->child, mh, h);
608
609 /* For TP, only print next-line header elements. */
610
611 if (MAN_TP == n->tok)
612 for (nn = n->child; nn; nn = nn->next)
613 if (nn->line > n->line)
614 print_man_node(m, nn, mh, h);
615
616 return(0);
617 }
618
619
620 /* ARGSUSED */
621 static int
622 man_HP_pre(MAN_ARGS)
623 {
624 struct htmlpair tag;
625 struct roffsu su;
626 const struct man_node *np;
627
628 bufinit(h);
629
630 np = MAN_BLOCK == n->type ?
631 n->head->child :
632 n->parent->head->child;
633
634 if (NULL == np || ! a2width(np, &su))
635 SCALE_HS_INIT(&su, INDENT);
636
637 if (MAN_HEAD == n->type) {
638 print_otag(h, TAG_TD, 0, NULL);
639 return(0);
640 } else if (MAN_BLOCK == n->type) {
641 print_bvspace(h, n);
642 print_otag(h, TAG_TABLE, 0, NULL);
643 bufcat_su(h, "width", &su);
644 PAIR_STYLE_INIT(&tag, h);
645 print_otag(h, TAG_COL, 1, &tag);
646 print_otag(h, TAG_COL, 0, NULL);
647 print_otag(h, TAG_TBODY, 0, NULL);
648 print_otag(h, TAG_TR, 0, NULL);
649 return(1);
650 }
651
652 su.scale = -su.scale;
653 bufcat_su(h, "text-indent", &su);
654 PAIR_STYLE_INIT(&tag, h);
655 print_otag(h, TAG_TD, 1, &tag);
656 return(1);
657 }
658
659
660 /* ARGSUSED */
661 static int
662 man_B_pre(MAN_ARGS)
663 {
664
665 print_otag(h, TAG_B, 0, NULL);
666 return(1);
667 }
668
669
670 /* ARGSUSED */
671 static int
672 man_I_pre(MAN_ARGS)
673 {
674
675 print_otag(h, TAG_I, 0, NULL);
676 return(1);
677 }
678
679
680 /* ARGSUSED */
681 static int
682 man_literal_pre(MAN_ARGS)
683 {
684
685 if (MAN_nf == n->tok) {
686 print_otag(h, TAG_BR, 0, NULL);
687 mh->fl |= MANH_LITERAL;
688 } else
689 mh->fl &= ~MANH_LITERAL;
690
691 return(0);
692 }
693
694
695 /* ARGSUSED */
696 static int
697 man_in_pre(MAN_ARGS)
698 {
699
700 print_otag(h, TAG_BR, 0, NULL);
701 return(0);
702 }
703
704
705 /* ARGSUSED */
706 static int
707 man_ign_pre(MAN_ARGS)
708 {
709
710 return(0);
711 }
712
713
714 /* ARGSUSED */
715 static int
716 man_RS_pre(MAN_ARGS)
717 {
718 struct htmlpair tag;
719 struct roffsu su;
720
721 if (MAN_HEAD == n->type)
722 return(0);
723 else if (MAN_BODY == n->type)
724 return(1);
725
726 SCALE_HS_INIT(&su, INDENT);
727 if (n->head->child)
728 a2width(n->head->child, &su);
729
730 bufinit(h);
731 bufcat_su(h, "margin-left", &su);
732 PAIR_STYLE_INIT(&tag, h);
733 print_otag(h, TAG_DIV, 1, &tag);
734 return(1);
735 }