]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.46 2010/12/05 16:14:16 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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 int 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_SB_pre(MAN_ARGS
);
78 static int man_SH_pre(MAN_ARGS
);
79 static int man_SM_pre(MAN_ARGS
);
80 static int man_SS_pre(MAN_ARGS
);
82 static const struct htmlman mans
[MAN_MAX
] = {
83 { man_br_pre
, NULL
}, /* br */
84 { NULL
, NULL
}, /* TH */
85 { man_SH_pre
, NULL
}, /* SH */
86 { man_SS_pre
, NULL
}, /* SS */
87 { man_IP_pre
, NULL
}, /* TP */
88 { man_PP_pre
, NULL
}, /* LP */
89 { man_PP_pre
, NULL
}, /* PP */
90 { man_PP_pre
, NULL
}, /* P */
91 { man_IP_pre
, NULL
}, /* IP */
92 { man_HP_pre
, NULL
}, /* HP */
93 { man_SM_pre
, NULL
}, /* SM */
94 { man_SB_pre
, NULL
}, /* SB */
95 { man_alt_pre
, NULL
}, /* BI */
96 { man_alt_pre
, NULL
}, /* IB */
97 { man_alt_pre
, NULL
}, /* BR */
98 { man_alt_pre
, NULL
}, /* RB */
99 { NULL
, NULL
}, /* R */
100 { man_B_pre
, NULL
}, /* B */
101 { man_I_pre
, NULL
}, /* I */
102 { man_alt_pre
, NULL
}, /* IR */
103 { man_alt_pre
, NULL
}, /* RI */
104 { NULL
, NULL
}, /* na */
105 { NULL
, NULL
}, /* i */
106 { man_br_pre
, NULL
}, /* sp */
107 { man_literal_pre
, NULL
}, /* nf */
108 { man_literal_pre
, NULL
}, /* fi */
109 { NULL
, NULL
}, /* r */
110 { NULL
, NULL
}, /* RE */
111 { man_RS_pre
, NULL
}, /* RS */
112 { man_ign_pre
, NULL
}, /* DT */
113 { man_ign_pre
, NULL
}, /* UC */
114 { man_ign_pre
, NULL
}, /* PD */
115 { man_ign_pre
, NULL
}, /* AT */
116 { man_in_pre
, NULL
}, /* in */
121 html_man(void *arg
, const struct man
*m
)
127 h
= (struct html
*)arg
;
131 memset(&mh
, 0, sizeof(struct mhtml
));
133 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
134 print_man(man_meta(m
), man_node(m
), &mh
, h
);
147 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
149 print_man_head(m
, n
, mh
, h
);
151 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
153 tag
.key
= ATTR_CLASS
;
155 print_otag(h
, TAG_DIV
, 1, &tag
);
157 print_man_nodelist(m
, n
, mh
, h
);
165 print_man_head(MAN_ARGS
)
170 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
172 print_otag(h
, TAG_TITLE
, 0, NULL
);
173 print_text(h
, h
->buf
);
178 print_man_nodelist(MAN_ARGS
)
181 print_man_node(m
, n
, mh
, h
);
183 print_man_nodelist(m
, n
->next
, mh
, h
);
188 print_man_node(MAN_ARGS
)
199 * FIXME: embedded elements within next-line scopes (e.g., `br'
200 * within an empty `B') will cause formatting to be forgotten
201 * due to scope closing out.
206 child
= man_root_pre(m
, n
, mh
, h
);
209 print_text(h
, n
->string
);
211 if (MANH_LITERAL
& mh
->fl
)
212 print_otag(h
, TAG_BR
, 0, NULL
);
217 * Close out scope of font prior to opening a macro
218 * scope. Assert that the metafont is on the top of the
219 * stack (it's never nested).
222 assert(h
->metaf
== t
);
223 print_tagq(h
, h
->metaf
);
224 assert(NULL
== h
->metaf
);
227 if (mans
[n
->tok
].pre
)
228 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
232 if (child
&& n
->child
)
233 print_man_nodelist(m
, n
->child
, mh
, h
);
235 /* This will automatically close out any font scope. */
242 man_root_post(m
, n
, mh
, h
);
247 if (mans
[n
->tok
].post
)
248 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
255 a2width(const struct man_node
*n
, struct roffsu
*su
)
258 if (MAN_TEXT
!= n
->type
)
260 if (a2roffsu(n
->string
, su
, SCALE_BU
))
269 man_root_pre(MAN_ARGS
)
271 struct htmlpair tag
[3];
273 char b
[BUFSIZ
], title
[BUFSIZ
];
277 (void)strlcat(b
, m
->vol
, BUFSIZ
);
279 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
281 PAIR_CLASS_INIT(&tag
[0], "header");
282 bufcat_style(h
, "width", "100%");
283 PAIR_STYLE_INIT(&tag
[1], h
);
284 PAIR_SUMMARY_INIT(&tag
[2], "header");
286 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
287 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
290 bufcat_style(h
, "width", "10%");
291 PAIR_STYLE_INIT(&tag
[0], h
);
292 print_otag(h
, TAG_TD
, 1, tag
);
293 print_text(h
, title
);
297 bufcat_style(h
, "width", "80%");
298 bufcat_style(h
, "white-space", "nowrap");
299 bufcat_style(h
, "text-align", "center");
300 PAIR_STYLE_INIT(&tag
[0], h
);
301 print_otag(h
, TAG_TD
, 1, tag
);
306 bufcat_style(h
, "width", "10%");
307 bufcat_style(h
, "text-align", "right");
308 PAIR_STYLE_INIT(&tag
[0], h
);
309 print_otag(h
, TAG_TD
, 1, tag
);
310 print_text(h
, title
);
318 man_root_post(MAN_ARGS
)
320 struct htmlpair tag
[3];
325 strlcpy(b
, m
->rawdate
, DATESIZ
);
327 time2a(m
->date
, b
, DATESIZ
);
329 PAIR_CLASS_INIT(&tag
[0], "footer");
330 bufcat_style(h
, "width", "100%");
331 PAIR_STYLE_INIT(&tag
[1], h
);
332 PAIR_SUMMARY_INIT(&tag
[2], "footer");
334 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
335 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
338 bufcat_style(h
, "width", "50%");
339 PAIR_STYLE_INIT(&tag
[0], h
);
340 print_otag(h
, TAG_TD
, 1, tag
);
345 bufcat_style(h
, "width", "50%");
346 bufcat_style(h
, "text-align", "right");
347 PAIR_STYLE_INIT(&tag
[0], h
);
348 print_otag(h
, TAG_TD
, 1, tag
);
350 print_text(h
, m
->source
);
363 SCALE_VS_INIT(&su
, 1);
368 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
375 bufcat_su(h
, "height", &su
);
376 PAIR_STYLE_INIT(&tag
, h
);
377 print_otag(h
, TAG_DIV
, 1, &tag
);
379 /* So the div isn't empty: */
380 print_text(h
, "\\~");
390 struct htmlpair tag
[2];
393 if (MAN_BODY
== n
->type
) {
394 SCALE_HS_INIT(&su
, INDENT
);
395 bufcat_su(h
, "margin-left", &su
);
396 PAIR_CLASS_INIT(&tag
[0], "sec-body");
397 PAIR_STYLE_INIT(&tag
[1], h
);
398 print_otag(h
, TAG_DIV
, 2, tag
);
400 } else if (MAN_BLOCK
== n
->type
) {
401 PAIR_CLASS_INIT(&tag
[0], "sec-block");
402 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
403 if (NULL
== n
->prev
->body
->child
) {
404 print_otag(h
, TAG_DIV
, 1, tag
);
408 SCALE_VS_INIT(&su
, 1);
409 bufcat_su(h
, "margin-top", &su
);
411 bufcat_su(h
, "margin-bottom", &su
);
412 PAIR_STYLE_INIT(&tag
[1], h
);
413 print_otag(h
, TAG_DIV
, 2, tag
);
417 PAIR_CLASS_INIT(&tag
[0], "sec-head");
418 print_otag(h
, TAG_DIV
, 1, tag
);
425 man_alt_pre(MAN_ARGS
)
427 const struct man_node
*nn
;
432 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
435 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
438 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
441 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
444 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
447 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
450 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
458 h
->flags
|= HTML_NOSPACE
;
461 * Open and close the scope with each argument, so that
462 * internal \f escapes, which are common, are also
463 * closed out with the scope.
465 t
= print_ofont(h
, fp
);
466 print_man_node(m
, nn
, mh
, h
);
480 /* FIXME: print_ofont(). */
481 PAIR_CLASS_INIT(&tag
, "small bold");
482 print_otag(h
, TAG_SPAN
, 1, &tag
);
493 PAIR_CLASS_INIT(&tag
, "small");
494 print_otag(h
, TAG_SPAN
, 1, &tag
);
503 struct htmlpair tag
[3];
506 SCALE_VS_INIT(&su
, 1);
508 if (MAN_BODY
== n
->type
) {
509 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
510 if (n
->parent
->next
&& n
->child
) {
511 bufcat_su(h
, "margin-bottom", &su
);
512 PAIR_STYLE_INIT(&tag
[1], h
);
513 print_otag(h
, TAG_DIV
, 2, tag
);
517 print_otag(h
, TAG_DIV
, 1, tag
);
519 } else if (MAN_BLOCK
== n
->type
) {
520 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
521 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
522 if (n
->prev
->body
->child
) {
523 bufcat_su(h
, "margin-top", &su
);
524 PAIR_STYLE_INIT(&tag
[1], h
);
525 print_otag(h
, TAG_DIV
, 2, tag
);
529 print_otag(h
, TAG_DIV
, 1, tag
);
533 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
534 bufcat_su(h
, "margin-left", &su
);
535 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
536 PAIR_STYLE_INIT(&tag
[1], h
);
537 print_otag(h
, TAG_DIV
, 2, tag
);
550 if (MAN_BLOCK
!= n
->type
)
555 if (MAN_ROOT
== n
->parent
->type
) {
556 SCALE_HS_INIT(&su
, INDENT
);
557 bufcat_su(h
, "margin-left", &su
);
561 SCALE_VS_INIT(&su
, 1);
562 bufcat_su(h
, "margin-top", &su
);
566 PAIR_STYLE_INIT(&tag
, h
);
567 print_otag(h
, TAG_DIV
, i
, &tag
);
578 const struct man_node
*nn
;
582 * This scattering of 1-BU margins and pads is to make sure that
583 * when text overruns its box, the subsequent text isn't flush
584 * up against it. However, the rest of the right-hand box must
585 * also be adjusted in consideration of this 1-BU space.
588 if (MAN_BODY
== n
->type
) {
589 SCALE_HS_INIT(&su
, INDENT
);
590 bufcat_su(h
, "margin-left", &su
);
591 PAIR_STYLE_INIT(&tag
, h
);
592 print_otag(h
, TAG_DIV
, 1, &tag
);
596 nn
= MAN_BLOCK
== n
->type
?
597 n
->head
->child
: n
->parent
->head
->child
;
599 SCALE_HS_INIT(&su
, INDENT
);
602 /* Width is the last token. */
604 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
605 if (NULL
!= (nn
= nn
->next
)) {
606 for ( ; nn
->next
; nn
= nn
->next
)
608 width
= a2width(nn
, &su
);
611 /* Width is the first token. */
613 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
614 /* Skip past non-text children. */
615 while (nn
&& MAN_TEXT
!= nn
->type
)
618 width
= a2width(nn
, &su
);
621 if (MAN_BLOCK
== n
->type
) {
622 bufcat_su(h
, "margin-left", &su
);
623 SCALE_VS_INIT(&su
, 1);
624 bufcat_su(h
, "margin-top", &su
);
625 bufcat_style(h
, "clear", "both");
626 PAIR_STYLE_INIT(&tag
, h
);
627 print_otag(h
, TAG_DIV
, 1, &tag
);
631 bufcat_su(h
, "min-width", &su
);
633 bufcat_su(h
, "margin-left", &su
);
634 SCALE_HS_INIT(&su
, 1);
635 bufcat_su(h
, "margin-right", &su
);
636 bufcat_style(h
, "clear", "left");
638 if (n
->next
&& n
->next
->child
)
639 bufcat_style(h
, "float", "left");
641 PAIR_STYLE_INIT(&tag
, h
);
642 print_otag(h
, TAG_DIV
, 1, &tag
);
645 * Without a length string, we can print all of our children.
652 * When a length has been specified, we need to carefully print
653 * our child context: IP gets all children printed but the last
654 * (the width), while TP gets all children printed but the first
658 if (MAN_IP
== n
->tok
)
659 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
660 print_man_node(m
, nn
, mh
, h
);
661 if (MAN_TP
== n
->tok
)
662 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
663 print_man_node(m
, nn
, mh
, h
);
673 const struct man_node
*nn
;
677 if (MAN_HEAD
== n
->type
)
680 nn
= MAN_BLOCK
== n
->type
?
681 n
->head
->child
: n
->parent
->head
->child
;
683 SCALE_HS_INIT(&su
, INDENT
);
686 (void)a2width(nn
, &su
);
688 if (MAN_BLOCK
== n
->type
) {
689 bufcat_su(h
, "margin-left", &su
);
690 SCALE_VS_INIT(&su
, 1);
691 bufcat_su(h
, "margin-top", &su
);
692 bufcat_style(h
, "clear", "both");
693 PAIR_STYLE_INIT(&tag
, h
);
694 print_otag(h
, TAG_DIV
, 1, &tag
);
698 bufcat_su(h
, "margin-left", &su
);
700 bufcat_su(h
, "text-indent", &su
);
702 PAIR_STYLE_INIT(&tag
, h
);
703 print_otag(h
, TAG_DIV
, 1, &tag
);
713 print_ofont(h
, HTMLFONT_BOLD
);
723 print_ofont(h
, HTMLFONT_ITALIC
);
730 man_literal_pre(MAN_ARGS
)
735 print_otag(h
, TAG_BR
, 0, NULL
);
736 mh
->fl
|= MANH_LITERAL
;
739 mh
->fl
&= ~MANH_LITERAL
;
752 print_otag(h
, TAG_BR
, 0, NULL
);
759 man_ign_pre(MAN_ARGS
)
773 if (MAN_HEAD
== n
->type
)
775 else if (MAN_BODY
== n
->type
)
778 SCALE_HS_INIT(&su
, INDENT
);
779 bufcat_su(h
, "margin-left", &su
);
781 if (n
->head
->child
) {
782 SCALE_VS_INIT(&su
, 1);
783 a2width(n
->head
->child
, &su
);
784 bufcat_su(h
, "margin-top", &su
);
787 PAIR_STYLE_INIT(&tag
, h
);
788 print_otag(h
, TAG_DIV
, 1, &tag
);