1 /* $Id: man_html.c,v 1.72 2011/05/17 11:34:31 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
21 #include <sys/types.h>
35 /* TODO: preserve ident widths. */
36 /* FIXME: have PD set the default vspace width. */
41 #define MAN_ARGS const struct man_meta *m, \
42 const struct man_node *n, \
48 #define MANH_LITERAL (1 << 0) /* literal context */
53 int (*post)(MAN_ARGS);
56 static void print_man(MAN_ARGS);
57 static void print_man_head(MAN_ARGS);
58 static void print_man_nodelist(MAN_ARGS);
59 static void print_man_node(MAN_ARGS);
61 static int a2width(const struct man_node *,
64 static int man_alt_pre(MAN_ARGS);
65 static int man_br_pre(MAN_ARGS);
66 static int man_ign_pre(MAN_ARGS);
67 static int man_in_pre(MAN_ARGS);
68 static int man_literal_pre(MAN_ARGS);
69 static void man_root_post(MAN_ARGS);
70 static void man_root_pre(MAN_ARGS);
71 static int man_B_pre(MAN_ARGS);
72 static int man_HP_pre(MAN_ARGS);
73 static int man_I_pre(MAN_ARGS);
74 static int man_IP_pre(MAN_ARGS);
75 static int man_PP_pre(MAN_ARGS);
76 static int man_RS_pre(MAN_ARGS);
77 static int man_SH_pre(MAN_ARGS);
78 static int man_SM_pre(MAN_ARGS);
79 static int man_SS_pre(MAN_ARGS);
81 static const struct htmlman mans[MAN_MAX] = {
82 { man_br_pre, NULL }, /* br */
83 { NULL, NULL }, /* TH */
84 { man_SH_pre, NULL }, /* SH */
85 { man_SS_pre, NULL }, /* SS */
86 { man_IP_pre, NULL }, /* TP */
87 { man_PP_pre, NULL }, /* LP */
88 { man_PP_pre, NULL }, /* PP */
89 { man_PP_pre, NULL }, /* P */
90 { man_IP_pre, NULL }, /* IP */
91 { man_HP_pre, NULL }, /* HP */
92 { man_SM_pre, NULL }, /* SM */
93 { man_SM_pre, NULL }, /* SB */
94 { man_alt_pre, NULL }, /* BI */
95 { man_alt_pre, NULL }, /* IB */
96 { man_alt_pre, NULL }, /* BR */
97 { man_alt_pre, NULL }, /* RB */
98 { NULL, NULL }, /* R */
99 { man_B_pre, NULL }, /* B */
100 { man_I_pre, NULL }, /* I */
101 { man_alt_pre, NULL }, /* IR */
102 { man_alt_pre, NULL }, /* RI */
103 { man_ign_pre, NULL }, /* na */
104 { man_br_pre, NULL }, /* sp */
105 { man_literal_pre, NULL }, /* nf */
106 { man_literal_pre, NULL }, /* fi */
107 { NULL, NULL }, /* RE */
108 { man_RS_pre, NULL }, /* RS */
109 { man_ign_pre, NULL }, /* DT */
110 { man_ign_pre, NULL }, /* UC */
111 { man_ign_pre, NULL }, /* PD */
112 { man_ign_pre, NULL }, /* AT */
113 { man_in_pre, NULL }, /* in */
114 { man_ign_pre, NULL }, /* ft */
119 html_man(void *arg, const struct man *m)
125 h = (struct html *)arg;
129 memset(&mh, 0, sizeof(struct mhtml));
131 t = print_otag(h, TAG_HTML, 0, NULL);
132 print_man(man_meta(m), man_node(m), &mh, h);
144 t = print_otag(h, TAG_HEAD, 0, NULL);
145 print_man_head(m, n, mh, h);
148 t = print_otag(h, TAG_BODY, 0, NULL);
149 print_man_nodelist(m, n, mh, h);
156 print_man_head(MAN_ARGS)
160 bufcat_fmt(h, "%s(%s)", m->title, m->msec);
161 print_otag(h, TAG_TITLE, 0, NULL);
162 print_text(h, h->buf);
167 print_man_nodelist(MAN_ARGS)
170 print_man_node(m, n, mh, h);
172 print_man_nodelist(m, n->next, mh, h);
177 print_man_node(MAN_ARGS)
188 man_root_pre(m, n, mh, h);
192 * If we have a blank line, output a vertical space.
193 * If we have a space as the first character, break
194 * before printing the line's data.
196 if ('\0' == *n->string) {
197 print_otag(h, TAG_P, 0, NULL);
199 } else if (' ' == *n->string && MAN_LINE & n->flags)
200 print_otag(h, TAG_BR, 0, NULL);
202 print_text(h, n->string);
205 * If we're in a literal context, make sure that words
206 * togehter on the same line stay together. This is a
207 * POST-printing call, so we check the NEXT word. Since
208 * -man doesn't have nested macros, we don't need to be
209 * more specific than this.
211 if (MANH_LITERAL & mh->fl &&
213 n->next->line > n->line))
214 print_otag(h, TAG_BR, 0, NULL);
217 PAIR_CLASS_INIT(&tag, "eqn");
218 print_otag(h, TAG_SPAN, 1, &tag);
219 print_text(h, n->eqn->data);
223 * This will take care of initialising all of the table
224 * state data for the first table, then tearing it down
227 print_tbl(h, n->span);
231 * Close out scope of font prior to opening a macro
234 if (HTMLFONT_NONE != h->metac) {
236 h->metac = HTMLFONT_NONE;
240 * Close out the current table, if it's open, and unset
241 * the "meta" table state. This will be reopened on the
242 * next table element.
248 if (mans[n->tok].pre)
249 child = (*mans[n->tok].pre)(m, n, mh, h);
253 if (child && n->child)
254 print_man_nodelist(m, n->child, mh, h);
256 /* This will automatically close out any font scope. */
261 man_root_post(m, n, mh, h);
266 if (mans[n->tok].post)
267 (*mans[n->tok].post)(m, n, mh, h);
274 a2width(const struct man_node *n, struct roffsu *su)
277 if (MAN_TEXT != n->type)
279 if (a2roffsu(n->string, su, SCALE_BU))
288 man_root_pre(MAN_ARGS)
290 struct htmlpair tag[3];
292 char b[BUFSIZ], title[BUFSIZ];
296 (void)strlcat(b, m->vol, BUFSIZ);
298 snprintf(title, BUFSIZ - 1, "%s(%s)", m->title, m->msec);
300 PAIR_SUMMARY_INIT(&tag[0], "Document Header");
301 PAIR_CLASS_INIT(&tag[1], "head");
302 if (NULL == h->style) {
303 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
304 t = print_otag(h, TAG_TABLE, 3, tag);
305 PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
306 print_otag(h, TAG_COL, 1, tag);
307 print_otag(h, TAG_COL, 1, tag);
308 print_otag(h, TAG_COL, 1, tag);
310 t = print_otag(h, TAG_TABLE, 2, tag);
312 print_otag(h, TAG_TBODY, 0, NULL);
314 tt = print_otag(h, TAG_TR, 0, NULL);
316 PAIR_CLASS_INIT(&tag[0], "head-ltitle");
317 print_otag(h, TAG_TD, 1, tag);
319 print_text(h, title);
322 PAIR_CLASS_INIT(&tag[0], "head-vol");
323 if (NULL == h->style) {
324 PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
325 print_otag(h, TAG_TD, 2, tag);
327 print_otag(h, TAG_TD, 1, tag);
332 PAIR_CLASS_INIT(&tag[0], "head-rtitle");
333 if (NULL == h->style) {
334 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
335 print_otag(h, TAG_TD, 2, tag);
337 print_otag(h, TAG_TD, 1, tag);
339 print_text(h, title);
346 man_root_post(MAN_ARGS)
348 struct htmlpair tag[3];
351 PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
352 PAIR_CLASS_INIT(&tag[1], "foot");
353 if (NULL == h->style) {
354 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
355 t = print_otag(h, TAG_TABLE, 3, tag);
356 PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
357 print_otag(h, TAG_COL, 1, tag);
358 print_otag(h, TAG_COL, 1, tag);
360 t = print_otag(h, TAG_TABLE, 2, tag);
362 tt = print_otag(h, TAG_TR, 0, NULL);
364 PAIR_CLASS_INIT(&tag[0], "foot-date");
365 print_otag(h, TAG_TD, 1, tag);
367 print_text(h, m->date);
370 PAIR_CLASS_INIT(&tag[0], "foot-os");
371 if (NULL == h->style) {
372 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
373 print_otag(h, TAG_TD, 2, tag);
375 print_otag(h, TAG_TD, 1, tag);
378 print_text(h, m->source);
391 SCALE_VS_INIT(&su, 1);
393 if (MAN_sp == n->tok) {
395 a2roffsu(n->child->string, &su, SCALE_VS);
400 bufcat_su(h, "height", &su);
401 PAIR_STYLE_INIT(&tag, h);
402 print_otag(h, TAG_DIV, 1, &tag);
404 /* So the div isn't empty: */
405 print_text(h, "\\~");
417 if (MAN_BLOCK == n->type) {
418 PAIR_CLASS_INIT(&tag, "section");
419 print_otag(h, TAG_DIV, 1, &tag);
421 } else if (MAN_BODY == n->type)
424 print_otag(h, TAG_H1, 0, NULL);
431 man_alt_pre(MAN_ARGS)
433 const struct man_node *nn;
438 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
442 fp = i % 2 ? TAG_I : TAG_B;
445 fp = i % 2 ? TAG_B : TAG_I;
448 fp = i % 2 ? TAG_I : TAG_MAX;
451 fp = i % 2 ? TAG_MAX : TAG_I;
454 fp = i % 2 ? TAG_MAX : TAG_B;
457 fp = i % 2 ? TAG_B : TAG_MAX;
465 h->flags |= HTML_NOSPACE;
468 t = print_otag(h, fp, 0, NULL);
470 print_man_node(m, nn, mh, h);
485 print_otag(h, TAG_SMALL, 0, NULL);
486 if (MAN_SB == n->tok)
487 print_otag(h, TAG_B, 0, NULL);
498 if (MAN_BLOCK == n->type) {
499 PAIR_CLASS_INIT(&tag, "subsection");
500 print_otag(h, TAG_DIV, 1, &tag);
502 } else if (MAN_BODY == n->type)
505 print_otag(h, TAG_H2, 0, NULL);
515 if (MAN_HEAD == n->type)
517 else if (MAN_BODY == n->type && n->prev)
518 print_otag(h, TAG_P, 0, NULL);
530 const struct man_node *nn;
533 * This scattering of 1-BU margins and pads is to make sure that
534 * when text overruns its box, the subsequent text isn't flush
535 * up against it. However, the rest of the right-hand box must
536 * also be adjusted in consideration of this 1-BU space.
539 if (MAN_BODY == n->type) {
540 print_otag(h, TAG_TD, 0, NULL);
544 nn = MAN_BLOCK == n->type ?
545 n->head->child : n->parent->head->child;
547 SCALE_HS_INIT(&su, INDENT);
549 /* Width is the second token. */
551 if (MAN_IP == n->tok && NULL != nn)
552 if (NULL != (nn = nn->next))
555 /* Width is the first token. */
557 if (MAN_TP == n->tok && NULL != nn) {
558 /* Skip past non-text children. */
559 while (nn && MAN_TEXT != nn->type)
565 if (MAN_BLOCK == n->type) {
566 print_otag(h, TAG_P, 0, NULL);
567 print_otag(h, TAG_TABLE, 0, NULL);
569 bufcat_su(h, "width", &su);
570 PAIR_STYLE_INIT(&tag, h);
571 print_otag(h, TAG_COL, 1, &tag);
572 print_otag(h, TAG_COL, 0, NULL);
573 print_otag(h, TAG_TBODY, 0, NULL);
574 print_otag(h, TAG_TR, 0, NULL);
578 print_otag(h, TAG_TD, 0, NULL);
580 /* For IP, only print the first header element. */
582 if (MAN_IP == n->tok && n->child)
583 print_man_node(m, n->child, mh, h);
585 /* For TP, only print next-line header elements. */
587 if (MAN_TP == n->tok)
588 for (nn = n->child; nn; nn = nn->next)
589 if (nn->line > n->line)
590 print_man_node(m, nn, mh, h);
602 const struct man_node *np;
606 np = MAN_BLOCK == n->type ?
608 n->parent->head->child;
610 if (NULL == np || ! a2width(np, &su))
611 SCALE_HS_INIT(&su, INDENT);
613 if (MAN_HEAD == n->type) {
614 print_otag(h, TAG_TD, 0, NULL);
616 } else if (MAN_BLOCK == n->type) {
617 print_otag(h, TAG_P, 0, NULL);
618 print_otag(h, TAG_TABLE, 0, NULL);
619 bufcat_su(h, "width", &su);
620 PAIR_STYLE_INIT(&tag, h);
621 print_otag(h, TAG_COL, 1, &tag);
622 print_otag(h, TAG_COL, 0, NULL);
623 print_otag(h, TAG_TBODY, 0, NULL);
624 print_otag(h, TAG_TR, 0, NULL);
628 su.scale = -su.scale;
629 bufcat_su(h, "text-indent", &su);
630 PAIR_STYLE_INIT(&tag, h);
631 print_otag(h, TAG_TD, 1, &tag);
641 print_otag(h, TAG_B, 0, NULL);
651 print_otag(h, TAG_I, 0, NULL);
658 man_literal_pre(MAN_ARGS)
661 if (MAN_nf == n->tok) {
662 print_otag(h, TAG_BR, 0, NULL);
663 mh->fl |= MANH_LITERAL;
665 mh->fl &= ~MANH_LITERAL;
676 print_otag(h, TAG_BR, 0, NULL);
683 man_ign_pre(MAN_ARGS)
697 if (MAN_HEAD == n->type)
699 else if (MAN_BODY == n->type)
702 SCALE_HS_INIT(&su, INDENT);
704 a2width(n->head->child, &su);
707 bufcat_su(h, "margin-left", &su);
708 PAIR_STYLE_INIT(&tag, h);
709 print_otag(h, TAG_DIV, 1, &tag);