]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.89 2012/11/17 00:26:33 schwarze Exp $ */
3 * Copyright (c) 2008-2012 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. */
40 #define MAN_ARGS const struct man_meta *man, \
41 const struct man_node *n, \
47 #define MANH_LITERAL (1 << 0) /* literal context */
52 int (*post
)(MAN_ARGS
);
55 static void print_bvspace(struct html
*,
56 const struct man_node
*);
57 static void print_man(MAN_ARGS
);
58 static void print_man_head(MAN_ARGS
);
59 static void print_man_nodelist(MAN_ARGS
);
60 static void print_man_node(MAN_ARGS
);
61 static int a2width(const struct man_node
*,
63 static int man_B_pre(MAN_ARGS
);
64 static int man_HP_pre(MAN_ARGS
);
65 static int man_IP_pre(MAN_ARGS
);
66 static int man_I_pre(MAN_ARGS
);
67 static int man_OP_pre(MAN_ARGS
);
68 static int man_PP_pre(MAN_ARGS
);
69 static int man_RS_pre(MAN_ARGS
);
70 static int man_SH_pre(MAN_ARGS
);
71 static int man_SM_pre(MAN_ARGS
);
72 static int man_SS_pre(MAN_ARGS
);
73 static int man_alt_pre(MAN_ARGS
);
74 static int man_br_pre(MAN_ARGS
);
75 static int man_ign_pre(MAN_ARGS
);
76 static int man_in_pre(MAN_ARGS
);
77 static int man_literal_pre(MAN_ARGS
);
78 static void man_root_post(MAN_ARGS
);
79 static void man_root_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 { man_ign_pre
, 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 */
115 { man_OP_pre
, NULL
}, /* OP */
116 { man_literal_pre
, NULL
}, /* EX */
117 { man_literal_pre
, NULL
}, /* EE */
121 * Printing leading vertical space before a block.
122 * This is used for the paragraph macros.
123 * The rules are pretty simple, since there's very little nesting going
124 * on here. Basically, if we're the first within another block (SS/SH),
125 * then don't emit vertical space. If we are (RS), then do. If not the
129 print_bvspace(struct html
*h
, const struct man_node
*n
)
132 if (n
->body
&& n
->body
->child
)
133 if (MAN_TBL
== n
->body
->child
->type
)
136 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
140 print_otag(h
, TAG_P
, 0, NULL
);
144 html_man(void *arg
, const struct man
*man
)
148 memset(&mh
, 0, sizeof(struct mhtml
));
149 print_man(man_meta(man
), man_node(man
), &mh
, (struct html
*)arg
);
159 PAIR_CLASS_INIT(&tag
, "mandoc");
161 if ( ! (HTML_FRAGMENT
& h
->oflags
)) {
163 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
164 tt
= print_otag(h
, TAG_HEAD
, 0, NULL
);
165 print_man_head(man
, n
, mh
, h
);
167 print_otag(h
, TAG_BODY
, 0, NULL
);
168 print_otag(h
, TAG_DIV
, 1, &tag
);
170 t
= print_otag(h
, TAG_DIV
, 1, &tag
);
172 print_man_nodelist(man
, n
, mh
, h
);
179 print_man_head(MAN_ARGS
)
185 bufcat_fmt(h
, "%s(%s)", man
->title
, man
->msec
);
186 print_otag(h
, TAG_TITLE
, 0, NULL
);
187 print_text(h
, h
->buf
);
192 print_man_nodelist(MAN_ARGS
)
195 print_man_node(man
, n
, mh
, h
);
197 print_man_nodelist(man
, n
->next
, mh
, h
);
202 print_man_node(MAN_ARGS
)
212 man_root_pre(man
, n
, mh
, h
);
216 * If we have a blank line, output a vertical space.
217 * If we have a space as the first character, break
218 * before printing the line's data.
220 if ('\0' == *n
->string
) {
221 print_otag(h
, TAG_P
, 0, NULL
);
225 if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
226 print_otag(h
, TAG_BR
, 0, NULL
);
227 else if (MANH_LITERAL
& mh
->fl
&& n
->prev
)
228 print_otag(h
, TAG_BR
, 0, NULL
);
230 print_text(h
, n
->string
);
233 print_eqn(h
, n
->eqn
);
237 * This will take care of initialising all of the table
238 * state data for the first table, then tearing it down
241 print_tbl(h
, n
->span
);
245 * Close out scope of font prior to opening a macro
248 if (HTMLFONT_NONE
!= h
->metac
) {
250 h
->metac
= HTMLFONT_NONE
;
254 * Close out the current table, if it's open, and unset
255 * the "meta" table state. This will be reopened on the
256 * next table element.
262 if (mans
[n
->tok
].pre
)
263 child
= (*mans
[n
->tok
].pre
)(man
, n
, mh
, h
);
267 if (child
&& n
->child
)
268 print_man_nodelist(man
, n
->child
, mh
, h
);
270 /* This will automatically close out any font scope. */
275 man_root_post(man
, n
, mh
, h
);
280 if (mans
[n
->tok
].post
)
281 (*mans
[n
->tok
].post
)(man
, n
, mh
, h
);
288 a2width(const struct man_node
*n
, struct roffsu
*su
)
291 if (MAN_TEXT
!= n
->type
)
293 if (a2roffsu(n
->string
, su
, SCALE_BU
))
302 man_root_pre(MAN_ARGS
)
304 struct htmlpair tag
[3];
306 char b
[BUFSIZ
], title
[BUFSIZ
];
310 (void)strlcat(b
, man
->vol
, BUFSIZ
);
314 snprintf(title
, BUFSIZ
- 1, "%s(%s)", man
->title
, man
->msec
);
316 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
317 PAIR_CLASS_INIT(&tag
[1], "head");
318 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
319 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
320 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
321 print_otag(h
, TAG_COL
, 1, tag
);
322 print_otag(h
, TAG_COL
, 1, tag
);
323 print_otag(h
, TAG_COL
, 1, tag
);
325 print_otag(h
, TAG_TBODY
, 0, NULL
);
327 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
329 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
330 print_otag(h
, TAG_TD
, 1, tag
);
331 print_text(h
, title
);
334 PAIR_CLASS_INIT(&tag
[0], "head-vol");
335 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
336 print_otag(h
, TAG_TD
, 2, tag
);
340 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
341 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
342 print_otag(h
, TAG_TD
, 2, tag
);
343 print_text(h
, title
);
350 man_root_post(MAN_ARGS
)
352 struct htmlpair tag
[3];
355 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
356 PAIR_CLASS_INIT(&tag
[1], "foot");
357 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
358 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
359 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
360 print_otag(h
, TAG_COL
, 1, tag
);
361 print_otag(h
, TAG_COL
, 1, tag
);
363 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
365 PAIR_CLASS_INIT(&tag
[0], "foot-date");
366 print_otag(h
, TAG_TD
, 1, tag
);
369 print_text(h
, man
->date
);
372 PAIR_CLASS_INIT(&tag
[0], "foot-os");
373 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
374 print_otag(h
, TAG_TD
, 2, tag
);
377 print_text(h
, man
->source
);
389 SCALE_VS_INIT(&su
, 1);
391 if (MAN_sp
== n
->tok
) {
392 if (NULL
!= (n
= n
->child
))
393 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
394 SCALE_VS_INIT(&su
, atoi(n
->string
));
399 bufcat_su(h
, "height", &su
);
400 PAIR_STYLE_INIT(&tag
, h
);
401 print_otag(h
, TAG_DIV
, 1, &tag
);
403 /* So the div isn't empty: */
404 print_text(h
, "\\~");
415 if (MAN_BLOCK
== n
->type
) {
416 mh
->fl
&= ~MANH_LITERAL
;
417 PAIR_CLASS_INIT(&tag
, "section");
418 print_otag(h
, TAG_DIV
, 1, &tag
);
420 } else if (MAN_BODY
== n
->type
)
423 print_otag(h
, TAG_H1
, 0, NULL
);
429 man_alt_pre(MAN_ARGS
)
431 const struct man_node
*nn
;
436 if ((savelit
= mh
->fl
& MANH_LITERAL
))
437 print_otag(h
, TAG_BR
, 0, NULL
);
439 mh
->fl
&= ~MANH_LITERAL
;
441 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
445 fp
= i
% 2 ? TAG_I
: TAG_B
;
448 fp
= i
% 2 ? TAG_B
: TAG_I
;
451 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
454 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
457 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
460 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
468 h
->flags
|= HTML_NOSPACE
;
471 t
= print_otag(h
, fp
, 0, NULL
);
473 print_man_node(man
, nn
, mh
, h
);
480 mh
->fl
|= MANH_LITERAL
;
490 print_otag(h
, TAG_SMALL
, 0, NULL
);
491 if (MAN_SB
== n
->tok
)
492 print_otag(h
, TAG_B
, 0, NULL
);
502 if (MAN_BLOCK
== n
->type
) {
503 mh
->fl
&= ~MANH_LITERAL
;
504 PAIR_CLASS_INIT(&tag
, "subsection");
505 print_otag(h
, TAG_DIV
, 1, &tag
);
507 } else if (MAN_BODY
== n
->type
)
510 print_otag(h
, TAG_H2
, 0, NULL
);
519 if (MAN_HEAD
== n
->type
)
521 else if (MAN_BLOCK
== n
->type
)
531 const struct man_node
*nn
;
533 if (MAN_BODY
== n
->type
) {
534 print_otag(h
, TAG_DD
, 0, NULL
);
536 } else if (MAN_HEAD
!= n
->type
) {
537 print_otag(h
, TAG_DL
, 0, NULL
);
541 /* FIXME: width specification. */
543 print_otag(h
, TAG_DT
, 0, NULL
);
545 /* For IP, only print the first header element. */
547 if (MAN_IP
== n
->tok
&& n
->child
)
548 print_man_node(man
, n
->child
, mh
, h
);
550 /* For TP, only print next-line header elements. */
552 if (MAN_TP
== n
->tok
)
553 for (nn
= n
->child
; nn
; nn
= nn
->next
)
554 if (nn
->line
> n
->line
)
555 print_man_node(man
, nn
, mh
, h
);
566 const struct man_node
*np
;
568 if (MAN_HEAD
== n
->type
)
570 else if (MAN_BLOCK
!= n
->type
)
575 if (NULL
== np
|| ! a2width(np
, &su
))
576 SCALE_HS_INIT(&su
, INDENT
);
581 bufcat_su(h
, "margin-left", &su
);
582 su
.scale
= -su
.scale
;
583 bufcat_su(h
, "text-indent", &su
);
584 PAIR_STYLE_INIT(&tag
, h
);
585 print_otag(h
, TAG_P
, 1, &tag
);
597 h
->flags
|= HTML_NOSPACE
;
598 PAIR_CLASS_INIT(&tag
, "opt");
599 tt
= print_otag(h
, TAG_SPAN
, 1, &tag
);
601 if (NULL
!= (n
= n
->child
)) {
602 print_otag(h
, TAG_B
, 0, NULL
);
603 print_text(h
, n
->string
);
608 if (NULL
!= n
&& NULL
!= n
->next
) {
609 print_otag(h
, TAG_I
, 0, NULL
);
610 print_text(h
, n
->next
->string
);
614 h
->flags
|= HTML_NOSPACE
;
625 print_otag(h
, TAG_B
, 0, NULL
);
634 print_otag(h
, TAG_I
, 0, NULL
);
640 man_literal_pre(MAN_ARGS
)
643 if (MAN_fi
== n
->tok
|| MAN_EE
== n
->tok
) {
644 print_otag(h
, TAG_BR
, 0, NULL
);
645 mh
->fl
&= ~MANH_LITERAL
;
647 mh
->fl
|= MANH_LITERAL
;
657 print_otag(h
, TAG_BR
, 0, NULL
);
663 man_ign_pre(MAN_ARGS
)
676 if (MAN_HEAD
== n
->type
)
678 else if (MAN_BODY
== n
->type
)
681 SCALE_HS_INIT(&su
, INDENT
);
683 a2width(n
->head
->child
, &su
);
686 bufcat_su(h
, "margin-left", &su
);
687 PAIR_STYLE_INIT(&tag
, h
);
688 print_otag(h
, TAG_DIV
, 1, &tag
);