]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.56 2010/12/22 09:51:27 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_SH_pre(MAN_ARGS
);
78 static int man_SM_pre(MAN_ARGS
);
79 static int man_SS_pre(MAN_ARGS
);
81 static const struct htmlman mans
[MAN_MAX
] = {
82 { man_br_pre
, NULL
}, /* br */
83 { NULL
, NULL
}, /* TH */
84 { man_SH_pre
, NULL
}, /* SH */
85 { man_SS_pre
, NULL
}, /* SS */
86 { man_IP_pre
, NULL
}, /* TP */
87 { man_PP_pre
, NULL
}, /* LP */
88 { man_PP_pre
, NULL
}, /* PP */
89 { man_PP_pre
, NULL
}, /* P */
90 { man_IP_pre
, NULL
}, /* IP */
91 { man_HP_pre
, NULL
}, /* HP */
92 { man_SM_pre
, NULL
}, /* SM */
93 { man_SM_pre
, NULL
}, /* SB */
94 { man_alt_pre
, NULL
}, /* BI */
95 { man_alt_pre
, NULL
}, /* IB */
96 { man_alt_pre
, NULL
}, /* BR */
97 { man_alt_pre
, NULL
}, /* RB */
98 { NULL
, NULL
}, /* R */
99 { man_B_pre
, NULL
}, /* B */
100 { man_I_pre
, NULL
}, /* I */
101 { man_alt_pre
, NULL
}, /* IR */
102 { man_alt_pre
, NULL
}, /* RI */
103 { NULL
, NULL
}, /* na */
104 { man_br_pre
, NULL
}, /* sp */
105 { man_literal_pre
, NULL
}, /* nf */
106 { man_literal_pre
, NULL
}, /* fi */
107 { NULL
, NULL
}, /* RE */
108 { man_RS_pre
, NULL
}, /* RS */
109 { man_ign_pre
, NULL
}, /* DT */
110 { man_ign_pre
, NULL
}, /* UC */
111 { man_ign_pre
, NULL
}, /* PD */
112 { man_ign_pre
, NULL
}, /* AT */
113 { man_in_pre
, NULL
}, /* in */
114 { man_ign_pre
, NULL
}, /* ft */
119 html_man(void *arg
, const struct man
*m
)
125 h
= (struct html
*)arg
;
129 memset(&mh
, 0, sizeof(struct mhtml
));
131 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
132 print_man(man_meta(m
), man_node(m
), &mh
, h
);
144 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
145 print_man_head(m
, n
, mh
, h
);
148 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
149 print_man_nodelist(m
, n
, mh
, h
);
156 print_man_head(MAN_ARGS
)
161 buffmt(h
, "%s(%s)", m
->title
, m
->msec
);
163 print_otag(h
, TAG_TITLE
, 0, NULL
);
164 print_text(h
, h
->buf
);
169 print_man_nodelist(MAN_ARGS
)
172 print_man_node(m
, n
, mh
, h
);
174 print_man_nodelist(m
, n
->next
, mh
, h
);
179 print_man_node(MAN_ARGS
)
190 * FIXME: embedded elements within next-line scopes (e.g., `br'
191 * within an empty `B') will cause formatting to be forgotten
192 * due to scope closing out.
197 child
= man_root_pre(m
, n
, mh
, h
);
200 print_text(h
, n
->string
);
202 if (MANH_LITERAL
& mh
->fl
)
203 print_otag(h
, TAG_BR
, 0, NULL
);
208 * Close out scope of font prior to opening a macro
209 * scope. Assert that the metafont is on the top of the
210 * stack (it's never nested).
213 assert(h
->metaf
== t
);
214 print_tagq(h
, h
->metaf
);
215 assert(NULL
== h
->metaf
);
218 if (mans
[n
->tok
].pre
)
219 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
223 if (child
&& n
->child
)
224 print_man_nodelist(m
, n
->child
, mh
, h
);
226 /* This will automatically close out any font scope. */
233 man_root_post(m
, n
, mh
, h
);
238 if (mans
[n
->tok
].post
)
239 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
246 a2width(const struct man_node
*n
, struct roffsu
*su
)
249 if (MAN_TEXT
!= n
->type
)
251 if (a2roffsu(n
->string
, su
, SCALE_BU
))
260 man_root_pre(MAN_ARGS
)
262 struct htmlpair tag
[3];
264 char b
[BUFSIZ
], title
[BUFSIZ
];
268 (void)strlcat(b
, m
->vol
, BUFSIZ
);
270 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
272 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
273 PAIR_CLASS_INIT(&tag
[1], "head");
274 if (NULL
== h
->style
) {
275 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
276 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
277 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
278 print_otag(h
, TAG_COL
, 1, tag
);
279 print_otag(h
, TAG_COL
, 1, tag
);
280 print_otag(h
, TAG_COL
, 1, tag
);
282 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
284 print_otag(h
, TAG_TBODY
, 0, NULL
);
286 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
288 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
289 print_otag(h
, TAG_TD
, 1, tag
);
291 print_text(h
, title
);
294 PAIR_CLASS_INIT(&tag
[0], "head-vol");
295 if (NULL
== h
->style
) {
296 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
297 print_otag(h
, TAG_TD
, 2, tag
);
299 print_otag(h
, TAG_TD
, 1, tag
);
304 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
305 if (NULL
== h
->style
) {
306 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
307 print_otag(h
, TAG_TD
, 2, tag
);
309 print_otag(h
, TAG_TD
, 1, tag
);
311 print_text(h
, title
);
319 man_root_post(MAN_ARGS
)
321 struct htmlpair tag
[3];
326 strlcpy(b
, m
->rawdate
, DATESIZ
);
328 time2a(m
->date
, b
, DATESIZ
);
330 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
331 PAIR_CLASS_INIT(&tag
[1], "foot");
332 if (NULL
== h
->style
) {
333 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
334 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
335 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
336 print_otag(h
, TAG_COL
, 1, tag
);
337 print_otag(h
, TAG_COL
, 1, tag
);
339 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
341 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
343 PAIR_CLASS_INIT(&tag
[0], "foot-date");
344 print_otag(h
, TAG_TD
, 1, tag
);
349 PAIR_CLASS_INIT(&tag
[0], "foot-os");
350 if (NULL
== h
->style
) {
351 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
352 print_otag(h
, TAG_TD
, 2, tag
);
354 print_otag(h
, TAG_TD
, 1, tag
);
357 print_text(h
, m
->source
);
370 SCALE_VS_INIT(&su
, 1);
372 if (MAN_sp
== n
->tok
) {
374 a2roffsu(n
->child
->string
, &su
, SCALE_VS
);
378 bufcat_su(h
, "height", &su
);
379 PAIR_STYLE_INIT(&tag
, h
);
380 print_otag(h
, TAG_DIV
, 1, &tag
);
382 /* So the div isn't empty: */
383 print_text(h
, "\\~");
395 if (MAN_BLOCK
== n
->type
) {
396 PAIR_CLASS_INIT(&tag
, "section");
397 print_otag(h
, TAG_DIV
, 1, &tag
);
399 } else if (MAN_BODY
== n
->type
)
402 print_otag(h
, TAG_H1
, 0, NULL
);
409 man_alt_pre(MAN_ARGS
)
411 const struct man_node
*nn
;
416 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
419 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_BOLD
;
422 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_ITALIC
;
425 fp
= i
% 2 ? HTMLFONT_ITALIC
: HTMLFONT_NONE
;
428 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_ITALIC
;
431 fp
= i
% 2 ? HTMLFONT_NONE
: HTMLFONT_BOLD
;
434 fp
= i
% 2 ? HTMLFONT_BOLD
: HTMLFONT_NONE
;
442 h
->flags
|= HTML_NOSPACE
;
445 * Open and close the scope with each argument, so that
446 * internal \f escapes, which are common, are also
447 * closed out with the scope.
449 t
= print_ofont(h
, fp
);
450 print_man_node(m
, nn
, mh
, h
);
464 /* FIXME: print_ofont(). */
465 PAIR_CLASS_INIT(&tag
, "small");
466 print_otag(h
, TAG_SPAN
, 1, &tag
);
467 if (MAN_SB
== n
->tok
)
468 print_ofont(h
, HTMLFONT_BOLD
);
479 if (MAN_BLOCK
== n
->type
) {
480 PAIR_CLASS_INIT(&tag
, "subsection");
481 print_otag(h
, TAG_DIV
, 1, &tag
);
483 } else if (MAN_BODY
== n
->type
)
486 print_otag(h
, TAG_H2
, 0, NULL
);
496 if (MAN_HEAD
== n
->type
)
498 else if (MAN_BODY
== n
->type
&& n
->prev
)
499 print_otag(h
, TAG_P
, 0, NULL
);
511 const struct man_node
*nn
;
515 * This scattering of 1-BU margins and pads is to make sure that
516 * when text overruns its box, the subsequent text isn't flush
517 * up against it. However, the rest of the right-hand box must
518 * also be adjusted in consideration of this 1-BU space.
521 if (MAN_BODY
== n
->type
) {
522 print_otag(h
, TAG_TD
, 0, NULL
);
526 nn
= MAN_BLOCK
== n
->type
?
527 n
->head
->child
: n
->parent
->head
->child
;
529 SCALE_HS_INIT(&su
, INDENT
);
532 /* Width is the last token. */
534 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
535 if (NULL
!= (nn
= nn
->next
)) {
536 for ( ; nn
->next
; nn
= nn
->next
)
538 width
= a2width(nn
, &su
);
541 /* Width is the first token. */
543 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
544 /* Skip past non-text children. */
545 while (nn
&& MAN_TEXT
!= nn
->type
)
548 width
= a2width(nn
, &su
);
551 if (MAN_BLOCK
== n
->type
) {
552 print_otag(h
, TAG_P
, 0, NULL
);
553 print_otag(h
, TAG_TABLE
, 0, NULL
);
554 bufcat_su(h
, "width", &su
);
555 PAIR_STYLE_INIT(&tag
, h
);
556 print_otag(h
, TAG_COL
, 1, &tag
);
557 print_otag(h
, TAG_COL
, 0, NULL
);
558 print_otag(h
, TAG_TBODY
, 0, NULL
);
559 print_otag(h
, TAG_TR
, 0, NULL
);
563 print_otag(h
, TAG_TD
, 0, NULL
);
566 * Without a length string, we can print all of our children.
573 * When a length has been specified, we need to carefully print
574 * our child context: IP gets all children printed but the last
575 * (the width), while TP gets all children printed but the first
579 if (MAN_IP
== n
->tok
)
580 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
581 print_man_node(m
, nn
, mh
, h
);
582 if (MAN_TP
== n
->tok
)
583 for (nn
= n
->child
->next
; nn
; nn
= nn
->next
)
584 print_man_node(m
, nn
, mh
, h
);
596 const struct man_node
*np
;
598 np
= MAN_BLOCK
== n
->type
?
600 n
->parent
->head
->child
;
602 if (NULL
== np
|| ! a2width(np
, &su
))
603 SCALE_HS_INIT(&su
, INDENT
);
605 if (MAN_HEAD
== n
->type
) {
606 print_otag(h
, TAG_TD
, 0, NULL
);
608 } else if (MAN_BLOCK
== n
->type
) {
609 print_otag(h
, TAG_P
, 0, NULL
);
610 print_otag(h
, TAG_TABLE
, 0, NULL
);
611 bufcat_su(h
, "width", &su
);
612 PAIR_STYLE_INIT(&tag
, h
);
613 print_otag(h
, TAG_COL
, 1, &tag
);
614 print_otag(h
, TAG_COL
, 0, NULL
);
615 print_otag(h
, TAG_TBODY
, 0, NULL
);
616 print_otag(h
, TAG_TR
, 0, NULL
);
620 su
.scale
= -su
.scale
;
621 bufcat_su(h
, "text-indent", &su
);
622 PAIR_STYLE_INIT(&tag
, h
);
623 print_otag(h
, TAG_TD
, 1, &tag
);
633 print_ofont(h
, HTMLFONT_BOLD
);
643 print_ofont(h
, HTMLFONT_ITALIC
);
650 man_literal_pre(MAN_ARGS
)
653 if (MAN_nf
== n
->tok
) {
654 print_otag(h
, TAG_BR
, 0, NULL
);
655 mh
->fl
|= MANH_LITERAL
;
657 mh
->fl
&= ~MANH_LITERAL
;
668 print_otag(h
, TAG_BR
, 0, NULL
);
675 man_ign_pre(MAN_ARGS
)
689 if (MAN_HEAD
== n
->type
)
691 else if (MAN_BODY
== n
->type
)
694 SCALE_HS_INIT(&su
, INDENT
);
696 a2width(n
->head
->child
, &su
);
698 bufcat_su(h
, "margin-left", &su
);
699 PAIR_STYLE_INIT(&tag
, h
);
700 print_otag(h
, TAG_DIV
, 1, &tag
);