]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.11 2009/10/18 19:03:36 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>
32 /* TODO: preserve ident widths. */
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 */
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
)
181 t
= SLIST_FIRST(&h
->tags
);
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
[2];
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 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
251 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
254 bufcat_style(h
, "width", "10%");
255 PAIR_STYLE_INIT(&tag
[0], h
);
256 print_otag(h
, TAG_TD
, 1, tag
);
257 print_text(h
, title
);
261 bufcat_style(h
, "width", "80%");
262 bufcat_style(h
, "white-space", "nowrap");
263 bufcat_style(h
, "text-align", "center");
264 PAIR_STYLE_INIT(&tag
[0], h
);
265 print_otag(h
, TAG_TD
, 1, tag
);
270 bufcat_style(h
, "width", "10%");
271 bufcat_style(h
, "text-align", "right");
272 PAIR_STYLE_INIT(&tag
[0], h
);
273 print_otag(h
, TAG_TD
, 1, tag
);
274 print_text(h
, title
);
282 man_root_post(MAN_ARGS
)
285 struct htmlpair tag
[2];
289 (void)localtime_r(&m
->date
, &tm
);
291 if (0 == strftime(b
, BUFSIZ
- 1, "%B %e, %Y", &tm
))
292 err(EXIT_FAILURE
, "strftime");
294 PAIR_CLASS_INIT(&tag
[0], "footer");
295 bufcat_style(h
, "width", "100%");
296 PAIR_STYLE_INIT(&tag
[1], h
);
297 t
= print_otag(h
, TAG_TABLE
, 2, 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
);
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 SCALE_VS_INIT(&su
, 1);
569 bufcat_su(h
, "margin-top", &su
);
570 bufcat_style(h
, "clear", "both");
571 PAIR_STYLE_INIT(&tag
, h
);
572 print_otag(h
, TAG_DIV
, 1, &tag
);
576 bufcat_su(h
, "min-width", &su
);
578 bufcat_su(h
, "margin-left", &su
);
579 SCALE_HS_INIT(&su
, 1);
580 bufcat_su(h
, "margin-right", &su
);
581 bufcat_style(h
, "clear", "left");
583 if (n
->next
&& n
->next
->child
)
584 bufcat_style(h
, "float", "left");
586 PAIR_STYLE_INIT(&tag
, h
);
587 print_otag(h
, TAG_DIV
, 1, &tag
);
589 /* With a length string, manually omit the last child. */
594 if (MAN_IP
== n
->tok
)
595 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
596 print_man_node(m
, nn
, h
);
597 if (MAN_TP
== n
->tok
)
598 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
599 print_man_node(m
, nn
, h
);
609 const struct man_node
*nn
;
613 if (MAN_HEAD
== n
->type
)
616 nn
= MAN_BLOCK
== n
->type
?
617 n
->head
->child
: n
->parent
->head
->child
;
619 SCALE_HS_INIT(&su
, INDENT
);
622 (void)a2width(nn
, &su
);
624 if (MAN_BLOCK
== n
->type
) {
625 bufcat_su(h
, "margin-left", &su
);
626 SCALE_VS_INIT(&su
, 1);
627 bufcat_su(h
, "margin-top", &su
);
628 bufcat_style(h
, "clear", "both");
629 PAIR_STYLE_INIT(&tag
, h
);
630 print_otag(h
, TAG_DIV
, 1, &tag
);
634 bufcat_su(h
, "margin-left", &su
);
636 bufcat_su(h
, "text-indent", &su
);
638 PAIR_STYLE_INIT(&tag
, h
);
639 print_otag(h
, TAG_DIV
, 1, &tag
);
650 PAIR_CLASS_INIT(&tag
, "bold");
651 print_otag(h
, TAG_SPAN
, 1, &tag
);
662 PAIR_CLASS_INIT(&tag
, "italic");
663 print_otag(h
, TAG_SPAN
, 1, &tag
);
670 man_ign_pre(MAN_ARGS
)
684 if (MAN_HEAD
== n
->type
)
686 else if (MAN_BODY
== n
->type
)
689 SCALE_HS_INIT(&su
, INDENT
);
690 bufcat_su(h
, "margin-left", &su
);
692 if (n
->head
->child
) {
693 SCALE_VS_INIT(&su
, 1);
694 a2width(n
->head
->child
, &su
);
695 bufcat_su(h
, "margin-top", &su
);
698 PAIR_STYLE_INIT(&tag
, h
);
699 print_otag(h
, TAG_DIV
, 1, &tag
);