]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.80 2011/07/23 22:57:13 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
)
210 man_root_pre(m
, n
, mh
, h
);
214 * If we have a blank line, output a vertical space.
215 * If we have a space as the first character, break
216 * before printing the line's data.
218 if ('\0' == *n
->string
) {
219 print_otag(h
, TAG_P
, 0, NULL
);
223 if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
224 print_otag(h
, TAG_BR
, 0, NULL
);
225 else if (MANH_LITERAL
& mh
->fl
&& n
->prev
)
226 print_otag(h
, TAG_BR
, 0, NULL
);
228 print_text(h
, n
->string
);
231 print_eqn(h
, n
->eqn
);
235 * This will take care of initialising all of the table
236 * state data for the first table, then tearing it down
239 print_tbl(h
, n
->span
);
243 * Close out scope of font prior to opening a macro
246 if (HTMLFONT_NONE
!= h
->metac
) {
248 h
->metac
= HTMLFONT_NONE
;
252 * Close out the current table, if it's open, and unset
253 * the "meta" table state. This will be reopened on the
254 * next table element.
260 if (mans
[n
->tok
].pre
)
261 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
265 if (child
&& n
->child
)
266 print_man_nodelist(m
, n
->child
, mh
, h
);
268 /* This will automatically close out any font scope. */
273 man_root_post(m
, n
, mh
, h
);
278 if (mans
[n
->tok
].post
)
279 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
286 a2width(const struct man_node
*n
, struct roffsu
*su
)
289 if (MAN_TEXT
!= n
->type
)
291 if (a2roffsu(n
->string
, su
, SCALE_BU
))
300 man_root_pre(MAN_ARGS
)
302 struct htmlpair tag
[3];
304 char b
[BUFSIZ
], title
[BUFSIZ
];
308 (void)strlcat(b
, m
->vol
, BUFSIZ
);
310 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
312 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
313 PAIR_CLASS_INIT(&tag
[1], "head");
314 if (NULL
== h
->style
) {
315 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
316 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
317 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
318 print_otag(h
, TAG_COL
, 1, tag
);
319 print_otag(h
, TAG_COL
, 1, tag
);
320 print_otag(h
, TAG_COL
, 1, tag
);
322 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
324 print_otag(h
, TAG_TBODY
, 0, NULL
);
326 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
328 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
329 print_otag(h
, TAG_TD
, 1, tag
);
331 print_text(h
, title
);
334 PAIR_CLASS_INIT(&tag
[0], "head-vol");
335 if (NULL
== h
->style
) {
336 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
337 print_otag(h
, TAG_TD
, 2, tag
);
339 print_otag(h
, TAG_TD
, 1, tag
);
344 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
345 if (NULL
== h
->style
) {
346 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
347 print_otag(h
, TAG_TD
, 2, tag
);
349 print_otag(h
, TAG_TD
, 1, tag
);
351 print_text(h
, title
);
358 man_root_post(MAN_ARGS
)
360 struct htmlpair tag
[3];
363 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
364 PAIR_CLASS_INIT(&tag
[1], "foot");
365 if (NULL
== h
->style
) {
366 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
367 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
368 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
369 print_otag(h
, TAG_COL
, 1, tag
);
370 print_otag(h
, TAG_COL
, 1, tag
);
372 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
374 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
376 PAIR_CLASS_INIT(&tag
[0], "foot-date");
377 print_otag(h
, TAG_TD
, 1, tag
);
379 print_text(h
, m
->date
);
382 PAIR_CLASS_INIT(&tag
[0], "foot-os");
383 if (NULL
== h
->style
) {
384 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
385 print_otag(h
, TAG_TD
, 2, tag
);
387 print_otag(h
, TAG_TD
, 1, tag
);
390 print_text(h
, m
->source
);
402 SCALE_VS_INIT(&su
, 1);
404 if (MAN_sp
== n
->tok
) {
405 if (NULL
!= (n
= n
->child
))
406 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
407 SCALE_VS_INIT(&su
, atoi(n
->string
));
412 bufcat_su(h
, "height", &su
);
413 PAIR_STYLE_INIT(&tag
, h
);
414 print_otag(h
, TAG_DIV
, 1, &tag
);
416 /* So the div isn't empty: */
417 print_text(h
, "\\~");
428 if (MAN_BLOCK
== n
->type
) {
429 mh
->fl
&= ~MANH_LITERAL
;
430 PAIR_CLASS_INIT(&tag
, "section");
431 print_otag(h
, TAG_DIV
, 1, &tag
);
433 } else if (MAN_BODY
== n
->type
)
436 print_otag(h
, TAG_H1
, 0, NULL
);
442 man_alt_pre(MAN_ARGS
)
444 const struct man_node
*nn
;
449 if ((savelit
= mh
->fl
& MANH_LITERAL
))
450 print_otag(h
, TAG_BR
, 0, NULL
);
452 mh
->fl
&= ~MANH_LITERAL
;
454 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
458 fp
= i
% 2 ? TAG_I
: TAG_B
;
461 fp
= i
% 2 ? TAG_B
: TAG_I
;
464 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
467 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
470 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
473 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
481 h
->flags
|= HTML_NOSPACE
;
484 t
= print_otag(h
, fp
, 0, NULL
);
486 print_man_node(m
, nn
, mh
, h
);
493 mh
->fl
|= MANH_LITERAL
;
503 print_otag(h
, TAG_SMALL
, 0, NULL
);
504 if (MAN_SB
== n
->tok
)
505 print_otag(h
, TAG_B
, 0, NULL
);
515 if (MAN_BLOCK
== n
->type
) {
516 mh
->fl
&= ~MANH_LITERAL
;
517 PAIR_CLASS_INIT(&tag
, "subsection");
518 print_otag(h
, TAG_DIV
, 1, &tag
);
520 } else if (MAN_BODY
== n
->type
)
523 print_otag(h
, TAG_H2
, 0, NULL
);
532 if (MAN_HEAD
== n
->type
)
534 else if (MAN_BLOCK
== n
->type
)
544 const struct man_node
*nn
;
546 if (MAN_BODY
== n
->type
) {
547 print_otag(h
, TAG_DD
, 0, NULL
);
549 } else if (MAN_HEAD
!= n
->type
) {
550 print_otag(h
, TAG_DL
, 0, NULL
);
554 /* FIXME: width specification. */
556 print_otag(h
, TAG_DT
, 0, NULL
);
558 /* For IP, only print the first header element. */
560 if (MAN_IP
== n
->tok
&& n
->child
)
561 print_man_node(m
, n
->child
, mh
, h
);
563 /* For TP, only print next-line header elements. */
565 if (MAN_TP
== n
->tok
)
566 for (nn
= n
->child
; nn
; nn
= nn
->next
)
567 if (nn
->line
> n
->line
)
568 print_man_node(m
, nn
, mh
, h
);
579 const struct man_node
*np
;
581 if (MAN_HEAD
== n
->type
)
583 else if (MAN_BLOCK
!= n
->type
)
588 if (NULL
== np
|| ! a2width(np
, &su
))
589 SCALE_HS_INIT(&su
, INDENT
);
594 bufcat_su(h
, "margin-left", &su
);
595 su
.scale
= -su
.scale
;
596 bufcat_su(h
, "text-indent", &su
);
597 PAIR_STYLE_INIT(&tag
, h
);
598 print_otag(h
, TAG_P
, 1, &tag
);
607 print_otag(h
, TAG_B
, 0, NULL
);
616 print_otag(h
, TAG_I
, 0, NULL
);
622 man_literal_pre(MAN_ARGS
)
625 if (MAN_nf
!= n
->tok
) {
626 print_otag(h
, TAG_BR
, 0, NULL
);
627 mh
->fl
&= ~MANH_LITERAL
;
629 mh
->fl
|= MANH_LITERAL
;
639 print_otag(h
, TAG_BR
, 0, NULL
);
645 man_ign_pre(MAN_ARGS
)
658 if (MAN_HEAD
== n
->type
)
660 else if (MAN_BODY
== n
->type
)
663 SCALE_HS_INIT(&su
, INDENT
);
665 a2width(n
->head
->child
, &su
);
668 bufcat_su(h
, "margin-left", &su
);
669 PAIR_STYLE_INIT(&tag
, h
);
670 print_otag(h
, TAG_DIV
, 1, &tag
);