]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.25 2010/01/01 17:14:28 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 */
110 html_man(void *arg
, const struct man
*m
)
115 h
= (struct html
*)arg
;
117 print_gen_doctype(h
);
119 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
120 print_man(man_meta(m
), man_node(m
), h
);
133 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
135 print_man_head(m
, n
, h
);
137 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
139 tag
.key
= ATTR_CLASS
;
141 print_otag(h
, TAG_DIV
, 1, &tag
);
143 print_man_nodelist(m
, n
, h
);
151 print_man_head(MAN_ARGS
)
156 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
158 print_otag(h
, TAG_TITLE
, 0, NULL
);
159 print_text(h
, h
->buf
);
164 print_man_nodelist(MAN_ARGS
)
167 print_man_node(m
, n
, h
);
169 print_man_nodelist(m
, n
->next
, h
);
174 print_man_node(MAN_ARGS
)
186 child
= man_root_pre(m
, n
, h
);
189 print_text(h
, n
->string
);
193 * Close out scope of font prior to opening a macro
194 * scope. Assert that the metafont is on the top of the
195 * stack (it's never nested).
198 assert(h
->metaf
== t
);
199 print_tagq(h
, h
->metaf
);
200 assert(NULL
== h
->metaf
);
203 if (mans
[n
->tok
].pre
)
204 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
208 if (child
&& n
->child
)
209 print_man_nodelist(m
, n
->child
, h
);
211 /* This will automatically close out any font scope. */
218 man_root_post(m
, n
, h
);
223 if (mans
[n
->tok
].post
)
224 (*mans
[n
->tok
].post
)(m
, n
, h
);
231 a2width(const struct man_node
*n
, struct roffsu
*su
)
234 if (MAN_TEXT
!= n
->type
)
236 if (a2roffsu(n
->string
, su
, SCALE_BU
))
245 man_root_pre(MAN_ARGS
)
247 struct htmlpair tag
[3];
249 char b
[BUFSIZ
], title
[BUFSIZ
];
253 (void)strlcat(b
, m
->vol
, BUFSIZ
);
255 snprintf(title
, BUFSIZ
- 1, "%s(%d)", m
->title
, m
->msec
);
257 PAIR_CLASS_INIT(&tag
[0], "header");
258 bufcat_style(h
, "width", "100%");
259 PAIR_STYLE_INIT(&tag
[1], h
);
260 PAIR_SUMMARY_INIT(&tag
[2], "header");
262 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
263 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
266 bufcat_style(h
, "width", "10%");
267 PAIR_STYLE_INIT(&tag
[0], h
);
268 print_otag(h
, TAG_TD
, 1, tag
);
269 print_text(h
, title
);
273 bufcat_style(h
, "width", "80%");
274 bufcat_style(h
, "white-space", "nowrap");
275 bufcat_style(h
, "text-align", "center");
276 PAIR_STYLE_INIT(&tag
[0], h
);
277 print_otag(h
, TAG_TD
, 1, tag
);
282 bufcat_style(h
, "width", "10%");
283 bufcat_style(h
, "text-align", "right");
284 PAIR_STYLE_INIT(&tag
[0], h
);
285 print_otag(h
, TAG_TD
, 1, tag
);
286 print_text(h
, title
);
294 man_root_post(MAN_ARGS
)
296 struct htmlpair tag
[3];
300 time2a(m
->date
, b
, DATESIZ
);
302 PAIR_CLASS_INIT(&tag
[0], "footer");
303 bufcat_style(h
, "width", "100%");
304 PAIR_STYLE_INIT(&tag
[1], h
);
305 PAIR_SUMMARY_INIT(&tag
[2], "footer");
307 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
308 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
311 bufcat_style(h
, "width", "50%");
312 PAIR_STYLE_INIT(&tag
[0], h
);
313 print_otag(h
, TAG_TD
, 1, tag
);
318 bufcat_style(h
, "width", "50%");
319 bufcat_style(h
, "text-align", "right");
320 PAIR_STYLE_INIT(&tag
[0], h
);
321 print_otag(h
, TAG_TD
, 1, tag
);
323 print_text(h
, m
->source
);
336 SCALE_VS_INIT(&su
, 1);
338 if (MAN_sp
== n
->tok
&& n
->child
)
339 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
340 else if (MAN_br
== n
->tok
)
343 bufcat_su(h
, "height", &su
);
344 PAIR_STYLE_INIT(&tag
, h
);
345 print_otag(h
, TAG_DIV
, 1, &tag
);
347 /* So the div isn't empty: */
348 print_text(h
, "\\~");
358 struct htmlpair tag
[2];
361 if (MAN_BODY
== n
->type
) {
362 SCALE_HS_INIT(&su
, INDENT
);
363 bufcat_su(h
, "margin-left", &su
);
364 PAIR_CLASS_INIT(&tag
[0], "sec-body");
365 PAIR_STYLE_INIT(&tag
[1], h
);
366 print_otag(h
, TAG_DIV
, 2, tag
);
368 } else if (MAN_BLOCK
== n
->type
) {
369 PAIR_CLASS_INIT(&tag
[0], "sec-block");
370 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
371 if (NULL
== n
->prev
->body
->child
) {
372 print_otag(h
, TAG_DIV
, 1, tag
);
376 SCALE_VS_INIT(&su
, 1);
377 bufcat_su(h
, "margin-top", &su
);
379 bufcat_su(h
, "margin-bottom", &su
);
380 PAIR_STYLE_INIT(&tag
[1], h
);
381 print_otag(h
, TAG_DIV
, 2, tag
);
385 PAIR_CLASS_INIT(&tag
[0], "sec-head");
386 print_otag(h
, TAG_DIV
, 1, tag
);
393 man_alt_pre(MAN_ARGS
)
395 const struct man_node
*nn
;
400 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
403 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
406 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
409 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
412 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
415 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
418 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
426 h
->flags
|= HTML_NOSPACE
;
429 * Open and close the scope with each argument, so that
430 * internal \f escapes, which are common, are also
431 * closed out with the scope.
433 t
= print_ofont(h
, fp
);
434 print_man_node(m
, nn
, h
);
448 /* FIXME: print_ofont(). */
449 PAIR_CLASS_INIT(&tag
, "small bold");
450 print_otag(h
, TAG_SPAN
, 1, &tag
);
461 PAIR_CLASS_INIT(&tag
, "small");
462 print_otag(h
, TAG_SPAN
, 1, &tag
);
471 struct htmlpair tag
[3];
474 SCALE_VS_INIT(&su
, 1);
476 if (MAN_BODY
== n
->type
) {
477 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
478 if (n
->parent
->next
&& n
->child
) {
479 bufcat_su(h
, "margin-bottom", &su
);
480 PAIR_STYLE_INIT(&tag
[1], h
);
481 print_otag(h
, TAG_DIV
, 2, tag
);
485 print_otag(h
, TAG_DIV
, 1, tag
);
487 } else if (MAN_BLOCK
== n
->type
) {
488 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
489 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
490 if (n
->prev
->body
->child
) {
491 bufcat_su(h
, "margin-top", &su
);
492 PAIR_STYLE_INIT(&tag
[1], h
);
493 print_otag(h
, TAG_DIV
, 2, tag
);
497 print_otag(h
, TAG_DIV
, 1, tag
);
501 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
502 bufcat_su(h
, "margin-left", &su
);
503 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
504 PAIR_STYLE_INIT(&tag
[1], h
);
505 print_otag(h
, TAG_DIV
, 2, tag
);
518 if (MAN_BLOCK
!= n
->type
)
523 if (MAN_ROOT
== n
->parent
->type
) {
524 SCALE_HS_INIT(&su
, INDENT
);
525 bufcat_su(h
, "margin-left", &su
);
529 SCALE_VS_INIT(&su
, 1);
530 bufcat_su(h
, "margin-top", &su
);
534 PAIR_STYLE_INIT(&tag
, h
);
535 print_otag(h
, TAG_DIV
, i
, &tag
);
546 const struct man_node
*nn
;
550 * This scattering of 1-BU margins and pads is to make sure that
551 * when text overruns its box, the subsequent text isn't flush
552 * up against it. However, the rest of the right-hand box must
553 * also be adjusted in consideration of this 1-BU space.
556 if (MAN_BODY
== n
->type
) {
557 SCALE_HS_INIT(&su
, INDENT
);
558 bufcat_su(h
, "margin-left", &su
);
559 PAIR_STYLE_INIT(&tag
, h
);
560 print_otag(h
, TAG_DIV
, 1, &tag
);
564 nn
= MAN_BLOCK
== n
->type
?
565 n
->head
->child
: n
->parent
->head
->child
;
567 SCALE_HS_INIT(&su
, INDENT
);
570 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
571 if (NULL
!= (nn
= nn
->next
)) {
572 for ( ; nn
->next
; nn
= nn
->next
)
574 width
= a2width(nn
, &su
);
577 if (MAN_TP
== n
->tok
&& NULL
!= nn
)
578 width
= a2width(nn
, &su
);
580 if (MAN_BLOCK
== n
->type
) {
581 bufcat_su(h
, "margin-left", &su
);
582 SCALE_VS_INIT(&su
, 1);
583 bufcat_su(h
, "margin-top", &su
);
584 bufcat_style(h
, "clear", "both");
585 PAIR_STYLE_INIT(&tag
, h
);
586 print_otag(h
, TAG_DIV
, 1, &tag
);
590 bufcat_su(h
, "min-width", &su
);
592 bufcat_su(h
, "margin-left", &su
);
593 SCALE_HS_INIT(&su
, 1);
594 bufcat_su(h
, "margin-right", &su
);
595 bufcat_style(h
, "clear", "left");
597 if (n
->next
&& n
->next
->child
)
598 bufcat_style(h
, "float", "left");
600 PAIR_STYLE_INIT(&tag
, h
);
601 print_otag(h
, TAG_DIV
, 1, &tag
);
603 /* With a length string, manually omit the last child. */
608 if (MAN_IP
== n
->tok
)
609 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
610 print_man_node(m
, nn
, h
);
611 if (MAN_TP
== n
->tok
)
612 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
613 print_man_node(m
, nn
, h
);
623 const struct man_node
*nn
;
627 if (MAN_HEAD
== n
->type
)
630 nn
= MAN_BLOCK
== n
->type
?
631 n
->head
->child
: n
->parent
->head
->child
;
633 SCALE_HS_INIT(&su
, INDENT
);
636 (void)a2width(nn
, &su
);
638 if (MAN_BLOCK
== n
->type
) {
639 bufcat_su(h
, "margin-left", &su
);
640 SCALE_VS_INIT(&su
, 1);
641 bufcat_su(h
, "margin-top", &su
);
642 bufcat_style(h
, "clear", "both");
643 PAIR_STYLE_INIT(&tag
, h
);
644 print_otag(h
, TAG_DIV
, 1, &tag
);
648 bufcat_su(h
, "margin-left", &su
);
650 bufcat_su(h
, "text-indent", &su
);
652 PAIR_STYLE_INIT(&tag
, h
);
653 print_otag(h
, TAG_DIV
, 1, &tag
);
663 print_ofont(h
, HTMLFONT_BOLD
);
673 print_ofont(h
, HTMLFONT_ITALIC
);
680 man_ign_pre(MAN_ARGS
)
694 if (MAN_HEAD
== n
->type
)
696 else if (MAN_BODY
== n
->type
)
699 SCALE_HS_INIT(&su
, INDENT
);
700 bufcat_su(h
, "margin-left", &su
);
702 if (n
->head
->child
) {
703 SCALE_VS_INIT(&su
, 1);
704 a2width(n
->head
->child
, &su
);
705 bufcat_su(h
, "margin-top", &su
);
708 PAIR_STYLE_INIT(&tag
, h
);
709 print_otag(h
, TAG_DIV
, 1, &tag
);