1 /* $Id: man_html.c,v 1.163 2019/01/05 09:14:44 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013-2015, 2017-2019 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
20 #include <sys/types.h>
28 #include "mandoc_aux.h"
36 /* FIXME: have PD set the default vspace width. */
38 #define MAN_ARGS const struct roff_meta *man, \
39 const struct roff_node *n, \
44 int (*post
)(MAN_ARGS
);
47 static void print_bvspace(struct html
*,
48 const struct roff_node
*);
49 static void print_man_head(const struct roff_meta
*,
51 static void print_man_nodelist(MAN_ARGS
);
52 static void print_man_node(MAN_ARGS
);
53 static int man_B_pre(MAN_ARGS
);
54 static int man_HP_pre(MAN_ARGS
);
55 static int man_IP_pre(MAN_ARGS
);
56 static int man_I_pre(MAN_ARGS
);
57 static int man_OP_pre(MAN_ARGS
);
58 static int man_PP_pre(MAN_ARGS
);
59 static int man_RS_pre(MAN_ARGS
);
60 static int man_SH_pre(MAN_ARGS
);
61 static int man_SM_pre(MAN_ARGS
);
62 static int man_SS_pre(MAN_ARGS
);
63 static int man_SY_pre(MAN_ARGS
);
64 static int man_UR_pre(MAN_ARGS
);
65 static int man_abort_pre(MAN_ARGS
);
66 static int man_alt_pre(MAN_ARGS
);
67 static int man_ign_pre(MAN_ARGS
);
68 static int man_in_pre(MAN_ARGS
);
69 static void man_root_post(const struct roff_meta
*,
71 static void man_root_pre(const struct roff_meta
*,
74 static const struct man_html_act man_html_acts
[MAN_MAX
- MAN_TH
] = {
75 { NULL
, NULL
}, /* TH */
76 { man_SH_pre
, NULL
}, /* SH */
77 { man_SS_pre
, NULL
}, /* SS */
78 { man_IP_pre
, NULL
}, /* TP */
79 { man_IP_pre
, NULL
}, /* TQ */
80 { man_abort_pre
, NULL
}, /* LP */
81 { man_PP_pre
, NULL
}, /* PP */
82 { man_abort_pre
, NULL
}, /* P */
83 { man_IP_pre
, NULL
}, /* IP */
84 { man_HP_pre
, NULL
}, /* HP */
85 { man_SM_pre
, NULL
}, /* SM */
86 { man_SM_pre
, NULL
}, /* SB */
87 { man_alt_pre
, NULL
}, /* BI */
88 { man_alt_pre
, NULL
}, /* IB */
89 { man_alt_pre
, NULL
}, /* BR */
90 { man_alt_pre
, NULL
}, /* RB */
91 { NULL
, NULL
}, /* R */
92 { man_B_pre
, NULL
}, /* B */
93 { man_I_pre
, NULL
}, /* I */
94 { man_alt_pre
, NULL
}, /* IR */
95 { man_alt_pre
, NULL
}, /* RI */
96 { NULL
, NULL
}, /* RE */
97 { man_RS_pre
, NULL
}, /* RS */
98 { man_ign_pre
, NULL
}, /* DT */
99 { man_ign_pre
, NULL
}, /* UC */
100 { man_ign_pre
, NULL
}, /* PD */
101 { man_ign_pre
, NULL
}, /* AT */
102 { man_in_pre
, NULL
}, /* in */
103 { man_SY_pre
, NULL
}, /* SY */
104 { NULL
, NULL
}, /* YS */
105 { man_OP_pre
, NULL
}, /* OP */
106 { NULL
, NULL
}, /* EX */
107 { NULL
, NULL
}, /* EE */
108 { man_UR_pre
, NULL
}, /* UR */
109 { NULL
, NULL
}, /* UE */
110 { man_UR_pre
, NULL
}, /* MT */
111 { NULL
, NULL
}, /* ME */
116 * Printing leading vertical space before a block.
117 * This is used for the paragraph macros.
118 * The rules are pretty simple, since there's very little nesting going
119 * on here. Basically, if we're the first within another block (SS/SH),
120 * then don't emit vertical space. If we are (RS), then do. If not the
124 print_bvspace(struct html
*h
, const struct roff_node
*n
)
127 if (n
->body
&& n
->body
->child
)
128 if (n
->body
->child
->type
== ROFFT_TBL
)
131 if (n
->parent
->type
== ROFFT_ROOT
|| n
->parent
->tok
!= MAN_RS
)
139 html_man(void *arg
, const struct roff_meta
*man
)
145 h
= (struct html
*)arg
;
146 n
= man
->first
->child
;
148 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
150 print_otag(h
, TAG_HTML
, "");
151 if (n
->type
== ROFFT_COMMENT
)
152 print_gen_comment(h
, n
);
153 t
= print_otag(h
, TAG_HEAD
, "");
154 print_man_head(man
, h
);
156 print_otag(h
, TAG_BODY
, "");
159 man_root_pre(man
, h
);
160 t
= print_otag(h
, TAG_DIV
, "c", "manual-text");
161 print_man_nodelist(man
, n
, h
);
163 man_root_post(man
, h
);
168 print_man_head(const struct roff_meta
*man
, struct html
*h
)
173 mandoc_asprintf(&cp
, "%s(%s)", man
->title
, man
->msec
);
174 print_otag(h
, TAG_TITLE
, "");
180 print_man_nodelist(MAN_ARGS
)
184 print_man_node(man
, n
, h
);
190 print_man_node(MAN_ARGS
)
195 html_fillmode(h
, n
->flags
& NODE_NOFILL
? ROFF_nf
: ROFF_fi
);
200 if (*n
->string
== '\0') {
205 if (*n
->string
== ' ' && n
->flags
& NODE_LINE
&&
206 (h
->flags
& HTML_NONEWLINE
) == 0)
208 else if (n
->flags
& NODE_DELIMC
)
209 h
->flags
|= HTML_NOSPACE
;
210 print_text(h
, n
->string
);
216 print_eqn(h
, n
->eqn
);
220 * This will take care of initialising all of the table
221 * state data for the first table, then tearing it down
224 print_tbl(h
, n
->span
);
228 * Close out scope of font prior to opening a macro
231 if (HTMLFONT_NONE
!= h
->metac
) {
233 h
->metac
= HTMLFONT_NONE
;
237 * Close out the current table, if it's open, and unset
238 * the "meta" table state. This will be reopened on the
239 * next table element.
245 if (n
->tok
< ROFF_MAX
) {
251 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
252 if (man_html_acts
[n
->tok
- MAN_TH
].pre
!= NULL
)
253 child
= (*man_html_acts
[n
->tok
- MAN_TH
].pre
)(man
,
258 if (child
&& n
->child
)
259 print_man_nodelist(man
, n
->child
, h
);
261 /* This will automatically close out any font scope. */
264 if (n
->flags
& NODE_NOFILL
&&
265 (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
)) {
266 /* In .nf = <pre>, print even empty lines. */
273 man_root_pre(const struct roff_meta
*man
, struct html
*h
)
280 mandoc_asprintf(&title
, "%s(%s)", man
->title
, man
->msec
);
282 t
= print_otag(h
, TAG_TABLE
, "c", "head");
283 tt
= print_otag(h
, TAG_TR
, "");
285 print_otag(h
, TAG_TD
, "c", "head-ltitle");
286 print_text(h
, title
);
289 print_otag(h
, TAG_TD
, "c", "head-vol");
290 if (NULL
!= man
->vol
)
291 print_text(h
, man
->vol
);
294 print_otag(h
, TAG_TD
, "c", "head-rtitle");
295 print_text(h
, title
);
301 man_root_post(const struct roff_meta
*man
, struct html
*h
)
305 t
= print_otag(h
, TAG_TABLE
, "c", "foot");
306 tt
= print_otag(h
, TAG_TR
, "");
308 print_otag(h
, TAG_TD
, "c", "foot-date");
309 print_text(h
, man
->date
);
312 print_otag(h
, TAG_TD
, "c", "foot-os");
314 print_text(h
, man
->os
);
323 if (n
->type
== ROFFT_HEAD
) {
324 id
= html_make_id(n
, 1);
325 print_otag(h
, TAG_H1
, "cTi", "Sh", id
);
327 print_otag(h
, TAG_A
, "chR", "permalink", id
);
333 man_alt_pre(MAN_ARGS
)
335 const struct roff_node
*nn
;
340 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
343 fp
= i
% 2 ? TAG_I
: TAG_B
;
346 fp
= i
% 2 ? TAG_B
: TAG_I
;
349 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
352 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
355 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
358 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
365 h
->flags
|= HTML_NOSPACE
;
368 t
= print_otag(h
, fp
, "");
370 print_text(h
, nn
->string
);
381 print_otag(h
, TAG_SMALL
, "");
382 if (MAN_SB
== n
->tok
)
383 print_otag(h
, TAG_B
, "");
392 if (n
->type
== ROFFT_HEAD
) {
393 id
= html_make_id(n
, 1);
394 print_otag(h
, TAG_H2
, "cTi", "Ss", id
);
396 print_otag(h
, TAG_A
, "chR", "permalink", id
);
405 if (n
->type
== ROFFT_HEAD
)
407 else if (n
->type
== ROFFT_BLOCK
)
416 const struct roff_node
*nn
;
418 if (n
->type
== ROFFT_BODY
) {
419 print_otag(h
, TAG_DD
, "");
421 } else if (n
->type
!= ROFFT_HEAD
) {
422 print_otag(h
, TAG_DL
, "c", "Bl-tag");
426 print_otag(h
, TAG_DT
, "");
429 case MAN_IP
: /* Only print the first header element. */
430 if (n
->child
!= NULL
)
431 print_man_node(man
, n
->child
, h
);
433 case MAN_TP
: /* Only print next-line header elements. */
436 while (nn
!= NULL
&& (NODE_LINE
& nn
->flags
) == 0)
439 print_man_node(man
, nn
, h
);
453 if (n
->type
== ROFFT_HEAD
)
456 if (n
->type
== ROFFT_BLOCK
) {
458 print_otag(h
, TAG_DIV
, "c", "HP");
469 h
->flags
|= HTML_NOSPACE
;
470 tt
= print_otag(h
, TAG_SPAN
, "c", "Op");
472 if (NULL
!= (n
= n
->child
)) {
473 print_otag(h
, TAG_B
, "");
474 print_text(h
, n
->string
);
479 if (NULL
!= n
&& NULL
!= n
->next
) {
480 print_otag(h
, TAG_I
, "");
481 print_text(h
, n
->next
->string
);
485 h
->flags
|= HTML_NOSPACE
;
493 print_otag(h
, TAG_B
, "");
500 print_otag(h
, TAG_I
, "");
507 print_otag(h
, TAG_BR
, "");
512 man_ign_pre(MAN_ARGS
)
521 if (n
->type
== ROFFT_HEAD
)
523 if (n
->type
== ROFFT_BLOCK
)
524 print_otag(h
, TAG_DIV
, "c", "Bd-indent");
533 print_otag(h
, TAG_TABLE
, "c", "Nm");
534 print_otag(h
, TAG_TR
, "");
537 print_otag(h
, TAG_TD
, "");
538 print_otag(h
, TAG_CODE
, "cT", "Nm");
541 print_otag(h
, TAG_TD
, "");
554 assert(n
->type
== ROFFT_HEAD
);
555 if (n
->child
!= NULL
) {
556 assert(n
->child
->type
== ROFFT_TEXT
);
557 if (n
->tok
== MAN_MT
) {
558 mandoc_asprintf(&cp
, "mailto:%s", n
->child
->string
);
559 print_otag(h
, TAG_A
, "cTh", "Mt", cp
);
562 print_otag(h
, TAG_A
, "cTh", "Lk", n
->child
->string
);
565 assert(n
->next
->type
== ROFFT_BODY
);
566 if (n
->next
->child
!= NULL
)
569 print_man_nodelist(man
, n
->child
, h
);
575 man_abort_pre(MAN_ARGS
)