]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.16 2009/10/28 08:00:18 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>
31 /* TODO: preserve ident widths. */
32 /* FIXME: have PD set the default vspace width. */
37 #define MAN_ARGS const struct man_meta *m, \
38 const struct man_node *n, \
43 int (*post
)(MAN_ARGS
);
46 static void print_man(MAN_ARGS
);
47 static void print_man_head(MAN_ARGS
);
48 static void print_man_nodelist(MAN_ARGS
);
49 static void print_man_node(MAN_ARGS
);
51 static int a2width(const struct man_node
*,
54 static int man_alt_pre(MAN_ARGS
);
55 static int man_br_pre(MAN_ARGS
);
56 static int man_ign_pre(MAN_ARGS
);
57 static void man_root_post(MAN_ARGS
);
58 static int man_root_pre(MAN_ARGS
);
59 static int man_B_pre(MAN_ARGS
);
60 static int man_HP_pre(MAN_ARGS
);
61 static int man_I_pre(MAN_ARGS
);
62 static int man_IP_pre(MAN_ARGS
);
63 static int man_PP_pre(MAN_ARGS
);
64 static int man_RS_pre(MAN_ARGS
);
65 static int man_SB_pre(MAN_ARGS
);
66 static int man_SH_pre(MAN_ARGS
);
67 static int man_SM_pre(MAN_ARGS
);
68 static int man_SS_pre(MAN_ARGS
);
71 extern size_t strlcpy(char *, const char *, size_t);
72 extern size_t strlcat(char *, const char *, size_t);
75 static const struct htmlman mans
[MAN_MAX
] = {
76 { man_br_pre
, NULL
}, /* br */
77 { NULL
, NULL
}, /* TH */
78 { man_SH_pre
, NULL
}, /* SH */
79 { man_SS_pre
, NULL
}, /* SS */
80 { man_IP_pre
, NULL
}, /* TP */
81 { man_PP_pre
, NULL
}, /* LP */
82 { man_PP_pre
, NULL
}, /* PP */
83 { man_PP_pre
, NULL
}, /* P */
84 { man_IP_pre
, NULL
}, /* IP */
85 { man_HP_pre
, NULL
}, /* HP */
86 { man_SM_pre
, NULL
}, /* SM */
87 { man_SB_pre
, NULL
}, /* SB */
88 { man_alt_pre
, NULL
}, /* BI */
89 { man_alt_pre
, NULL
}, /* IB */
90 { man_alt_pre
, NULL
}, /* BR */
91 { man_alt_pre
, NULL
}, /* RB */
92 { NULL
, NULL
}, /* R */
93 { man_B_pre
, NULL
}, /* B */
94 { man_I_pre
, NULL
}, /* I */
95 { man_alt_pre
, NULL
}, /* IR */
96 { man_alt_pre
, NULL
}, /* RI */
97 { NULL
, NULL
}, /* na */
98 { NULL
, NULL
}, /* i */
99 { man_br_pre
, NULL
}, /* sp */
100 { NULL
, NULL
}, /* nf */
101 { NULL
, NULL
}, /* fi */
102 { NULL
, NULL
}, /* r */
103 { NULL
, NULL
}, /* RE */
104 { man_RS_pre
, NULL
}, /* RS */
105 { man_ign_pre
, NULL
}, /* DT */
106 { man_ign_pre
, NULL
}, /* UC */
107 { man_ign_pre
, NULL
}, /* PD */
112 html_man(void *arg
, const struct man
*m
)
117 h
= (struct html
*)arg
;
119 print_gen_doctype(h
);
121 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
122 print_man(man_meta(m
), man_node(m
), h
);
135 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
137 print_man_head(m
, n
, h
);
139 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
141 tag
.key
= ATTR_CLASS
;
143 print_otag(h
, TAG_DIV
, 1, &tag
);
145 print_man_nodelist(m
, n
, h
);
153 print_man_head(MAN_ARGS
)
158 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
160 print_otag(h
, TAG_TITLE
, 0, NULL
);
161 print_text(h
, h
->buf
);
166 print_man_nodelist(MAN_ARGS
)
169 print_man_node(m
, n
, h
);
171 print_man_nodelist(m
, n
->next
, h
);
176 print_man_node(MAN_ARGS
)
188 child
= man_root_pre(m
, n
, h
);
191 print_text(h
, n
->string
);
194 if (mans
[n
->tok
].pre
)
195 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
199 if (child
&& n
->child
)
200 print_man_nodelist(m
, n
->child
, h
);
208 man_root_post(m
, n
, h
);
213 if (mans
[n
->tok
].post
)
214 (*mans
[n
->tok
].post
)(m
, n
, h
);
221 a2width(const struct man_node
*n
, struct roffsu
*su
)
224 if (MAN_TEXT
!= n
->type
)
226 if (a2roffsu(n
->string
, su
, SCALE_BU
))
235 man_root_pre(MAN_ARGS
)
237 struct htmlpair tag
[3];
239 char b
[BUFSIZ
], title
[BUFSIZ
];
243 (void)strlcat(b
, m
->vol
, BUFSIZ
);
245 (void)snprintf(title
, BUFSIZ
- 1,
246 "%s(%d)", m
->title
, m
->msec
);
248 PAIR_CLASS_INIT(&tag
[0], "header");
249 bufcat_style(h
, "width", "100%");
250 PAIR_STYLE_INIT(&tag
[1], h
);
251 PAIR_SUMMARY_INIT(&tag
[2], "header");
253 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
254 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
257 bufcat_style(h
, "width", "10%");
258 PAIR_STYLE_INIT(&tag
[0], h
);
259 print_otag(h
, TAG_TD
, 1, tag
);
260 print_text(h
, title
);
264 bufcat_style(h
, "width", "80%");
265 bufcat_style(h
, "white-space", "nowrap");
266 bufcat_style(h
, "text-align", "center");
267 PAIR_STYLE_INIT(&tag
[0], h
);
268 print_otag(h
, TAG_TD
, 1, tag
);
273 bufcat_style(h
, "width", "10%");
274 bufcat_style(h
, "text-align", "right");
275 PAIR_STYLE_INIT(&tag
[0], h
);
276 print_otag(h
, TAG_TD
, 1, tag
);
277 print_text(h
, title
);
285 man_root_post(MAN_ARGS
)
287 struct htmlpair tag
[3];
291 time2a(m
->date
, b
, DATESIZ
);
293 PAIR_CLASS_INIT(&tag
[0], "footer");
294 bufcat_style(h
, "width", "100%");
295 PAIR_STYLE_INIT(&tag
[1], h
);
296 PAIR_SUMMARY_INIT(&tag
[2], "footer");
298 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
299 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
302 bufcat_style(h
, "width", "50%");
303 PAIR_STYLE_INIT(&tag
[0], h
);
304 print_otag(h
, TAG_TD
, 1, tag
);
309 bufcat_style(h
, "width", "50%");
310 bufcat_style(h
, "text-align", "right");
311 PAIR_STYLE_INIT(&tag
[0], h
);
312 print_otag(h
, TAG_TD
, 1, tag
);
314 print_text(h
, m
->source
);
327 SCALE_VS_INIT(&su
, 1);
329 if (MAN_sp
== n
->tok
&& n
->child
)
330 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
331 else if (MAN_br
== n
->tok
)
334 bufcat_su(h
, "height", &su
);
335 PAIR_STYLE_INIT(&tag
, h
);
336 print_otag(h
, TAG_DIV
, 1, &tag
);
337 /* So the div isn't empty: */
338 print_text(h
, "\\~");
348 struct htmlpair tag
[2];
351 if (MAN_BODY
== n
->type
) {
352 SCALE_HS_INIT(&su
, INDENT
);
353 bufcat_su(h
, "margin-left", &su
);
354 PAIR_CLASS_INIT(&tag
[0], "sec-body");
355 PAIR_STYLE_INIT(&tag
[1], h
);
356 print_otag(h
, TAG_DIV
, 2, tag
);
358 } else if (MAN_BLOCK
== n
->type
) {
359 PAIR_CLASS_INIT(&tag
[0], "sec-block");
360 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
361 if (NULL
== n
->prev
->body
->child
) {
362 print_otag(h
, TAG_DIV
, 1, tag
);
366 SCALE_VS_INIT(&su
, 1);
367 bufcat_su(h
, "margin-top", &su
);
369 bufcat_su(h
, "margin-bottom", &su
);
370 PAIR_STYLE_INIT(&tag
[1], h
);
371 print_otag(h
, TAG_DIV
, 2, tag
);
375 PAIR_CLASS_INIT(&tag
[0], "sec-head");
376 print_otag(h
, TAG_DIV
, 1, tag
);
383 man_alt_pre(MAN_ARGS
)
385 const struct man_node
*nn
;
388 struct htmlpair tagi
, tagb
, *tagp
;
390 PAIR_CLASS_INIT(&tagi
, "italic");
391 PAIR_CLASS_INIT(&tagb
, "bold");
393 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
396 tagp
= i
% 2 ? &tagi
: &tagb
;
399 tagp
= i
% 2 ? &tagb
: &tagi
;
402 tagp
= i
% 2 ? &tagi
: NULL
;
405 tagp
= i
% 2 ? NULL
: &tagi
;
408 tagp
= i
% 2 ? NULL
: &tagb
;
411 tagp
= i
% 2 ? &tagb
: NULL
;
419 h
->flags
|= HTML_NOSPACE
;
422 t
= print_otag(h
, TAG_SPAN
, 1, tagp
);
423 print_man_node(m
, nn
, h
);
426 print_man_node(m
, nn
, h
);
439 PAIR_CLASS_INIT(&tag
, "small bold");
440 print_otag(h
, TAG_SPAN
, 1, &tag
);
451 PAIR_CLASS_INIT(&tag
, "small");
452 print_otag(h
, TAG_SPAN
, 1, &tag
);
461 struct htmlpair tag
[3];
464 SCALE_VS_INIT(&su
, 1);
466 if (MAN_BODY
== n
->type
) {
467 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
468 if (n
->parent
->next
&& n
->child
) {
469 bufcat_su(h
, "margin-bottom", &su
);
470 PAIR_STYLE_INIT(&tag
[1], h
);
471 print_otag(h
, TAG_DIV
, 2, tag
);
475 print_otag(h
, TAG_DIV
, 1, tag
);
477 } else if (MAN_BLOCK
== n
->type
) {
478 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
479 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
480 if (n
->prev
->body
->child
) {
481 bufcat_su(h
, "margin-top", &su
);
482 PAIR_STYLE_INIT(&tag
[1], h
);
483 print_otag(h
, TAG_DIV
, 2, tag
);
487 print_otag(h
, TAG_DIV
, 1, tag
);
491 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
492 bufcat_su(h
, "margin-left", &su
);
493 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
494 PAIR_STYLE_INIT(&tag
[1], h
);
495 print_otag(h
, TAG_DIV
, 2, tag
);
508 if (MAN_BLOCK
!= n
->type
)
513 if (MAN_ROOT
== n
->parent
->tok
) {
514 SCALE_HS_INIT(&su
, INDENT
);
515 bufcat_su(h
, "margin-left", &su
);
518 if (n
->next
&& n
->next
->child
) {
519 SCALE_VS_INIT(&su
, 1);
520 bufcat_su(h
, "margin-bottom", &su
);
524 PAIR_STYLE_INIT(&tag
, h
);
525 print_otag(h
, TAG_DIV
, i
? 1 : 0, &tag
);
536 const struct man_node
*nn
;
540 * This scattering of 1-BU margins and pads is to make sure that
541 * when text overruns its box, the subsequent text isn't flush
542 * up against it. However, the rest of the right-hand box must
543 * also be adjusted in consideration of this 1-BU space.
546 if (MAN_BODY
== n
->type
) {
547 SCALE_HS_INIT(&su
, INDENT
);
548 bufcat_su(h
, "margin-left", &su
);
549 PAIR_STYLE_INIT(&tag
, h
);
550 print_otag(h
, TAG_DIV
, 1, &tag
);
554 nn
= MAN_BLOCK
== n
->type
?
555 n
->head
->child
: n
->parent
->head
->child
;
557 SCALE_HS_INIT(&su
, INDENT
);
560 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
561 if (NULL
!= (nn
= nn
->next
)) {
562 for ( ; nn
->next
; nn
= nn
->next
)
564 width
= a2width(nn
, &su
);
567 if (MAN_TP
== n
->tok
&& NULL
!= nn
)
568 width
= a2width(nn
, &su
);
570 if (MAN_BLOCK
== n
->type
) {
571 bufcat_su(h
, "margin-left", &su
);
572 SCALE_VS_INIT(&su
, 1);
573 bufcat_su(h
, "margin-top", &su
);
574 bufcat_style(h
, "clear", "both");
575 PAIR_STYLE_INIT(&tag
, h
);
576 print_otag(h
, TAG_DIV
, 1, &tag
);
580 bufcat_su(h
, "min-width", &su
);
582 bufcat_su(h
, "margin-left", &su
);
583 SCALE_HS_INIT(&su
, 1);
584 bufcat_su(h
, "margin-right", &su
);
585 bufcat_style(h
, "clear", "left");
587 if (n
->next
&& n
->next
->child
)
588 bufcat_style(h
, "float", "left");
590 PAIR_STYLE_INIT(&tag
, h
);
591 print_otag(h
, TAG_DIV
, 1, &tag
);
593 /* With a length string, manually omit the last child. */
598 if (MAN_IP
== n
->tok
)
599 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
600 print_man_node(m
, nn
, h
);
601 if (MAN_TP
== n
->tok
)
602 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
603 print_man_node(m
, nn
, h
);
613 const struct man_node
*nn
;
617 if (MAN_HEAD
== n
->type
)
620 nn
= MAN_BLOCK
== n
->type
?
621 n
->head
->child
: n
->parent
->head
->child
;
623 SCALE_HS_INIT(&su
, INDENT
);
626 (void)a2width(nn
, &su
);
628 if (MAN_BLOCK
== n
->type
) {
629 bufcat_su(h
, "margin-left", &su
);
630 SCALE_VS_INIT(&su
, 1);
631 bufcat_su(h
, "margin-top", &su
);
632 bufcat_style(h
, "clear", "both");
633 PAIR_STYLE_INIT(&tag
, h
);
634 print_otag(h
, TAG_DIV
, 1, &tag
);
638 bufcat_su(h
, "margin-left", &su
);
640 bufcat_su(h
, "text-indent", &su
);
642 PAIR_STYLE_INIT(&tag
, h
);
643 print_otag(h
, TAG_DIV
, 1, &tag
);
654 PAIR_CLASS_INIT(&tag
, "bold");
655 print_otag(h
, TAG_SPAN
, 1, &tag
);
666 PAIR_CLASS_INIT(&tag
, "italic");
667 print_otag(h
, TAG_SPAN
, 1, &tag
);
674 man_ign_pre(MAN_ARGS
)
688 if (MAN_HEAD
== n
->type
)
690 else if (MAN_BODY
== n
->type
)
693 SCALE_HS_INIT(&su
, INDENT
);
694 bufcat_su(h
, "margin-left", &su
);
696 if (n
->head
->child
) {
697 SCALE_VS_INIT(&su
, 1);
698 a2width(n
->head
->child
, &su
);
699 bufcat_su(h
, "margin-top", &su
);
702 PAIR_STYLE_INIT(&tag
, h
);
703 print_otag(h
, TAG_DIV
, 1, &tag
);