1 /* $Id: man_html.c,v 1.175 2020/02/12 21:15:53 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013-2015, 2017-2020 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_man_head(const struct roff_meta
*,
47 static void print_man_nodelist(MAN_ARGS
);
48 static void print_man_node(MAN_ARGS
);
49 static char list_continues(const struct roff_node
*,
50 const struct roff_node
*);
51 static int man_B_pre(MAN_ARGS
);
52 static int man_IP_pre(MAN_ARGS
);
53 static int man_I_pre(MAN_ARGS
);
54 static int man_OP_pre(MAN_ARGS
);
55 static int man_PP_pre(MAN_ARGS
);
56 static int man_RS_pre(MAN_ARGS
);
57 static int man_SH_pre(MAN_ARGS
);
58 static int man_SM_pre(MAN_ARGS
);
59 static int man_SY_pre(MAN_ARGS
);
60 static int man_UR_pre(MAN_ARGS
);
61 static int man_abort_pre(MAN_ARGS
);
62 static int man_alt_pre(MAN_ARGS
);
63 static int man_ign_pre(MAN_ARGS
);
64 static int man_in_pre(MAN_ARGS
);
65 static void man_root_post(const struct roff_meta
*,
67 static void man_root_pre(const struct roff_meta
*,
70 static const struct man_html_act man_html_acts
[MAN_MAX
- MAN_TH
] = {
71 { NULL
, NULL
}, /* TH */
72 { man_SH_pre
, NULL
}, /* SH */
73 { man_SH_pre
, NULL
}, /* SS */
74 { man_IP_pre
, NULL
}, /* TP */
75 { man_IP_pre
, NULL
}, /* TQ */
76 { man_abort_pre
, NULL
}, /* LP */
77 { man_PP_pre
, NULL
}, /* PP */
78 { man_abort_pre
, NULL
}, /* P */
79 { man_IP_pre
, NULL
}, /* IP */
80 { man_PP_pre
, NULL
}, /* HP */
81 { man_SM_pre
, NULL
}, /* SM */
82 { man_SM_pre
, NULL
}, /* SB */
83 { man_alt_pre
, NULL
}, /* BI */
84 { man_alt_pre
, NULL
}, /* IB */
85 { man_alt_pre
, NULL
}, /* BR */
86 { man_alt_pre
, NULL
}, /* RB */
87 { NULL
, NULL
}, /* R */
88 { man_B_pre
, NULL
}, /* B */
89 { man_I_pre
, NULL
}, /* I */
90 { man_alt_pre
, NULL
}, /* IR */
91 { man_alt_pre
, NULL
}, /* RI */
92 { NULL
, NULL
}, /* RE */
93 { man_RS_pre
, NULL
}, /* RS */
94 { man_ign_pre
, NULL
}, /* DT */
95 { man_ign_pre
, NULL
}, /* UC */
96 { man_ign_pre
, NULL
}, /* PD */
97 { man_ign_pre
, NULL
}, /* AT */
98 { man_in_pre
, NULL
}, /* in */
99 { man_SY_pre
, NULL
}, /* SY */
100 { NULL
, NULL
}, /* YS */
101 { man_OP_pre
, NULL
}, /* OP */
102 { NULL
, NULL
}, /* EX */
103 { NULL
, NULL
}, /* EE */
104 { man_UR_pre
, NULL
}, /* UR */
105 { NULL
, NULL
}, /* UE */
106 { man_UR_pre
, NULL
}, /* MT */
107 { NULL
, NULL
}, /* ME */
112 html_man(void *arg
, const struct roff_meta
*man
)
118 h
= (struct html
*)arg
;
119 n
= man
->first
->child
;
121 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
123 print_otag(h
, TAG_HTML
, "");
124 if (n
!= NULL
&& n
->type
== ROFFT_COMMENT
)
125 print_gen_comment(h
, n
);
126 t
= print_otag(h
, TAG_HEAD
, "");
127 print_man_head(man
, h
);
129 print_otag(h
, TAG_BODY
, "");
132 man_root_pre(man
, h
);
133 t
= print_otag(h
, TAG_DIV
, "c", "manual-text");
134 print_man_nodelist(man
, n
, h
);
136 man_root_post(man
, h
);
141 print_man_head(const struct roff_meta
*man
, struct html
*h
)
146 mandoc_asprintf(&cp
, "%s(%s)", man
->title
, man
->msec
);
147 print_otag(h
, TAG_TITLE
, "");
153 print_man_nodelist(MAN_ARGS
)
156 print_man_node(man
, n
, h
);
162 print_man_node(MAN_ARGS
)
167 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
170 html_fillmode(h
, n
->flags
& NODE_NOFILL
? ROFF_nf
: ROFF_fi
);
175 if (*n
->string
== '\0') {
179 if (*n
->string
== ' ' && n
->flags
& NODE_LINE
&&
180 (h
->flags
& HTML_NONEWLINE
) == 0)
181 print_otag(h
, TAG_BR
, "");
182 else if (n
->flags
& NODE_DELIMC
)
183 h
->flags
|= HTML_NOSPACE
;
186 print_text(h
, n
->string
);
191 print_eqn(h
, n
->eqn
);
195 * This will take care of initialising all of the table
196 * state data for the first table, then tearing it down
199 print_tbl(h
, n
->span
);
203 * Close out scope of font prior to opening a macro
206 if (h
->metac
!= ESCAPE_FONTROMAN
) {
208 h
->metac
= ESCAPE_FONTROMAN
;
212 * Close out the current table, if it's open, and unset
213 * the "meta" table state. This will be reopened on the
214 * next table element.
220 if (n
->tok
< ROFF_MAX
) {
226 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
227 if (man_html_acts
[n
->tok
- MAN_TH
].pre
!= NULL
)
228 child
= (*man_html_acts
[n
->tok
- MAN_TH
].pre
)(man
,
233 if (child
&& n
->child
!= NULL
)
234 print_man_nodelist(man
, n
->child
, h
);
236 /* This will automatically close out any font scope. */
238 if (n
->type
== ROFFT_BLOCK
&&
239 (n
->tok
== MAN_IP
|| n
->tok
== MAN_TP
|| n
->tok
== MAN_TQ
)) {
241 while (t
->tag
!= TAG_DL
&& t
->tag
!= TAG_UL
)
244 * Close the list if no further item of the same type
245 * follows; otherwise, close the item only.
247 if (list_continues(n
, n
->next
) == '\0') {
255 if (n
->flags
& NODE_NOFILL
&& n
->tok
!= MAN_YS
&&
256 (n
->next
!= NULL
&& n
->next
->flags
& NODE_LINE
)) {
257 /* In .nf = <pre>, print even empty lines. */
264 man_root_pre(const struct roff_meta
*man
, struct html
*h
)
271 mandoc_asprintf(&title
, "%s(%s)", man
->title
, man
->msec
);
273 t
= print_otag(h
, TAG_TABLE
, "c", "head");
274 tt
= print_otag(h
, TAG_TR
, "");
276 print_otag(h
, TAG_TD
, "c", "head-ltitle");
277 print_text(h
, title
);
280 print_otag(h
, TAG_TD
, "c", "head-vol");
281 if (man
->vol
!= NULL
)
282 print_text(h
, man
->vol
);
285 print_otag(h
, TAG_TD
, "c", "head-rtitle");
286 print_text(h
, title
);
292 man_root_post(const struct roff_meta
*man
, struct html
*h
)
296 t
= print_otag(h
, TAG_TABLE
, "c", "foot");
297 tt
= print_otag(h
, TAG_TR
, "");
299 print_otag(h
, TAG_TD
, "c", "foot-date");
300 print_text(h
, man
->date
);
303 print_otag(h
, TAG_TD
, "c", "foot-os");
305 print_text(h
, man
->os
);
316 if (n
->tok
== MAN_SH
) {
325 html_close_paragraph(h
);
326 print_otag(h
, TAG_SECTION
, "c", class);
329 id
= html_make_id(n
, 1);
330 print_otag(h
, tag
, "ci", class, id
);
332 print_otag(h
, TAG_A
, "chR", "permalink", id
);
343 man_alt_pre(MAN_ARGS
)
345 const struct roff_node
*nn
;
350 for (i
= 0, nn
= n
->child
; nn
!= NULL
; nn
= nn
->next
, i
++) {
353 fp
= i
% 2 ? TAG_I
: TAG_B
;
356 fp
= i
% 2 ? TAG_B
: TAG_I
;
359 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
362 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
365 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
368 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
375 h
->flags
|= HTML_NOSPACE
;
378 t
= print_otag(h
, fp
, "");
380 print_text(h
, nn
->string
);
391 print_otag(h
, TAG_SMALL
, "");
392 if (n
->tok
== MAN_SB
)
393 print_otag(h
, TAG_B
, "");
402 html_close_paragraph(h
);
407 if (n
->child
!= NULL
&&
408 (n
->child
->flags
& NODE_NOFILL
) == 0)
409 print_otag(h
, TAG_P
, "c",
410 n
->tok
== MAN_PP
? "Pp" : "Pp HP");
419 list_continues(const struct roff_node
*n1
, const struct roff_node
*n2
)
424 if (n1
== NULL
|| n1
->type
!= ROFFT_BLOCK
||
425 n2
== NULL
|| n2
->type
!= ROFFT_BLOCK
)
427 if ((n1
->tok
== MAN_TP
|| n1
->tok
== MAN_TQ
) &&
428 (n2
->tok
== MAN_TP
|| n2
->tok
== MAN_TQ
))
430 if (n1
->tok
!= MAN_IP
|| n2
->tok
!= MAN_IP
)
432 n1
= n1
->head
->child
;
433 n2
= n2
->head
->child
;
434 s1
= n1
== NULL
? "" : n1
->string
;
435 s2
= n2
== NULL
? "" : n2
->string
;
436 c1
= strcmp(s1
, "*") == 0 ? '*' :
437 strcmp(s1
, "\\-") == 0 ? '-' :
438 strcmp(s1
, "\\(bu") == 0 ? 'b' : ' ';
439 c2
= strcmp(s2
, "*") == 0 ? '*' :
440 strcmp(s2
, "\\-") == 0 ? '-' :
441 strcmp(s2
, "\\(bu") == 0 ? 'b' : ' ';
442 return c1
!= c2
? '\0' : c1
== 'b' ? '*' : c1
;
448 const struct roff_node
*nn
;
449 const char *list_class
;
450 enum htmltag list_elem
, body_elem
;
453 nn
= n
->type
== ROFFT_BLOCK
? n
: n
->parent
;
454 if ((list_type
= list_continues(nn
->prev
, nn
)) == '\0') {
455 /* Start a new list. */
456 if ((list_type
= list_continues(nn
, nn
->next
)) == '\0')
460 list_class
= "Bl-tag";
464 list_class
= "Bl-bullet";
468 list_class
= "Bl-dash";
475 /* Continue a list that was started earlier. */
479 body_elem
= list_type
== ' ' ? TAG_DD
: TAG_LI
;
483 html_close_paragraph(h
);
484 if (list_elem
!= TAG_MAX
)
485 print_otag(h
, list_elem
, "c", list_class
);
488 if (body_elem
== TAG_LI
)
490 print_otag(h
, TAG_DT
, "");
493 print_otag(h
, body_elem
, "");
500 case MAN_IP
: /* Only print the first header element. */
501 if (n
->child
!= NULL
)
502 print_man_node(man
, n
->child
, h
);
504 case MAN_TP
: /* Only print next-line header elements. */
507 while (nn
!= NULL
&& (NODE_LINE
& nn
->flags
) == 0)
510 print_man_node(man
, nn
, h
);
526 h
->flags
|= HTML_NOSPACE
;
527 tt
= print_otag(h
, TAG_SPAN
, "c", "Op");
529 if ((n
= n
->child
) != NULL
) {
530 print_otag(h
, TAG_B
, "");
531 print_text(h
, n
->string
);
536 if (n
!= NULL
&& n
->next
!= NULL
) {
537 print_otag(h
, TAG_I
, "");
538 print_text(h
, n
->next
->string
);
542 h
->flags
|= HTML_NOSPACE
;
550 print_otag(h
, TAG_B
, "");
557 print_otag(h
, TAG_I
, "");
564 print_otag(h
, TAG_BR
, "");
569 man_ign_pre(MAN_ARGS
)
579 html_close_paragraph(h
);
584 print_otag(h
, TAG_DIV
, "c", "Bd-indent");
597 html_close_paragraph(h
);
598 print_otag(h
, TAG_TABLE
, "c", "Nm");
599 print_otag(h
, TAG_TR
, "");
602 print_otag(h
, TAG_TD
, "");
603 print_otag(h
, TAG_CODE
, "c", "Nm");
606 print_otag(h
, TAG_TD
, "");
620 assert(n
->type
== ROFFT_HEAD
);
621 if (n
->child
!= NULL
) {
622 assert(n
->child
->type
== ROFFT_TEXT
);
623 if (n
->tok
== MAN_MT
) {
624 mandoc_asprintf(&cp
, "mailto:%s", n
->child
->string
);
625 print_otag(h
, TAG_A
, "ch", "Mt", cp
);
628 print_otag(h
, TAG_A
, "ch", "Lk", n
->child
->string
);
631 assert(n
->next
->type
== ROFFT_BODY
);
632 if (n
->next
->child
!= NULL
)
635 print_man_nodelist(man
, n
->child
, h
);
640 man_abort_pre(MAN_ARGS
)