]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.30 2010/03/24 20:10:53 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 { man_ign_pre
, NULL
}, /* ig */
114 { NULL
, NULL
}, /* . */
119 html_man(void *arg
, const struct man
*m
)
124 h
= (struct html
*)arg
;
128 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
129 print_man(man_meta(m
), man_node(m
), h
);
142 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
144 print_man_head(m
, n
, h
);
146 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
148 tag
.key
= ATTR_CLASS
;
150 print_otag(h
, TAG_DIV
, 1, &tag
);
152 print_man_nodelist(m
, n
, h
);
160 print_man_head(MAN_ARGS
)
165 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
167 print_otag(h
, TAG_TITLE
, 0, NULL
);
168 print_text(h
, h
->buf
);
173 print_man_nodelist(MAN_ARGS
)
176 print_man_node(m
, n
, h
);
178 print_man_nodelist(m
, n
->next
, h
);
183 print_man_node(MAN_ARGS
)
194 * FIXME: embedded elements within next-line scopes (e.g., `br'
195 * within an empty `B') will cause formatting to be forgotten
196 * due to scope closing out.
201 child
= man_root_pre(m
, n
, h
);
204 print_text(h
, n
->string
);
208 * Close out scope of font prior to opening a macro
209 * scope. Assert that the metafont is on the top of the
210 * stack (it's never nested).
213 assert(h
->metaf
== t
);
214 print_tagq(h
, h
->metaf
);
215 assert(NULL
== h
->metaf
);
218 if (mans
[n
->tok
].pre
)
219 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
223 if (child
&& n
->child
)
224 print_man_nodelist(m
, n
->child
, h
);
226 /* This will automatically close out any font scope. */
233 man_root_post(m
, n
, h
);
238 if (mans
[n
->tok
].post
)
239 (*mans
[n
->tok
].post
)(m
, n
, h
);
246 a2width(const struct man_node
*n
, struct roffsu
*su
)
249 if (MAN_TEXT
!= n
->type
)
251 if (a2roffsu(n
->string
, su
, SCALE_BU
))
260 man_root_pre(MAN_ARGS
)
262 struct htmlpair tag
[3];
264 char b
[BUFSIZ
], title
[BUFSIZ
];
268 (void)strlcat(b
, m
->vol
, BUFSIZ
);
270 snprintf(title
, BUFSIZ
- 1, "%s(%d)", m
->title
, m
->msec
);
272 PAIR_CLASS_INIT(&tag
[0], "header");
273 bufcat_style(h
, "width", "100%");
274 PAIR_STYLE_INIT(&tag
[1], h
);
275 PAIR_SUMMARY_INIT(&tag
[2], "header");
277 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
278 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
281 bufcat_style(h
, "width", "10%");
282 PAIR_STYLE_INIT(&tag
[0], h
);
283 print_otag(h
, TAG_TD
, 1, tag
);
284 print_text(h
, title
);
288 bufcat_style(h
, "width", "80%");
289 bufcat_style(h
, "white-space", "nowrap");
290 bufcat_style(h
, "text-align", "center");
291 PAIR_STYLE_INIT(&tag
[0], h
);
292 print_otag(h
, TAG_TD
, 1, tag
);
297 bufcat_style(h
, "width", "10%");
298 bufcat_style(h
, "text-align", "right");
299 PAIR_STYLE_INIT(&tag
[0], h
);
300 print_otag(h
, TAG_TD
, 1, tag
);
301 print_text(h
, title
);
309 man_root_post(MAN_ARGS
)
311 struct htmlpair tag
[3];
315 time2a(m
->date
, b
, DATESIZ
);
317 PAIR_CLASS_INIT(&tag
[0], "footer");
318 bufcat_style(h
, "width", "100%");
319 PAIR_STYLE_INIT(&tag
[1], h
);
320 PAIR_SUMMARY_INIT(&tag
[2], "footer");
322 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
323 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
326 bufcat_style(h
, "width", "50%");
327 PAIR_STYLE_INIT(&tag
[0], h
);
328 print_otag(h
, TAG_TD
, 1, tag
);
333 bufcat_style(h
, "width", "50%");
334 bufcat_style(h
, "text-align", "right");
335 PAIR_STYLE_INIT(&tag
[0], h
);
336 print_otag(h
, TAG_TD
, 1, tag
);
338 print_text(h
, m
->source
);
351 SCALE_VS_INIT(&su
, 1);
355 SCALE_VS_INIT(&su
, 0.5);
359 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
366 bufcat_su(h
, "height", &su
);
367 PAIR_STYLE_INIT(&tag
, h
);
368 print_otag(h
, TAG_DIV
, 1, &tag
);
370 /* So the div isn't empty: */
371 print_text(h
, "\\~");
381 struct htmlpair tag
[2];
384 if (MAN_BODY
== n
->type
) {
385 SCALE_HS_INIT(&su
, INDENT
);
386 bufcat_su(h
, "margin-left", &su
);
387 PAIR_CLASS_INIT(&tag
[0], "sec-body");
388 PAIR_STYLE_INIT(&tag
[1], h
);
389 print_otag(h
, TAG_DIV
, 2, tag
);
391 } else if (MAN_BLOCK
== n
->type
) {
392 PAIR_CLASS_INIT(&tag
[0], "sec-block");
393 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
394 if (NULL
== n
->prev
->body
->child
) {
395 print_otag(h
, TAG_DIV
, 1, tag
);
399 SCALE_VS_INIT(&su
, 1);
400 bufcat_su(h
, "margin-top", &su
);
402 bufcat_su(h
, "margin-bottom", &su
);
403 PAIR_STYLE_INIT(&tag
[1], h
);
404 print_otag(h
, TAG_DIV
, 2, tag
);
408 PAIR_CLASS_INIT(&tag
[0], "sec-head");
409 print_otag(h
, TAG_DIV
, 1, tag
);
416 man_alt_pre(MAN_ARGS
)
418 const struct man_node
*nn
;
423 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
426 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
429 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
432 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
435 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
438 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
441 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
449 h
->flags
|= HTML_NOSPACE
;
452 * Open and close the scope with each argument, so that
453 * internal \f escapes, which are common, are also
454 * closed out with the scope.
456 t
= print_ofont(h
, fp
);
457 print_man_node(m
, nn
, h
);
471 /* FIXME: print_ofont(). */
472 PAIR_CLASS_INIT(&tag
, "small bold");
473 print_otag(h
, TAG_SPAN
, 1, &tag
);
484 PAIR_CLASS_INIT(&tag
, "small");
485 print_otag(h
, TAG_SPAN
, 1, &tag
);
494 struct htmlpair tag
[3];
497 SCALE_VS_INIT(&su
, 1);
499 if (MAN_BODY
== n
->type
) {
500 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
501 if (n
->parent
->next
&& n
->child
) {
502 bufcat_su(h
, "margin-bottom", &su
);
503 PAIR_STYLE_INIT(&tag
[1], h
);
504 print_otag(h
, TAG_DIV
, 2, tag
);
508 print_otag(h
, TAG_DIV
, 1, tag
);
510 } else if (MAN_BLOCK
== n
->type
) {
511 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
512 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
513 if (n
->prev
->body
->child
) {
514 bufcat_su(h
, "margin-top", &su
);
515 PAIR_STYLE_INIT(&tag
[1], h
);
516 print_otag(h
, TAG_DIV
, 2, tag
);
520 print_otag(h
, TAG_DIV
, 1, tag
);
524 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
525 bufcat_su(h
, "margin-left", &su
);
526 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
527 PAIR_STYLE_INIT(&tag
[1], h
);
528 print_otag(h
, TAG_DIV
, 2, tag
);
541 if (MAN_BLOCK
!= n
->type
)
546 if (MAN_ROOT
== n
->parent
->type
) {
547 SCALE_HS_INIT(&su
, INDENT
);
548 bufcat_su(h
, "margin-left", &su
);
552 SCALE_VS_INIT(&su
, 1);
553 bufcat_su(h
, "margin-top", &su
);
557 PAIR_STYLE_INIT(&tag
, h
);
558 print_otag(h
, TAG_DIV
, i
, &tag
);
569 const struct man_node
*nn
;
573 * This scattering of 1-BU margins and pads is to make sure that
574 * when text overruns its box, the subsequent text isn't flush
575 * up against it. However, the rest of the right-hand box must
576 * also be adjusted in consideration of this 1-BU space.
579 if (MAN_BODY
== n
->type
) {
580 SCALE_HS_INIT(&su
, INDENT
);
581 bufcat_su(h
, "margin-left", &su
);
582 PAIR_STYLE_INIT(&tag
, h
);
583 print_otag(h
, TAG_DIV
, 1, &tag
);
587 nn
= MAN_BLOCK
== n
->type
?
588 n
->head
->child
: n
->parent
->head
->child
;
590 SCALE_HS_INIT(&su
, INDENT
);
593 /* Width is the last token. */
595 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
596 if (NULL
!= (nn
= nn
->next
)) {
597 for ( ; nn
->next
; nn
= nn
->next
)
599 width
= a2width(nn
, &su
);
602 /* Width is the first token. */
604 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
605 /* Skip past non-text children. */
606 while (nn
&& MAN_TEXT
!= nn
->type
)
609 width
= a2width(nn
, &su
);
612 if (MAN_BLOCK
== n
->type
) {
613 bufcat_su(h
, "margin-left", &su
);
614 SCALE_VS_INIT(&su
, 1);
615 bufcat_su(h
, "margin-top", &su
);
616 bufcat_style(h
, "clear", "both");
617 PAIR_STYLE_INIT(&tag
, h
);
618 print_otag(h
, TAG_DIV
, 1, &tag
);
622 bufcat_su(h
, "min-width", &su
);
624 bufcat_su(h
, "margin-left", &su
);
625 SCALE_HS_INIT(&su
, 1);
626 bufcat_su(h
, "margin-right", &su
);
627 bufcat_style(h
, "clear", "left");
629 if (n
->next
&& n
->next
->child
)
630 bufcat_style(h
, "float", "left");
632 PAIR_STYLE_INIT(&tag
, h
);
633 print_otag(h
, TAG_DIV
, 1, &tag
);
636 * Without a length string, we can print all of our children.
643 * When a length has been specified, we need to carefully print
644 * our child context: IP gets all children printed but the last
645 * (the width), while TP gets all children printed but the first
649 if (MAN_IP
== n
->tok
)
650 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
651 print_man_node(m
, nn
, h
);
652 if (MAN_TP
== n
->tok
)
653 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
654 print_man_node(m
, nn
, h
);
664 const struct man_node
*nn
;
668 if (MAN_HEAD
== n
->type
)
671 nn
= MAN_BLOCK
== n
->type
?
672 n
->head
->child
: n
->parent
->head
->child
;
674 SCALE_HS_INIT(&su
, INDENT
);
677 (void)a2width(nn
, &su
);
679 if (MAN_BLOCK
== n
->type
) {
680 bufcat_su(h
, "margin-left", &su
);
681 SCALE_VS_INIT(&su
, 1);
682 bufcat_su(h
, "margin-top", &su
);
683 bufcat_style(h
, "clear", "both");
684 PAIR_STYLE_INIT(&tag
, h
);
685 print_otag(h
, TAG_DIV
, 1, &tag
);
689 bufcat_su(h
, "margin-left", &su
);
691 bufcat_su(h
, "text-indent", &su
);
693 PAIR_STYLE_INIT(&tag
, h
);
694 print_otag(h
, TAG_DIV
, 1, &tag
);
704 print_ofont(h
, HTMLFONT_BOLD
);
714 print_ofont(h
, HTMLFONT_ITALIC
);
721 man_ign_pre(MAN_ARGS
)
735 if (MAN_HEAD
== n
->type
)
737 else if (MAN_BODY
== n
->type
)
740 SCALE_HS_INIT(&su
, INDENT
);
741 bufcat_su(h
, "margin-left", &su
);
743 if (n
->head
->child
) {
744 SCALE_VS_INIT(&su
, 1);
745 a2width(n
->head
->child
, &su
);
746 bufcat_su(h
, "margin-top", &su
);
749 PAIR_STYLE_INIT(&tag
, h
);
750 print_otag(h
, TAG_DIV
, 1, &tag
);