]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.17 2009/10/30 18:53:08 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.
17 #include <sys/types.h>
30 /* TODO: preserve ident widths. */
31 /* FIXME: have PD set the default vspace width. */
36 #define MAN_ARGS const struct man_meta *m, \
37 const struct man_node *n, \
42 int (*post
)(MAN_ARGS
);
45 static void print_man(MAN_ARGS
);
46 static void print_man_head(MAN_ARGS
);
47 static void print_man_nodelist(MAN_ARGS
);
48 static void print_man_node(MAN_ARGS
);
50 static int a2width(const struct man_node
*,
53 static int man_alt_pre(MAN_ARGS
);
54 static int man_br_pre(MAN_ARGS
);
55 static int man_ign_pre(MAN_ARGS
);
56 static void man_root_post(MAN_ARGS
);
57 static int man_root_pre(MAN_ARGS
);
58 static int man_B_pre(MAN_ARGS
);
59 static int man_HP_pre(MAN_ARGS
);
60 static int man_I_pre(MAN_ARGS
);
61 static int man_IP_pre(MAN_ARGS
);
62 static int man_PP_pre(MAN_ARGS
);
63 static int man_RS_pre(MAN_ARGS
);
64 static int man_SB_pre(MAN_ARGS
);
65 static int man_SH_pre(MAN_ARGS
);
66 static int man_SM_pre(MAN_ARGS
);
67 static int man_SS_pre(MAN_ARGS
);
70 extern size_t strlcpy(char *, const char *, size_t);
71 extern size_t strlcat(char *, const char *, size_t);
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 */
111 html_man(void *arg
, const struct man
*m
)
116 h
= (struct html
*)arg
;
118 print_gen_doctype(h
);
120 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
121 print_man(man_meta(m
), man_node(m
), h
);
134 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
136 print_man_head(m
, n
, h
);
138 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
140 tag
.key
= ATTR_CLASS
;
142 print_otag(h
, TAG_DIV
, 1, &tag
);
144 print_man_nodelist(m
, n
, h
);
152 print_man_head(MAN_ARGS
)
157 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
159 print_otag(h
, TAG_TITLE
, 0, NULL
);
160 print_text(h
, h
->buf
);
165 print_man_nodelist(MAN_ARGS
)
168 print_man_node(m
, n
, h
);
170 print_man_nodelist(m
, n
->next
, h
);
175 print_man_node(MAN_ARGS
)
187 child
= man_root_pre(m
, n
, h
);
190 print_text(h
, n
->string
);
193 if (mans
[n
->tok
].pre
)
194 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
198 if (child
&& n
->child
)
199 print_man_nodelist(m
, n
->child
, h
);
207 man_root_post(m
, n
, h
);
212 if (mans
[n
->tok
].post
)
213 (*mans
[n
->tok
].post
)(m
, n
, h
);
220 a2width(const struct man_node
*n
, struct roffsu
*su
)
223 if (MAN_TEXT
!= n
->type
)
225 if (a2roffsu(n
->string
, su
, SCALE_BU
))
234 man_root_pre(MAN_ARGS
)
236 struct htmlpair tag
[3];
238 char b
[BUFSIZ
], title
[BUFSIZ
];
242 (void)strlcat(b
, m
->vol
, BUFSIZ
);
244 (void)snprintf(title
, BUFSIZ
- 1,
245 "%s(%d)", m
->title
, m
->msec
);
247 PAIR_CLASS_INIT(&tag
[0], "header");
248 bufcat_style(h
, "width", "100%");
249 PAIR_STYLE_INIT(&tag
[1], h
);
250 PAIR_SUMMARY_INIT(&tag
[2], "header");
252 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
253 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
256 bufcat_style(h
, "width", "10%");
257 PAIR_STYLE_INIT(&tag
[0], h
);
258 print_otag(h
, TAG_TD
, 1, tag
);
259 print_text(h
, title
);
263 bufcat_style(h
, "width", "80%");
264 bufcat_style(h
, "white-space", "nowrap");
265 bufcat_style(h
, "text-align", "center");
266 PAIR_STYLE_INIT(&tag
[0], h
);
267 print_otag(h
, TAG_TD
, 1, tag
);
272 bufcat_style(h
, "width", "10%");
273 bufcat_style(h
, "text-align", "right");
274 PAIR_STYLE_INIT(&tag
[0], h
);
275 print_otag(h
, TAG_TD
, 1, tag
);
276 print_text(h
, title
);
284 man_root_post(MAN_ARGS
)
286 struct htmlpair tag
[3];
290 time2a(m
->date
, b
, DATESIZ
);
292 PAIR_CLASS_INIT(&tag
[0], "footer");
293 bufcat_style(h
, "width", "100%");
294 PAIR_STYLE_INIT(&tag
[1], h
);
295 PAIR_SUMMARY_INIT(&tag
[2], "footer");
297 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
298 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
301 bufcat_style(h
, "width", "50%");
302 PAIR_STYLE_INIT(&tag
[0], h
);
303 print_otag(h
, TAG_TD
, 1, tag
);
308 bufcat_style(h
, "width", "50%");
309 bufcat_style(h
, "text-align", "right");
310 PAIR_STYLE_INIT(&tag
[0], h
);
311 print_otag(h
, TAG_TD
, 1, tag
);
313 print_text(h
, m
->source
);
326 SCALE_VS_INIT(&su
, 1);
328 if (MAN_sp
== n
->tok
&& n
->child
)
329 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
330 else if (MAN_br
== n
->tok
)
333 bufcat_su(h
, "height", &su
);
334 PAIR_STYLE_INIT(&tag
, h
);
335 print_otag(h
, TAG_DIV
, 1, &tag
);
336 /* So the div isn't empty: */
337 print_text(h
, "\\~");
347 struct htmlpair tag
[2];
350 if (MAN_BODY
== n
->type
) {
351 SCALE_HS_INIT(&su
, INDENT
);
352 bufcat_su(h
, "margin-left", &su
);
353 PAIR_CLASS_INIT(&tag
[0], "sec-body");
354 PAIR_STYLE_INIT(&tag
[1], h
);
355 print_otag(h
, TAG_DIV
, 2, tag
);
357 } else if (MAN_BLOCK
== n
->type
) {
358 PAIR_CLASS_INIT(&tag
[0], "sec-block");
359 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
360 if (NULL
== n
->prev
->body
->child
) {
361 print_otag(h
, TAG_DIV
, 1, tag
);
365 SCALE_VS_INIT(&su
, 1);
366 bufcat_su(h
, "margin-top", &su
);
368 bufcat_su(h
, "margin-bottom", &su
);
369 PAIR_STYLE_INIT(&tag
[1], h
);
370 print_otag(h
, TAG_DIV
, 2, tag
);
374 PAIR_CLASS_INIT(&tag
[0], "sec-head");
375 print_otag(h
, TAG_DIV
, 1, tag
);
382 man_alt_pre(MAN_ARGS
)
384 const struct man_node
*nn
;
387 struct htmlpair tagi
, tagb
, *tagp
;
389 PAIR_CLASS_INIT(&tagi
, "italic");
390 PAIR_CLASS_INIT(&tagb
, "bold");
392 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
395 tagp
= i
% 2 ? &tagi
: &tagb
;
398 tagp
= i
% 2 ? &tagb
: &tagi
;
401 tagp
= i
% 2 ? &tagi
: NULL
;
404 tagp
= i
% 2 ? NULL
: &tagi
;
407 tagp
= i
% 2 ? NULL
: &tagb
;
410 tagp
= i
% 2 ? &tagb
: NULL
;
418 h
->flags
|= HTML_NOSPACE
;
421 t
= print_otag(h
, TAG_SPAN
, 1, tagp
);
422 print_man_node(m
, nn
, h
);
425 print_man_node(m
, nn
, h
);
438 PAIR_CLASS_INIT(&tag
, "small bold");
439 print_otag(h
, TAG_SPAN
, 1, &tag
);
450 PAIR_CLASS_INIT(&tag
, "small");
451 print_otag(h
, TAG_SPAN
, 1, &tag
);
460 struct htmlpair tag
[3];
463 SCALE_VS_INIT(&su
, 1);
465 if (MAN_BODY
== n
->type
) {
466 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
467 if (n
->parent
->next
&& n
->child
) {
468 bufcat_su(h
, "margin-bottom", &su
);
469 PAIR_STYLE_INIT(&tag
[1], h
);
470 print_otag(h
, TAG_DIV
, 2, tag
);
474 print_otag(h
, TAG_DIV
, 1, tag
);
476 } else if (MAN_BLOCK
== n
->type
) {
477 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
478 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
479 if (n
->prev
->body
->child
) {
480 bufcat_su(h
, "margin-top", &su
);
481 PAIR_STYLE_INIT(&tag
[1], h
);
482 print_otag(h
, TAG_DIV
, 2, tag
);
486 print_otag(h
, TAG_DIV
, 1, tag
);
490 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
491 bufcat_su(h
, "margin-left", &su
);
492 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
493 PAIR_STYLE_INIT(&tag
[1], h
);
494 print_otag(h
, TAG_DIV
, 2, tag
);
507 if (MAN_BLOCK
!= n
->type
)
512 if (MAN_ROOT
== n
->parent
->tok
) {
513 SCALE_HS_INIT(&su
, INDENT
);
514 bufcat_su(h
, "margin-left", &su
);
517 if (n
->next
&& n
->next
->child
) {
518 SCALE_VS_INIT(&su
, 1);
519 bufcat_su(h
, "margin-bottom", &su
);
523 PAIR_STYLE_INIT(&tag
, h
);
524 print_otag(h
, TAG_DIV
, i
? 1 : 0, &tag
);
535 const struct man_node
*nn
;
539 * This scattering of 1-BU margins and pads is to make sure that
540 * when text overruns its box, the subsequent text isn't flush
541 * up against it. However, the rest of the right-hand box must
542 * also be adjusted in consideration of this 1-BU space.
545 if (MAN_BODY
== n
->type
) {
546 SCALE_HS_INIT(&su
, INDENT
);
547 bufcat_su(h
, "margin-left", &su
);
548 PAIR_STYLE_INIT(&tag
, h
);
549 print_otag(h
, TAG_DIV
, 1, &tag
);
553 nn
= MAN_BLOCK
== n
->type
?
554 n
->head
->child
: n
->parent
->head
->child
;
556 SCALE_HS_INIT(&su
, INDENT
);
559 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
560 if (NULL
!= (nn
= nn
->next
)) {
561 for ( ; nn
->next
; nn
= nn
->next
)
563 width
= a2width(nn
, &su
);
566 if (MAN_TP
== n
->tok
&& NULL
!= nn
)
567 width
= a2width(nn
, &su
);
569 if (MAN_BLOCK
== n
->type
) {
570 bufcat_su(h
, "margin-left", &su
);
571 SCALE_VS_INIT(&su
, 1);
572 bufcat_su(h
, "margin-top", &su
);
573 bufcat_style(h
, "clear", "both");
574 PAIR_STYLE_INIT(&tag
, h
);
575 print_otag(h
, TAG_DIV
, 1, &tag
);
579 bufcat_su(h
, "min-width", &su
);
581 bufcat_su(h
, "margin-left", &su
);
582 SCALE_HS_INIT(&su
, 1);
583 bufcat_su(h
, "margin-right", &su
);
584 bufcat_style(h
, "clear", "left");
586 if (n
->next
&& n
->next
->child
)
587 bufcat_style(h
, "float", "left");
589 PAIR_STYLE_INIT(&tag
, h
);
590 print_otag(h
, TAG_DIV
, 1, &tag
);
592 /* With a length string, manually omit the last child. */
597 if (MAN_IP
== n
->tok
)
598 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
599 print_man_node(m
, nn
, h
);
600 if (MAN_TP
== n
->tok
)
601 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
602 print_man_node(m
, nn
, h
);
612 const struct man_node
*nn
;
616 if (MAN_HEAD
== n
->type
)
619 nn
= MAN_BLOCK
== n
->type
?
620 n
->head
->child
: n
->parent
->head
->child
;
622 SCALE_HS_INIT(&su
, INDENT
);
625 (void)a2width(nn
, &su
);
627 if (MAN_BLOCK
== n
->type
) {
628 bufcat_su(h
, "margin-left", &su
);
629 SCALE_VS_INIT(&su
, 1);
630 bufcat_su(h
, "margin-top", &su
);
631 bufcat_style(h
, "clear", "both");
632 PAIR_STYLE_INIT(&tag
, h
);
633 print_otag(h
, TAG_DIV
, 1, &tag
);
637 bufcat_su(h
, "margin-left", &su
);
639 bufcat_su(h
, "text-indent", &su
);
641 PAIR_STYLE_INIT(&tag
, h
);
642 print_otag(h
, TAG_DIV
, 1, &tag
);
653 PAIR_CLASS_INIT(&tag
, "bold");
654 print_otag(h
, TAG_SPAN
, 1, &tag
);
665 PAIR_CLASS_INIT(&tag
, "italic");
666 print_otag(h
, TAG_SPAN
, 1, &tag
);
673 man_ign_pre(MAN_ARGS
)
687 if (MAN_HEAD
== n
->type
)
689 else if (MAN_BODY
== n
->type
)
692 SCALE_HS_INIT(&su
, INDENT
);
693 bufcat_su(h
, "margin-left", &su
);
695 if (n
->head
->child
) {
696 SCALE_VS_INIT(&su
, 1);
697 a2width(n
->head
->child
, &su
);
698 bufcat_su(h
, "margin-top", &su
);
701 PAIR_STYLE_INIT(&tag
, h
);
702 print_otag(h
, TAG_DIV
, 1, &tag
);