1 /* $Id: man_html.c,v 1.153 2018/07/27 17:49:31 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_UR_pre(MAN_ARGS);
65 static int man_alt_pre(MAN_ARGS);
66 static int man_ign_pre(MAN_ARGS);
67 static int man_in_pre(MAN_ARGS);
68 static void man_root_post(const struct roff_meta *,
70 static void man_root_pre(const struct roff_meta *,
73 static const struct htmlman __mans[MAN_MAX - MAN_TH] = {
74 { NULL, NULL }, /* TH */
75 { man_SH_pre, NULL }, /* SH */
76 { man_SS_pre, NULL }, /* SS */
77 { man_IP_pre, NULL }, /* TP */
78 { man_PP_pre, NULL }, /* LP */
79 { man_PP_pre, NULL }, /* PP */
80 { man_PP_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 }, /* nf */
95 { NULL, NULL }, /* fi */
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_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 */
111 static const struct htmlman *const mans = __mans - MAN_TH;
115 * Printing leading vertical space before a block.
116 * This is used for the paragraph macros.
117 * The rules are pretty simple, since there's very little nesting going
118 * on here. Basically, if we're the first within another block (SS/SH),
119 * then don't emit vertical space. If we are (RS), then do. If not the
123 print_bvspace(struct html *h, const struct roff_node *n)
126 if (n->body && n->body->child)
127 if (n->body->child->type == ROFFT_TBL)
130 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
138 html_man(void *arg, const struct roff_man *man)
144 h = (struct html *)arg;
145 n = man->first->child;
147 if ((h->oflags & HTML_FRAGMENT) == 0) {
149 print_otag(h, TAG_HTML, "");
150 if (n->type == ROFFT_COMMENT)
151 print_gen_comment(h, n);
152 t = print_otag(h, TAG_HEAD, "");
153 print_man_head(&man->meta, h);
155 print_otag(h, TAG_BODY, "");
158 man_root_pre(&man->meta, h);
159 t = print_otag(h, TAG_DIV, "c", "manual-text");
160 print_man_nodelist(&man->meta, n, h);
162 man_root_post(&man->meta, h);
167 print_man_head(const struct roff_meta *man, struct html *h)
172 mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
173 print_otag(h, TAG_TITLE, "");
179 print_man_nodelist(MAN_ARGS)
183 print_man_node(man, n, h);
189 print_man_node(MAN_ARGS)
191 static int want_fillmode = MAN_fi;
192 static int save_fillmode;
198 * Handle fill mode switch requests up front,
199 * they would just cause trouble in the subsequent code.
205 want_fillmode = MAN_nf;
209 want_fillmode = MAN_fi;
210 if (fillmode(h, 0) == MAN_fi)
211 print_otag(h, TAG_BR, "");
217 /* Set up fill mode for the upcoming node. */
222 /* Some block macros suspend or cancel .nf. */
224 case MAN_TP: /* Tagged paragraphs */
225 case MAN_IP: /* temporarily disable .nf */
226 case MAN_HP: /* for the head. */
227 save_fillmode = want_fillmode;
229 case MAN_SH: /* Section headers */
230 case MAN_SS: /* permanently cancel .nf. */
231 want_fillmode = MAN_fi;
233 case MAN_PP: /* These have no head. */
234 case MAN_LP: /* They will simply */
235 case MAN_P: /* reopen .nf in the body. */
250 * Some in-line macros produce tags and/or text
251 * in the handler, so they require fill mode to be
252 * configured up front just like for text nodes.
253 * For the others, keep the traditional approach
254 * of doing the same, for now.
256 fillmode(h, want_fillmode);
259 if (fillmode(h, want_fillmode) == MAN_fi &&
260 want_fillmode == MAN_fi &&
261 n->flags & NODE_LINE && *n->string == ' ' &&
262 (h->flags & HTML_NONEWLINE) == 0)
263 print_otag(h, TAG_BR, "");
264 if (*n->string != '\0')
274 /* Produce output for this node. */
280 print_text(h, n->string);
284 print_eqn(h, n->eqn);
288 * This will take care of initialising all of the table
289 * state data for the first table, then tearing it down
292 print_tbl(h, n->span);
296 * Close out scope of font prior to opening a macro
299 if (HTMLFONT_NONE != h->metac) {
301 h->metac = HTMLFONT_NONE;
305 * Close out the current table, if it's open, and unset
306 * the "meta" table state. This will be reopened on the
307 * next table element.
313 if (n->tok < ROFF_MAX) {
319 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
320 if (mans[n->tok].pre)
321 child = (*mans[n->tok].pre)(man, n, h);
323 /* Some block macros resume .nf in the body. */
324 if (save_fillmode && n->type == ROFFT_BODY)
325 want_fillmode = save_fillmode;
330 if (child && n->child)
331 print_man_nodelist(man, n->child, h);
333 /* This will automatically close out any font scope. */
336 if (fillmode(h, 0) == MAN_nf &&
337 n->next != NULL && n->next->flags & NODE_LINE)
342 * MAN_nf switches to no-fill mode, MAN_fi to fill mode.
343 * Other arguments do not switch.
344 * The old mode is returned.
347 fillmode(struct html *h, int want)
352 for (pre = h->tag; pre != NULL; pre = pre->next)
353 if (pre->tag == TAG_PRE)
356 had = pre == NULL ? MAN_fi : MAN_nf;
358 if (want && want != had) {
360 print_otag(h, TAG_PRE, "");
368 man_root_pre(const struct roff_meta *man, struct html *h)
375 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
377 t = print_otag(h, TAG_TABLE, "c", "head");
378 tt = print_otag(h, TAG_TR, "");
380 print_otag(h, TAG_TD, "c", "head-ltitle");
381 print_text(h, title);
384 print_otag(h, TAG_TD, "c", "head-vol");
385 if (NULL != man->vol)
386 print_text(h, man->vol);
389 print_otag(h, TAG_TD, "c", "head-rtitle");
390 print_text(h, title);
396 man_root_post(const struct roff_meta *man, struct html *h)
400 t = print_otag(h, TAG_TABLE, "c", "foot");
401 tt = print_otag(h, TAG_TR, "");
403 print_otag(h, TAG_TD, "c", "foot-date");
404 print_text(h, man->date);
407 print_otag(h, TAG_TD, "c", "foot-os");
409 print_text(h, man->os);
418 if (n->type == ROFFT_HEAD) {
419 id = html_make_id(n, 1);
420 print_otag(h, TAG_H1, "cTi", "Sh", id);
422 print_otag(h, TAG_A, "chR", "permalink", id);
428 man_alt_pre(MAN_ARGS)
430 const struct roff_node *nn;
435 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
438 fp = i % 2 ? TAG_I : TAG_B;
441 fp = i % 2 ? TAG_B : TAG_I;
444 fp = i % 2 ? TAG_I : TAG_MAX;
447 fp = i % 2 ? TAG_MAX : TAG_I;
450 fp = i % 2 ? TAG_MAX : TAG_B;
453 fp = i % 2 ? TAG_B : TAG_MAX;
460 h->flags |= HTML_NOSPACE;
463 t = print_otag(h, fp, "");
465 print_text(h, nn->string);
476 print_otag(h, TAG_SMALL, "");
477 if (MAN_SB == n->tok)
478 print_otag(h, TAG_B, "");
487 if (n->type == ROFFT_HEAD) {
488 id = html_make_id(n, 1);
489 print_otag(h, TAG_H2, "cTi", "Ss", id);
491 print_otag(h, TAG_A, "chR", "permalink", id);
500 if (n->type == ROFFT_HEAD)
502 else if (n->type == ROFFT_BLOCK)
511 const struct roff_node *nn;
513 if (n->type == ROFFT_BODY) {
514 print_otag(h, TAG_DD, "");
516 } else if (n->type != ROFFT_HEAD) {
517 print_otag(h, TAG_DL, "c", "Bl-tag");
521 /* FIXME: width specification. */
523 print_otag(h, TAG_DT, "");
525 /* For IP, only print the first header element. */
527 if (MAN_IP == n->tok && n->child)
528 print_man_node(man, n->child, h);
530 /* For TP, only print next-line header elements. */
532 if (MAN_TP == n->tok) {
534 while (NULL != nn && 0 == (NODE_LINE & nn->flags))
537 print_man_node(man, nn, h);
548 if (n->type == ROFFT_HEAD)
551 if (n->type == ROFFT_BLOCK) {
553 print_otag(h, TAG_DIV, "c", "HP");
564 h->flags |= HTML_NOSPACE;
565 tt = print_otag(h, TAG_SPAN, "c", "Op");
567 if (NULL != (n = n->child)) {
568 print_otag(h, TAG_B, "");
569 print_text(h, n->string);
574 if (NULL != n && NULL != n->next) {
575 print_otag(h, TAG_I, "");
576 print_text(h, n->next->string);
580 h->flags |= HTML_NOSPACE;
588 print_otag(h, TAG_B, "");
595 print_otag(h, TAG_I, "");
602 print_otag(h, TAG_BR, "");
607 man_ign_pre(MAN_ARGS)
616 if (n->type == ROFFT_HEAD)
618 if (n->type == ROFFT_BLOCK)
619 print_otag(h, TAG_DIV, "c", "Bd-indent");
628 assert(n->type == ROFFT_HEAD);
629 if (n->child != NULL) {
630 assert(n->child->type == ROFFT_TEXT);
631 if (n->tok == MAN_MT) {
632 mandoc_asprintf(&cp, "mailto:%s", n->child->string);
633 print_otag(h, TAG_A, "cTh", "Mt", cp);
636 print_otag(h, TAG_A, "cTh", "Lk", n->child->string);
639 assert(n->next->type == ROFFT_BODY);
640 if (n->next->child != NULL)
643 print_man_nodelist(man, n->child, h);