]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.54 2010/12/17 00:18:29 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 { man_br_pre
, NULL
}, /* sp */
106 { man_literal_pre
, NULL
}, /* nf */
107 { man_literal_pre
, NULL
}, /* fi */
108 { NULL
, NULL
}, /* RE */
109 { man_RS_pre
, NULL
}, /* RS */
110 { man_ign_pre
, NULL
}, /* DT */
111 { man_ign_pre
, NULL
}, /* UC */
112 { man_ign_pre
, NULL
}, /* PD */
113 { man_ign_pre
, NULL
}, /* AT */
114 { man_in_pre
, NULL
}, /* in */
115 { man_ign_pre
, NULL
}, /* ft */
120 html_man(void *arg
, const struct man
*m
)
126 h
= (struct html
*)arg
;
130 memset(&mh
, 0, sizeof(struct mhtml
));
132 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
133 print_man(man_meta(m
), man_node(m
), &mh
, h
);
145 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
146 print_man_head(m
, n
, mh
, h
);
149 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
150 print_man_nodelist(m
, n
, mh
, h
);
157 print_man_head(MAN_ARGS
)
162 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
164 print_otag(h
, TAG_TITLE
, 0, NULL
);
165 print_text(h
, h
->buf
);
170 print_man_nodelist(MAN_ARGS
)
173 print_man_node(m
, n
, mh
, h
);
175 print_man_nodelist(m
, n
->next
, mh
, h
);
180 print_man_node(MAN_ARGS
)
191 * FIXME: embedded elements within next-line scopes (e.g., `br'
192 * within an empty `B') will cause formatting to be forgotten
193 * due to scope closing out.
198 child
= man_root_pre(m
, n
, mh
, h
);
201 print_text(h
, n
->string
);
203 if (MANH_LITERAL
& mh
->fl
)
204 print_otag(h
, TAG_BR
, 0, NULL
);
209 * Close out scope of font prior to opening a macro
210 * scope. Assert that the metafont is on the top of the
211 * stack (it's never nested).
214 assert(h
->metaf
== t
);
215 print_tagq(h
, h
->metaf
);
216 assert(NULL
== h
->metaf
);
219 if (mans
[n
->tok
].pre
)
220 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
224 if (child
&& n
->child
)
225 print_man_nodelist(m
, n
->child
, mh
, h
);
227 /* This will automatically close out any font scope. */
234 man_root_post(m
, n
, mh
, h
);
239 if (mans
[n
->tok
].post
)
240 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
247 a2width(const struct man_node
*n
, struct roffsu
*su
)
250 if (MAN_TEXT
!= n
->type
)
252 if (a2roffsu(n
->string
, su
, SCALE_BU
))
261 man_root_pre(MAN_ARGS
)
263 struct htmlpair tag
[3];
265 char b
[BUFSIZ
], title
[BUFSIZ
];
269 (void)strlcat(b
, m
->vol
, BUFSIZ
);
271 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
273 PAIR_CLASS_INIT(&tag
[0], "header");
274 bufcat_style(h
, "width", "100%");
275 PAIR_STYLE_INIT(&tag
[1], h
);
276 PAIR_SUMMARY_INIT(&tag
[2], "header");
278 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
279 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
282 bufcat_style(h
, "width", "10%");
283 PAIR_STYLE_INIT(&tag
[0], h
);
284 print_otag(h
, TAG_TD
, 1, tag
);
285 print_text(h
, title
);
289 bufcat_style(h
, "width", "80%");
290 bufcat_style(h
, "white-space", "nowrap");
291 bufcat_style(h
, "text-align", "center");
292 PAIR_STYLE_INIT(&tag
[0], h
);
293 print_otag(h
, TAG_TD
, 1, tag
);
298 bufcat_style(h
, "width", "10%");
299 bufcat_style(h
, "text-align", "right");
300 PAIR_STYLE_INIT(&tag
[0], h
);
301 print_otag(h
, TAG_TD
, 1, tag
);
302 print_text(h
, title
);
310 man_root_post(MAN_ARGS
)
312 struct htmlpair tag
[3];
317 strlcpy(b
, m
->rawdate
, DATESIZ
);
319 time2a(m
->date
, b
, DATESIZ
);
321 PAIR_CLASS_INIT(&tag
[0], "footer");
322 bufcat_style(h
, "width", "100%");
323 PAIR_STYLE_INIT(&tag
[1], h
);
324 PAIR_SUMMARY_INIT(&tag
[2], "footer");
326 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
327 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
330 bufcat_style(h
, "width", "50%");
331 PAIR_STYLE_INIT(&tag
[0], h
);
332 print_otag(h
, TAG_TD
, 1, tag
);
337 bufcat_style(h
, "width", "50%");
338 bufcat_style(h
, "text-align", "right");
339 PAIR_STYLE_INIT(&tag
[0], h
);
340 print_otag(h
, TAG_TD
, 1, tag
);
342 print_text(h
, m
->source
);
355 SCALE_VS_INIT(&su
, 1);
357 if (MAN_sp
== n
->tok
) {
359 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
363 bufcat_su(h
, "height", &su
);
364 PAIR_STYLE_INIT(&tag
, h
);
365 print_otag(h
, TAG_DIV
, 1, &tag
);
367 /* So the div isn't empty: */
368 print_text(h
, "\\~");
380 if (MAN_BLOCK
== n
->type
) {
381 PAIR_CLASS_INIT(&tag
, "section");
382 print_otag(h
, TAG_DIV
, 1, &tag
);
384 } else if (MAN_BODY
== n
->type
)
387 print_otag(h
, TAG_H1
, 0, NULL
);
394 man_alt_pre(MAN_ARGS
)
396 const struct man_node
*nn
;
401 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
404 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
407 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
410 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
413 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
416 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
419 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
427 h
->flags
|= HTML_NOSPACE
;
430 * Open and close the scope with each argument, so that
431 * internal \f escapes, which are common, are also
432 * closed out with the scope.
434 t
= print_ofont(h
, fp
);
435 print_man_node(m
, nn
, mh
, h
);
449 /* FIXME: print_ofont(). */
450 PAIR_CLASS_INIT(&tag
, "small bold");
451 print_otag(h
, TAG_SPAN
, 1, &tag
);
462 PAIR_CLASS_INIT(&tag
, "small");
463 print_otag(h
, TAG_SPAN
, 1, &tag
);
474 if (MAN_BLOCK
== n
->type
) {
475 PAIR_CLASS_INIT(&tag
, "subsection");
476 print_otag(h
, TAG_DIV
, 1, &tag
);
478 } else if (MAN_BODY
== n
->type
)
481 print_otag(h
, TAG_H2
, 0, NULL
);
494 if (MAN_BODY
== n
->type
)
496 if (MAN_HEAD
== n
->type
)
501 if (MAN_ROOT
== n
->parent
->type
) {
502 SCALE_HS_INIT(&su
, INDENT
);
503 bufcat_su(h
, "margin-left", &su
);
507 SCALE_VS_INIT(&su
, 1);
508 bufcat_su(h
, "margin-top", &su
);
512 PAIR_STYLE_INIT(&tag
, h
);
513 print_otag(h
, TAG_DIV
, i
, &tag
);
525 const struct man_node
*nn
;
529 * This scattering of 1-BU margins and pads is to make sure that
530 * when text overruns its box, the subsequent text isn't flush
531 * up against it. However, the rest of the right-hand box must
532 * also be adjusted in consideration of this 1-BU space.
535 if (MAN_BODY
== n
->type
) {
536 SCALE_HS_INIT(&su
, INDENT
);
537 bufcat_su(h
, "margin-left", &su
);
538 PAIR_STYLE_INIT(&tag
, h
);
539 print_otag(h
, TAG_DIV
, 1, &tag
);
543 nn
= MAN_BLOCK
== n
->type
?
544 n
->head
->child
: n
->parent
->head
->child
;
546 SCALE_HS_INIT(&su
, INDENT
);
549 /* Width is the last token. */
551 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
552 if (NULL
!= (nn
= nn
->next
)) {
553 for ( ; nn
->next
; nn
= nn
->next
)
555 width
= a2width(nn
, &su
);
558 /* Width is the first token. */
560 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
561 /* Skip past non-text children. */
562 while (nn
&& MAN_TEXT
!= nn
->type
)
565 width
= a2width(nn
, &su
);
568 if (MAN_BLOCK
== n
->type
) {
569 bufcat_su(h
, "margin-left", &su
);
570 SCALE_VS_INIT(&su
, 1);
571 bufcat_su(h
, "margin-top", &su
);
572 bufcat_style(h
, "clear", "both");
573 PAIR_STYLE_INIT(&tag
, h
);
574 print_otag(h
, TAG_DIV
, 1, &tag
);
578 bufcat_su(h
, "min-width", &su
);
580 bufcat_su(h
, "margin-left", &su
);
581 SCALE_HS_INIT(&su
, 1);
582 bufcat_su(h
, "margin-right", &su
);
583 bufcat_style(h
, "clear", "left");
585 if (n
->next
&& n
->next
->child
)
586 bufcat_style(h
, "float", "left");
588 PAIR_STYLE_INIT(&tag
, h
);
589 print_otag(h
, TAG_DIV
, 1, &tag
);
592 * Without a length string, we can print all of our children.
599 * When a length has been specified, we need to carefully print
600 * our child context: IP gets all children printed but the last
601 * (the width), while TP gets all children printed but the first
605 if (MAN_IP
== n
->tok
)
606 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
607 print_man_node(m
, nn
, mh
, h
);
608 if (MAN_TP
== n
->tok
)
609 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
610 print_man_node(m
, nn
, mh
, h
);
620 const struct man_node
*nn
;
624 if (MAN_HEAD
== n
->type
)
627 nn
= MAN_BLOCK
== n
->type
?
628 n
->head
->child
: n
->parent
->head
->child
;
630 SCALE_HS_INIT(&su
, INDENT
);
633 (void)a2width(nn
, &su
);
635 if (MAN_BLOCK
== n
->type
) {
636 bufcat_su(h
, "margin-left", &su
);
637 SCALE_VS_INIT(&su
, 1);
638 bufcat_su(h
, "margin-top", &su
);
639 bufcat_style(h
, "clear", "both");
640 PAIR_STYLE_INIT(&tag
, h
);
641 print_otag(h
, TAG_DIV
, 1, &tag
);
645 bufcat_su(h
, "margin-left", &su
);
647 bufcat_su(h
, "text-indent", &su
);
649 PAIR_STYLE_INIT(&tag
, h
);
650 print_otag(h
, TAG_DIV
, 1, &tag
);
660 print_ofont(h
, HTMLFONT_BOLD
);
670 print_ofont(h
, HTMLFONT_ITALIC
);
677 man_literal_pre(MAN_ARGS
)
680 if (MAN_nf
== n
->tok
) {
681 print_otag(h
, TAG_BR
, 0, NULL
);
682 mh
->fl
|= MANH_LITERAL
;
684 mh
->fl
&= ~MANH_LITERAL
;
695 print_otag(h
, TAG_BR
, 0, NULL
);
702 man_ign_pre(MAN_ARGS
)
716 if (MAN_HEAD
== n
->type
)
718 else if (MAN_BODY
== n
->type
)
721 SCALE_HS_INIT(&su
, INDENT
);
722 bufcat_su(h
, "margin-left", &su
);
724 if (n
->head
->child
) {
725 SCALE_VS_INIT(&su
, 1);
726 a2width(n
->head
->child
, &su
);
727 bufcat_su(h
, "margin-top", &su
);
730 PAIR_STYLE_INIT(&tag
, h
);
731 print_otag(h
, TAG_DIV
, 1, &tag
);