]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.32 2010/05/15 20:51:40 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 */
109 { man_ign_pre
, NULL
}, /* de */
110 { man_ign_pre
, NULL
}, /* dei */
111 { man_ign_pre
, NULL
}, /* am */
112 { man_ign_pre
, NULL
}, /* ami */
113 { NULL
, NULL
}, /* . */
118 html_man(void *arg
, const struct man
*m
)
123 h
= (struct html
*)arg
;
127 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
128 print_man(man_meta(m
), man_node(m
), h
);
141 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
143 print_man_head(m
, n
, h
);
145 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
147 tag
.key
= ATTR_CLASS
;
149 print_otag(h
, TAG_DIV
, 1, &tag
);
151 print_man_nodelist(m
, n
, h
);
159 print_man_head(MAN_ARGS
)
164 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
166 print_otag(h
, TAG_TITLE
, 0, NULL
);
167 print_text(h
, h
->buf
);
172 print_man_nodelist(MAN_ARGS
)
175 print_man_node(m
, n
, h
);
177 print_man_nodelist(m
, n
->next
, h
);
182 print_man_node(MAN_ARGS
)
193 * FIXME: embedded elements within next-line scopes (e.g., `br'
194 * within an empty `B') will cause formatting to be forgotten
195 * due to scope closing out.
200 child
= man_root_pre(m
, n
, h
);
203 print_text(h
, n
->string
);
207 * Close out scope of font prior to opening a macro
208 * scope. Assert that the metafont is on the top of the
209 * stack (it's never nested).
212 assert(h
->metaf
== t
);
213 print_tagq(h
, h
->metaf
);
214 assert(NULL
== h
->metaf
);
217 if (mans
[n
->tok
].pre
)
218 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
222 if (child
&& n
->child
)
223 print_man_nodelist(m
, n
->child
, h
);
225 /* This will automatically close out any font scope. */
232 man_root_post(m
, n
, h
);
237 if (mans
[n
->tok
].post
)
238 (*mans
[n
->tok
].post
)(m
, n
, h
);
245 a2width(const struct man_node
*n
, struct roffsu
*su
)
248 if (MAN_TEXT
!= n
->type
)
250 if (a2roffsu(n
->string
, su
, SCALE_BU
))
259 man_root_pre(MAN_ARGS
)
261 struct htmlpair tag
[3];
263 char b
[BUFSIZ
], title
[BUFSIZ
];
267 (void)strlcat(b
, m
->vol
, BUFSIZ
);
269 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
271 PAIR_CLASS_INIT(&tag
[0], "header");
272 bufcat_style(h
, "width", "100%");
273 PAIR_STYLE_INIT(&tag
[1], h
);
274 PAIR_SUMMARY_INIT(&tag
[2], "header");
276 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
277 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
280 bufcat_style(h
, "width", "10%");
281 PAIR_STYLE_INIT(&tag
[0], h
);
282 print_otag(h
, TAG_TD
, 1, tag
);
283 print_text(h
, title
);
287 bufcat_style(h
, "width", "80%");
288 bufcat_style(h
, "white-space", "nowrap");
289 bufcat_style(h
, "text-align", "center");
290 PAIR_STYLE_INIT(&tag
[0], h
);
291 print_otag(h
, TAG_TD
, 1, tag
);
296 bufcat_style(h
, "width", "10%");
297 bufcat_style(h
, "text-align", "right");
298 PAIR_STYLE_INIT(&tag
[0], h
);
299 print_otag(h
, TAG_TD
, 1, tag
);
300 print_text(h
, title
);
308 man_root_post(MAN_ARGS
)
310 struct htmlpair tag
[3];
314 time2a(m
->date
, b
, DATESIZ
);
316 PAIR_CLASS_INIT(&tag
[0], "footer");
317 bufcat_style(h
, "width", "100%");
318 PAIR_STYLE_INIT(&tag
[1], h
);
319 PAIR_SUMMARY_INIT(&tag
[2], "footer");
321 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
322 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
325 bufcat_style(h
, "width", "50%");
326 PAIR_STYLE_INIT(&tag
[0], h
);
327 print_otag(h
, TAG_TD
, 1, tag
);
332 bufcat_style(h
, "width", "50%");
333 bufcat_style(h
, "text-align", "right");
334 PAIR_STYLE_INIT(&tag
[0], h
);
335 print_otag(h
, TAG_TD
, 1, tag
);
337 print_text(h
, m
->source
);
350 SCALE_VS_INIT(&su
, 1);
354 SCALE_VS_INIT(&su
, 0.5);
358 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
365 bufcat_su(h
, "height", &su
);
366 PAIR_STYLE_INIT(&tag
, h
);
367 print_otag(h
, TAG_DIV
, 1, &tag
);
369 /* So the div isn't empty: */
370 print_text(h
, "\\~");
380 struct htmlpair tag
[2];
383 if (MAN_BODY
== n
->type
) {
384 SCALE_HS_INIT(&su
, INDENT
);
385 bufcat_su(h
, "margin-left", &su
);
386 PAIR_CLASS_INIT(&tag
[0], "sec-body");
387 PAIR_STYLE_INIT(&tag
[1], h
);
388 print_otag(h
, TAG_DIV
, 2, tag
);
390 } else if (MAN_BLOCK
== n
->type
) {
391 PAIR_CLASS_INIT(&tag
[0], "sec-block");
392 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
393 if (NULL
== n
->prev
->body
->child
) {
394 print_otag(h
, TAG_DIV
, 1, tag
);
398 SCALE_VS_INIT(&su
, 1);
399 bufcat_su(h
, "margin-top", &su
);
401 bufcat_su(h
, "margin-bottom", &su
);
402 PAIR_STYLE_INIT(&tag
[1], h
);
403 print_otag(h
, TAG_DIV
, 2, tag
);
407 PAIR_CLASS_INIT(&tag
[0], "sec-head");
408 print_otag(h
, TAG_DIV
, 1, tag
);
415 man_alt_pre(MAN_ARGS
)
417 const struct man_node
*nn
;
422 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
425 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
428 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
431 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
434 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
437 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
440 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
448 h
->flags
|= HTML_NOSPACE
;
451 * Open and close the scope with each argument, so that
452 * internal \f escapes, which are common, are also
453 * closed out with the scope.
455 t
= print_ofont(h
, fp
);
456 print_man_node(m
, nn
, h
);
470 /* FIXME: print_ofont(). */
471 PAIR_CLASS_INIT(&tag
, "small bold");
472 print_otag(h
, TAG_SPAN
, 1, &tag
);
483 PAIR_CLASS_INIT(&tag
, "small");
484 print_otag(h
, TAG_SPAN
, 1, &tag
);
493 struct htmlpair tag
[3];
496 SCALE_VS_INIT(&su
, 1);
498 if (MAN_BODY
== n
->type
) {
499 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
500 if (n
->parent
->next
&& n
->child
) {
501 bufcat_su(h
, "margin-bottom", &su
);
502 PAIR_STYLE_INIT(&tag
[1], h
);
503 print_otag(h
, TAG_DIV
, 2, tag
);
507 print_otag(h
, TAG_DIV
, 1, tag
);
509 } else if (MAN_BLOCK
== n
->type
) {
510 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
511 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
512 if (n
->prev
->body
->child
) {
513 bufcat_su(h
, "margin-top", &su
);
514 PAIR_STYLE_INIT(&tag
[1], h
);
515 print_otag(h
, TAG_DIV
, 2, tag
);
519 print_otag(h
, TAG_DIV
, 1, tag
);
523 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
524 bufcat_su(h
, "margin-left", &su
);
525 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
526 PAIR_STYLE_INIT(&tag
[1], h
);
527 print_otag(h
, TAG_DIV
, 2, tag
);
540 if (MAN_BLOCK
!= n
->type
)
545 if (MAN_ROOT
== n
->parent
->type
) {
546 SCALE_HS_INIT(&su
, INDENT
);
547 bufcat_su(h
, "margin-left", &su
);
551 SCALE_VS_INIT(&su
, 1);
552 bufcat_su(h
, "margin-top", &su
);
556 PAIR_STYLE_INIT(&tag
, h
);
557 print_otag(h
, TAG_DIV
, i
, &tag
);
568 const struct man_node
*nn
;
572 * This scattering of 1-BU margins and pads is to make sure that
573 * when text overruns its box, the subsequent text isn't flush
574 * up against it. However, the rest of the right-hand box must
575 * also be adjusted in consideration of this 1-BU space.
578 if (MAN_BODY
== n
->type
) {
579 SCALE_HS_INIT(&su
, INDENT
);
580 bufcat_su(h
, "margin-left", &su
);
581 PAIR_STYLE_INIT(&tag
, h
);
582 print_otag(h
, TAG_DIV
, 1, &tag
);
586 nn
= MAN_BLOCK
== n
->type
?
587 n
->head
->child
: n
->parent
->head
->child
;
589 SCALE_HS_INIT(&su
, INDENT
);
592 /* Width is the last token. */
594 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
595 if (NULL
!= (nn
= nn
->next
)) {
596 for ( ; nn
->next
; nn
= nn
->next
)
598 width
= a2width(nn
, &su
);
601 /* Width is the first token. */
603 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
604 /* Skip past non-text children. */
605 while (nn
&& MAN_TEXT
!= nn
->type
)
608 width
= a2width(nn
, &su
);
611 if (MAN_BLOCK
== n
->type
) {
612 bufcat_su(h
, "margin-left", &su
);
613 SCALE_VS_INIT(&su
, 1);
614 bufcat_su(h
, "margin-top", &su
);
615 bufcat_style(h
, "clear", "both");
616 PAIR_STYLE_INIT(&tag
, h
);
617 print_otag(h
, TAG_DIV
, 1, &tag
);
621 bufcat_su(h
, "min-width", &su
);
623 bufcat_su(h
, "margin-left", &su
);
624 SCALE_HS_INIT(&su
, 1);
625 bufcat_su(h
, "margin-right", &su
);
626 bufcat_style(h
, "clear", "left");
628 if (n
->next
&& n
->next
->child
)
629 bufcat_style(h
, "float", "left");
631 PAIR_STYLE_INIT(&tag
, h
);
632 print_otag(h
, TAG_DIV
, 1, &tag
);
635 * Without a length string, we can print all of our children.
642 * When a length has been specified, we need to carefully print
643 * our child context: IP gets all children printed but the last
644 * (the width), while TP gets all children printed but the first
648 if (MAN_IP
== n
->tok
)
649 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
650 print_man_node(m
, nn
, h
);
651 if (MAN_TP
== n
->tok
)
652 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
653 print_man_node(m
, nn
, h
);
663 const struct man_node
*nn
;
667 if (MAN_HEAD
== n
->type
)
670 nn
= MAN_BLOCK
== n
->type
?
671 n
->head
->child
: n
->parent
->head
->child
;
673 SCALE_HS_INIT(&su
, INDENT
);
676 (void)a2width(nn
, &su
);
678 if (MAN_BLOCK
== n
->type
) {
679 bufcat_su(h
, "margin-left", &su
);
680 SCALE_VS_INIT(&su
, 1);
681 bufcat_su(h
, "margin-top", &su
);
682 bufcat_style(h
, "clear", "both");
683 PAIR_STYLE_INIT(&tag
, h
);
684 print_otag(h
, TAG_DIV
, 1, &tag
);
688 bufcat_su(h
, "margin-left", &su
);
690 bufcat_su(h
, "text-indent", &su
);
692 PAIR_STYLE_INIT(&tag
, h
);
693 print_otag(h
, TAG_DIV
, 1, &tag
);
703 print_ofont(h
, HTMLFONT_BOLD
);
713 print_ofont(h
, HTMLFONT_ITALIC
);
720 man_ign_pre(MAN_ARGS
)
734 if (MAN_HEAD
== n
->type
)
736 else if (MAN_BODY
== n
->type
)
739 SCALE_HS_INIT(&su
, INDENT
);
740 bufcat_su(h
, "margin-left", &su
);
742 if (n
->head
->child
) {
743 SCALE_VS_INIT(&su
, 1);
744 a2width(n
->head
->child
, &su
);
745 bufcat_su(h
, "margin-top", &su
);
748 PAIR_STYLE_INIT(&tag
, h
);
749 print_otag(h
, TAG_DIV
, 1, &tag
);