]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.8 2009/10/08 23:00:15 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>
18 #include <sys/queue.h>
31 /* TODO: preserve ident widths. */
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_SB_pre(MAN_ARGS
);
64 static int man_SH_pre(MAN_ARGS
);
65 static int man_SM_pre(MAN_ARGS
);
66 static int man_SS_pre(MAN_ARGS
);
69 extern size_t strlcpy(char *, const char *, size_t);
70 extern size_t strlcat(char *, const char *, size_t);
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 { NULL
, NULL
}, /* RS */
103 { man_ign_pre
, NULL
}, /* DT */
104 { man_ign_pre
, NULL
}, /* UC */
109 html_man(void *arg
, const struct man
*m
)
114 h
= (struct html
*)arg
;
116 print_gen_doctype(h
);
118 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
119 print_man(man_meta(m
), man_node(m
), h
);
132 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
134 print_man_head(m
, n
, h
);
136 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
138 tag
.key
= ATTR_CLASS
;
140 print_otag(h
, TAG_DIV
, 1, &tag
);
142 print_man_nodelist(m
, n
, h
);
150 print_man_head(MAN_ARGS
)
155 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
157 print_otag(h
, TAG_TITLE
, 0, NULL
);
158 print_text(h
, h
->buf
);
163 print_man_nodelist(MAN_ARGS
)
166 print_man_node(m
, n
, h
);
168 print_man_nodelist(m
, n
->next
, h
);
173 print_man_node(MAN_ARGS
)
179 t
= SLIST_FIRST(&h
->tags
);
185 child
= man_root_pre(m
, n
, h
);
188 print_text(h
, n
->string
);
191 if (mans
[n
->tok
].pre
)
192 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
196 if (child
&& n
->child
)
197 print_man_nodelist(m
, n
->child
, h
);
205 man_root_post(m
, n
, h
);
210 if (mans
[n
->tok
].post
)
211 (*mans
[n
->tok
].post
)(m
, n
, h
);
218 a2width(const struct man_node
*n
, struct roffsu
*su
)
221 if (MAN_TEXT
!= n
->type
)
223 if (a2roffsu(n
->string
, su
))
232 man_root_pre(MAN_ARGS
)
234 struct htmlpair tag
[2];
236 char b
[BUFSIZ
], title
[BUFSIZ
];
240 (void)strlcat(b
, m
->vol
, BUFSIZ
);
242 (void)snprintf(title
, BUFSIZ
- 1,
243 "%s(%d)", m
->title
, m
->msec
);
245 PAIR_CLASS_INIT(&tag
[0], "header");
246 bufcat_style(h
, "width", "100%");
247 PAIR_STYLE_INIT(&tag
[1], h
);
248 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
249 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
252 bufcat_style(h
, "width", "10%");
253 PAIR_STYLE_INIT(&tag
[0], h
);
254 print_otag(h
, TAG_TD
, 1, tag
);
255 print_text(h
, title
);
259 bufcat_style(h
, "width", "80%");
260 bufcat_style(h
, "white-space", "nowrap");
261 bufcat_style(h
, "text-align", "center");
262 PAIR_STYLE_INIT(&tag
[0], h
);
263 print_otag(h
, TAG_TD
, 1, tag
);
268 bufcat_style(h
, "width", "10%");
269 bufcat_style(h
, "text-align", "right");
270 PAIR_STYLE_INIT(&tag
[0], h
);
271 print_otag(h
, TAG_TD
, 1, tag
);
272 print_text(h
, title
);
280 man_root_post(MAN_ARGS
)
283 struct htmlpair tag
[2];
287 (void)localtime_r(&m
->date
, &tm
);
289 if (0 == strftime(b
, BUFSIZ
- 1, "%B %e, %Y", &tm
))
290 err(EXIT_FAILURE
, "strftime");
292 PAIR_CLASS_INIT(&tag
[0], "footer");
293 bufcat_style(h
, "width", "100%");
294 PAIR_STYLE_INIT(&tag
[1], h
);
295 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
296 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
299 bufcat_style(h
, "width", "50%");
300 PAIR_STYLE_INIT(&tag
[0], h
);
301 print_otag(h
, TAG_TD
, 1, tag
);
306 bufcat_style(h
, "width", "50%");
307 bufcat_style(h
, "text-align", "right");
308 PAIR_STYLE_INIT(&tag
[0], h
);
309 print_otag(h
, TAG_TD
, 1, tag
);
311 print_text(h
, m
->source
);
324 SCALE_VS_INIT(&su
, 1);
326 if (MAN_sp
== n
->tok
) {
329 a2roffsu(n
->child
->string
, &su
);
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
);
344 struct htmlpair tag
[2];
347 if (MAN_BODY
== n
->type
) {
348 SCALE_HS_INIT(&su
, INDENT
);
349 bufcat_su(h
, "margin-left", &su
);
350 PAIR_CLASS_INIT(&tag
[0], "sec-body");
351 PAIR_STYLE_INIT(&tag
[1], h
);
352 print_otag(h
, TAG_DIV
, 2, tag
);
354 } else if (MAN_BLOCK
== n
->type
) {
355 PAIR_CLASS_INIT(&tag
[0], "sec-block");
356 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
357 if (NULL
== n
->prev
->body
->child
) {
358 print_otag(h
, TAG_DIV
, 1, tag
);
362 SCALE_VS_INIT(&su
, 1);
363 bufcat_su(h
, "margin-top", &su
);
365 bufcat_su(h
, "margin-bottom", &su
);
366 PAIR_STYLE_INIT(&tag
[1], h
);
367 print_otag(h
, TAG_DIV
, 2, tag
);
371 PAIR_CLASS_INIT(&tag
[0], "sec-head");
372 print_otag(h
, TAG_DIV
, 1, tag
);
379 man_alt_pre(MAN_ARGS
)
381 const struct man_node
*nn
;
384 struct htmlpair tagi
, tagb
, *tagp
;
386 PAIR_CLASS_INIT(&tagi
, "italic");
387 PAIR_CLASS_INIT(&tagb
, "bold");
389 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
392 tagp
= i
% 2 ? &tagi
: &tagb
;
395 tagp
= i
% 2 ? &tagb
: &tagi
;
398 tagp
= i
% 2 ? &tagi
: NULL
;
401 tagp
= i
% 2 ? NULL
: &tagi
;
404 tagp
= i
% 2 ? NULL
: &tagb
;
407 tagp
= i
% 2 ? &tagb
: NULL
;
415 h
->flags
|= HTML_NOSPACE
;
418 t
= print_otag(h
, TAG_SPAN
, 1, tagp
);
419 print_man_node(m
, nn
, h
);
422 print_man_node(m
, nn
, h
);
435 PAIR_CLASS_INIT(&tag
, "small bold");
436 print_otag(h
, TAG_SPAN
, 1, &tag
);
447 PAIR_CLASS_INIT(&tag
, "small");
448 print_otag(h
, TAG_SPAN
, 1, &tag
);
457 struct htmlpair tag
[3];
460 SCALE_VS_INIT(&su
, 1);
462 if (MAN_BODY
== n
->type
) {
463 PAIR_CLASS_INIT(&tag
[0], "ssec-body");
464 if (n
->parent
->next
&& n
->child
) {
465 bufcat_su(h
, "margin-bottom", &su
);
466 PAIR_STYLE_INIT(&tag
[1], h
);
467 print_otag(h
, TAG_DIV
, 2, tag
);
471 print_otag(h
, TAG_DIV
, 1, tag
);
473 } else if (MAN_BLOCK
== n
->type
) {
474 PAIR_CLASS_INIT(&tag
[0], "ssec-block");
475 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
476 if (n
->prev
->body
->child
) {
477 bufcat_su(h
, "margin-top", &su
);
478 PAIR_STYLE_INIT(&tag
[1], h
);
479 print_otag(h
, TAG_DIV
, 2, tag
);
483 print_otag(h
, TAG_DIV
, 1, tag
);
487 SCALE_HS_INIT(&su
, INDENT
- HALFINDENT
);
488 bufcat_su(h
, "margin-left", &su
);
489 PAIR_CLASS_INIT(&tag
[0], "ssec-head");
490 PAIR_STYLE_INIT(&tag
[1], h
);
491 print_otag(h
, TAG_DIV
, 2, tag
);
504 if (MAN_BLOCK
!= n
->type
)
509 if (MAN_ROOT
== n
->parent
->tok
) {
510 SCALE_HS_INIT(&su
, INDENT
);
511 bufcat_su(h
, "margin-left", &su
);
514 if (n
->next
&& n
->next
->child
) {
515 SCALE_VS_INIT(&su
, 1);
516 bufcat_su(h
, "margin-bottom", &su
);
520 PAIR_STYLE_INIT(&tag
, h
);
521 print_otag(h
, TAG_DIV
, i
? 1 : 0, &tag
);
532 const struct man_node
*nn
;
536 * This scattering of 1-BU margins and pads is to make sure that
537 * when text overruns its box, the subsequent text isn't flush
538 * up against it. However, the rest of the right-hand box must
539 * also be adjusted in consideration of this 1-BU space.
542 if (MAN_BODY
== n
->type
) {
543 SCALE_HS_INIT(&su
, INDENT
);
544 bufcat_su(h
, "margin-left", &su
);
545 PAIR_STYLE_INIT(&tag
, h
);
546 print_otag(h
, TAG_DIV
, 1, &tag
);
550 nn
= MAN_BLOCK
== n
->type
?
551 n
->head
->child
: n
->parent
->head
->child
;
553 SCALE_HS_INIT(&su
, INDENT
);
556 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
557 if (NULL
!= (nn
= nn
->next
)) {
558 for ( ; nn
->next
; nn
= nn
->next
)
560 width
= a2width(nn
, &su
);
563 if (MAN_TP
== n
->tok
&& NULL
!= nn
)
564 width
= a2width(nn
, &su
);
566 if (MAN_BLOCK
== n
->type
) {
567 bufcat_su(h
, "margin-left", &su
);
568 bufcat_style(h
, "clear", "both");
569 PAIR_STYLE_INIT(&tag
, h
);
570 print_otag(h
, TAG_DIV
, 1, &tag
);
574 bufcat_su(h
, "min-width", &su
);
576 bufcat_su(h
, "margin-left", &su
);
577 SCALE_HS_INIT(&su
, 1);
578 bufcat_su(h
, "margin-right", &su
);
579 bufcat_style(h
, "clear", "left");
581 if (n
->next
&& n
->next
->child
)
582 bufcat_style(h
, "float", "left");
584 PAIR_STYLE_INIT(&tag
, h
);
585 print_otag(h
, TAG_DIV
, 1, &tag
);
587 /* With a length string, manually omit the last child. */
592 if (MAN_IP
== n
->tok
)
593 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
594 print_man_node(m
, nn
, h
);
595 if (MAN_TP
== n
->tok
)
596 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
597 print_man_node(m
, nn
, h
);
607 const struct man_node
*nn
;
611 if (MAN_HEAD
== n
->type
)
614 nn
= MAN_BLOCK
== n
->type
?
615 n
->head
->child
: n
->parent
->head
->child
;
617 SCALE_HS_INIT(&su
, INDENT
);
620 (void)a2width(nn
, &su
);
622 if (MAN_BLOCK
== n
->type
) {
623 bufcat_su(h
, "margin-left", &su
);
624 bufcat_style(h
, "clear", "both");
625 PAIR_STYLE_INIT(&tag
, h
);
626 print_otag(h
, TAG_DIV
, 1, &tag
);
630 bufcat_su(h
, "margin-left", &su
);
632 bufcat_su(h
, "text-indent", &su
);
634 PAIR_STYLE_INIT(&tag
, h
);
635 print_otag(h
, TAG_DIV
, 1, &tag
);
646 PAIR_CLASS_INIT(&tag
, "bold");
647 print_otag(h
, TAG_SPAN
, 1, &tag
);
658 PAIR_CLASS_INIT(&tag
, "italic");
659 print_otag(h
, TAG_SPAN
, 1, &tag
);
666 man_ign_pre(MAN_ARGS
)