]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.45 2010/07/23 12:27:28 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_br_pre
, NULL
}, /* Sp */
116 { man_literal_pre
, NULL
}, /* Vb */
117 { man_literal_pre
, NULL
}, /* Ve */
118 { man_ign_pre
, NULL
}, /* AT */
119 { man_in_pre
, NULL
}, /* in */
124 html_man(void *arg
, const struct man
*m
)
130 h
= (struct html
*)arg
;
134 memset(&mh
, 0, sizeof(struct mhtml
));
136 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
137 print_man(man_meta(m
), man_node(m
), &mh
, h
);
150 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
152 print_man_head(m
, n
, mh
, h
);
154 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
156 tag
.key
= ATTR_CLASS
;
158 print_otag(h
, TAG_DIV
, 1, &tag
);
160 print_man_nodelist(m
, n
, mh
, h
);
168 print_man_head(MAN_ARGS
)
173 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
175 print_otag(h
, TAG_TITLE
, 0, NULL
);
176 print_text(h
, h
->buf
);
181 print_man_nodelist(MAN_ARGS
)
184 print_man_node(m
, n
, mh
, h
);
186 print_man_nodelist(m
, n
->next
, mh
, h
);
191 print_man_node(MAN_ARGS
)
202 * FIXME: embedded elements within next-line scopes (e.g., `br'
203 * within an empty `B') will cause formatting to be forgotten
204 * due to scope closing out.
209 child
= man_root_pre(m
, n
, mh
, h
);
212 print_text(h
, n
->string
);
214 if (MANH_LITERAL
& mh
->fl
)
215 print_otag(h
, TAG_BR
, 0, NULL
);
220 * Close out scope of font prior to opening a macro
221 * scope. Assert that the metafont is on the top of the
222 * stack (it's never nested).
225 assert(h
->metaf
== t
);
226 print_tagq(h
, h
->metaf
);
227 assert(NULL
== h
->metaf
);
230 if (mans
[n
->tok
].pre
)
231 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
235 if (child
&& n
->child
)
236 print_man_nodelist(m
, n
->child
, mh
, h
);
238 /* This will automatically close out any font scope. */
245 man_root_post(m
, n
, mh
, h
);
250 if (mans
[n
->tok
].post
)
251 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
258 a2width(const struct man_node
*n
, struct roffsu
*su
)
261 if (MAN_TEXT
!= n
->type
)
263 if (a2roffsu(n
->string
, su
, SCALE_BU
))
272 man_root_pre(MAN_ARGS
)
274 struct htmlpair tag
[3];
276 char b
[BUFSIZ
], title
[BUFSIZ
];
280 (void)strlcat(b
, m
->vol
, BUFSIZ
);
282 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
284 PAIR_CLASS_INIT(&tag
[0], "header");
285 bufcat_style(h
, "width", "100%");
286 PAIR_STYLE_INIT(&tag
[1], h
);
287 PAIR_SUMMARY_INIT(&tag
[2], "header");
289 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
290 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
293 bufcat_style(h
, "width", "10%");
294 PAIR_STYLE_INIT(&tag
[0], h
);
295 print_otag(h
, TAG_TD
, 1, tag
);
296 print_text(h
, title
);
300 bufcat_style(h
, "width", "80%");
301 bufcat_style(h
, "white-space", "nowrap");
302 bufcat_style(h
, "text-align", "center");
303 PAIR_STYLE_INIT(&tag
[0], h
);
304 print_otag(h
, TAG_TD
, 1, tag
);
309 bufcat_style(h
, "width", "10%");
310 bufcat_style(h
, "text-align", "right");
311 PAIR_STYLE_INIT(&tag
[0], h
);
312 print_otag(h
, TAG_TD
, 1, tag
);
313 print_text(h
, title
);
321 man_root_post(MAN_ARGS
)
323 struct htmlpair tag
[3];
328 strlcpy(b
, m
->rawdate
, DATESIZ
);
330 time2a(m
->date
, b
, DATESIZ
);
332 PAIR_CLASS_INIT(&tag
[0], "footer");
333 bufcat_style(h
, "width", "100%");
334 PAIR_STYLE_INIT(&tag
[1], h
);
335 PAIR_SUMMARY_INIT(&tag
[2], "footer");
337 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
338 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
341 bufcat_style(h
, "width", "50%");
342 PAIR_STYLE_INIT(&tag
[0], h
);
343 print_otag(h
, TAG_TD
, 1, tag
);
348 bufcat_style(h
, "width", "50%");
349 bufcat_style(h
, "text-align", "right");
350 PAIR_STYLE_INIT(&tag
[0], h
);
351 print_otag(h
, TAG_TD
, 1, tag
);
353 print_text(h
, m
->source
);
366 SCALE_VS_INIT(&su
, 1);
370 SCALE_VS_INIT(&su
, 0.5);
374 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
381 bufcat_su(h
, "height", &su
);
382 PAIR_STYLE_INIT(&tag
, h
);
383 print_otag(h
, TAG_DIV
, 1, &tag
);
385 /* So the div isn't empty: */
386 print_text(h
, "\\~");
396 struct htmlpair tag
[2];
399 if (MAN_BODY
== n
->type
) {
400 SCALE_HS_INIT(&su
, INDENT
);
401 bufcat_su(h
, "margin-left", &su
);
402 PAIR_CLASS_INIT(&tag
[0], "sec-body");
403 PAIR_STYLE_INIT(&tag
[1], h
);
404 print_otag(h
, TAG_DIV
, 2, tag
);
406 } else if (MAN_BLOCK
== n
->type
) {
407 PAIR_CLASS_INIT(&tag
[0], "sec-block");
408 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
409 if (NULL
== n
->prev
->body
->child
) {
410 print_otag(h
, TAG_DIV
, 1, tag
);
414 SCALE_VS_INIT(&su
, 1);
415 bufcat_su(h
, "margin-top", &su
);
417 bufcat_su(h
, "margin-bottom", &su
);
418 PAIR_STYLE_INIT(&tag
[1], h
);
419 print_otag(h
, TAG_DIV
, 2, tag
);
423 PAIR_CLASS_INIT(&tag
[0], "sec-head");
424 print_otag(h
, TAG_DIV
, 1, tag
);
431 man_alt_pre(MAN_ARGS
)
433 const struct man_node
*nn
;
438 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
441 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
444 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
447 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
450 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
453 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
456 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
464 h
->flags
|= HTML_NOSPACE
;
467 * Open and close the scope with each argument, so that
468 * internal \f escapes, which are common, are also
469 * closed out with the scope.
471 t
= print_ofont(h
, fp
);
472 print_man_node(m
, nn
, mh
, h
);
486 /* FIXME: print_ofont(). */
487 PAIR_CLASS_INIT(&tag
, "small bold");
488 print_otag(h
, TAG_SPAN
, 1, &tag
);
499 PAIR_CLASS_INIT(&tag
, "small");
500 print_otag(h
, TAG_SPAN
, 1, &tag
);
509 struct htmlpair tag
[3];
512 SCALE_VS_INIT(&su
, 1);
514 if (MAN_BODY
== n
->type
) {
515 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
516 if (n
->parent
->next
&& n
->child
) {
517 bufcat_su(h
, "margin-bottom", &su
);
518 PAIR_STYLE_INIT(&tag
[1], h
);
519 print_otag(h
, TAG_DIV
, 2, tag
);
523 print_otag(h
, TAG_DIV
, 1, tag
);
525 } else if (MAN_BLOCK
== n
->type
) {
526 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
527 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
528 if (n
->prev
->body
->child
) {
529 bufcat_su(h
, "margin-top", &su
);
530 PAIR_STYLE_INIT(&tag
[1], h
);
531 print_otag(h
, TAG_DIV
, 2, tag
);
535 print_otag(h
, TAG_DIV
, 1, tag
);
539 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
540 bufcat_su(h
, "margin-left", &su
);
541 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
542 PAIR_STYLE_INIT(&tag
[1], h
);
543 print_otag(h
, TAG_DIV
, 2, tag
);
556 if (MAN_BLOCK
!= n
->type
)
561 if (MAN_ROOT
== n
->parent
->type
) {
562 SCALE_HS_INIT(&su
, INDENT
);
563 bufcat_su(h
, "margin-left", &su
);
567 SCALE_VS_INIT(&su
, 1);
568 bufcat_su(h
, "margin-top", &su
);
572 PAIR_STYLE_INIT(&tag
, h
);
573 print_otag(h
, TAG_DIV
, i
, &tag
);
584 const struct man_node
*nn
;
588 * This scattering of 1-BU margins and pads is to make sure that
589 * when text overruns its box, the subsequent text isn't flush
590 * up against it. However, the rest of the right-hand box must
591 * also be adjusted in consideration of this 1-BU space.
594 if (MAN_BODY
== n
->type
) {
595 SCALE_HS_INIT(&su
, INDENT
);
596 bufcat_su(h
, "margin-left", &su
);
597 PAIR_STYLE_INIT(&tag
, h
);
598 print_otag(h
, TAG_DIV
, 1, &tag
);
602 nn
= MAN_BLOCK
== n
->type
?
603 n
->head
->child
: n
->parent
->head
->child
;
605 SCALE_HS_INIT(&su
, INDENT
);
608 /* Width is the last token. */
610 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
611 if (NULL
!= (nn
= nn
->next
)) {
612 for ( ; nn
->next
; nn
= nn
->next
)
614 width
= a2width(nn
, &su
);
617 /* Width is the first token. */
619 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
620 /* Skip past non-text children. */
621 while (nn
&& MAN_TEXT
!= nn
->type
)
624 width
= a2width(nn
, &su
);
627 if (MAN_BLOCK
== n
->type
) {
628 bufcat_su(h
, "margin-left", &su
);
629 SCALE_VS_INIT(&su
, 1);
630 bufcat_su(h
, "margin-top", &su
);
631 bufcat_style(h
, "clear", "both");
632 PAIR_STYLE_INIT(&tag
, h
);
633 print_otag(h
, TAG_DIV
, 1, &tag
);
637 bufcat_su(h
, "min-width", &su
);
639 bufcat_su(h
, "margin-left", &su
);
640 SCALE_HS_INIT(&su
, 1);
641 bufcat_su(h
, "margin-right", &su
);
642 bufcat_style(h
, "clear", "left");
644 if (n
->next
&& n
->next
->child
)
645 bufcat_style(h
, "float", "left");
647 PAIR_STYLE_INIT(&tag
, h
);
648 print_otag(h
, TAG_DIV
, 1, &tag
);
651 * Without a length string, we can print all of our children.
658 * When a length has been specified, we need to carefully print
659 * our child context: IP gets all children printed but the last
660 * (the width), while TP gets all children printed but the first
664 if (MAN_IP
== n
->tok
)
665 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
666 print_man_node(m
, nn
, mh
, h
);
667 if (MAN_TP
== n
->tok
)
668 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
669 print_man_node(m
, nn
, mh
, h
);
679 const struct man_node
*nn
;
683 if (MAN_HEAD
== n
->type
)
686 nn
= MAN_BLOCK
== n
->type
?
687 n
->head
->child
: n
->parent
->head
->child
;
689 SCALE_HS_INIT(&su
, INDENT
);
692 (void)a2width(nn
, &su
);
694 if (MAN_BLOCK
== n
->type
) {
695 bufcat_su(h
, "margin-left", &su
);
696 SCALE_VS_INIT(&su
, 1);
697 bufcat_su(h
, "margin-top", &su
);
698 bufcat_style(h
, "clear", "both");
699 PAIR_STYLE_INIT(&tag
, h
);
700 print_otag(h
, TAG_DIV
, 1, &tag
);
704 bufcat_su(h
, "margin-left", &su
);
706 bufcat_su(h
, "text-indent", &su
);
708 PAIR_STYLE_INIT(&tag
, h
);
709 print_otag(h
, TAG_DIV
, 1, &tag
);
719 print_ofont(h
, HTMLFONT_BOLD
);
729 print_ofont(h
, HTMLFONT_ITALIC
);
736 man_literal_pre(MAN_ARGS
)
743 print_otag(h
, TAG_BR
, 0, NULL
);
744 mh
->fl
|= MANH_LITERAL
;
745 return(MAN_Vb
!= n
->tok
);
747 mh
->fl
&= ~MANH_LITERAL
;
760 print_otag(h
, TAG_BR
, 0, NULL
);
767 man_ign_pre(MAN_ARGS
)
781 if (MAN_HEAD
== n
->type
)
783 else if (MAN_BODY
== n
->type
)
786 SCALE_HS_INIT(&su
, INDENT
);
787 bufcat_su(h
, "margin-left", &su
);
789 if (n
->head
->child
) {
790 SCALE_VS_INIT(&su
, 1);
791 a2width(n
->head
->child
, &su
);
792 bufcat_su(h
, "margin-top", &su
);
795 PAIR_STYLE_INIT(&tag
, h
);
796 print_otag(h
, TAG_DIV
, 1, &tag
);