1 /* $Id: man_html.c,v 1.2 2009/10/27 21:40:07 schwarze 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>
31 /* TODO: preserve ident widths. */
32 /* FIXME: have PD set the default vspace width. */
37 #define MAN_ARGS const struct man_meta *m, \
38 const struct man_node *n, \
43 int (*post)(MAN_ARGS);
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);
51 static int a2width(const struct man_node *,
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);
70 static const struct htmlman mans[MAN_MAX] = {
71 { man_br_pre, NULL }, /* br */
72 { NULL, NULL }, /* TH */
73 { man_SH_pre, NULL }, /* SH */
74 { man_SS_pre, NULL }, /* SS */
75 { man_IP_pre, NULL }, /* TP */
76 { man_PP_pre, NULL }, /* LP */
77 { man_PP_pre, NULL }, /* PP */
78 { man_PP_pre, NULL }, /* P */
79 { man_IP_pre, NULL }, /* IP */
80 { man_HP_pre, NULL }, /* HP */
81 { man_SM_pre, NULL }, /* SM */
82 { man_SB_pre, NULL }, /* SB */
83 { man_alt_pre, NULL }, /* BI */
84 { man_alt_pre, NULL }, /* IB */
85 { man_alt_pre, NULL }, /* BR */
86 { man_alt_pre, NULL }, /* RB */
87 { NULL, NULL }, /* R */
88 { man_B_pre, NULL }, /* B */
89 { man_I_pre, NULL }, /* I */
90 { man_alt_pre, NULL }, /* IR */
91 { man_alt_pre, NULL }, /* RI */
92 { NULL, NULL }, /* na */
93 { NULL, NULL }, /* i */
94 { man_br_pre, NULL }, /* sp */
95 { NULL, NULL }, /* nf */
96 { NULL, NULL }, /* fi */
97 { NULL, NULL }, /* r */
98 { NULL, NULL }, /* RE */
99 { man_RS_pre, NULL }, /* RS */
100 { man_ign_pre, NULL }, /* DT */
101 { man_ign_pre, NULL }, /* UC */
102 { man_ign_pre, NULL }, /* PD */
107 html_man(void *arg, const struct man *m)
112 h = (struct html *)arg;
114 print_gen_doctype(h);
116 t = print_otag(h, TAG_HTML, 0, NULL);
117 print_man(man_meta(m), man_node(m), h);
130 t = print_otag(h, TAG_HEAD, 0, NULL);
132 print_man_head(m, n, h);
134 t = print_otag(h, TAG_BODY, 0, NULL);
136 tag.key = ATTR_CLASS;
138 print_otag(h, TAG_DIV, 1, &tag);
140 print_man_nodelist(m, n, h);
148 print_man_head(MAN_ARGS)
153 buffmt(h, "%s(%d)", m->title, m->msec);
155 print_otag(h, TAG_TITLE, 0, NULL);
156 print_text(h, h->buf);
161 print_man_nodelist(MAN_ARGS)
164 print_man_node(m, n, h);
166 print_man_nodelist(m, n->next, h);
171 print_man_node(MAN_ARGS)
183 child = man_root_pre(m, n, h);
186 print_text(h, n->string);
189 if (mans[n->tok].pre)
190 child = (*mans[n->tok].pre)(m, n, h);
194 if (child && n->child)
195 print_man_nodelist(m, n->child, h);
203 man_root_post(m, n, h);
208 if (mans[n->tok].post)
209 (*mans[n->tok].post)(m, n, h);
216 a2width(const struct man_node *n, struct roffsu *su)
219 if (MAN_TEXT != n->type)
221 if (a2roffsu(n->string, su, SCALE_BU))
230 man_root_pre(MAN_ARGS)
232 struct htmlpair tag[2];
234 char b[BUFSIZ], title[BUFSIZ];
238 (void)strlcat(b, m->vol, BUFSIZ);
240 (void)snprintf(title, BUFSIZ - 1,
241 "%s(%d)", m->title, m->msec);
243 PAIR_CLASS_INIT(&tag[0], "header");
244 bufcat_style(h, "width", "100%");
245 PAIR_STYLE_INIT(&tag[1], h);
246 t = print_otag(h, TAG_TABLE, 2, tag);
247 tt = print_otag(h, TAG_TR, 0, NULL);
250 bufcat_style(h, "width", "10%");
251 PAIR_STYLE_INIT(&tag[0], h);
252 print_otag(h, TAG_TD, 1, tag);
253 print_text(h, title);
257 bufcat_style(h, "width", "80%");
258 bufcat_style(h, "white-space", "nowrap");
259 bufcat_style(h, "text-align", "center");
260 PAIR_STYLE_INIT(&tag[0], h);
261 print_otag(h, TAG_TD, 1, tag);
266 bufcat_style(h, "width", "10%");
267 bufcat_style(h, "text-align", "right");
268 PAIR_STYLE_INIT(&tag[0], h);
269 print_otag(h, TAG_TD, 1, tag);
270 print_text(h, title);
278 man_root_post(MAN_ARGS)
280 struct htmlpair tag[2];
284 time2a(m->date, b, DATESIZ);
286 PAIR_CLASS_INIT(&tag[0], "footer");
287 bufcat_style(h, "width", "100%");
288 PAIR_STYLE_INIT(&tag[1], h);
289 t = print_otag(h, TAG_TABLE, 2, tag);
290 tt = print_otag(h, TAG_TR, 0, NULL);
293 bufcat_style(h, "width", "50%");
294 PAIR_STYLE_INIT(&tag[0], h);
295 print_otag(h, TAG_TD, 1, tag);
300 bufcat_style(h, "width", "50%");
301 bufcat_style(h, "text-align", "right");
302 PAIR_STYLE_INIT(&tag[0], h);
303 print_otag(h, TAG_TD, 1, tag);
305 print_text(h, m->source);
318 SCALE_VS_INIT(&su, 1);
320 if (MAN_sp == n->tok && n->child)
321 a2roffsu(n->child->string, &su, SCALE_VS);
322 else if (MAN_br == n->tok)
325 bufcat_su(h, "height", &su);
326 PAIR_STYLE_INIT(&tag, h);
327 print_otag(h, TAG_DIV, 1, &tag);
336 struct htmlpair tag[2];
339 if (MAN_BODY == n->type) {
340 SCALE_HS_INIT(&su, INDENT);
341 bufcat_su(h, "margin-left", &su);
342 PAIR_CLASS_INIT(&tag[0], "sec-body");
343 PAIR_STYLE_INIT(&tag[1], h);
344 print_otag(h, TAG_DIV, 2, tag);
346 } else if (MAN_BLOCK == n->type) {
347 PAIR_CLASS_INIT(&tag[0], "sec-block");
348 if (n->prev && MAN_SH == n->prev->tok)
349 if (NULL == n->prev->body->child) {
350 print_otag(h, TAG_DIV, 1, tag);
354 SCALE_VS_INIT(&su, 1);
355 bufcat_su(h, "margin-top", &su);
357 bufcat_su(h, "margin-bottom", &su);
358 PAIR_STYLE_INIT(&tag[1], h);
359 print_otag(h, TAG_DIV, 2, tag);
363 PAIR_CLASS_INIT(&tag[0], "sec-head");
364 print_otag(h, TAG_DIV, 1, tag);
371 man_alt_pre(MAN_ARGS)
373 const struct man_node *nn;
376 struct htmlpair tagi, tagb, *tagp;
378 PAIR_CLASS_INIT(&tagi, "italic");
379 PAIR_CLASS_INIT(&tagb, "bold");
381 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
384 tagp = i % 2 ? &tagi : &tagb;
387 tagp = i % 2 ? &tagb : &tagi;
390 tagp = i % 2 ? &tagi : NULL;
393 tagp = i % 2 ? NULL : &tagi;
396 tagp = i % 2 ? NULL : &tagb;
399 tagp = i % 2 ? &tagb : NULL;
407 h->flags |= HTML_NOSPACE;
410 t = print_otag(h, TAG_SPAN, 1, tagp);
411 print_man_node(m, nn, h);
414 print_man_node(m, nn, h);
427 PAIR_CLASS_INIT(&tag, "small bold");
428 print_otag(h, TAG_SPAN, 1, &tag);
439 PAIR_CLASS_INIT(&tag, "small");
440 print_otag(h, TAG_SPAN, 1, &tag);
449 struct htmlpair tag[3];
452 SCALE_VS_INIT(&su, 1);
454 if (MAN_BODY == n->type) {
455 PAIR_CLASS_INIT(&tag[0], "ssec-body");
456 if (n->parent->next && n->child) {
457 bufcat_su(h, "margin-bottom", &su);
458 PAIR_STYLE_INIT(&tag[1], h);
459 print_otag(h, TAG_DIV, 2, tag);
463 print_otag(h, TAG_DIV, 1, tag);
465 } else if (MAN_BLOCK == n->type) {
466 PAIR_CLASS_INIT(&tag[0], "ssec-block");
467 if (n->prev && MAN_SS == n->prev->tok)
468 if (n->prev->body->child) {
469 bufcat_su(h, "margin-top", &su);
470 PAIR_STYLE_INIT(&tag[1], h);
471 print_otag(h, TAG_DIV, 2, tag);
475 print_otag(h, TAG_DIV, 1, tag);
479 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
480 bufcat_su(h, "margin-left", &su);
481 PAIR_CLASS_INIT(&tag[0], "ssec-head");
482 PAIR_STYLE_INIT(&tag[1], h);
483 print_otag(h, TAG_DIV, 2, tag);
496 if (MAN_BLOCK != n->type)
501 if (MAN_ROOT == n->parent->tok) {
502 SCALE_HS_INIT(&su, INDENT);
503 bufcat_su(h, "margin-left", &su);
506 if (n->next && n->next->child) {
507 SCALE_VS_INIT(&su, 1);
508 bufcat_su(h, "margin-bottom", &su);
512 PAIR_STYLE_INIT(&tag, h);
513 print_otag(h, TAG_DIV, i ? 1 : 0, &tag);
524 const struct man_node *nn;
528 * This scattering of 1-BU margins and pads is to make sure that
529 * when text overruns its box, the subsequent text isn't flush
530 * up against it. However, the rest of the right-hand box must
531 * also be adjusted in consideration of this 1-BU space.
534 if (MAN_BODY == n->type) {
535 SCALE_HS_INIT(&su, INDENT);
536 bufcat_su(h, "margin-left", &su);
537 PAIR_STYLE_INIT(&tag, h);
538 print_otag(h, TAG_DIV, 1, &tag);
542 nn = MAN_BLOCK == n->type ?
543 n->head->child : n->parent->head->child;
545 SCALE_HS_INIT(&su, INDENT);
548 if (MAN_IP == n->tok && NULL != nn)
549 if (NULL != (nn = nn->next)) {
550 for ( ; nn->next; nn = nn->next)
552 width = a2width(nn, &su);
555 if (MAN_TP == n->tok && NULL != nn)
556 width = a2width(nn, &su);
558 if (MAN_BLOCK == n->type) {
559 bufcat_su(h, "margin-left", &su);
560 SCALE_VS_INIT(&su, 1);
561 bufcat_su(h, "margin-top", &su);
562 bufcat_style(h, "clear", "both");
563 PAIR_STYLE_INIT(&tag, h);
564 print_otag(h, TAG_DIV, 1, &tag);
568 bufcat_su(h, "min-width", &su);
570 bufcat_su(h, "margin-left", &su);
571 SCALE_HS_INIT(&su, 1);
572 bufcat_su(h, "margin-right", &su);
573 bufcat_style(h, "clear", "left");
575 if (n->next && n->next->child)
576 bufcat_style(h, "float", "left");
578 PAIR_STYLE_INIT(&tag, h);
579 print_otag(h, TAG_DIV, 1, &tag);
581 /* With a length string, manually omit the last child. */
586 if (MAN_IP == n->tok)
587 for (nn = n->child; nn->next; nn = nn->next)
588 print_man_node(m, nn, h);
589 if (MAN_TP == n->tok)
590 for (nn = n->child->next; nn; nn = nn->next)
591 print_man_node(m, nn, h);
601 const struct man_node *nn;
605 if (MAN_HEAD == n->type)
608 nn = MAN_BLOCK == n->type ?
609 n->head->child : n->parent->head->child;
611 SCALE_HS_INIT(&su, INDENT);
614 (void)a2width(nn, &su);
616 if (MAN_BLOCK == n->type) {
617 bufcat_su(h, "margin-left", &su);
618 SCALE_VS_INIT(&su, 1);
619 bufcat_su(h, "margin-top", &su);
620 bufcat_style(h, "clear", "both");
621 PAIR_STYLE_INIT(&tag, h);
622 print_otag(h, TAG_DIV, 1, &tag);
626 bufcat_su(h, "margin-left", &su);
628 bufcat_su(h, "text-indent", &su);
630 PAIR_STYLE_INIT(&tag, h);
631 print_otag(h, TAG_DIV, 1, &tag);
642 PAIR_CLASS_INIT(&tag, "bold");
643 print_otag(h, TAG_SPAN, 1, &tag);
654 PAIR_CLASS_INIT(&tag, "italic");
655 print_otag(h, TAG_SPAN, 1, &tag);
662 man_ign_pre(MAN_ARGS)
676 if (MAN_HEAD == n->type)
678 else if (MAN_BODY == n->type)
681 SCALE_HS_INIT(&su, INDENT);
682 bufcat_su(h, "margin-left", &su);
684 if (n->head->child) {
685 SCALE_VS_INIT(&su, 1);
686 a2width(n->head->child, &su);
687 bufcat_su(h, "margin-top", &su);
690 PAIR_STYLE_INIT(&tag, h);
691 print_otag(h, TAG_DIV, 1, &tag);