1 /* $Id: man_html.c,v 1.156 2018/08/18 02:08:27 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013,2014,2015,2017,2018 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 fillmode(struct html
*, int);
54 static int man_B_pre(MAN_ARGS
);
55 static int man_HP_pre(MAN_ARGS
);
56 static int man_IP_pre(MAN_ARGS
);
57 static int man_I_pre(MAN_ARGS
);
58 static int man_OP_pre(MAN_ARGS
);
59 static int man_PP_pre(MAN_ARGS
);
60 static int man_RS_pre(MAN_ARGS
);
61 static int man_SH_pre(MAN_ARGS
);
62 static int man_SM_pre(MAN_ARGS
);
63 static int man_SS_pre(MAN_ARGS
);
64 static int man_SY_pre(MAN_ARGS
);
65 static int man_UR_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_PP_pre
, NULL
}, /* LP */
81 { man_PP_pre
, NULL
}, /* PP */
82 { man_PP_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
}, /* nf */
97 { NULL
, NULL
}, /* fi */
98 { NULL
, NULL
}, /* RE */
99 { man_RS_pre
, NULL
}, /* RS */
100 { man_ign_pre
, NULL
}, /* DT */
101 { man_ign_pre
, NULL
}, /* UC */
102 { man_ign_pre
, NULL
}, /* PD */
103 { man_ign_pre
, NULL
}, /* AT */
104 { man_in_pre
, NULL
}, /* in */
105 { man_SY_pre
, NULL
}, /* SY */
106 { NULL
, NULL
}, /* YS */
107 { man_OP_pre
, NULL
}, /* OP */
108 { NULL
, NULL
}, /* EX */
109 { NULL
, NULL
}, /* EE */
110 { man_UR_pre
, NULL
}, /* UR */
111 { NULL
, NULL
}, /* UE */
112 { man_UR_pre
, NULL
}, /* MT */
113 { NULL
, NULL
}, /* ME */
118 * Printing leading vertical space before a block.
119 * This is used for the paragraph macros.
120 * The rules are pretty simple, since there's very little nesting going
121 * on here. Basically, if we're the first within another block (SS/SH),
122 * then don't emit vertical space. If we are (RS), then do. If not the
126 print_bvspace(struct html
*h
, const struct roff_node
*n
)
129 if (n
->body
&& n
->body
->child
)
130 if (n
->body
->child
->type
== ROFFT_TBL
)
133 if (n
->parent
->type
== ROFFT_ROOT
|| n
->parent
->tok
!= MAN_RS
)
141 html_man(void *arg
, const struct roff_man
*man
)
147 h
= (struct html
*)arg
;
148 n
= man
->first
->child
;
150 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
152 print_otag(h
, TAG_HTML
, "");
153 if (n
->type
== ROFFT_COMMENT
)
154 print_gen_comment(h
, n
);
155 t
= print_otag(h
, TAG_HEAD
, "");
156 print_man_head(&man
->meta
, h
);
158 print_otag(h
, TAG_BODY
, "");
161 man_root_pre(&man
->meta
, h
);
162 t
= print_otag(h
, TAG_DIV
, "c", "manual-text");
163 print_man_nodelist(&man
->meta
, n
, h
);
165 man_root_post(&man
->meta
, h
);
170 print_man_head(const struct roff_meta
*man
, struct html
*h
)
175 mandoc_asprintf(&cp
, "%s(%s)", man
->title
, man
->msec
);
176 print_otag(h
, TAG_TITLE
, "");
182 print_man_nodelist(MAN_ARGS
)
186 print_man_node(man
, n
, h
);
192 print_man_node(MAN_ARGS
)
194 static int want_fillmode
= MAN_fi
;
195 static int save_fillmode
;
201 * Handle fill mode switch requests up front,
202 * they would just cause trouble in the subsequent code.
208 want_fillmode
= MAN_nf
;
212 want_fillmode
= MAN_fi
;
213 if (fillmode(h
, 0) == MAN_fi
)
214 print_otag(h
, TAG_BR
, "");
220 /* Set up fill mode for the upcoming node. */
225 /* Some block macros suspend or cancel .nf. */
227 case MAN_TP
: /* Tagged paragraphs */
228 case MAN_IP
: /* temporarily disable .nf */
229 case MAN_HP
: /* for the head. */
230 save_fillmode
= want_fillmode
;
232 case MAN_SH
: /* Section headers */
233 case MAN_SS
: /* permanently cancel .nf. */
234 want_fillmode
= MAN_fi
;
236 case MAN_PP
: /* These have no head. */
237 case MAN_LP
: /* They will simply */
238 case MAN_P
: /* reopen .nf in the body. */
253 * Some in-line macros produce tags and/or text
254 * in the handler, so they require fill mode to be
255 * configured up front just like for text nodes.
256 * For the others, keep the traditional approach
257 * of doing the same, for now.
259 fillmode(h
, want_fillmode
);
262 if (fillmode(h
, want_fillmode
) == MAN_fi
&&
263 want_fillmode
== MAN_fi
&&
264 n
->flags
& NODE_LINE
&& *n
->string
== ' ' &&
265 (h
->flags
& HTML_NONEWLINE
) == 0)
266 print_otag(h
, TAG_BR
, "");
267 if (*n
->string
!= '\0')
277 /* Produce output for this node. */
283 print_text(h
, n
->string
);
287 print_eqn(h
, n
->eqn
);
291 * This will take care of initialising all of the table
292 * state data for the first table, then tearing it down
295 print_tbl(h
, n
->span
);
299 * Close out scope of font prior to opening a macro
302 if (HTMLFONT_NONE
!= h
->metac
) {
304 h
->metac
= HTMLFONT_NONE
;
308 * Close out the current table, if it's open, and unset
309 * the "meta" table state. This will be reopened on the
310 * next table element.
316 if (n
->tok
< ROFF_MAX
) {
322 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
323 if (man_html_acts
[n
->tok
- MAN_TH
].pre
!= NULL
)
324 child
= (*man_html_acts
[n
->tok
- MAN_TH
].pre
)(man
,
327 /* Some block macros resume .nf in the body. */
328 if (save_fillmode
&& n
->type
== ROFFT_BODY
)
329 want_fillmode
= save_fillmode
;
334 if (child
&& n
->child
)
335 print_man_nodelist(man
, n
->child
, h
);
337 /* This will automatically close out any font scope. */
340 if (fillmode(h
, 0) == MAN_nf
&&
341 n
->next
!= NULL
&& n
->next
->flags
& NODE_LINE
)
346 * MAN_nf switches to no-fill mode, MAN_fi to fill mode.
347 * Other arguments do not switch.
348 * The old mode is returned.
351 fillmode(struct html
*h
, int want
)
356 for (pre
= h
->tag
; pre
!= NULL
; pre
= pre
->next
)
357 if (pre
->tag
== TAG_PRE
)
360 had
= pre
== NULL
? MAN_fi
: MAN_nf
;
362 if (want
&& want
!= had
) {
364 print_otag(h
, TAG_PRE
, "");
372 man_root_pre(const struct roff_meta
*man
, struct html
*h
)
379 mandoc_asprintf(&title
, "%s(%s)", man
->title
, man
->msec
);
381 t
= print_otag(h
, TAG_TABLE
, "c", "head");
382 tt
= print_otag(h
, TAG_TR
, "");
384 print_otag(h
, TAG_TD
, "c", "head-ltitle");
385 print_text(h
, title
);
388 print_otag(h
, TAG_TD
, "c", "head-vol");
389 if (NULL
!= man
->vol
)
390 print_text(h
, man
->vol
);
393 print_otag(h
, TAG_TD
, "c", "head-rtitle");
394 print_text(h
, title
);
400 man_root_post(const struct roff_meta
*man
, struct html
*h
)
404 t
= print_otag(h
, TAG_TABLE
, "c", "foot");
405 tt
= print_otag(h
, TAG_TR
, "");
407 print_otag(h
, TAG_TD
, "c", "foot-date");
408 print_text(h
, man
->date
);
411 print_otag(h
, TAG_TD
, "c", "foot-os");
413 print_text(h
, man
->os
);
422 if (n
->type
== ROFFT_HEAD
) {
423 id
= html_make_id(n
, 1);
424 print_otag(h
, TAG_H1
, "cTi", "Sh", id
);
426 print_otag(h
, TAG_A
, "chR", "permalink", id
);
432 man_alt_pre(MAN_ARGS
)
434 const struct roff_node
*nn
;
439 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
442 fp
= i
% 2 ? TAG_I
: TAG_B
;
445 fp
= i
% 2 ? TAG_B
: TAG_I
;
448 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
451 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
454 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
457 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
464 h
->flags
|= HTML_NOSPACE
;
467 t
= print_otag(h
, fp
, "");
469 print_text(h
, nn
->string
);
480 print_otag(h
, TAG_SMALL
, "");
481 if (MAN_SB
== n
->tok
)
482 print_otag(h
, TAG_B
, "");
491 if (n
->type
== ROFFT_HEAD
) {
492 id
= html_make_id(n
, 1);
493 print_otag(h
, TAG_H2
, "cTi", "Ss", id
);
495 print_otag(h
, TAG_A
, "chR", "permalink", id
);
504 if (n
->type
== ROFFT_HEAD
)
506 else if (n
->type
== ROFFT_BLOCK
)
515 const struct roff_node
*nn
;
517 if (n
->type
== ROFFT_BODY
) {
518 print_otag(h
, TAG_DD
, "");
520 } else if (n
->type
!= ROFFT_HEAD
) {
521 print_otag(h
, TAG_DL
, "c", "Bl-tag");
525 print_otag(h
, TAG_DT
, "");
528 case MAN_IP
: /* Only print the first header element. */
529 if (n
->child
!= NULL
)
530 print_man_node(man
, n
->child
, h
);
532 case MAN_TP
: /* Only print next-line header elements. */
535 while (nn
!= NULL
&& (NODE_LINE
& nn
->flags
) == 0)
538 print_man_node(man
, nn
, h
);
552 if (n
->type
== ROFFT_HEAD
)
555 if (n
->type
== ROFFT_BLOCK
) {
557 print_otag(h
, TAG_DIV
, "c", "HP");
568 h
->flags
|= HTML_NOSPACE
;
569 tt
= print_otag(h
, TAG_SPAN
, "c", "Op");
571 if (NULL
!= (n
= n
->child
)) {
572 print_otag(h
, TAG_B
, "");
573 print_text(h
, n
->string
);
578 if (NULL
!= n
&& NULL
!= n
->next
) {
579 print_otag(h
, TAG_I
, "");
580 print_text(h
, n
->next
->string
);
584 h
->flags
|= HTML_NOSPACE
;
592 print_otag(h
, TAG_B
, "");
599 print_otag(h
, TAG_I
, "");
606 print_otag(h
, TAG_BR
, "");
611 man_ign_pre(MAN_ARGS
)
620 if (n
->type
== ROFFT_HEAD
)
622 if (n
->type
== ROFFT_BLOCK
)
623 print_otag(h
, TAG_DIV
, "c", "Bd-indent");
632 print_otag(h
, TAG_TABLE
, "c", "Nm");
633 print_otag(h
, TAG_TR
, "");
636 print_otag(h
, TAG_TD
, "");
637 print_otag(h
, TAG_CODE
, "cT", "Nm");
640 print_otag(h
, TAG_TD
, "");
653 assert(n
->type
== ROFFT_HEAD
);
654 if (n
->child
!= NULL
) {
655 assert(n
->child
->type
== ROFFT_TEXT
);
656 if (n
->tok
== MAN_MT
) {
657 mandoc_asprintf(&cp
, "mailto:%s", n
->child
->string
);
658 print_otag(h
, TAG_A
, "cTh", "Mt", cp
);
661 print_otag(h
, TAG_A
, "cTh", "Lk", n
->child
->string
);
664 assert(n
->next
->type
== ROFFT_BODY
);
665 if (n
->next
->child
!= NULL
)
668 print_man_nodelist(man
, n
->child
, h
);