1 /* $Vendor-Id: man_html.c,v 1.24 2009/11/16 08:46:59 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
);
70 extern size_t strlcpy(char *, const char *, size_t);
71 extern size_t strlcat(char *, const char *, size_t);
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 */
111 html_man(void *arg
, const struct man
*m
)
116 h
= (struct html
*)arg
;
118 print_gen_doctype(h
);
120 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
121 print_man(man_meta(m
), man_node(m
), h
);
134 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
136 print_man_head(m
, n
, h
);
138 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
140 tag
.key
= ATTR_CLASS
;
142 print_otag(h
, TAG_DIV
, 1, &tag
);
144 print_man_nodelist(m
, n
, h
);
152 print_man_head(MAN_ARGS
)
157 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
159 print_otag(h
, TAG_TITLE
, 0, NULL
);
160 print_text(h
, h
->buf
);
165 print_man_nodelist(MAN_ARGS
)
168 print_man_node(m
, n
, h
);
170 print_man_nodelist(m
, n
->next
, h
);
175 print_man_node(MAN_ARGS
)
187 child
= man_root_pre(m
, n
, h
);
190 print_text(h
, n
->string
);
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).
199 assert(h
->metaf
== t
);
200 print_tagq(h
, h
->metaf
);
201 assert(NULL
== h
->metaf
);
204 if (mans
[n
->tok
].pre
)
205 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
209 if (child
&& n
->child
)
210 print_man_nodelist(m
, n
->child
, h
);
212 /* This will automatically close out any font scope. */
219 man_root_post(m
, n
, h
);
224 if (mans
[n
->tok
].post
)
225 (*mans
[n
->tok
].post
)(m
, n
, h
);
232 a2width(const struct man_node
*n
, struct roffsu
*su
)
235 if (MAN_TEXT
!= n
->type
)
237 if (a2roffsu(n
->string
, su
, SCALE_BU
))
246 man_root_pre(MAN_ARGS
)
248 struct htmlpair tag
[3];
250 char b
[BUFSIZ
], title
[BUFSIZ
];
254 (void)strlcat(b
, m
->vol
, BUFSIZ
);
256 snprintf(title
, BUFSIZ
- 1, "%s(%d)", m
->title
, m
->msec
);
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");
263 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
264 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
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
);
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
);
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
);
295 man_root_post(MAN_ARGS
)
297 struct htmlpair tag
[3];
301 time2a(m
->date
, b
, DATESIZ
);
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");
308 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
309 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
312 bufcat_style(h
, "width", "50%");
313 PAIR_STYLE_INIT(&tag
[0], h
);
314 print_otag(h
, TAG_TD
, 1, tag
);
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
);
324 print_text(h
, m
->source
);
337 SCALE_VS_INIT(&su
, 1);
339 if (MAN_sp
== n
->tok
&& n
->child
)
340 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
341 else if (MAN_br
== n
->tok
)
344 bufcat_su(h
, "height", &su
);
345 PAIR_STYLE_INIT(&tag
, h
);
346 print_otag(h
, TAG_DIV
, 1, &tag
);
348 /* So the div isn't empty: */
349 print_text(h
, "\\~");
359 struct htmlpair tag
[2];
362 if (MAN_BODY
== n
->type
) {
363 SCALE_HS_INIT(&su
, INDENT
);
364 bufcat_su(h
, "margin-left", &su
);
365 PAIR_CLASS_INIT(&tag
[0], "sec-body");
366 PAIR_STYLE_INIT(&tag
[1], h
);
367 print_otag(h
, TAG_DIV
, 2, tag
);
369 } else if (MAN_BLOCK
== n
->type
) {
370 PAIR_CLASS_INIT(&tag
[0], "sec-block");
371 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
372 if (NULL
== n
->prev
->body
->child
) {
373 print_otag(h
, TAG_DIV
, 1, tag
);
377 SCALE_VS_INIT(&su
, 1);
378 bufcat_su(h
, "margin-top", &su
);
380 bufcat_su(h
, "margin-bottom", &su
);
381 PAIR_STYLE_INIT(&tag
[1], h
);
382 print_otag(h
, TAG_DIV
, 2, tag
);
386 PAIR_CLASS_INIT(&tag
[0], "sec-head");
387 print_otag(h
, TAG_DIV
, 1, tag
);
394 man_alt_pre(MAN_ARGS
)
396 const struct man_node
*nn
;
401 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
404 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
407 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
410 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
413 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
416 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
419 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
427 h
->flags
|= HTML_NOSPACE
;
430 * Open and close the scope with each argument, so that
431 * internal \f escapes, which are common, are also
432 * closed out with the scope.
434 t
= print_ofont(h
, fp
);
435 print_man_node(m
, nn
, h
);
449 /* FIXME: print_ofont(). */
450 PAIR_CLASS_INIT(&tag
, "small bold");
451 print_otag(h
, TAG_SPAN
, 1, &tag
);
462 PAIR_CLASS_INIT(&tag
, "small");
463 print_otag(h
, TAG_SPAN
, 1, &tag
);
472 struct htmlpair tag
[3];
475 SCALE_VS_INIT(&su
, 1);
477 if (MAN_BODY
== n
->type
) {
478 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
479 if (n
->parent
->next
&& n
->child
) {
480 bufcat_su(h
, "margin-bottom", &su
);
481 PAIR_STYLE_INIT(&tag
[1], h
);
482 print_otag(h
, TAG_DIV
, 2, tag
);
486 print_otag(h
, TAG_DIV
, 1, tag
);
488 } else if (MAN_BLOCK
== n
->type
) {
489 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
490 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
491 if (n
->prev
->body
->child
) {
492 bufcat_su(h
, "margin-top", &su
);
493 PAIR_STYLE_INIT(&tag
[1], h
);
494 print_otag(h
, TAG_DIV
, 2, tag
);
498 print_otag(h
, TAG_DIV
, 1, tag
);
502 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
503 bufcat_su(h
, "margin-left", &su
);
504 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
505 PAIR_STYLE_INIT(&tag
[1], h
);
506 print_otag(h
, TAG_DIV
, 2, tag
);
519 if (MAN_BLOCK
!= n
->type
)
524 if (MAN_ROOT
== n
->parent
->type
) {
525 SCALE_HS_INIT(&su
, INDENT
);
526 bufcat_su(h
, "margin-left", &su
);
530 SCALE_VS_INIT(&su
, 1);
531 bufcat_su(h
, "margin-top", &su
);
535 PAIR_STYLE_INIT(&tag
, h
);
536 print_otag(h
, TAG_DIV
, i
, &tag
);
547 const struct man_node
*nn
;
551 * This scattering of 1-BU margins and pads is to make sure that
552 * when text overruns its box, the subsequent text isn't flush
553 * up against it. However, the rest of the right-hand box must
554 * also be adjusted in consideration of this 1-BU space.
557 if (MAN_BODY
== n
->type
) {
558 SCALE_HS_INIT(&su
, INDENT
);
559 bufcat_su(h
, "margin-left", &su
);
560 PAIR_STYLE_INIT(&tag
, h
);
561 print_otag(h
, TAG_DIV
, 1, &tag
);
565 nn
= MAN_BLOCK
== n
->type
?
566 n
->head
->child
: n
->parent
->head
->child
;
568 SCALE_HS_INIT(&su
, INDENT
);
571 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
572 if (NULL
!= (nn
= nn
->next
)) {
573 for ( ; nn
->next
; nn
= nn
->next
)
575 width
= a2width(nn
, &su
);
578 if (MAN_TP
== n
->tok
&& NULL
!= nn
)
579 width
= a2width(nn
, &su
);
581 if (MAN_BLOCK
== n
->type
) {
582 bufcat_su(h
, "margin-left", &su
);
583 SCALE_VS_INIT(&su
, 1);
584 bufcat_su(h
, "margin-top", &su
);
585 bufcat_style(h
, "clear", "both");
586 PAIR_STYLE_INIT(&tag
, h
);
587 print_otag(h
, TAG_DIV
, 1, &tag
);
591 bufcat_su(h
, "min-width", &su
);
593 bufcat_su(h
, "margin-left", &su
);
594 SCALE_HS_INIT(&su
, 1);
595 bufcat_su(h
, "margin-right", &su
);
596 bufcat_style(h
, "clear", "left");
598 if (n
->next
&& n
->next
->child
)
599 bufcat_style(h
, "float", "left");
601 PAIR_STYLE_INIT(&tag
, h
);
602 print_otag(h
, TAG_DIV
, 1, &tag
);
604 /* With a length string, manually omit the last child. */
609 if (MAN_IP
== n
->tok
)
610 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
611 print_man_node(m
, nn
, h
);
612 if (MAN_TP
== n
->tok
)
613 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
614 print_man_node(m
, nn
, h
);
624 const struct man_node
*nn
;
628 if (MAN_HEAD
== n
->type
)
631 nn
= MAN_BLOCK
== n
->type
?
632 n
->head
->child
: n
->parent
->head
->child
;
634 SCALE_HS_INIT(&su
, INDENT
);
637 (void)a2width(nn
, &su
);
639 if (MAN_BLOCK
== n
->type
) {
640 bufcat_su(h
, "margin-left", &su
);
641 SCALE_VS_INIT(&su
, 1);
642 bufcat_su(h
, "margin-top", &su
);
643 bufcat_style(h
, "clear", "both");
644 PAIR_STYLE_INIT(&tag
, h
);
645 print_otag(h
, TAG_DIV
, 1, &tag
);
649 bufcat_su(h
, "margin-left", &su
);
651 bufcat_su(h
, "text-indent", &su
);
653 PAIR_STYLE_INIT(&tag
, h
);
654 print_otag(h
, TAG_DIV
, 1, &tag
);
664 print_ofont(h
, HTMLFONT_BOLD
);
674 print_ofont(h
, HTMLFONT_ITALIC
);
681 man_ign_pre(MAN_ARGS
)
695 if (MAN_HEAD
== n
->type
)
697 else if (MAN_BODY
== n
->type
)
700 SCALE_HS_INIT(&su
, INDENT
);
701 bufcat_su(h
, "margin-left", &su
);
703 if (n
->head
->child
) {
704 SCALE_VS_INIT(&su
, 1);
705 a2width(n
->head
->child
, &su
);
706 bufcat_su(h
, "margin-top", &su
);
709 PAIR_STYLE_INIT(&tag
, h
);
710 print_otag(h
, TAG_DIV
, 1, &tag
);