]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.76 2011/07/03 22:57:32 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 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_bvspace(struct html
*,
57 const struct man_node
*);
58 static void print_man(MAN_ARGS
);
59 static void print_man_head(MAN_ARGS
);
60 static void print_man_nodelist(MAN_ARGS
);
61 static void print_man_node(MAN_ARGS
);
63 static int a2width(const struct man_node
*,
66 static int man_alt_pre(MAN_ARGS
);
67 static int man_br_pre(MAN_ARGS
);
68 static int man_ign_pre(MAN_ARGS
);
69 static int man_in_pre(MAN_ARGS
);
70 static int man_literal_pre(MAN_ARGS
);
71 static void man_root_post(MAN_ARGS
);
72 static void man_root_pre(MAN_ARGS
);
73 static int man_B_pre(MAN_ARGS
);
74 static int man_HP_pre(MAN_ARGS
);
75 static int man_I_pre(MAN_ARGS
);
76 static int man_IP_pre(MAN_ARGS
);
77 static int man_PP_pre(MAN_ARGS
);
78 static int man_RS_pre(MAN_ARGS
);
79 static int man_SH_pre(MAN_ARGS
);
80 static int man_SM_pre(MAN_ARGS
);
81 static int man_SS_pre(MAN_ARGS
);
83 static const struct htmlman mans
[MAN_MAX
] = {
84 { man_br_pre
, NULL
}, /* br */
85 { NULL
, NULL
}, /* TH */
86 { man_SH_pre
, NULL
}, /* SH */
87 { man_SS_pre
, NULL
}, /* SS */
88 { man_IP_pre
, NULL
}, /* TP */
89 { man_PP_pre
, NULL
}, /* LP */
90 { man_PP_pre
, NULL
}, /* PP */
91 { man_PP_pre
, NULL
}, /* P */
92 { man_IP_pre
, NULL
}, /* IP */
93 { man_HP_pre
, NULL
}, /* HP */
94 { man_SM_pre
, NULL
}, /* SM */
95 { man_SM_pre
, NULL
}, /* SB */
96 { man_alt_pre
, NULL
}, /* BI */
97 { man_alt_pre
, NULL
}, /* IB */
98 { man_alt_pre
, NULL
}, /* BR */
99 { man_alt_pre
, NULL
}, /* RB */
100 { NULL
, NULL
}, /* R */
101 { man_B_pre
, NULL
}, /* B */
102 { man_I_pre
, NULL
}, /* I */
103 { man_alt_pre
, NULL
}, /* IR */
104 { man_alt_pre
, NULL
}, /* RI */
105 { man_ign_pre
, NULL
}, /* na */
106 { man_br_pre
, NULL
}, /* sp */
107 { man_literal_pre
, NULL
}, /* nf */
108 { man_literal_pre
, NULL
}, /* fi */
109 { NULL
, NULL
}, /* RE */
110 { man_RS_pre
, NULL
}, /* RS */
111 { man_ign_pre
, NULL
}, /* DT */
112 { man_ign_pre
, NULL
}, /* UC */
113 { man_ign_pre
, NULL
}, /* PD */
114 { man_ign_pre
, NULL
}, /* AT */
115 { man_in_pre
, NULL
}, /* in */
116 { man_ign_pre
, NULL
}, /* ft */
120 * Printing leading vertical space before a block.
121 * This is used for the paragraph macros.
122 * The rules are pretty simple, since there's very little nesting going
123 * on here. Basically, if we're the first within another block (SS/SH),
124 * then don't emit vertical space. If we are (RS), then do. If not the
128 print_bvspace(struct html
*h
, const struct man_node
*n
)
131 if (n
->body
&& n
->body
->child
)
132 if (MAN_TBL
== n
->body
->child
->type
)
135 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
139 print_otag(h
, TAG_P
, 0, NULL
);
143 html_man(void *arg
, const struct man
*m
)
149 h
= (struct html
*)arg
;
153 memset(&mh
, 0, sizeof(struct mhtml
));
155 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
156 print_man(man_meta(m
), man_node(m
), &mh
, h
);
167 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
168 print_man_head(m
, n
, mh
, h
);
171 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
172 print_man_nodelist(m
, n
, mh
, h
);
179 print_man_head(MAN_ARGS
)
183 bufcat_fmt(h
, "%s(%s)", m
->title
, m
->msec
);
184 print_otag(h
, TAG_TITLE
, 0, NULL
);
185 print_text(h
, h
->buf
);
190 print_man_nodelist(MAN_ARGS
)
193 print_man_node(m
, n
, mh
, h
);
195 print_man_nodelist(m
, n
->next
, mh
, h
);
200 print_man_node(MAN_ARGS
)
211 man_root_pre(m
, n
, mh
, h
);
215 * If we have a blank line, output a vertical space.
216 * If we have a space as the first character, break
217 * before printing the line's data.
219 if ('\0' == *n
->string
) {
220 print_otag(h
, TAG_P
, 0, NULL
);
222 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
223 print_otag(h
, TAG_BR
, 0, NULL
);
225 print_text(h
, n
->string
);
228 * If we're in a literal context, make sure that words
229 * togehter on the same line stay together. This is a
230 * POST-printing call, so we check the NEXT word. Since
231 * -man doesn't have nested macros, we don't need to be
232 * more specific than this.
234 if (MANH_LITERAL
& mh
->fl
&&
236 n
->next
->line
> n
->line
))
237 print_otag(h
, TAG_BR
, 0, NULL
);
240 PAIR_CLASS_INIT(&tag
, "eqn");
241 print_otag(h
, TAG_SPAN
, 1, &tag
);
242 print_text(h
, n
->eqn
->data
);
246 * This will take care of initialising all of the table
247 * state data for the first table, then tearing it down
250 print_tbl(h
, n
->span
);
254 * Close out scope of font prior to opening a macro
257 if (HTMLFONT_NONE
!= h
->metac
) {
259 h
->metac
= HTMLFONT_NONE
;
263 * Close out the current table, if it's open, and unset
264 * the "meta" table state. This will be reopened on the
265 * next table element.
271 if (mans
[n
->tok
].pre
)
272 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
276 if (child
&& n
->child
)
277 print_man_nodelist(m
, n
->child
, mh
, h
);
279 /* This will automatically close out any font scope. */
284 man_root_post(m
, n
, mh
, h
);
289 if (mans
[n
->tok
].post
)
290 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
297 a2width(const struct man_node
*n
, struct roffsu
*su
)
300 if (MAN_TEXT
!= n
->type
)
302 if (a2roffsu(n
->string
, su
, SCALE_BU
))
311 man_root_pre(MAN_ARGS
)
313 struct htmlpair tag
[3];
315 char b
[BUFSIZ
], title
[BUFSIZ
];
319 (void)strlcat(b
, m
->vol
, BUFSIZ
);
321 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
323 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
324 PAIR_CLASS_INIT(&tag
[1], "head");
325 if (NULL
== h
->style
) {
326 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
327 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
328 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
329 print_otag(h
, TAG_COL
, 1, tag
);
330 print_otag(h
, TAG_COL
, 1, tag
);
331 print_otag(h
, TAG_COL
, 1, tag
);
333 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
335 print_otag(h
, TAG_TBODY
, 0, NULL
);
337 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
339 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
340 print_otag(h
, TAG_TD
, 1, tag
);
342 print_text(h
, title
);
345 PAIR_CLASS_INIT(&tag
[0], "head-vol");
346 if (NULL
== h
->style
) {
347 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
348 print_otag(h
, TAG_TD
, 2, tag
);
350 print_otag(h
, TAG_TD
, 1, tag
);
355 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
356 if (NULL
== h
->style
) {
357 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
358 print_otag(h
, TAG_TD
, 2, tag
);
360 print_otag(h
, TAG_TD
, 1, tag
);
362 print_text(h
, title
);
369 man_root_post(MAN_ARGS
)
371 struct htmlpair tag
[3];
374 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
375 PAIR_CLASS_INIT(&tag
[1], "foot");
376 if (NULL
== h
->style
) {
377 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
378 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
379 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
380 print_otag(h
, TAG_COL
, 1, tag
);
381 print_otag(h
, TAG_COL
, 1, tag
);
383 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
385 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
387 PAIR_CLASS_INIT(&tag
[0], "foot-date");
388 print_otag(h
, TAG_TD
, 1, tag
);
390 print_text(h
, m
->date
);
393 PAIR_CLASS_INIT(&tag
[0], "foot-os");
394 if (NULL
== h
->style
) {
395 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
396 print_otag(h
, TAG_TD
, 2, tag
);
398 print_otag(h
, TAG_TD
, 1, tag
);
401 print_text(h
, m
->source
);
414 SCALE_VS_INIT(&su
, 1);
416 if (MAN_sp
== n
->tok
) {
417 if (NULL
!= (n
= n
->child
))
418 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
419 SCALE_VS_INIT(&su
, atoi(n
->string
));
424 bufcat_su(h
, "height", &su
);
425 PAIR_STYLE_INIT(&tag
, h
);
426 print_otag(h
, TAG_DIV
, 1, &tag
);
428 /* So the div isn't empty: */
429 print_text(h
, "\\~");
441 if (MAN_BLOCK
== n
->type
) {
442 mh
->fl
&= ~MANH_LITERAL
;
443 PAIR_CLASS_INIT(&tag
, "section");
444 print_otag(h
, TAG_DIV
, 1, &tag
);
446 } else if (MAN_BODY
== n
->type
)
449 print_otag(h
, TAG_H1
, 0, NULL
);
456 man_alt_pre(MAN_ARGS
)
458 const struct man_node
*nn
;
463 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
467 fp
= i
% 2 ? TAG_I
: TAG_B
;
470 fp
= i
% 2 ? TAG_B
: TAG_I
;
473 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
476 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
479 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
482 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
490 h
->flags
|= HTML_NOSPACE
;
493 t
= print_otag(h
, fp
, 0, NULL
);
495 print_man_node(m
, nn
, mh
, h
);
510 print_otag(h
, TAG_SMALL
, 0, NULL
);
511 if (MAN_SB
== n
->tok
)
512 print_otag(h
, TAG_B
, 0, NULL
);
523 if (MAN_BLOCK
== n
->type
) {
524 mh
->fl
&= ~MANH_LITERAL
;
525 PAIR_CLASS_INIT(&tag
, "subsection");
526 print_otag(h
, TAG_DIV
, 1, &tag
);
528 } else if (MAN_BODY
== n
->type
)
531 print_otag(h
, TAG_H2
, 0, NULL
);
541 if (MAN_HEAD
== n
->type
)
543 else if (MAN_BLOCK
== n
->type
)
556 const struct man_node
*nn
;
559 * This scattering of 1-BU margins and pads is to make sure that
560 * when text overruns its box, the subsequent text isn't flush
561 * up against it. However, the rest of the right-hand box must
562 * also be adjusted in consideration of this 1-BU space.
565 if (MAN_BODY
== n
->type
) {
566 print_otag(h
, TAG_TD
, 0, NULL
);
570 nn
= MAN_BLOCK
== n
->type
?
571 n
->head
->child
: n
->parent
->head
->child
;
573 SCALE_HS_INIT(&su
, INDENT
);
575 /* Width is the second token. */
577 if (MAN_IP
== n
->tok
&& NULL
!= nn
)
578 if (NULL
!= (nn
= nn
->next
))
581 /* Width is the first token. */
583 if (MAN_TP
== n
->tok
&& NULL
!= nn
) {
584 /* Skip past non-text children. */
585 while (nn
&& MAN_TEXT
!= nn
->type
)
591 if (MAN_BLOCK
== n
->type
) {
593 print_otag(h
, TAG_TABLE
, 0, NULL
);
595 bufcat_su(h
, "width", &su
);
596 PAIR_STYLE_INIT(&tag
, h
);
597 print_otag(h
, TAG_COL
, 1, &tag
);
598 print_otag(h
, TAG_COL
, 0, NULL
);
599 print_otag(h
, TAG_TBODY
, 0, NULL
);
600 print_otag(h
, TAG_TR
, 0, NULL
);
604 print_otag(h
, TAG_TD
, 0, NULL
);
606 /* For IP, only print the first header element. */
608 if (MAN_IP
== n
->tok
&& n
->child
)
609 print_man_node(m
, n
->child
, mh
, h
);
611 /* For TP, only print next-line header elements. */
613 if (MAN_TP
== n
->tok
)
614 for (nn
= n
->child
; nn
; nn
= nn
->next
)
615 if (nn
->line
> n
->line
)
616 print_man_node(m
, nn
, mh
, h
);
628 const struct man_node
*np
;
632 np
= MAN_BLOCK
== n
->type
?
634 n
->parent
->head
->child
;
636 if (NULL
== np
|| ! a2width(np
, &su
))
637 SCALE_HS_INIT(&su
, INDENT
);
639 if (MAN_HEAD
== n
->type
) {
640 print_otag(h
, TAG_TD
, 0, NULL
);
642 } else if (MAN_BLOCK
== n
->type
) {
644 print_otag(h
, TAG_TABLE
, 0, NULL
);
645 bufcat_su(h
, "width", &su
);
646 PAIR_STYLE_INIT(&tag
, h
);
647 print_otag(h
, TAG_COL
, 1, &tag
);
648 print_otag(h
, TAG_COL
, 0, NULL
);
649 print_otag(h
, TAG_TBODY
, 0, NULL
);
650 print_otag(h
, TAG_TR
, 0, NULL
);
654 su
.scale
= -su
.scale
;
655 bufcat_su(h
, "text-indent", &su
);
656 PAIR_STYLE_INIT(&tag
, h
);
657 print_otag(h
, TAG_TD
, 1, &tag
);
667 print_otag(h
, TAG_B
, 0, NULL
);
677 print_otag(h
, TAG_I
, 0, NULL
);
684 man_literal_pre(MAN_ARGS
)
687 if (MAN_nf
== n
->tok
) {
688 print_otag(h
, TAG_BR
, 0, NULL
);
689 mh
->fl
|= MANH_LITERAL
;
691 mh
->fl
&= ~MANH_LITERAL
;
702 print_otag(h
, TAG_BR
, 0, NULL
);
709 man_ign_pre(MAN_ARGS
)
723 if (MAN_HEAD
== n
->type
)
725 else if (MAN_BODY
== n
->type
)
728 SCALE_HS_INIT(&su
, INDENT
);
730 a2width(n
->head
->child
, &su
);
733 bufcat_su(h
, "margin-left", &su
);
734 PAIR_STYLE_INIT(&tag
, h
);
735 print_otag(h
, TAG_DIV
, 1, &tag
);