1 /* $Id: man_html.c,v 1.164 2019/01/05 09:46:34 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 #define MAN_ARGS const struct roff_meta *man, \
37 const struct roff_node *n, \
42 int (*post
)(MAN_ARGS
);
45 static void print_bvspace(struct html
*,
46 const struct roff_node
*);
47 static void print_man_head(const struct roff_meta
*,
49 static void print_man_nodelist(MAN_ARGS
);
50 static void print_man_node(MAN_ARGS
);
51 static int man_B_pre(MAN_ARGS
);
52 static int man_HP_pre(MAN_ARGS
);
53 static int man_IP_pre(MAN_ARGS
);
54 static int man_I_pre(MAN_ARGS
);
55 static int man_OP_pre(MAN_ARGS
);
56 static int man_PP_pre(MAN_ARGS
);
57 static int man_RS_pre(MAN_ARGS
);
58 static int man_SH_pre(MAN_ARGS
);
59 static int man_SM_pre(MAN_ARGS
);
60 static int man_SS_pre(MAN_ARGS
);
61 static int man_SY_pre(MAN_ARGS
);
62 static int man_UR_pre(MAN_ARGS
);
63 static int man_abort_pre(MAN_ARGS
);
64 static int man_alt_pre(MAN_ARGS
);
65 static int man_ign_pre(MAN_ARGS
);
66 static int man_in_pre(MAN_ARGS
);
67 static void man_root_post(const struct roff_meta
*,
69 static void man_root_pre(const struct roff_meta
*,
72 static const struct man_html_act man_html_acts
[MAN_MAX
- MAN_TH
] = {
73 { NULL
, NULL
}, /* TH */
74 { man_SH_pre
, NULL
}, /* SH */
75 { man_SS_pre
, NULL
}, /* SS */
76 { man_IP_pre
, NULL
}, /* TP */
77 { man_IP_pre
, NULL
}, /* TQ */
78 { man_abort_pre
, NULL
}, /* LP */
79 { man_PP_pre
, NULL
}, /* PP */
80 { man_abort_pre
, NULL
}, /* P */
81 { man_IP_pre
, NULL
}, /* IP */
82 { man_HP_pre
, NULL
}, /* HP */
83 { man_SM_pre
, NULL
}, /* SM */
84 { man_SM_pre
, NULL
}, /* SB */
85 { man_alt_pre
, NULL
}, /* BI */
86 { man_alt_pre
, NULL
}, /* IB */
87 { man_alt_pre
, NULL
}, /* BR */
88 { man_alt_pre
, NULL
}, /* RB */
89 { NULL
, NULL
}, /* R */
90 { man_B_pre
, NULL
}, /* B */
91 { man_I_pre
, NULL
}, /* I */
92 { man_alt_pre
, NULL
}, /* IR */
93 { man_alt_pre
, NULL
}, /* RI */
94 { NULL
, NULL
}, /* RE */
95 { man_RS_pre
, NULL
}, /* RS */
96 { man_ign_pre
, NULL
}, /* DT */
97 { man_ign_pre
, NULL
}, /* UC */
98 { man_ign_pre
, NULL
}, /* PD */
99 { man_ign_pre
, NULL
}, /* AT */
100 { man_in_pre
, NULL
}, /* in */
101 { man_SY_pre
, NULL
}, /* SY */
102 { NULL
, NULL
}, /* YS */
103 { man_OP_pre
, NULL
}, /* OP */
104 { NULL
, NULL
}, /* EX */
105 { NULL
, NULL
}, /* EE */
106 { man_UR_pre
, NULL
}, /* UR */
107 { NULL
, NULL
}, /* UE */
108 { man_UR_pre
, NULL
}, /* MT */
109 { NULL
, NULL
}, /* ME */
114 * Printing leading vertical space before a block.
115 * This is used for the paragraph macros.
116 * The rules are pretty simple, since there's very little nesting going
117 * on here. Basically, if we're the first within another block (SS/SH),
118 * then don't emit vertical space. If we are (RS), then do. If not the
122 print_bvspace(struct html
*h
, const struct roff_node
*n
)
124 if (n
->body
!= NULL
&& n
->body
->child
!= NULL
&&
125 n
->body
->child
->type
== ROFFT_TBL
)
128 if (n
->prev
== NULL
&& n
->parent
->tok
!= MAN_RS
)
135 html_man(void *arg
, const struct roff_meta
*man
)
141 h
= (struct html
*)arg
;
142 n
= man
->first
->child
;
144 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
146 print_otag(h
, TAG_HTML
, "");
147 if (n
->type
== ROFFT_COMMENT
)
148 print_gen_comment(h
, n
);
149 t
= print_otag(h
, TAG_HEAD
, "");
150 print_man_head(man
, h
);
152 print_otag(h
, TAG_BODY
, "");
155 man_root_pre(man
, h
);
156 t
= print_otag(h
, TAG_DIV
, "c", "manual-text");
157 print_man_nodelist(man
, n
, h
);
159 man_root_post(man
, h
);
164 print_man_head(const struct roff_meta
*man
, struct html
*h
)
169 mandoc_asprintf(&cp
, "%s(%s)", man
->title
, man
->msec
);
170 print_otag(h
, TAG_TITLE
, "");
176 print_man_nodelist(MAN_ARGS
)
179 print_man_node(man
, n
, h
);
185 print_man_node(MAN_ARGS
)
190 html_fillmode(h
, n
->flags
& NODE_NOFILL
? ROFF_nf
: ROFF_fi
);
195 if (*n
->string
== '\0') {
200 if (*n
->string
== ' ' && n
->flags
& NODE_LINE
&&
201 (h
->flags
& HTML_NONEWLINE
) == 0)
203 else if (n
->flags
& NODE_DELIMC
)
204 h
->flags
|= HTML_NOSPACE
;
205 print_text(h
, n
->string
);
211 print_eqn(h
, n
->eqn
);
215 * This will take care of initialising all of the table
216 * state data for the first table, then tearing it down
219 print_tbl(h
, n
->span
);
223 * Close out scope of font prior to opening a macro
226 if (HTMLFONT_NONE
!= h
->metac
) {
228 h
->metac
= HTMLFONT_NONE
;
232 * Close out the current table, if it's open, and unset
233 * the "meta" table state. This will be reopened on the
234 * next table element.
240 if (n
->tok
< ROFF_MAX
) {
246 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
247 if (man_html_acts
[n
->tok
- MAN_TH
].pre
!= NULL
)
248 child
= (*man_html_acts
[n
->tok
- MAN_TH
].pre
)(man
,
253 if (child
&& n
->child
!= NULL
)
254 print_man_nodelist(man
, n
->child
, h
);
256 /* This will automatically close out any font scope. */
259 if (n
->flags
& NODE_NOFILL
&&
260 (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
)) {
261 /* In .nf = <pre>, print even empty lines. */
268 man_root_pre(const struct roff_meta
*man
, struct html
*h
)
275 mandoc_asprintf(&title
, "%s(%s)", man
->title
, man
->msec
);
277 t
= print_otag(h
, TAG_TABLE
, "c", "head");
278 tt
= print_otag(h
, TAG_TR
, "");
280 print_otag(h
, TAG_TD
, "c", "head-ltitle");
281 print_text(h
, title
);
284 print_otag(h
, TAG_TD
, "c", "head-vol");
285 if (man
->vol
!= NULL
)
286 print_text(h
, man
->vol
);
289 print_otag(h
, TAG_TD
, "c", "head-rtitle");
290 print_text(h
, title
);
296 man_root_post(const struct roff_meta
*man
, struct html
*h
)
300 t
= print_otag(h
, TAG_TABLE
, "c", "foot");
301 tt
= print_otag(h
, TAG_TR
, "");
303 print_otag(h
, TAG_TD
, "c", "foot-date");
304 print_text(h
, man
->date
);
307 print_otag(h
, TAG_TD
, "c", "foot-os");
309 print_text(h
, man
->os
);
318 if (n
->type
== ROFFT_HEAD
) {
319 id
= html_make_id(n
, 1);
320 print_otag(h
, TAG_H1
, "cTi", "Sh", id
);
322 print_otag(h
, TAG_A
, "chR", "permalink", id
);
328 man_alt_pre(MAN_ARGS
)
330 const struct roff_node
*nn
;
335 for (i
= 0, nn
= n
->child
; nn
!= NULL
; nn
= nn
->next
, i
++) {
338 fp
= i
% 2 ? TAG_I
: TAG_B
;
341 fp
= i
% 2 ? TAG_B
: TAG_I
;
344 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
347 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
350 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
353 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
360 h
->flags
|= HTML_NOSPACE
;
363 t
= print_otag(h
, fp
, "");
365 print_text(h
, nn
->string
);
376 print_otag(h
, TAG_SMALL
, "");
377 if (n
->tok
== MAN_SB
)
378 print_otag(h
, TAG_B
, "");
387 if (n
->type
== ROFFT_HEAD
) {
388 id
= html_make_id(n
, 1);
389 print_otag(h
, TAG_H2
, "cTi", "Ss", id
);
391 print_otag(h
, TAG_A
, "chR", "permalink", id
);
399 if (n
->type
== ROFFT_HEAD
)
401 else if (n
->type
== ROFFT_BLOCK
)
410 const struct roff_node
*nn
;
412 if (n
->type
== ROFFT_BODY
) {
413 print_otag(h
, TAG_DD
, "");
415 } else if (n
->type
!= ROFFT_HEAD
) {
416 print_otag(h
, TAG_DL
, "c", "Bl-tag");
420 print_otag(h
, TAG_DT
, "");
423 case MAN_IP
: /* Only print the first header element. */
424 if (n
->child
!= NULL
)
425 print_man_node(man
, n
->child
, h
);
427 case MAN_TP
: /* Only print next-line header elements. */
430 while (nn
!= NULL
&& (NODE_LINE
& nn
->flags
) == 0)
433 print_man_node(man
, nn
, h
);
446 if (n
->type
== ROFFT_HEAD
)
449 if (n
->type
== ROFFT_BLOCK
) {
451 print_otag(h
, TAG_DIV
, "c", "HP");
462 h
->flags
|= HTML_NOSPACE
;
463 tt
= print_otag(h
, TAG_SPAN
, "c", "Op");
465 if ((n
= n
->child
) != NULL
) {
466 print_otag(h
, TAG_B
, "");
467 print_text(h
, n
->string
);
472 if (n
!= NULL
&& n
->next
!= NULL
) {
473 print_otag(h
, TAG_I
, "");
474 print_text(h
, n
->next
->string
);
478 h
->flags
|= HTML_NOSPACE
;
486 print_otag(h
, TAG_B
, "");
493 print_otag(h
, TAG_I
, "");
500 print_otag(h
, TAG_BR
, "");
505 man_ign_pre(MAN_ARGS
)
513 if (n
->type
== ROFFT_HEAD
)
515 if (n
->type
== ROFFT_BLOCK
)
516 print_otag(h
, TAG_DIV
, "c", "Bd-indent");
525 print_otag(h
, TAG_TABLE
, "c", "Nm");
526 print_otag(h
, TAG_TR
, "");
529 print_otag(h
, TAG_TD
, "");
530 print_otag(h
, TAG_CODE
, "cT", "Nm");
533 print_otag(h
, TAG_TD
, "");
547 assert(n
->type
== ROFFT_HEAD
);
548 if (n
->child
!= NULL
) {
549 assert(n
->child
->type
== ROFFT_TEXT
);
550 if (n
->tok
== MAN_MT
) {
551 mandoc_asprintf(&cp
, "mailto:%s", n
->child
->string
);
552 print_otag(h
, TAG_A
, "cTh", "Mt", cp
);
555 print_otag(h
, TAG_A
, "cTh", "Lk", n
->child
->string
);
558 assert(n
->next
->type
== ROFFT_BODY
);
559 if (n
->next
->child
!= NULL
)
562 print_man_nodelist(man
, n
->child
, h
);
567 man_abort_pre(MAN_ARGS
)