]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.83 2011/10/09 22:10:53 schwarze 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. */
40 #define MAN_ARGS const struct man_meta *m, \
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
);
62 static int a2width(const struct man_node
*,
65 static int man_alt_pre(MAN_ARGS
);
66 static int man_br_pre(MAN_ARGS
);
67 static int man_ign_pre(MAN_ARGS
);
68 static int man_in_pre(MAN_ARGS
);
69 static int man_literal_pre(MAN_ARGS
);
70 static void man_root_post(MAN_ARGS
);
71 static void man_root_pre(MAN_ARGS
);
72 static int man_B_pre(MAN_ARGS
);
73 static int man_HP_pre(MAN_ARGS
);
74 static int man_I_pre(MAN_ARGS
);
75 static int man_IP_pre(MAN_ARGS
);
76 static int man_PP_pre(MAN_ARGS
);
77 static int man_RS_pre(MAN_ARGS
);
78 static int man_SH_pre(MAN_ARGS
);
79 static int man_SM_pre(MAN_ARGS
);
80 static int man_SS_pre(MAN_ARGS
);
82 static const struct htmlman mans
[MAN_MAX
] = {
83 { man_br_pre
, NULL
}, /* br */
84 { NULL
, NULL
}, /* TH */
85 { man_SH_pre
, NULL
}, /* SH */
86 { man_SS_pre
, NULL
}, /* SS */
87 { man_IP_pre
, NULL
}, /* TP */
88 { man_PP_pre
, NULL
}, /* LP */
89 { man_PP_pre
, NULL
}, /* PP */
90 { man_PP_pre
, NULL
}, /* P */
91 { man_IP_pre
, NULL
}, /* IP */
92 { man_HP_pre
, NULL
}, /* HP */
93 { man_SM_pre
, NULL
}, /* SM */
94 { man_SM_pre
, NULL
}, /* SB */
95 { man_alt_pre
, NULL
}, /* BI */
96 { man_alt_pre
, NULL
}, /* IB */
97 { man_alt_pre
, NULL
}, /* BR */
98 { man_alt_pre
, NULL
}, /* RB */
99 { NULL
, NULL
}, /* R */
100 { man_B_pre
, NULL
}, /* B */
101 { man_I_pre
, NULL
}, /* I */
102 { man_alt_pre
, NULL
}, /* IR */
103 { man_alt_pre
, NULL
}, /* RI */
104 { man_ign_pre
, NULL
}, /* na */
105 { man_br_pre
, NULL
}, /* sp */
106 { man_literal_pre
, NULL
}, /* nf */
107 { man_literal_pre
, NULL
}, /* fi */
108 { NULL
, NULL
}, /* RE */
109 { man_RS_pre
, NULL
}, /* RS */
110 { man_ign_pre
, NULL
}, /* DT */
111 { man_ign_pre
, NULL
}, /* UC */
112 { man_ign_pre
, NULL
}, /* PD */
113 { man_ign_pre
, NULL
}, /* AT */
114 { man_in_pre
, NULL
}, /* in */
115 { man_ign_pre
, NULL
}, /* ft */
119 * Printing leading vertical space before a block.
120 * This is used for the paragraph macros.
121 * The rules are pretty simple, since there's very little nesting going
122 * on here. Basically, if we're the first within another block (SS/SH),
123 * then don't emit vertical space. If we are (RS), then do. If not the
127 print_bvspace(struct html
*h
, const struct man_node
*n
)
130 if (n
->body
&& n
->body
->child
)
131 if (MAN_TBL
== n
->body
->child
->type
)
134 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
138 print_otag(h
, TAG_P
, 0, NULL
);
142 html_man(void *arg
, const struct man
*m
)
146 memset(&mh
, 0, sizeof(struct mhtml
));
147 print_man(man_meta(m
), man_node(m
), &mh
, (struct html
*)arg
);
157 PAIR_CLASS_INIT(&tag
, "mandoc");
159 if ( ! (HTML_FRAGMENT
& h
->oflags
)) {
161 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
162 tt
= print_otag(h
, TAG_HEAD
, 0, NULL
);
163 print_man_head(m
, n
, mh
, h
);
165 print_otag(h
, TAG_BODY
, 0, NULL
);
166 print_otag(h
, TAG_DIV
, 1, &tag
);
168 t
= print_otag(h
, TAG_DIV
, 1, &tag
);
170 print_man_nodelist(m
, n
, mh
, h
);
177 print_man_head(MAN_ARGS
)
181 bufcat_fmt(h
, "%s(%s)", m
->title
, m
->msec
);
182 print_otag(h
, TAG_TITLE
, 0, NULL
);
183 print_text(h
, h
->buf
);
188 print_man_nodelist(MAN_ARGS
)
191 print_man_node(m
, n
, mh
, h
);
193 print_man_nodelist(m
, n
->next
, mh
, h
);
198 print_man_node(MAN_ARGS
)
208 man_root_pre(m
, n
, mh
, h
);
212 * If we have a blank line, output a vertical space.
213 * If we have a space as the first character, break
214 * before printing the line's data.
216 if ('\0' == *n
->string
) {
217 print_otag(h
, TAG_P
, 0, NULL
);
221 if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
222 print_otag(h
, TAG_BR
, 0, NULL
);
223 else if (MANH_LITERAL
& mh
->fl
&& n
->prev
)
224 print_otag(h
, TAG_BR
, 0, NULL
);
226 print_text(h
, n
->string
);
229 print_eqn(h
, n
->eqn
);
233 * This will take care of initialising all of the table
234 * state data for the first table, then tearing it down
237 print_tbl(h
, n
->span
);
241 * Close out scope of font prior to opening a macro
244 if (HTMLFONT_NONE
!= h
->metac
) {
246 h
->metac
= HTMLFONT_NONE
;
250 * Close out the current table, if it's open, and unset
251 * the "meta" table state. This will be reopened on the
252 * next table element.
258 if (mans
[n
->tok
].pre
)
259 child
= (*mans
[n
->tok
].pre
)(m
, n
, mh
, h
);
263 if (child
&& n
->child
)
264 print_man_nodelist(m
, n
->child
, mh
, h
);
266 /* This will automatically close out any font scope. */
271 man_root_post(m
, n
, mh
, h
);
276 if (mans
[n
->tok
].post
)
277 (*mans
[n
->tok
].post
)(m
, n
, mh
, h
);
284 a2width(const struct man_node
*n
, struct roffsu
*su
)
287 if (MAN_TEXT
!= n
->type
)
289 if (a2roffsu(n
->string
, su
, SCALE_BU
))
298 man_root_pre(MAN_ARGS
)
300 struct htmlpair tag
[3];
302 char b
[BUFSIZ
], title
[BUFSIZ
];
306 (void)strlcat(b
, m
->vol
, BUFSIZ
);
308 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
310 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
311 PAIR_CLASS_INIT(&tag
[1], "head");
312 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
313 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
314 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
315 print_otag(h
, TAG_COL
, 1, tag
);
316 print_otag(h
, TAG_COL
, 1, tag
);
317 print_otag(h
, TAG_COL
, 1, tag
);
319 print_otag(h
, TAG_TBODY
, 0, NULL
);
321 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
323 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
324 print_otag(h
, TAG_TD
, 1, tag
);
325 print_text(h
, title
);
328 PAIR_CLASS_INIT(&tag
[0], "head-vol");
329 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
330 print_otag(h
, TAG_TD
, 2, tag
);
334 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
335 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
336 print_otag(h
, TAG_TD
, 2, tag
);
337 print_text(h
, title
);
344 man_root_post(MAN_ARGS
)
346 struct htmlpair tag
[3];
349 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
350 PAIR_CLASS_INIT(&tag
[1], "foot");
351 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
352 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
353 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
354 print_otag(h
, TAG_COL
, 1, tag
);
355 print_otag(h
, TAG_COL
, 1, tag
);
357 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
359 PAIR_CLASS_INIT(&tag
[0], "foot-date");
360 print_otag(h
, TAG_TD
, 1, tag
);
362 print_text(h
, m
->date
);
365 PAIR_CLASS_INIT(&tag
[0], "foot-os");
366 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
367 print_otag(h
, TAG_TD
, 2, tag
);
370 print_text(h
, m
->source
);
382 SCALE_VS_INIT(&su
, 1);
384 if (MAN_sp
== n
->tok
) {
385 if (NULL
!= (n
= n
->child
))
386 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
387 SCALE_VS_INIT(&su
, atoi(n
->string
));
392 bufcat_su(h
, "height", &su
);
393 PAIR_STYLE_INIT(&tag
, h
);
394 print_otag(h
, TAG_DIV
, 1, &tag
);
396 /* So the div isn't empty: */
397 print_text(h
, "\\~");
408 if (MAN_BLOCK
== n
->type
) {
409 mh
->fl
&= ~MANH_LITERAL
;
410 PAIR_CLASS_INIT(&tag
, "section");
411 print_otag(h
, TAG_DIV
, 1, &tag
);
413 } else if (MAN_BODY
== n
->type
)
416 print_otag(h
, TAG_H1
, 0, NULL
);
422 man_alt_pre(MAN_ARGS
)
424 const struct man_node
*nn
;
429 if ((savelit
= mh
->fl
& MANH_LITERAL
))
430 print_otag(h
, TAG_BR
, 0, NULL
);
432 mh
->fl
&= ~MANH_LITERAL
;
434 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
438 fp
= i
% 2 ? TAG_I
: TAG_B
;
441 fp
= i
% 2 ? TAG_B
: TAG_I
;
444 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
447 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
450 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
453 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
461 h
->flags
|= HTML_NOSPACE
;
464 t
= print_otag(h
, fp
, 0, NULL
);
466 print_man_node(m
, nn
, mh
, h
);
473 mh
->fl
|= MANH_LITERAL
;
483 print_otag(h
, TAG_SMALL
, 0, NULL
);
484 if (MAN_SB
== n
->tok
)
485 print_otag(h
, TAG_B
, 0, NULL
);
495 if (MAN_BLOCK
== n
->type
) {
496 mh
->fl
&= ~MANH_LITERAL
;
497 PAIR_CLASS_INIT(&tag
, "subsection");
498 print_otag(h
, TAG_DIV
, 1, &tag
);
500 } else if (MAN_BODY
== n
->type
)
503 print_otag(h
, TAG_H2
, 0, NULL
);
512 if (MAN_HEAD
== n
->type
)
514 else if (MAN_BLOCK
== n
->type
)
524 const struct man_node
*nn
;
526 if (MAN_BODY
== n
->type
) {
527 print_otag(h
, TAG_DD
, 0, NULL
);
529 } else if (MAN_HEAD
!= n
->type
) {
530 print_otag(h
, TAG_DL
, 0, NULL
);
534 /* FIXME: width specification. */
536 print_otag(h
, TAG_DT
, 0, NULL
);
538 /* For IP, only print the first header element. */
540 if (MAN_IP
== n
->tok
&& n
->child
)
541 print_man_node(m
, n
->child
, mh
, h
);
543 /* For TP, only print next-line header elements. */
545 if (MAN_TP
== n
->tok
)
546 for (nn
= n
->child
; nn
; nn
= nn
->next
)
547 if (nn
->line
> n
->line
)
548 print_man_node(m
, nn
, mh
, h
);
559 const struct man_node
*np
;
561 if (MAN_HEAD
== n
->type
)
563 else if (MAN_BLOCK
!= n
->type
)
568 if (NULL
== np
|| ! a2width(np
, &su
))
569 SCALE_HS_INIT(&su
, INDENT
);
574 bufcat_su(h
, "margin-left", &su
);
575 su
.scale
= -su
.scale
;
576 bufcat_su(h
, "text-indent", &su
);
577 PAIR_STYLE_INIT(&tag
, h
);
578 print_otag(h
, TAG_P
, 1, &tag
);
587 print_otag(h
, TAG_B
, 0, NULL
);
596 print_otag(h
, TAG_I
, 0, NULL
);
602 man_literal_pre(MAN_ARGS
)
605 if (MAN_nf
!= n
->tok
) {
606 print_otag(h
, TAG_BR
, 0, NULL
);
607 mh
->fl
&= ~MANH_LITERAL
;
609 mh
->fl
|= MANH_LITERAL
;
619 print_otag(h
, TAG_BR
, 0, NULL
);
625 man_ign_pre(MAN_ARGS
)
638 if (MAN_HEAD
== n
->type
)
640 else if (MAN_BODY
== n
->type
)
643 SCALE_HS_INIT(&su
, INDENT
);
645 a2width(n
->head
->child
, &su
);
648 bufcat_su(h
, "margin-left", &su
);
649 PAIR_STYLE_INIT(&tag
, h
);
650 print_otag(h
, TAG_DIV
, 1, &tag
);