]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.51 2010/12/06 15:31:44 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 */
117 { man_ign_pre
, NULL
}, /* ft */
122 html_man(void *arg
, const struct man
*m
)
128 h
= (struct html
*)arg
;
132 memset(&mh
, 0, sizeof(struct mhtml
));
134 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
135 print_man(man_meta(m
), man_node(m
), &mh
, h
);
148 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
150 print_man_head(m
, n
, mh
, h
);
152 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
154 tag
.key
= ATTR_CLASS
;
156 print_otag(h
, TAG_DIV
, 1, &tag
);
158 print_man_nodelist(m
, n
, mh
, h
);
166 print_man_head(MAN_ARGS
)
171 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
173 print_otag(h
, TAG_TITLE
, 0, NULL
);
174 print_text(h
, h
->buf
);
179 print_man_nodelist(MAN_ARGS
)
182 print_man_node(m
, n
, mh
, h
);
184 print_man_nodelist(m
, n
->next
, mh
, h
);
189 print_man_node(MAN_ARGS
)
200 * FIXME: embedded elements within next-line scopes (e.g., `br'
201 * within an empty `B') will cause formatting to be forgotten
202 * due to scope closing out.
207 child
= man_root_pre(m
, n
, mh
, h
);
210 print_text(h
, n
->string
);
212 if (MANH_LITERAL
& mh
->fl
)
213 print_otag(h
, TAG_BR
, 0, NULL
);
218 * Close out scope of font prior to opening a macro
219 * scope. Assert that the metafont is on the top of the
220 * stack (it's never nested).
223 assert(h
->metaf
== t
);
224 print_tagq(h
, h
->metaf
);
225 assert(NULL
== h
->metaf
);
228 if (mans
[n
->tok
].pre
)
229 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
233 if (child
&& n
->child
)
234 print_man_nodelist(m
, n
->child
, mh
, h
);
236 /* This will automatically close out any font scope. */
243 man_root_post(m
, n
, mh
, h
);
248 if (mans
[n
->tok
].post
)
249 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
256 a2width(const struct man_node
*n
, struct roffsu
*su
)
259 if (MAN_TEXT
!= n
->type
)
261 if (a2roffsu(n
->string
, su
, SCALE_BU
))
270 man_root_pre(MAN_ARGS
)
272 struct htmlpair tag
[3];
274 char b
[BUFSIZ
], title
[BUFSIZ
];
278 (void)strlcat(b
, m
->vol
, BUFSIZ
);
280 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
282 PAIR_CLASS_INIT(&tag
[0], "header");
283 bufcat_style(h
, "width", "100%");
284 PAIR_STYLE_INIT(&tag
[1], h
);
285 PAIR_SUMMARY_INIT(&tag
[2], "header");
287 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
288 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
291 bufcat_style(h
, "width", "10%");
292 PAIR_STYLE_INIT(&tag
[0], h
);
293 print_otag(h
, TAG_TD
, 1, tag
);
294 print_text(h
, title
);
298 bufcat_style(h
, "width", "80%");
299 bufcat_style(h
, "white-space", "nowrap");
300 bufcat_style(h
, "text-align", "center");
301 PAIR_STYLE_INIT(&tag
[0], h
);
302 print_otag(h
, TAG_TD
, 1, tag
);
307 bufcat_style(h
, "width", "10%");
308 bufcat_style(h
, "text-align", "right");
309 PAIR_STYLE_INIT(&tag
[0], h
);
310 print_otag(h
, TAG_TD
, 1, tag
);
311 print_text(h
, title
);
319 man_root_post(MAN_ARGS
)
321 struct htmlpair tag
[3];
326 strlcpy(b
, m
->rawdate
, DATESIZ
);
328 time2a(m
->date
, b
, DATESIZ
);
330 PAIR_CLASS_INIT(&tag
[0], "footer");
331 bufcat_style(h
, "width", "100%");
332 PAIR_STYLE_INIT(&tag
[1], h
);
333 PAIR_SUMMARY_INIT(&tag
[2], "footer");
335 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
336 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
339 bufcat_style(h
, "width", "50%");
340 PAIR_STYLE_INIT(&tag
[0], h
);
341 print_otag(h
, TAG_TD
, 1, tag
);
346 bufcat_style(h
, "width", "50%");
347 bufcat_style(h
, "text-align", "right");
348 PAIR_STYLE_INIT(&tag
[0], h
);
349 print_otag(h
, TAG_TD
, 1, tag
);
351 print_text(h
, m
->source
);
364 SCALE_VS_INIT(&su
, 1);
366 if (MAN_sp
== n
->tok
) {
368 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
372 bufcat_su(h
, "height", &su
);
373 PAIR_STYLE_INIT(&tag
, h
);
374 print_otag(h
, TAG_DIV
, 1, &tag
);
376 /* So the div isn't empty: */
377 print_text(h
, "\\~");
387 struct htmlpair tag
[2];
390 if (MAN_BODY
== n
->type
) {
391 SCALE_HS_INIT(&su
, INDENT
);
392 bufcat_su(h
, "margin-left", &su
);
393 PAIR_CLASS_INIT(&tag
[0], "sec-body");
394 PAIR_STYLE_INIT(&tag
[1], h
);
395 print_otag(h
, TAG_DIV
, 2, tag
);
397 } else if (MAN_BLOCK
== n
->type
) {
398 PAIR_CLASS_INIT(&tag
[0], "sec-block");
399 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
400 if (NULL
== n
->prev
->body
->child
) {
401 print_otag(h
, TAG_DIV
, 1, tag
);
405 SCALE_VS_INIT(&su
, 1);
406 bufcat_su(h
, "margin-top", &su
);
408 bufcat_su(h
, "margin-bottom", &su
);
409 PAIR_STYLE_INIT(&tag
[1], h
);
410 print_otag(h
, TAG_DIV
, 2, tag
);
414 PAIR_CLASS_INIT(&tag
[0], "sec-head");
415 print_otag(h
, TAG_DIV
, 1, tag
);
422 man_alt_pre(MAN_ARGS
)
424 const struct man_node
*nn
;
429 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
432 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
435 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
438 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
441 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
444 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
447 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
455 h
->flags
|= HTML_NOSPACE
;
458 * Open and close the scope with each argument, so that
459 * internal \f escapes, which are common, are also
460 * closed out with the scope.
462 t
= print_ofont(h
, fp
);
463 print_man_node(m
, nn
, mh
, h
);
477 /* FIXME: print_ofont(). */
478 PAIR_CLASS_INIT(&tag
, "small bold");
479 print_otag(h
, TAG_SPAN
, 1, &tag
);
490 PAIR_CLASS_INIT(&tag
, "small");
491 print_otag(h
, TAG_SPAN
, 1, &tag
);
500 struct htmlpair tag
[3];
503 SCALE_VS_INIT(&su
, 1);
505 if (MAN_BODY
== n
->type
) {
506 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
507 if (n
->parent
->next
&& n
->child
) {
508 bufcat_su(h
, "margin-bottom", &su
);
509 PAIR_STYLE_INIT(&tag
[1], h
);
510 print_otag(h
, TAG_DIV
, 2, tag
);
514 print_otag(h
, TAG_DIV
, 1, tag
);
516 } else if (MAN_BLOCK
== n
->type
) {
517 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
518 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
519 if (n
->prev
->body
->child
) {
520 bufcat_su(h
, "margin-top", &su
);
521 PAIR_STYLE_INIT(&tag
[1], h
);
522 print_otag(h
, TAG_DIV
, 2, tag
);
526 print_otag(h
, TAG_DIV
, 1, tag
);
530 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
531 bufcat_su(h
, "margin-left", &su
);
532 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
533 PAIR_STYLE_INIT(&tag
[1], h
);
534 print_otag(h
, TAG_DIV
, 2, tag
);
547 if (MAN_BODY
== n
->type
)
549 if (MAN_HEAD
== n
->type
)
554 if (MAN_ROOT
== n
->parent
->type
) {
555 SCALE_HS_INIT(&su
, INDENT
);
556 bufcat_su(h
, "margin-left", &su
);
560 SCALE_VS_INIT(&su
, 1);
561 bufcat_su(h
, "margin-top", &su
);
565 PAIR_STYLE_INIT(&tag
, h
);
566 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
)
733 if (MAN_nf
== n
->tok
) {
734 print_otag(h
, TAG_BR
, 0, NULL
);
735 mh
->fl
|= MANH_LITERAL
;
737 mh
->fl
&= ~MANH_LITERAL
;
748 print_otag(h
, TAG_BR
, 0, NULL
);
755 man_ign_pre(MAN_ARGS
)
769 if (MAN_HEAD
== n
->type
)
771 else if (MAN_BODY
== n
->type
)
774 SCALE_HS_INIT(&su
, INDENT
);
775 bufcat_su(h
, "margin-left", &su
);
777 if (n
->head
->child
) {
778 SCALE_VS_INIT(&su
, 1);
779 a2width(n
->head
->child
, &su
);
780 bufcat_su(h
, "margin-top", &su
);
783 PAIR_STYLE_INIT(&tag
, h
);
784 print_otag(h
, TAG_DIV
, 1, &tag
);