1 /* Id: man_html.c,v 1.90 2013/10/17 20:54:58 schwarze Exp */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
36 /* TODO: preserve ident widths. */
37 /* FIXME: have PD set the default vspace width. */
41 #define MAN_ARGS const struct man_meta *man, \
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
);
62 static int a2width(const struct man_node
*,
64 static int man_B_pre(MAN_ARGS
);
65 static int man_HP_pre(MAN_ARGS
);
66 static int man_IP_pre(MAN_ARGS
);
67 static int man_I_pre(MAN_ARGS
);
68 static int man_OP_pre(MAN_ARGS
);
69 static int man_PP_pre(MAN_ARGS
);
70 static int man_RS_pre(MAN_ARGS
);
71 static int man_SH_pre(MAN_ARGS
);
72 static int man_SM_pre(MAN_ARGS
);
73 static int man_SS_pre(MAN_ARGS
);
74 static int man_UR_pre(MAN_ARGS
);
75 static int man_alt_pre(MAN_ARGS
);
76 static int man_br_pre(MAN_ARGS
);
77 static int man_ign_pre(MAN_ARGS
);
78 static int man_in_pre(MAN_ARGS
);
79 static int man_literal_pre(MAN_ARGS
);
80 static void man_root_post(MAN_ARGS
);
81 static void man_root_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 */
117 { man_OP_pre
, NULL
}, /* OP */
118 { man_literal_pre
, NULL
}, /* EX */
119 { man_literal_pre
, NULL
}, /* EE */
120 { man_UR_pre
, NULL
}, /* UR */
121 { NULL
, NULL
}, /* UE */
125 * Printing leading vertical space before a block.
126 * This is used for the paragraph macros.
127 * The rules are pretty simple, since there's very little nesting going
128 * on here. Basically, if we're the first within another block (SS/SH),
129 * then don't emit vertical space. If we are (RS), then do. If not the
133 print_bvspace(struct html
*h
, const struct man_node
*n
)
136 if (n
->body
&& n
->body
->child
)
137 if (MAN_TBL
== n
->body
->child
->type
)
140 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
144 print_otag(h
, TAG_P
, 0, NULL
);
148 html_man(void *arg
, const struct man
*man
)
152 memset(&mh
, 0, sizeof(struct mhtml
));
153 print_man(man_meta(man
), man_node(man
), &mh
, (struct html
*)arg
);
163 PAIR_CLASS_INIT(&tag
, "mandoc");
165 if ( ! (HTML_FRAGMENT
& h
->oflags
)) {
167 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
168 tt
= print_otag(h
, TAG_HEAD
, 0, NULL
);
169 print_man_head(man
, n
, mh
, h
);
171 print_otag(h
, TAG_BODY
, 0, NULL
);
172 print_otag(h
, TAG_DIV
, 1, &tag
);
174 t
= print_otag(h
, TAG_DIV
, 1, &tag
);
176 print_man_nodelist(man
, n
, mh
, h
);
183 print_man_head(MAN_ARGS
)
189 bufcat_fmt(h
, "%s(%s)", man
->title
, man
->msec
);
190 print_otag(h
, TAG_TITLE
, 0, NULL
);
191 print_text(h
, h
->buf
);
196 print_man_nodelist(MAN_ARGS
)
199 print_man_node(man
, n
, mh
, h
);
201 print_man_nodelist(man
, n
->next
, mh
, h
);
206 print_man_node(MAN_ARGS
)
216 man_root_pre(man
, n
, mh
, h
);
220 * If we have a blank line, output a vertical space.
221 * If we have a space as the first character, break
222 * before printing the line's data.
224 if ('\0' == *n
->string
) {
225 print_otag(h
, TAG_P
, 0, NULL
);
229 if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
230 print_otag(h
, TAG_BR
, 0, NULL
);
231 else if (MANH_LITERAL
& mh
->fl
&& n
->prev
)
232 print_otag(h
, TAG_BR
, 0, NULL
);
234 print_text(h
, n
->string
);
237 print_eqn(h
, n
->eqn
);
241 * This will take care of initialising all of the table
242 * state data for the first table, then tearing it down
245 print_tbl(h
, n
->span
);
249 * Close out scope of font prior to opening a macro
252 if (HTMLFONT_NONE
!= h
->metac
) {
254 h
->metac
= HTMLFONT_NONE
;
258 * Close out the current table, if it's open, and unset
259 * the "meta" table state. This will be reopened on the
260 * next table element.
266 if (mans
[n
->tok
].pre
)
267 child
= (*mans
[n
->tok
].pre
)(man
, n
, mh
, h
);
271 if (child
&& n
->child
)
272 print_man_nodelist(man
, n
->child
, mh
, h
);
274 /* This will automatically close out any font scope. */
279 man_root_post(man
, n
, mh
, h
);
284 if (mans
[n
->tok
].post
)
285 (*mans
[n
->tok
].post
)(man
, n
, mh
, h
);
292 a2width(const struct man_node
*n
, struct roffsu
*su
)
295 if (MAN_TEXT
!= n
->type
)
297 if (a2roffsu(n
->string
, su
, SCALE_BU
))
306 man_root_pre(MAN_ARGS
)
308 struct htmlpair tag
[3];
310 char b
[BUFSIZ
], title
[BUFSIZ
];
314 (void)strlcat(b
, man
->vol
, BUFSIZ
);
318 snprintf(title
, BUFSIZ
- 1, "%s(%s)", man
->title
, man
->msec
);
320 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
321 PAIR_CLASS_INIT(&tag
[1], "head");
322 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
323 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
324 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
325 print_otag(h
, TAG_COL
, 1, tag
);
326 print_otag(h
, TAG_COL
, 1, tag
);
327 print_otag(h
, TAG_COL
, 1, tag
);
329 print_otag(h
, TAG_TBODY
, 0, NULL
);
331 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
333 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
334 print_otag(h
, TAG_TD
, 1, tag
);
335 print_text(h
, title
);
338 PAIR_CLASS_INIT(&tag
[0], "head-vol");
339 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
340 print_otag(h
, TAG_TD
, 2, tag
);
344 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
345 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
346 print_otag(h
, TAG_TD
, 2, tag
);
347 print_text(h
, title
);
354 man_root_post(MAN_ARGS
)
356 struct htmlpair tag
[3];
359 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
360 PAIR_CLASS_INIT(&tag
[1], "foot");
361 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
362 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
363 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
364 print_otag(h
, TAG_COL
, 1, tag
);
365 print_otag(h
, TAG_COL
, 1, tag
);
367 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
369 PAIR_CLASS_INIT(&tag
[0], "foot-date");
370 print_otag(h
, TAG_TD
, 1, tag
);
373 print_text(h
, man
->date
);
376 PAIR_CLASS_INIT(&tag
[0], "foot-os");
377 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
378 print_otag(h
, TAG_TD
, 2, tag
);
381 print_text(h
, man
->source
);
393 SCALE_VS_INIT(&su
, 1);
395 if (MAN_sp
== n
->tok
) {
396 if (NULL
!= (n
= n
->child
))
397 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
398 SCALE_VS_INIT(&su
, atoi(n
->string
));
403 bufcat_su(h
, "height", &su
);
404 PAIR_STYLE_INIT(&tag
, h
);
405 print_otag(h
, TAG_DIV
, 1, &tag
);
407 /* So the div isn't empty: */
408 print_text(h
, "\\~");
419 if (MAN_BLOCK
== n
->type
) {
420 mh
->fl
&= ~MANH_LITERAL
;
421 PAIR_CLASS_INIT(&tag
, "section");
422 print_otag(h
, TAG_DIV
, 1, &tag
);
424 } else if (MAN_BODY
== n
->type
)
427 print_otag(h
, TAG_H1
, 0, NULL
);
433 man_alt_pre(MAN_ARGS
)
435 const struct man_node
*nn
;
440 if ((savelit
= mh
->fl
& MANH_LITERAL
))
441 print_otag(h
, TAG_BR
, 0, NULL
);
443 mh
->fl
&= ~MANH_LITERAL
;
445 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
449 fp
= i
% 2 ? TAG_I
: TAG_B
;
452 fp
= i
% 2 ? TAG_B
: TAG_I
;
455 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
458 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
461 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
464 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
472 h
->flags
|= HTML_NOSPACE
;
475 t
= print_otag(h
, fp
, 0, NULL
);
477 print_man_node(man
, nn
, mh
, h
);
484 mh
->fl
|= MANH_LITERAL
;
494 print_otag(h
, TAG_SMALL
, 0, NULL
);
495 if (MAN_SB
== n
->tok
)
496 print_otag(h
, TAG_B
, 0, NULL
);
506 if (MAN_BLOCK
== n
->type
) {
507 mh
->fl
&= ~MANH_LITERAL
;
508 PAIR_CLASS_INIT(&tag
, "subsection");
509 print_otag(h
, TAG_DIV
, 1, &tag
);
511 } else if (MAN_BODY
== n
->type
)
514 print_otag(h
, TAG_H2
, 0, NULL
);
523 if (MAN_HEAD
== n
->type
)
525 else if (MAN_BLOCK
== n
->type
)
535 const struct man_node
*nn
;
537 if (MAN_BODY
== n
->type
) {
538 print_otag(h
, TAG_DD
, 0, NULL
);
540 } else if (MAN_HEAD
!= n
->type
) {
541 print_otag(h
, TAG_DL
, 0, NULL
);
545 /* FIXME: width specification. */
547 print_otag(h
, TAG_DT
, 0, NULL
);
549 /* For IP, only print the first header element. */
551 if (MAN_IP
== n
->tok
&& n
->child
)
552 print_man_node(man
, n
->child
, mh
, h
);
554 /* For TP, only print next-line header elements. */
556 if (MAN_TP
== n
->tok
)
557 for (nn
= n
->child
; nn
; nn
= nn
->next
)
558 if (nn
->line
> n
->line
)
559 print_man_node(man
, nn
, mh
, h
);
570 const struct man_node
*np
;
572 if (MAN_HEAD
== n
->type
)
574 else if (MAN_BLOCK
!= n
->type
)
579 if (NULL
== np
|| ! a2width(np
, &su
))
580 SCALE_HS_INIT(&su
, INDENT
);
585 bufcat_su(h
, "margin-left", &su
);
586 su
.scale
= -su
.scale
;
587 bufcat_su(h
, "text-indent", &su
);
588 PAIR_STYLE_INIT(&tag
, h
);
589 print_otag(h
, TAG_P
, 1, &tag
);
601 h
->flags
|= HTML_NOSPACE
;
602 PAIR_CLASS_INIT(&tag
, "opt");
603 tt
= print_otag(h
, TAG_SPAN
, 1, &tag
);
605 if (NULL
!= (n
= n
->child
)) {
606 print_otag(h
, TAG_B
, 0, NULL
);
607 print_text(h
, n
->string
);
612 if (NULL
!= n
&& NULL
!= n
->next
) {
613 print_otag(h
, TAG_I
, 0, NULL
);
614 print_text(h
, n
->next
->string
);
618 h
->flags
|= HTML_NOSPACE
;
629 print_otag(h
, TAG_B
, 0, NULL
);
638 print_otag(h
, TAG_I
, 0, NULL
);
644 man_literal_pre(MAN_ARGS
)
647 if (MAN_fi
== n
->tok
|| MAN_EE
== n
->tok
) {
648 print_otag(h
, TAG_BR
, 0, NULL
);
649 mh
->fl
&= ~MANH_LITERAL
;
651 mh
->fl
|= MANH_LITERAL
;
661 print_otag(h
, TAG_BR
, 0, NULL
);
667 man_ign_pre(MAN_ARGS
)
680 if (MAN_HEAD
== n
->type
)
682 else if (MAN_BODY
== n
->type
)
685 SCALE_HS_INIT(&su
, INDENT
);
687 a2width(n
->head
->child
, &su
);
690 bufcat_su(h
, "margin-left", &su
);
691 PAIR_STYLE_INIT(&tag
, h
);
692 print_otag(h
, TAG_DIV
, 1, &tag
);
700 struct htmlpair tag
[2];
703 assert(MAN_HEAD
== n
->type
);
705 assert(MAN_TEXT
== n
->child
->type
);
706 PAIR_CLASS_INIT(&tag
[0], "link-ext");
707 PAIR_HREF_INIT(&tag
[1], n
->child
->string
);
708 print_otag(h
, TAG_A
, 2, tag
);
711 assert(MAN_BODY
== n
->next
->type
);
715 print_man_nodelist(man
, n
->child
, mh
, h
);