1 /* $Vendor-Id: man_html.c,v 1.86 2012/01/03 15:16:24 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. */
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
);
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 */
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
)
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
);
312 snprintf(title
, BUFSIZ
- 1, "%s(%s)", m
->title
, m
->msec
);
314 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
315 PAIR_CLASS_INIT(&tag
[1], "head");
316 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
317 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
318 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
319 print_otag(h
, TAG_COL
, 1, tag
);
320 print_otag(h
, TAG_COL
, 1, tag
);
321 print_otag(h
, TAG_COL
, 1, tag
);
323 print_otag(h
, TAG_TBODY
, 0, NULL
);
325 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
327 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
328 print_otag(h
, TAG_TD
, 1, tag
);
329 print_text(h
, title
);
332 PAIR_CLASS_INIT(&tag
[0], "head-vol");
333 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
334 print_otag(h
, TAG_TD
, 2, tag
);
338 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
339 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
340 print_otag(h
, TAG_TD
, 2, tag
);
341 print_text(h
, title
);
348 man_root_post(MAN_ARGS
)
350 struct htmlpair tag
[3];
353 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
354 PAIR_CLASS_INIT(&tag
[1], "foot");
355 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
356 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
357 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
358 print_otag(h
, TAG_COL
, 1, tag
);
359 print_otag(h
, TAG_COL
, 1, tag
);
361 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
363 PAIR_CLASS_INIT(&tag
[0], "foot-date");
364 print_otag(h
, TAG_TD
, 1, tag
);
367 print_text(h
, m
->date
);
370 PAIR_CLASS_INIT(&tag
[0], "foot-os");
371 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
372 print_otag(h
, TAG_TD
, 2, tag
);
375 print_text(h
, m
->source
);
387 SCALE_VS_INIT(&su
, 1);
389 if (MAN_sp
== n
->tok
) {
390 if (NULL
!= (n
= n
->child
))
391 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
392 SCALE_VS_INIT(&su
, atoi(n
->string
));
397 bufcat_su(h
, "height", &su
);
398 PAIR_STYLE_INIT(&tag
, h
);
399 print_otag(h
, TAG_DIV
, 1, &tag
);
401 /* So the div isn't empty: */
402 print_text(h
, "\\~");
413 if (MAN_BLOCK
== n
->type
) {
414 mh
->fl
&= ~MANH_LITERAL
;
415 PAIR_CLASS_INIT(&tag
, "section");
416 print_otag(h
, TAG_DIV
, 1, &tag
);
418 } else if (MAN_BODY
== n
->type
)
421 print_otag(h
, TAG_H1
, 0, NULL
);
427 man_alt_pre(MAN_ARGS
)
429 const struct man_node
*nn
;
434 if ((savelit
= mh
->fl
& MANH_LITERAL
))
435 print_otag(h
, TAG_BR
, 0, NULL
);
437 mh
->fl
&= ~MANH_LITERAL
;
439 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
443 fp
= i
% 2 ? TAG_I
: TAG_B
;
446 fp
= i
% 2 ? TAG_B
: TAG_I
;
449 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
452 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
455 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
458 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
466 h
->flags
|= HTML_NOSPACE
;
469 t
= print_otag(h
, fp
, 0, NULL
);
471 print_man_node(m
, nn
, mh
, h
);
478 mh
->fl
|= MANH_LITERAL
;
488 print_otag(h
, TAG_SMALL
, 0, NULL
);
489 if (MAN_SB
== n
->tok
)
490 print_otag(h
, TAG_B
, 0, NULL
);
500 if (MAN_BLOCK
== n
->type
) {
501 mh
->fl
&= ~MANH_LITERAL
;
502 PAIR_CLASS_INIT(&tag
, "subsection");
503 print_otag(h
, TAG_DIV
, 1, &tag
);
505 } else if (MAN_BODY
== n
->type
)
508 print_otag(h
, TAG_H2
, 0, NULL
);
517 if (MAN_HEAD
== n
->type
)
519 else if (MAN_BLOCK
== n
->type
)
529 const struct man_node
*nn
;
531 if (MAN_BODY
== n
->type
) {
532 print_otag(h
, TAG_DD
, 0, NULL
);
534 } else if (MAN_HEAD
!= n
->type
) {
535 print_otag(h
, TAG_DL
, 0, NULL
);
539 /* FIXME: width specification. */
541 print_otag(h
, TAG_DT
, 0, NULL
);
543 /* For IP, only print the first header element. */
545 if (MAN_IP
== n
->tok
&& n
->child
)
546 print_man_node(m
, n
->child
, mh
, h
);
548 /* For TP, only print next-line header elements. */
550 if (MAN_TP
== n
->tok
)
551 for (nn
= n
->child
; nn
; nn
= nn
->next
)
552 if (nn
->line
> n
->line
)
553 print_man_node(m
, nn
, mh
, h
);
564 const struct man_node
*np
;
566 if (MAN_HEAD
== n
->type
)
568 else if (MAN_BLOCK
!= n
->type
)
573 if (NULL
== np
|| ! a2width(np
, &su
))
574 SCALE_HS_INIT(&su
, INDENT
);
579 bufcat_su(h
, "margin-left", &su
);
580 su
.scale
= -su
.scale
;
581 bufcat_su(h
, "text-indent", &su
);
582 PAIR_STYLE_INIT(&tag
, h
);
583 print_otag(h
, TAG_P
, 1, &tag
);
595 h
->flags
|= HTML_NOSPACE
;
596 PAIR_CLASS_INIT(&tag
, "opt");
597 tt
= print_otag(h
, TAG_SPAN
, 1, &tag
);
599 if (NULL
!= (n
= n
->child
)) {
600 print_otag(h
, TAG_B
, 0, NULL
);
601 print_text(h
, n
->string
);
606 if (NULL
!= n
&& NULL
!= n
->next
) {
607 print_otag(h
, TAG_I
, 0, NULL
);
608 print_text(h
, n
->next
->string
);
612 h
->flags
|= HTML_NOSPACE
;
623 print_otag(h
, TAG_B
, 0, NULL
);
632 print_otag(h
, TAG_I
, 0, NULL
);
638 man_literal_pre(MAN_ARGS
)
641 if (MAN_nf
!= n
->tok
) {
642 print_otag(h
, TAG_BR
, 0, NULL
);
643 mh
->fl
&= ~MANH_LITERAL
;
645 mh
->fl
|= MANH_LITERAL
;
655 print_otag(h
, TAG_BR
, 0, NULL
);
661 man_ign_pre(MAN_ARGS
)
674 if (MAN_HEAD
== n
->type
)
676 else if (MAN_BODY
== n
->type
)
679 SCALE_HS_INIT(&su
, INDENT
);
681 a2width(n
->head
->child
, &su
);
684 bufcat_su(h
, "margin-left", &su
);
685 PAIR_STYLE_INIT(&tag
, h
);
686 print_otag(h
, TAG_DIV
, 1, &tag
);