1 /* $Id: man_html.c,v 1.184 2022/07/06 16:05:40 schwarze Exp $ */
3 * Copyright (c) 2013-2015,2017-2020,2022 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
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.
18 * HTML formatter for man(7) used by mandoc(1).
22 #include <sys/types.h>
30 #include "mandoc_aux.h"
38 #define MAN_ARGS const struct roff_meta *man, \
39 struct roff_node *n, \
44 int (*post
)(MAN_ARGS
);
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 char list_continues(const struct roff_node
*,
52 const struct roff_node
*);
53 static int man_B_pre(MAN_ARGS
);
54 static int man_IP_pre(MAN_ARGS
);
55 static int man_I_pre(MAN_ARGS
);
56 static int man_OP_pre(MAN_ARGS
);
57 static int man_PP_pre(MAN_ARGS
);
58 static int man_RS_pre(MAN_ARGS
);
59 static int man_SH_pre(MAN_ARGS
);
60 static int man_SM_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_SH_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_PP_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 html_man(void *arg
, const struct roff_meta
*man
)
120 h
= (struct html
*)arg
;
121 n
= man
->first
->child
;
123 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
125 print_otag(h
, TAG_HTML
, "");
126 t
= print_otag(h
, TAG_HEAD
, "");
127 print_man_head(man
, h
);
129 if (n
!= NULL
&& n
->type
== ROFFT_COMMENT
)
130 print_gen_comment(h
, n
);
131 print_otag(h
, TAG_BODY
, "");
134 man_root_pre(man
, h
);
135 t
= print_otag(h
, TAG_MAIN
, "c", "manual-text");
136 print_man_nodelist(man
, n
, h
);
138 man_root_post(man
, h
);
143 print_man_head(const struct roff_meta
*man
, struct html
*h
)
148 mandoc_asprintf(&cp
, "%s(%s)", man
->title
, man
->msec
);
149 print_otag(h
, TAG_TITLE
, "");
155 print_man_nodelist(MAN_ARGS
)
158 print_man_node(man
, n
, h
);
164 print_man_node(MAN_ARGS
)
169 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
172 if ((n
->flags
& NODE_NOFILL
) == 0)
173 html_fillmode(h
, ROFF_fi
);
174 else if (html_fillmode(h
, ROFF_nf
) == ROFF_nf
&&
175 n
->tok
!= ROFF_fi
&& n
->flags
& NODE_LINE
&&
176 (n
->prev
== NULL
|| n
->prev
->tok
!= MAN_YS
))
182 if (*n
->string
== '\0') {
186 if (*n
->string
== ' ' && n
->flags
& NODE_LINE
&&
187 (h
->flags
& HTML_NONEWLINE
) == 0)
188 print_otag(h
, TAG_BR
, "");
189 else if (n
->flags
& NODE_DELIMC
)
190 h
->flags
|= HTML_NOSPACE
;
193 print_text(h
, n
->string
);
198 print_eqn(h
, n
->eqn
);
202 * This will take care of initialising all of the table
203 * state data for the first table, then tearing it down
206 print_tbl(h
, n
->span
);
210 * Close out scope of font prior to opening a macro
213 if (h
->metac
!= ESCAPE_FONTROMAN
) {
215 h
->metac
= ESCAPE_FONTROMAN
;
219 * Close out the current table, if it's open, and unset
220 * the "meta" table state. This will be reopened on the
221 * next table element.
227 if (n
->tok
< ROFF_MAX
) {
233 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
234 if (man_html_acts
[n
->tok
- MAN_TH
].pre
!= NULL
)
235 child
= (*man_html_acts
[n
->tok
- MAN_TH
].pre
)(man
,
240 if (child
&& n
->child
!= NULL
)
241 print_man_nodelist(man
, n
->child
, h
);
243 /* This will automatically close out any font scope. */
245 if (n
->type
== ROFFT_BLOCK
&&
246 (n
->tok
== MAN_IP
|| n
->tok
== MAN_TP
|| n
->tok
== MAN_TQ
)) {
248 while (t
->tag
!= TAG_DL
&& t
->tag
!= TAG_UL
)
251 * Close the list if no further item of the same type
252 * follows; otherwise, close the item only.
254 if (list_continues(n
, roff_node_next(n
)) == '\0') {
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_DIV
, "cr?", "head", "doc-pageheader",
274 "aria-label", "Manual header line");
276 print_otag(h
, TAG_SPAN
, "c", "head-ltitle");
277 print_text(h
, title
);
280 print_otag(h
, TAG_SPAN
, "c", "head-vol");
281 if (man
->vol
!= NULL
)
282 print_text(h
, man
->vol
);
285 print_otag(h
, TAG_SPAN
, "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_DIV
, "cr?", "foot", "doc-pagefooter",
297 "aria-label", "Manual footer line");
299 print_otag(h
, TAG_SPAN
, "c", "foot-left");
302 print_otag(h
, TAG_SPAN
, "c", "foot-date");
303 print_text(h
, man
->date
);
306 print_otag(h
, TAG_SPAN
, "c", "foot-os");
308 print_text(h
, man
->os
);
318 if (n
->tok
== MAN_SH
) {
327 html_close_paragraph(h
);
328 print_otag(h
, TAG_SECTION
, "c", class);
331 print_otag_id(h
, tag
, class, n
);
342 man_alt_pre(MAN_ARGS
)
344 const struct roff_node
*nn
;
349 for (i
= 0, nn
= n
->child
; nn
!= NULL
; nn
= nn
->next
, i
++) {
352 fp
= i
% 2 ? TAG_I
: TAG_B
;
355 fp
= i
% 2 ? TAG_B
: TAG_I
;
358 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
361 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
364 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
367 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
374 h
->flags
|= HTML_NOSPACE
;
377 t
= print_otag(h
, fp
, "");
379 print_text(h
, nn
->string
);
390 print_otag(h
, TAG_SMALL
, "");
391 if (n
->tok
== MAN_SB
)
392 print_otag(h
, TAG_B
, "");
401 html_close_paragraph(h
);
406 if (n
->child
!= NULL
&&
407 (n
->child
->flags
& NODE_NOFILL
) == 0)
408 print_otag(h
, TAG_P
, "c",
409 n
->tok
== MAN_PP
? "Pp" : "Pp HP");
418 list_continues(const struct roff_node
*n1
, const struct roff_node
*n2
)
423 if (n1
== NULL
|| n1
->type
!= ROFFT_BLOCK
||
424 n2
== NULL
|| n2
->type
!= ROFFT_BLOCK
)
426 if ((n1
->tok
== MAN_TP
|| n1
->tok
== MAN_TQ
) &&
427 (n2
->tok
== MAN_TP
|| n2
->tok
== MAN_TQ
))
429 if (n1
->tok
!= MAN_IP
|| n2
->tok
!= MAN_IP
)
431 n1
= n1
->head
->child
;
432 n2
= n2
->head
->child
;
433 s1
= n1
== NULL
? "" : n1
->string
;
434 s2
= n2
== NULL
? "" : n2
->string
;
435 c1
= strcmp(s1
, "*") == 0 ? '*' :
436 strcmp(s1
, "\\-") == 0 ? '-' :
437 strcmp(s1
, "\\(bu") == 0 ? 'b' : ' ';
438 c2
= strcmp(s2
, "*") == 0 ? '*' :
439 strcmp(s2
, "\\-") == 0 ? '-' :
440 strcmp(s2
, "\\(bu") == 0 ? 'b' : ' ';
441 return c1
!= c2
? '\0' : c1
== 'b' ? '*' : c1
;
447 struct roff_node
*nn
;
448 const char *list_class
;
449 enum htmltag list_elem
, body_elem
;
452 nn
= n
->type
== ROFFT_BLOCK
? n
: n
->parent
;
453 list_type
= list_continues(roff_node_prev(nn
), nn
);
454 if (list_type
== '\0') {
455 /* Start a new list. */
456 list_type
= list_continues(nn
, roff_node_next(nn
));
457 if (list_type
== '\0')
461 list_class
= "Bl-tag";
465 list_class
= "Bl-bullet";
469 list_class
= "Bl-dash";
476 /* Continue a list that was started earlier. */
480 body_elem
= list_type
== ' ' ? TAG_DD
: TAG_LI
;
484 html_close_paragraph(h
);
485 if (list_elem
!= TAG_MAX
)
486 print_otag(h
, list_elem
, "c", list_class
);
489 if (body_elem
== TAG_LI
)
491 print_otag_id(h
, TAG_DT
, NULL
, n
);
494 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
)