]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.35 2010/05/17 22:11:42 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>
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, \
47 int (*post
)(MAN_ARGS
);
50 static void print_man(MAN_ARGS
);
51 static void print_man_head(MAN_ARGS
);
52 static void print_man_nodelist(MAN_ARGS
);
53 static void print_man_node(MAN_ARGS
);
55 static int a2width(const struct man_node
*,
58 static int man_alt_pre(MAN_ARGS
);
59 static int man_br_pre(MAN_ARGS
);
60 static int man_ign_pre(MAN_ARGS
);
61 static void man_root_post(MAN_ARGS
);
62 static int man_root_pre(MAN_ARGS
);
63 static int man_B_pre(MAN_ARGS
);
64 static int man_HP_pre(MAN_ARGS
);
65 static int man_I_pre(MAN_ARGS
);
66 static int man_IP_pre(MAN_ARGS
);
67 static int man_PP_pre(MAN_ARGS
);
68 static int man_RS_pre(MAN_ARGS
);
69 static int man_SB_pre(MAN_ARGS
);
70 static int man_SH_pre(MAN_ARGS
);
71 static int man_SM_pre(MAN_ARGS
);
72 static int man_SS_pre(MAN_ARGS
);
74 static const struct htmlman mans
[MAN_MAX
] = {
75 { man_br_pre
, NULL
}, /* br */
76 { NULL
, NULL
}, /* TH */
77 { man_SH_pre
, NULL
}, /* SH */
78 { man_SS_pre
, NULL
}, /* SS */
79 { man_IP_pre
, NULL
}, /* TP */
80 { man_PP_pre
, NULL
}, /* LP */
81 { man_PP_pre
, NULL
}, /* PP */
82 { man_PP_pre
, NULL
}, /* P */
83 { man_IP_pre
, NULL
}, /* IP */
84 { man_HP_pre
, NULL
}, /* HP */
85 { man_SM_pre
, NULL
}, /* SM */
86 { man_SB_pre
, NULL
}, /* SB */
87 { man_alt_pre
, NULL
}, /* BI */
88 { man_alt_pre
, NULL
}, /* IB */
89 { man_alt_pre
, NULL
}, /* BR */
90 { man_alt_pre
, NULL
}, /* RB */
91 { NULL
, NULL
}, /* R */
92 { man_B_pre
, NULL
}, /* B */
93 { man_I_pre
, NULL
}, /* I */
94 { man_alt_pre
, NULL
}, /* IR */
95 { man_alt_pre
, NULL
}, /* RI */
96 { NULL
, NULL
}, /* na */
97 { NULL
, NULL
}, /* i */
98 { man_br_pre
, NULL
}, /* sp */
99 { NULL
, NULL
}, /* nf */
100 { NULL
, NULL
}, /* fi */
101 { NULL
, NULL
}, /* r */
102 { NULL
, NULL
}, /* RE */
103 { man_RS_pre
, NULL
}, /* RS */
104 { man_ign_pre
, NULL
}, /* DT */
105 { man_ign_pre
, NULL
}, /* UC */
106 { man_ign_pre
, NULL
}, /* PD */
107 { man_br_pre
, NULL
}, /* Sp */
108 { man_ign_pre
, NULL
}, /* Vb */
109 { NULL
, NULL
}, /* Ve */
110 { man_ign_pre
, NULL
}, /* AT */
115 html_man(void *arg
, const struct man
*m
)
120 h
= (struct html
*)arg
;
124 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
125 print_man(man_meta(m
), man_node(m
), h
);
138 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
140 print_man_head(m
, n
, h
);
142 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
144 tag
.key
= ATTR_CLASS
;
146 print_otag(h
, TAG_DIV
, 1, &tag
);
148 print_man_nodelist(m
, n
, h
);
156 print_man_head(MAN_ARGS
)
161 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
163 print_otag(h
, TAG_TITLE
, 0, NULL
);
164 print_text(h
, h
->buf
);
169 print_man_nodelist(MAN_ARGS
)
172 print_man_node(m
, n
, h
);
174 print_man_nodelist(m
, n
->next
, h
);
179 print_man_node(MAN_ARGS
)
190 * FIXME: embedded elements within next-line scopes (e.g., `br'
191 * within an empty `B') will cause formatting to be forgotten
192 * due to scope closing out.
197 child
= man_root_pre(m
, n
, h
);
200 print_text(h
, n
->string
);
204 * Close out scope of font prior to opening a macro
205 * scope. Assert that the metafont is on the top of the
206 * stack (it's never nested).
209 assert(h
->metaf
== t
);
210 print_tagq(h
, h
->metaf
);
211 assert(NULL
== h
->metaf
);
214 if (mans
[n
->tok
].pre
)
215 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
219 if (child
&& n
->child
)
220 print_man_nodelist(m
, n
->child
, h
);
222 /* This will automatically close out any font scope. */
229 man_root_post(m
, n
, h
);
234 if (mans
[n
->tok
].post
)
235 (*mans
[n
->tok
].post
)(m
, n
, h
);
242 a2width(const struct man_node
*n
, struct roffsu
*su
)
245 if (MAN_TEXT
!= n
->type
)
247 if (a2roffsu(n
->string
, su
, SCALE_BU
))
256 man_root_pre(MAN_ARGS
)
258 struct htmlpair tag
[3];
260 char b
[BUFSIZ
], title
[BUFSIZ
];
264 (void)strlcat(b
, m
->vol
, BUFSIZ
);
266 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
268 PAIR_CLASS_INIT(&tag
[0], "header");
269 bufcat_style(h
, "width", "100%");
270 PAIR_STYLE_INIT(&tag
[1], h
);
271 PAIR_SUMMARY_INIT(&tag
[2], "header");
273 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
274 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
277 bufcat_style(h
, "width", "10%");
278 PAIR_STYLE_INIT(&tag
[0], h
);
279 print_otag(h
, TAG_TD
, 1, tag
);
280 print_text(h
, title
);
284 bufcat_style(h
, "width", "80%");
285 bufcat_style(h
, "white-space", "nowrap");
286 bufcat_style(h
, "text-align", "center");
287 PAIR_STYLE_INIT(&tag
[0], h
);
288 print_otag(h
, TAG_TD
, 1, tag
);
293 bufcat_style(h
, "width", "10%");
294 bufcat_style(h
, "text-align", "right");
295 PAIR_STYLE_INIT(&tag
[0], h
);
296 print_otag(h
, TAG_TD
, 1, tag
);
297 print_text(h
, title
);
305 man_root_post(MAN_ARGS
)
307 struct htmlpair tag
[3];
311 time2a(m
->date
, b
, DATESIZ
);
313 PAIR_CLASS_INIT(&tag
[0], "footer");
314 bufcat_style(h
, "width", "100%");
315 PAIR_STYLE_INIT(&tag
[1], h
);
316 PAIR_SUMMARY_INIT(&tag
[2], "footer");
318 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
319 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
322 bufcat_style(h
, "width", "50%");
323 PAIR_STYLE_INIT(&tag
[0], h
);
324 print_otag(h
, TAG_TD
, 1, tag
);
329 bufcat_style(h
, "width", "50%");
330 bufcat_style(h
, "text-align", "right");
331 PAIR_STYLE_INIT(&tag
[0], h
);
332 print_otag(h
, TAG_TD
, 1, tag
);
334 print_text(h
, m
->source
);
347 SCALE_VS_INIT(&su
, 1);
351 SCALE_VS_INIT(&su
, 0.5);
355 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
362 bufcat_su(h
, "height", &su
);
363 PAIR_STYLE_INIT(&tag
, h
);
364 print_otag(h
, TAG_DIV
, 1, &tag
);
366 /* So the div isn't empty: */
367 print_text(h
, "\\~");
377 struct htmlpair tag
[2];
380 if (MAN_BODY
== n
->type
) {
381 SCALE_HS_INIT(&su
, INDENT
);
382 bufcat_su(h
, "margin-left", &su
);
383 PAIR_CLASS_INIT(&tag
[0], "sec-body");
384 PAIR_STYLE_INIT(&tag
[1], h
);
385 print_otag(h
, TAG_DIV
, 2, tag
);
387 } else if (MAN_BLOCK
== n
->type
) {
388 PAIR_CLASS_INIT(&tag
[0], "sec-block");
389 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
390 if (NULL
== n
->prev
->body
->child
) {
391 print_otag(h
, TAG_DIV
, 1, tag
);
395 SCALE_VS_INIT(&su
, 1);
396 bufcat_su(h
, "margin-top", &su
);
398 bufcat_su(h
, "margin-bottom", &su
);
399 PAIR_STYLE_INIT(&tag
[1], h
);
400 print_otag(h
, TAG_DIV
, 2, tag
);
404 PAIR_CLASS_INIT(&tag
[0], "sec-head");
405 print_otag(h
, TAG_DIV
, 1, tag
);
412 man_alt_pre(MAN_ARGS
)
414 const struct man_node
*nn
;
419 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
422 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
425 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
428 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
431 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
434 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
437 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
445 h
->flags
|= HTML_NOSPACE
;
448 * Open and close the scope with each argument, so that
449 * internal \f escapes, which are common, are also
450 * closed out with the scope.
452 t
= print_ofont(h
, fp
);
453 print_man_node(m
, nn
, h
);
467 /* FIXME: print_ofont(). */
468 PAIR_CLASS_INIT(&tag
, "small bold");
469 print_otag(h
, TAG_SPAN
, 1, &tag
);
480 PAIR_CLASS_INIT(&tag
, "small");
481 print_otag(h
, TAG_SPAN
, 1, &tag
);
490 struct htmlpair tag
[3];
493 SCALE_VS_INIT(&su
, 1);
495 if (MAN_BODY
== n
->type
) {
496 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
497 if (n
->parent
->next
&& n
->child
) {
498 bufcat_su(h
, "margin-bottom", &su
);
499 PAIR_STYLE_INIT(&tag
[1], h
);
500 print_otag(h
, TAG_DIV
, 2, tag
);
504 print_otag(h
, TAG_DIV
, 1, tag
);
506 } else if (MAN_BLOCK
== n
->type
) {
507 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
508 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
509 if (n
->prev
->body
->child
) {
510 bufcat_su(h
, "margin-top", &su
);
511 PAIR_STYLE_INIT(&tag
[1], h
);
512 print_otag(h
, TAG_DIV
, 2, tag
);
516 print_otag(h
, TAG_DIV
, 1, tag
);
520 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
521 bufcat_su(h
, "margin-left", &su
);
522 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
523 PAIR_STYLE_INIT(&tag
[1], h
);
524 print_otag(h
, TAG_DIV
, 2, tag
);
537 if (MAN_BLOCK
!= n
->type
)
542 if (MAN_ROOT
== n
->parent
->type
) {
543 SCALE_HS_INIT(&su
, INDENT
);
544 bufcat_su(h
, "margin-left", &su
);
548 SCALE_VS_INIT(&su
, 1);
549 bufcat_su(h
, "margin-top", &su
);
553 PAIR_STYLE_INIT(&tag
, h
);
554 print_otag(h
, TAG_DIV
, i
, &tag
);
565 const struct man_node
*nn
;
569 * This scattering of 1-BU margins and pads is to make sure that
570 * when text overruns its box, the subsequent text isn't flush
571 * up against it. However, the rest of the right-hand box must
572 * also be adjusted in consideration of this 1-BU space.
575 if (MAN_BODY
== n
->type
) {
576 SCALE_HS_INIT(&su
, INDENT
);
577 bufcat_su(h
, "margin-left", &su
);
578 PAIR_STYLE_INIT(&tag
, h
);
579 print_otag(h
, TAG_DIV
, 1, &tag
);
583 nn
= MAN_BLOCK
== n
->type
?
584 n
->head
->child
: n
->parent
->head
->child
;
586 SCALE_HS_INIT(&su
, INDENT
);
589 /* Width is the last token. */
591 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
592 if (NULL
!= (nn
= nn
->next
)) {
593 for ( ; nn
->next
; nn
= nn
->next
)
595 width
= a2width(nn
, &su
);
598 /* Width is the first token. */
600 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
601 /* Skip past non-text children. */
602 while (nn
&& MAN_TEXT
!= nn
->type
)
605 width
= a2width(nn
, &su
);
608 if (MAN_BLOCK
== n
->type
) {
609 bufcat_su(h
, "margin-left", &su
);
610 SCALE_VS_INIT(&su
, 1);
611 bufcat_su(h
, "margin-top", &su
);
612 bufcat_style(h
, "clear", "both");
613 PAIR_STYLE_INIT(&tag
, h
);
614 print_otag(h
, TAG_DIV
, 1, &tag
);
618 bufcat_su(h
, "min-width", &su
);
620 bufcat_su(h
, "margin-left", &su
);
621 SCALE_HS_INIT(&su
, 1);
622 bufcat_su(h
, "margin-right", &su
);
623 bufcat_style(h
, "clear", "left");
625 if (n
->next
&& n
->next
->child
)
626 bufcat_style(h
, "float", "left");
628 PAIR_STYLE_INIT(&tag
, h
);
629 print_otag(h
, TAG_DIV
, 1, &tag
);
632 * Without a length string, we can print all of our children.
639 * When a length has been specified, we need to carefully print
640 * our child context: IP gets all children printed but the last
641 * (the width), while TP gets all children printed but the first
645 if (MAN_IP
== n
->tok
)
646 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
647 print_man_node(m
, nn
, h
);
648 if (MAN_TP
== n
->tok
)
649 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
650 print_man_node(m
, nn
, h
);
660 const struct man_node
*nn
;
664 if (MAN_HEAD
== n
->type
)
667 nn
= MAN_BLOCK
== n
->type
?
668 n
->head
->child
: n
->parent
->head
->child
;
670 SCALE_HS_INIT(&su
, INDENT
);
673 (void)a2width(nn
, &su
);
675 if (MAN_BLOCK
== n
->type
) {
676 bufcat_su(h
, "margin-left", &su
);
677 SCALE_VS_INIT(&su
, 1);
678 bufcat_su(h
, "margin-top", &su
);
679 bufcat_style(h
, "clear", "both");
680 PAIR_STYLE_INIT(&tag
, h
);
681 print_otag(h
, TAG_DIV
, 1, &tag
);
685 bufcat_su(h
, "margin-left", &su
);
687 bufcat_su(h
, "text-indent", &su
);
689 PAIR_STYLE_INIT(&tag
, h
);
690 print_otag(h
, TAG_DIV
, 1, &tag
);
700 print_ofont(h
, HTMLFONT_BOLD
);
710 print_ofont(h
, HTMLFONT_ITALIC
);
717 man_ign_pre(MAN_ARGS
)
731 if (MAN_HEAD
== n
->type
)
733 else if (MAN_BODY
== n
->type
)
736 SCALE_HS_INIT(&su
, INDENT
);
737 bufcat_su(h
, "margin-left", &su
);
739 if (n
->head
->child
) {
740 SCALE_VS_INIT(&su
, 1);
741 a2width(n
->head
->child
, &su
);
742 bufcat_su(h
, "margin-top", &su
);
745 PAIR_STYLE_INIT(&tag
, h
);
746 print_otag(h
, TAG_DIV
, 1, &tag
);