]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.33 2010/05/15 22:44:04 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.
21 #include <sys/types.h>
34 /* TODO: preserve ident widths. */
35 /* FIXME: have PD set the default vspace width. */
40 #define MAN_ARGS const struct man_meta *m, \
41 const struct man_node *n, \
46 int (*post
)(MAN_ARGS
);
49 static void print_man(MAN_ARGS
);
50 static void print_man_head(MAN_ARGS
);
51 static void print_man_nodelist(MAN_ARGS
);
52 static void print_man_node(MAN_ARGS
);
54 static int a2width(const struct man_node
*,
57 static int man_alt_pre(MAN_ARGS
);
58 static int man_br_pre(MAN_ARGS
);
59 static int man_ign_pre(MAN_ARGS
);
60 static void man_root_post(MAN_ARGS
);
61 static int man_root_pre(MAN_ARGS
);
62 static int man_B_pre(MAN_ARGS
);
63 static int man_HP_pre(MAN_ARGS
);
64 static int man_I_pre(MAN_ARGS
);
65 static int man_IP_pre(MAN_ARGS
);
66 static int man_PP_pre(MAN_ARGS
);
67 static int man_RS_pre(MAN_ARGS
);
68 static int man_SB_pre(MAN_ARGS
);
69 static int man_SH_pre(MAN_ARGS
);
70 static int man_SM_pre(MAN_ARGS
);
71 static int man_SS_pre(MAN_ARGS
);
73 static const struct htmlman mans
[MAN_MAX
] = {
74 { man_br_pre
, NULL
}, /* br */
75 { NULL
, NULL
}, /* TH */
76 { man_SH_pre
, NULL
}, /* SH */
77 { man_SS_pre
, NULL
}, /* SS */
78 { man_IP_pre
, NULL
}, /* TP */
79 { man_PP_pre
, NULL
}, /* LP */
80 { man_PP_pre
, NULL
}, /* PP */
81 { man_PP_pre
, NULL
}, /* P */
82 { man_IP_pre
, NULL
}, /* IP */
83 { man_HP_pre
, NULL
}, /* HP */
84 { man_SM_pre
, NULL
}, /* SM */
85 { man_SB_pre
, NULL
}, /* SB */
86 { man_alt_pre
, NULL
}, /* BI */
87 { man_alt_pre
, NULL
}, /* IB */
88 { man_alt_pre
, NULL
}, /* BR */
89 { man_alt_pre
, NULL
}, /* RB */
90 { NULL
, NULL
}, /* R */
91 { man_B_pre
, NULL
}, /* B */
92 { man_I_pre
, NULL
}, /* I */
93 { man_alt_pre
, NULL
}, /* IR */
94 { man_alt_pre
, NULL
}, /* RI */
95 { NULL
, NULL
}, /* na */
96 { NULL
, NULL
}, /* i */
97 { man_br_pre
, NULL
}, /* sp */
98 { NULL
, NULL
}, /* nf */
99 { NULL
, NULL
}, /* fi */
100 { NULL
, NULL
}, /* r */
101 { NULL
, NULL
}, /* RE */
102 { man_RS_pre
, NULL
}, /* RS */
103 { man_ign_pre
, NULL
}, /* DT */
104 { man_ign_pre
, NULL
}, /* UC */
105 { man_ign_pre
, NULL
}, /* PD */
106 { man_br_pre
, NULL
}, /* Sp */
107 { man_ign_pre
, NULL
}, /* Vb */
108 { NULL
, NULL
}, /* Ve */
113 html_man(void *arg
, const struct man
*m
)
118 h
= (struct html
*)arg
;
122 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
123 print_man(man_meta(m
), man_node(m
), h
);
136 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
138 print_man_head(m
, n
, h
);
140 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
142 tag
.key
= ATTR_CLASS
;
144 print_otag(h
, TAG_DIV
, 1, &tag
);
146 print_man_nodelist(m
, n
, h
);
154 print_man_head(MAN_ARGS
)
159 buffmt(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
, h
);
172 print_man_nodelist(m
, n
->next
, h
);
177 print_man_node(MAN_ARGS
)
188 * FIXME: embedded elements within next-line scopes (e.g., `br'
189 * within an empty `B') will cause formatting to be forgotten
190 * due to scope closing out.
195 child
= man_root_pre(m
, n
, h
);
198 print_text(h
, n
->string
);
202 * Close out scope of font prior to opening a macro
203 * scope. Assert that the metafont is on the top of the
204 * stack (it's never nested).
207 assert(h
->metaf
== t
);
208 print_tagq(h
, h
->metaf
);
209 assert(NULL
== h
->metaf
);
212 if (mans
[n
->tok
].pre
)
213 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
217 if (child
&& n
->child
)
218 print_man_nodelist(m
, n
->child
, h
);
220 /* This will automatically close out any font scope. */
227 man_root_post(m
, n
, h
);
232 if (mans
[n
->tok
].post
)
233 (*mans
[n
->tok
].post
)(m
, n
, h
);
240 a2width(const struct man_node
*n
, struct roffsu
*su
)
243 if (MAN_TEXT
!= n
->type
)
245 if (a2roffsu(n
->string
, su
, SCALE_BU
))
254 man_root_pre(MAN_ARGS
)
256 struct htmlpair tag
[3];
258 char b
[BUFSIZ
], title
[BUFSIZ
];
262 (void)strlcat(b
, m
->vol
, BUFSIZ
);
264 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
266 PAIR_CLASS_INIT(&tag
[0], "header");
267 bufcat_style(h
, "width", "100%");
268 PAIR_STYLE_INIT(&tag
[1], h
);
269 PAIR_SUMMARY_INIT(&tag
[2], "header");
271 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
272 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
275 bufcat_style(h
, "width", "10%");
276 PAIR_STYLE_INIT(&tag
[0], h
);
277 print_otag(h
, TAG_TD
, 1, tag
);
278 print_text(h
, title
);
282 bufcat_style(h
, "width", "80%");
283 bufcat_style(h
, "white-space", "nowrap");
284 bufcat_style(h
, "text-align", "center");
285 PAIR_STYLE_INIT(&tag
[0], h
);
286 print_otag(h
, TAG_TD
, 1, tag
);
291 bufcat_style(h
, "width", "10%");
292 bufcat_style(h
, "text-align", "right");
293 PAIR_STYLE_INIT(&tag
[0], h
);
294 print_otag(h
, TAG_TD
, 1, tag
);
295 print_text(h
, title
);
303 man_root_post(MAN_ARGS
)
305 struct htmlpair tag
[3];
309 time2a(m
->date
, b
, DATESIZ
);
311 PAIR_CLASS_INIT(&tag
[0], "footer");
312 bufcat_style(h
, "width", "100%");
313 PAIR_STYLE_INIT(&tag
[1], h
);
314 PAIR_SUMMARY_INIT(&tag
[2], "footer");
316 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
317 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
320 bufcat_style(h
, "width", "50%");
321 PAIR_STYLE_INIT(&tag
[0], h
);
322 print_otag(h
, TAG_TD
, 1, tag
);
327 bufcat_style(h
, "width", "50%");
328 bufcat_style(h
, "text-align", "right");
329 PAIR_STYLE_INIT(&tag
[0], h
);
330 print_otag(h
, TAG_TD
, 1, tag
);
332 print_text(h
, m
->source
);
345 SCALE_VS_INIT(&su
, 1);
349 SCALE_VS_INIT(&su
, 0.5);
353 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
360 bufcat_su(h
, "height", &su
);
361 PAIR_STYLE_INIT(&tag
, h
);
362 print_otag(h
, TAG_DIV
, 1, &tag
);
364 /* So the div isn't empty: */
365 print_text(h
, "\\~");
375 struct htmlpair tag
[2];
378 if (MAN_BODY
== n
->type
) {
379 SCALE_HS_INIT(&su
, INDENT
);
380 bufcat_su(h
, "margin-left", &su
);
381 PAIR_CLASS_INIT(&tag
[0], "sec-body");
382 PAIR_STYLE_INIT(&tag
[1], h
);
383 print_otag(h
, TAG_DIV
, 2, tag
);
385 } else if (MAN_BLOCK
== n
->type
) {
386 PAIR_CLASS_INIT(&tag
[0], "sec-block");
387 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
388 if (NULL
== n
->prev
->body
->child
) {
389 print_otag(h
, TAG_DIV
, 1, tag
);
393 SCALE_VS_INIT(&su
, 1);
394 bufcat_su(h
, "margin-top", &su
);
396 bufcat_su(h
, "margin-bottom", &su
);
397 PAIR_STYLE_INIT(&tag
[1], h
);
398 print_otag(h
, TAG_DIV
, 2, tag
);
402 PAIR_CLASS_INIT(&tag
[0], "sec-head");
403 print_otag(h
, TAG_DIV
, 1, tag
);
410 man_alt_pre(MAN_ARGS
)
412 const struct man_node
*nn
;
417 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
420 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
423 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
426 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
429 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
432 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
435 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
443 h
->flags
|= HTML_NOSPACE
;
446 * Open and close the scope with each argument, so that
447 * internal \f escapes, which are common, are also
448 * closed out with the scope.
450 t
= print_ofont(h
, fp
);
451 print_man_node(m
, nn
, h
);
465 /* FIXME: print_ofont(). */
466 PAIR_CLASS_INIT(&tag
, "small bold");
467 print_otag(h
, TAG_SPAN
, 1, &tag
);
478 PAIR_CLASS_INIT(&tag
, "small");
479 print_otag(h
, TAG_SPAN
, 1, &tag
);
488 struct htmlpair tag
[3];
491 SCALE_VS_INIT(&su
, 1);
493 if (MAN_BODY
== n
->type
) {
494 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
495 if (n
->parent
->next
&& n
->child
) {
496 bufcat_su(h
, "margin-bottom", &su
);
497 PAIR_STYLE_INIT(&tag
[1], h
);
498 print_otag(h
, TAG_DIV
, 2, tag
);
502 print_otag(h
, TAG_DIV
, 1, tag
);
504 } else if (MAN_BLOCK
== n
->type
) {
505 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
506 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
507 if (n
->prev
->body
->child
) {
508 bufcat_su(h
, "margin-top", &su
);
509 PAIR_STYLE_INIT(&tag
[1], h
);
510 print_otag(h
, TAG_DIV
, 2, tag
);
514 print_otag(h
, TAG_DIV
, 1, tag
);
518 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
519 bufcat_su(h
, "margin-left", &su
);
520 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
521 PAIR_STYLE_INIT(&tag
[1], h
);
522 print_otag(h
, TAG_DIV
, 2, tag
);
535 if (MAN_BLOCK
!= n
->type
)
540 if (MAN_ROOT
== n
->parent
->type
) {
541 SCALE_HS_INIT(&su
, INDENT
);
542 bufcat_su(h
, "margin-left", &su
);
546 SCALE_VS_INIT(&su
, 1);
547 bufcat_su(h
, "margin-top", &su
);
551 PAIR_STYLE_INIT(&tag
, h
);
552 print_otag(h
, TAG_DIV
, i
, &tag
);
563 const struct man_node
*nn
;
567 * This scattering of 1-BU margins and pads is to make sure that
568 * when text overruns its box, the subsequent text isn't flush
569 * up against it. However, the rest of the right-hand box must
570 * also be adjusted in consideration of this 1-BU space.
573 if (MAN_BODY
== n
->type
) {
574 SCALE_HS_INIT(&su
, INDENT
);
575 bufcat_su(h
, "margin-left", &su
);
576 PAIR_STYLE_INIT(&tag
, h
);
577 print_otag(h
, TAG_DIV
, 1, &tag
);
581 nn
= MAN_BLOCK
== n
->type
?
582 n
->head
->child
: n
->parent
->head
->child
;
584 SCALE_HS_INIT(&su
, INDENT
);
587 /* Width is the last token. */
589 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
590 if (NULL
!= (nn
= nn
->next
)) {
591 for ( ; nn
->next
; nn
= nn
->next
)
593 width
= a2width(nn
, &su
);
596 /* Width is the first token. */
598 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
599 /* Skip past non-text children. */
600 while (nn
&& MAN_TEXT
!= nn
->type
)
603 width
= a2width(nn
, &su
);
606 if (MAN_BLOCK
== n
->type
) {
607 bufcat_su(h
, "margin-left", &su
);
608 SCALE_VS_INIT(&su
, 1);
609 bufcat_su(h
, "margin-top", &su
);
610 bufcat_style(h
, "clear", "both");
611 PAIR_STYLE_INIT(&tag
, h
);
612 print_otag(h
, TAG_DIV
, 1, &tag
);
616 bufcat_su(h
, "min-width", &su
);
618 bufcat_su(h
, "margin-left", &su
);
619 SCALE_HS_INIT(&su
, 1);
620 bufcat_su(h
, "margin-right", &su
);
621 bufcat_style(h
, "clear", "left");
623 if (n
->next
&& n
->next
->child
)
624 bufcat_style(h
, "float", "left");
626 PAIR_STYLE_INIT(&tag
, h
);
627 print_otag(h
, TAG_DIV
, 1, &tag
);
630 * Without a length string, we can print all of our children.
637 * When a length has been specified, we need to carefully print
638 * our child context: IP gets all children printed but the last
639 * (the width), while TP gets all children printed but the first
643 if (MAN_IP
== n
->tok
)
644 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
645 print_man_node(m
, nn
, h
);
646 if (MAN_TP
== n
->tok
)
647 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
648 print_man_node(m
, nn
, h
);
658 const struct man_node
*nn
;
662 if (MAN_HEAD
== n
->type
)
665 nn
= MAN_BLOCK
== n
->type
?
666 n
->head
->child
: n
->parent
->head
->child
;
668 SCALE_HS_INIT(&su
, INDENT
);
671 (void)a2width(nn
, &su
);
673 if (MAN_BLOCK
== n
->type
) {
674 bufcat_su(h
, "margin-left", &su
);
675 SCALE_VS_INIT(&su
, 1);
676 bufcat_su(h
, "margin-top", &su
);
677 bufcat_style(h
, "clear", "both");
678 PAIR_STYLE_INIT(&tag
, h
);
679 print_otag(h
, TAG_DIV
, 1, &tag
);
683 bufcat_su(h
, "margin-left", &su
);
685 bufcat_su(h
, "text-indent", &su
);
687 PAIR_STYLE_INIT(&tag
, h
);
688 print_otag(h
, TAG_DIV
, 1, &tag
);
698 print_ofont(h
, HTMLFONT_BOLD
);
708 print_ofont(h
, HTMLFONT_ITALIC
);
715 man_ign_pre(MAN_ARGS
)
729 if (MAN_HEAD
== n
->type
)
731 else if (MAN_BODY
== n
->type
)
734 SCALE_HS_INIT(&su
, INDENT
);
735 bufcat_su(h
, "margin-left", &su
);
737 if (n
->head
->child
) {
738 SCALE_VS_INIT(&su
, 1);
739 a2width(n
->head
->child
, &su
);
740 bufcat_su(h
, "margin-top", &su
);
743 PAIR_STYLE_INIT(&tag
, h
);
744 print_otag(h
, TAG_DIV
, 1, &tag
);