1 /* $Id: man_html.c,v 1.26 2010/01/29 14:39:38 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
17 #include <sys/types.h>
30 /* TODO: preserve ident widths. */
31 /* FIXME: have PD set the default vspace width. */
36 #define MAN_ARGS const struct man_meta *m, \
37 const struct man_node *n, \
42 int (*post)(MAN_ARGS);
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);
50 static int a2width(const struct man_node *,
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);
69 static const struct htmlman mans[MAN_MAX] = {
70 { man_br_pre, NULL }, /* br */
71 { NULL, NULL }, /* TH */
72 { man_SH_pre, NULL }, /* SH */
73 { man_SS_pre, NULL }, /* SS */
74 { man_IP_pre, NULL }, /* TP */
75 { man_PP_pre, NULL }, /* LP */
76 { man_PP_pre, NULL }, /* PP */
77 { man_PP_pre, NULL }, /* P */
78 { man_IP_pre, NULL }, /* IP */
79 { man_HP_pre, NULL }, /* HP */
80 { man_SM_pre, NULL }, /* SM */
81 { man_SB_pre, NULL }, /* SB */
82 { man_alt_pre, NULL }, /* BI */
83 { man_alt_pre, NULL }, /* IB */
84 { man_alt_pre, NULL }, /* BR */
85 { man_alt_pre, NULL }, /* RB */
86 { NULL, NULL }, /* R */
87 { man_B_pre, NULL }, /* B */
88 { man_I_pre, NULL }, /* I */
89 { man_alt_pre, NULL }, /* IR */
90 { man_alt_pre, NULL }, /* RI */
91 { NULL, NULL }, /* na */
92 { NULL, NULL }, /* i */
93 { man_br_pre, NULL }, /* sp */
94 { NULL, NULL }, /* nf */
95 { NULL, NULL }, /* fi */
96 { NULL, NULL }, /* r */
97 { NULL, NULL }, /* RE */
98 { man_RS_pre, NULL }, /* RS */
99 { man_ign_pre, NULL }, /* DT */
100 { man_ign_pre, NULL }, /* UC */
101 { man_ign_pre, NULL }, /* PD */
106 html_man(void *arg, const struct man *m)
111 h = (struct html *)arg;
115 t = print_otag(h, TAG_HTML, 0, NULL);
116 print_man(man_meta(m), man_node(m), h);
129 t = print_otag(h, TAG_HEAD, 0, NULL);
131 print_man_head(m, n, h);
133 t = print_otag(h, TAG_BODY, 0, NULL);
135 tag.key = ATTR_CLASS;
137 print_otag(h, TAG_DIV, 1, &tag);
139 print_man_nodelist(m, n, h);
147 print_man_head(MAN_ARGS)
152 buffmt(h, "%s(%d)", m->title, m->msec);
154 print_otag(h, TAG_TITLE, 0, NULL);
155 print_text(h, h->buf);
160 print_man_nodelist(MAN_ARGS)
163 print_man_node(m, n, h);
165 print_man_nodelist(m, n->next, h);
170 print_man_node(MAN_ARGS)
182 child = man_root_pre(m, n, h);
185 print_text(h, n->string);
189 * Close out scope of font prior to opening a macro
190 * scope. Assert that the metafont is on the top of the
191 * stack (it's never nested).
194 assert(h->metaf == t);
195 print_tagq(h, h->metaf);
196 assert(NULL == h->metaf);
199 if (mans[n->tok].pre)
200 child = (*mans[n->tok].pre)(m, n, h);
204 if (child && n->child)
205 print_man_nodelist(m, n->child, h);
207 /* This will automatically close out any font scope. */
214 man_root_post(m, n, h);
219 if (mans[n->tok].post)
220 (*mans[n->tok].post)(m, n, h);
227 a2width(const struct man_node *n, struct roffsu *su)
230 if (MAN_TEXT != n->type)
232 if (a2roffsu(n->string, su, SCALE_BU))
241 man_root_pre(MAN_ARGS)
243 struct htmlpair tag[3];
245 char b[BUFSIZ], title[BUFSIZ];
249 (void)strlcat(b, m->vol, BUFSIZ);
251 snprintf(title, BUFSIZ - 1, "%s(%d)", m->title, m->msec);
253 PAIR_CLASS_INIT(&tag[0], "header");
254 bufcat_style(h, "width", "100%");
255 PAIR_STYLE_INIT(&tag[1], h);
256 PAIR_SUMMARY_INIT(&tag[2], "header");
258 t = print_otag(h, TAG_TABLE, 3, tag);
259 tt = print_otag(h, TAG_TR, 0, NULL);
262 bufcat_style(h, "width", "10%");
263 PAIR_STYLE_INIT(&tag[0], h);
264 print_otag(h, TAG_TD, 1, tag);
265 print_text(h, title);
269 bufcat_style(h, "width", "80%");
270 bufcat_style(h, "white-space", "nowrap");
271 bufcat_style(h, "text-align", "center");
272 PAIR_STYLE_INIT(&tag[0], h);
273 print_otag(h, TAG_TD, 1, tag);
278 bufcat_style(h, "width", "10%");
279 bufcat_style(h, "text-align", "right");
280 PAIR_STYLE_INIT(&tag[0], h);
281 print_otag(h, TAG_TD, 1, tag);
282 print_text(h, title);
290 man_root_post(MAN_ARGS)
292 struct htmlpair tag[3];
296 time2a(m->date, b, DATESIZ);
298 PAIR_CLASS_INIT(&tag[0], "footer");
299 bufcat_style(h, "width", "100%");
300 PAIR_STYLE_INIT(&tag[1], h);
301 PAIR_SUMMARY_INIT(&tag[2], "footer");
303 t = print_otag(h, TAG_TABLE, 3, tag);
304 tt = print_otag(h, TAG_TR, 0, NULL);
307 bufcat_style(h, "width", "50%");
308 PAIR_STYLE_INIT(&tag[0], h);
309 print_otag(h, TAG_TD, 1, tag);
314 bufcat_style(h, "width", "50%");
315 bufcat_style(h, "text-align", "right");
316 PAIR_STYLE_INIT(&tag[0], h);
317 print_otag(h, TAG_TD, 1, tag);
319 print_text(h, m->source);
332 SCALE_VS_INIT(&su, 1);
334 if (MAN_sp == n->tok && n->child)
335 a2roffsu(n->child->string, &su, SCALE_VS);
336 else if (MAN_br == n->tok)
339 bufcat_su(h, "height", &su);
340 PAIR_STYLE_INIT(&tag, h);
341 print_otag(h, TAG_DIV, 1, &tag);
343 /* So the div isn't empty: */
344 print_text(h, "\\~");
354 struct htmlpair tag[2];
357 if (MAN_BODY == n->type) {
358 SCALE_HS_INIT(&su, INDENT);
359 bufcat_su(h, "margin-left", &su);
360 PAIR_CLASS_INIT(&tag[0], "sec-body");
361 PAIR_STYLE_INIT(&tag[1], h);
362 print_otag(h, TAG_DIV, 2, tag);
364 } else if (MAN_BLOCK == n->type) {
365 PAIR_CLASS_INIT(&tag[0], "sec-block");
366 if (n->prev && MAN_SH == n->prev->tok)
367 if (NULL == n->prev->body->child) {
368 print_otag(h, TAG_DIV, 1, tag);
372 SCALE_VS_INIT(&su, 1);
373 bufcat_su(h, "margin-top", &su);
375 bufcat_su(h, "margin-bottom", &su);
376 PAIR_STYLE_INIT(&tag[1], h);
377 print_otag(h, TAG_DIV, 2, tag);
381 PAIR_CLASS_INIT(&tag[0], "sec-head");
382 print_otag(h, TAG_DIV, 1, tag);
389 man_alt_pre(MAN_ARGS)
391 const struct man_node *nn;
396 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
399 fp = i % 2 ? HTMLFONT_ITALIC : HTMLFONT_BOLD;
402 fp = i % 2 ? HTMLFONT_BOLD : HTMLFONT_ITALIC;
405 fp = i % 2 ? HTMLFONT_ITALIC : HTMLFONT_NONE;
408 fp = i % 2 ? HTMLFONT_NONE : HTMLFONT_ITALIC;
411 fp = i % 2 ? HTMLFONT_NONE : HTMLFONT_BOLD;
414 fp = i % 2 ? HTMLFONT_BOLD : HTMLFONT_NONE;
422 h->flags |= HTML_NOSPACE;
425 * Open and close the scope with each argument, so that
426 * internal \f escapes, which are common, are also
427 * closed out with the scope.
429 t = print_ofont(h, fp);
430 print_man_node(m, nn, h);
444 /* FIXME: print_ofont(). */
445 PAIR_CLASS_INIT(&tag, "small bold");
446 print_otag(h, TAG_SPAN, 1, &tag);
457 PAIR_CLASS_INIT(&tag, "small");
458 print_otag(h, TAG_SPAN, 1, &tag);
467 struct htmlpair tag[3];
470 SCALE_VS_INIT(&su, 1);
472 if (MAN_BODY == n->type) {
473 PAIR_CLASS_INIT(&tag[0], "ssec-body");
474 if (n->parent->next && n->child) {
475 bufcat_su(h, "margin-bottom", &su);
476 PAIR_STYLE_INIT(&tag[1], h);
477 print_otag(h, TAG_DIV, 2, tag);
481 print_otag(h, TAG_DIV, 1, tag);
483 } else if (MAN_BLOCK == n->type) {
484 PAIR_CLASS_INIT(&tag[0], "ssec-block");
485 if (n->prev && MAN_SS == n->prev->tok)
486 if (n->prev->body->child) {
487 bufcat_su(h, "margin-top", &su);
488 PAIR_STYLE_INIT(&tag[1], h);
489 print_otag(h, TAG_DIV, 2, tag);
493 print_otag(h, TAG_DIV, 1, tag);
497 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
498 bufcat_su(h, "margin-left", &su);
499 PAIR_CLASS_INIT(&tag[0], "ssec-head");
500 PAIR_STYLE_INIT(&tag[1], h);
501 print_otag(h, TAG_DIV, 2, tag);
514 if (MAN_BLOCK != n->type)
519 if (MAN_ROOT == n->parent->type) {
520 SCALE_HS_INIT(&su, INDENT);
521 bufcat_su(h, "margin-left", &su);
525 SCALE_VS_INIT(&su, 1);
526 bufcat_su(h, "margin-top", &su);
530 PAIR_STYLE_INIT(&tag, h);
531 print_otag(h, TAG_DIV, i, &tag);
542 const struct man_node *nn;
546 * This scattering of 1-BU margins and pads is to make sure that
547 * when text overruns its box, the subsequent text isn't flush
548 * up against it. However, the rest of the right-hand box must
549 * also be adjusted in consideration of this 1-BU space.
552 if (MAN_BODY == n->type) {
553 SCALE_HS_INIT(&su, INDENT);
554 bufcat_su(h, "margin-left", &su);
555 PAIR_STYLE_INIT(&tag, h);
556 print_otag(h, TAG_DIV, 1, &tag);
560 nn = MAN_BLOCK == n->type ?
561 n->head->child : n->parent->head->child;
563 SCALE_HS_INIT(&su, INDENT);
566 if (MAN_IP == n->tok && NULL != nn)
567 if (NULL != (nn = nn->next)) {
568 for ( ; nn->next; nn = nn->next)
570 width = a2width(nn, &su);
573 if (MAN_TP == n->tok && NULL != nn)
574 width = a2width(nn, &su);
576 if (MAN_BLOCK == n->type) {
577 bufcat_su(h, "margin-left", &su);
578 SCALE_VS_INIT(&su, 1);
579 bufcat_su(h, "margin-top", &su);
580 bufcat_style(h, "clear", "both");
581 PAIR_STYLE_INIT(&tag, h);
582 print_otag(h, TAG_DIV, 1, &tag);
586 bufcat_su(h, "min-width", &su);
588 bufcat_su(h, "margin-left", &su);
589 SCALE_HS_INIT(&su, 1);
590 bufcat_su(h, "margin-right", &su);
591 bufcat_style(h, "clear", "left");
593 if (n->next && n->next->child)
594 bufcat_style(h, "float", "left");
596 PAIR_STYLE_INIT(&tag, h);
597 print_otag(h, TAG_DIV, 1, &tag);
599 /* With a length string, manually omit the last child. */
604 if (MAN_IP == n->tok)
605 for (nn = n->child; nn->next; nn = nn->next)
606 print_man_node(m, nn, h);
607 if (MAN_TP == n->tok)
608 for (nn = n->child->next; nn; nn = nn->next)
609 print_man_node(m, nn, h);
619 const struct man_node *nn;
623 if (MAN_HEAD == n->type)
626 nn = MAN_BLOCK == n->type ?
627 n->head->child : n->parent->head->child;
629 SCALE_HS_INIT(&su, INDENT);
632 (void)a2width(nn, &su);
634 if (MAN_BLOCK == n->type) {
635 bufcat_su(h, "margin-left", &su);
636 SCALE_VS_INIT(&su, 1);
637 bufcat_su(h, "margin-top", &su);
638 bufcat_style(h, "clear", "both");
639 PAIR_STYLE_INIT(&tag, h);
640 print_otag(h, TAG_DIV, 1, &tag);
644 bufcat_su(h, "margin-left", &su);
646 bufcat_su(h, "text-indent", &su);
648 PAIR_STYLE_INIT(&tag, h);
649 print_otag(h, TAG_DIV, 1, &tag);
659 print_ofont(h, HTMLFONT_BOLD);
669 print_ofont(h, HTMLFONT_ITALIC);
676 man_ign_pre(MAN_ARGS)
690 if (MAN_HEAD == n->type)
692 else if (MAN_BODY == n->type)
695 SCALE_HS_INIT(&su, INDENT);
696 bufcat_su(h, "margin-left", &su);
698 if (n->head->child) {
699 SCALE_VS_INIT(&su, 1);
700 a2width(n->head->child, &su);
701 bufcat_su(h, "margin-top", &su);
704 PAIR_STYLE_INIT(&tag, h);
705 print_otag(h, TAG_DIV, 1, &tag);