1 /* $Id: man_html.c,v 1.145 2017/06/25 11:42:02 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015, 2017 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. */
40 #define MAN_ARGS const struct roff_meta *man, \
41 const struct roff_node *n, \
46 int (*post)(MAN_ARGS);
49 static void print_bvspace(struct html *,
50 const struct roff_node *);
51 static void print_man_head(MAN_ARGS);
52 static void print_man_nodelist(MAN_ARGS);
53 static void print_man_node(MAN_ARGS);
54 static int fillmode(struct html *, int);
55 static int a2width(const struct roff_node *,
57 static int man_B_pre(MAN_ARGS);
58 static int man_HP_pre(MAN_ARGS);
59 static int man_IP_pre(MAN_ARGS);
60 static int man_I_pre(MAN_ARGS);
61 static int man_OP_pre(MAN_ARGS);
62 static int man_PP_pre(MAN_ARGS);
63 static int man_RS_pre(MAN_ARGS);
64 static int man_SH_pre(MAN_ARGS);
65 static int man_SM_pre(MAN_ARGS);
66 static int man_SS_pre(MAN_ARGS);
67 static int man_UR_pre(MAN_ARGS);
68 static int man_alt_pre(MAN_ARGS);
69 static int man_ign_pre(MAN_ARGS);
70 static int man_in_pre(MAN_ARGS);
71 static void man_root_post(MAN_ARGS);
72 static void man_root_pre(MAN_ARGS);
74 static const struct htmlman __mans[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_PP_pre, NULL }, /* LP */
80 { man_PP_pre, NULL }, /* PP */
81 { man_PP_pre, NULL }, /* P */
82 { man_IP_pre, NULL }, /* IP */
83 { man_HP_pre, NULL }, /* HP */
84 { man_SM_pre, NULL }, /* SM */
85 { man_SM_pre, NULL }, /* SB */
86 { man_alt_pre, NULL }, /* BI */
87 { man_alt_pre, NULL }, /* IB */
88 { man_alt_pre, NULL }, /* BR */
89 { man_alt_pre, NULL }, /* RB */
90 { NULL, NULL }, /* R */
91 { man_B_pre, NULL }, /* B */
92 { man_I_pre, NULL }, /* I */
93 { man_alt_pre, NULL }, /* IR */
94 { man_alt_pre, NULL }, /* RI */
95 { NULL, NULL }, /* nf */
96 { NULL, NULL }, /* fi */
97 { NULL, NULL }, /* RE */
98 { man_RS_pre, NULL }, /* RS */
99 { man_ign_pre, NULL }, /* DT */
100 { man_ign_pre, NULL }, /* UC */
101 { man_ign_pre, NULL }, /* PD */
102 { man_ign_pre, NULL }, /* AT */
103 { man_in_pre, NULL }, /* in */
104 { man_OP_pre, NULL }, /* OP */
105 { NULL, NULL }, /* EX */
106 { NULL, NULL }, /* EE */
107 { man_UR_pre, NULL }, /* UR */
108 { NULL, NULL }, /* UE */
109 { man_UR_pre, NULL }, /* MT */
110 { NULL, NULL }, /* ME */
112 static const struct htmlman *const mans = __mans - MAN_TH;
116 * Printing leading vertical space before a block.
117 * This is used for the paragraph macros.
118 * The rules are pretty simple, since there's very little nesting going
119 * on here. Basically, if we're the first within another block (SS/SH),
120 * then don't emit vertical space. If we are (RS), then do. If not the
124 print_bvspace(struct html *h, const struct roff_node *n)
127 if (n->body && n->body->child)
128 if (n->body->child->type == ROFFT_TBL)
131 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
139 html_man(void *arg, const struct roff_man *man)
144 h = (struct html *)arg;
146 if ((h->oflags & HTML_FRAGMENT) == 0) {
148 print_otag(h, TAG_HTML, "");
149 t = print_otag(h, TAG_HEAD, "");
150 print_man_head(&man->meta, man->first, h);
152 print_otag(h, TAG_BODY, "");
155 man_root_pre(&man->meta, man->first, h);
156 t = print_otag(h, TAG_DIV, "c", "manual-text");
157 print_man_nodelist(&man->meta, man->first->child, h);
159 man_root_post(&man->meta, man->first, h);
164 print_man_head(MAN_ARGS)
169 mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
170 print_otag(h, TAG_TITLE, "");
176 print_man_nodelist(MAN_ARGS)
180 print_man_node(man, n, h);
186 print_man_node(MAN_ARGS)
188 static int want_fillmode = MAN_fi;
189 static int save_fillmode;
195 * Handle fill mode switch requests up front,
196 * they would just cause trouble in the subsequent code.
202 want_fillmode = MAN_nf;
206 want_fillmode = MAN_fi;
207 if (fillmode(h, 0) == MAN_fi)
208 print_otag(h, TAG_BR, "");
214 /* Set up fill mode for the upcoming node. */
219 /* Some block macros suspend or cancel .nf. */
221 case MAN_TP: /* Tagged paragraphs */
222 case MAN_IP: /* temporarily disable .nf */
223 case MAN_HP: /* for the head. */
224 save_fillmode = want_fillmode;
226 case MAN_SH: /* Section headers */
227 case MAN_SS: /* permanently cancel .nf. */
228 want_fillmode = MAN_fi;
230 case MAN_PP: /* These have no head. */
231 case MAN_LP: /* They will simply */
232 case MAN_P: /* reopen .nf in the body. */
247 * Some in-line macros produce tags and/or text
248 * in the handler, so they require fill mode to be
249 * configured up front just like for text nodes.
250 * For the others, keep the traditional approach
251 * of doing the same, for now.
253 fillmode(h, want_fillmode);
256 if (fillmode(h, want_fillmode) == MAN_fi &&
257 want_fillmode == MAN_fi &&
258 n->flags & NODE_LINE && *n->string == ' ' &&
259 (h->flags & HTML_NONEWLINE) == 0)
260 print_otag(h, TAG_BR, "");
261 if (*n->string != '\0')
269 /* Produce output for this node. */
275 print_text(h, n->string);
279 print_eqn(h, n->eqn);
283 * This will take care of initialising all of the table
284 * state data for the first table, then tearing it down
287 print_tbl(h, n->span);
291 * Close out scope of font prior to opening a macro
294 if (HTMLFONT_NONE != h->metac) {
296 h->metac = HTMLFONT_NONE;
300 * Close out the current table, if it's open, and unset
301 * the "meta" table state. This will be reopened on the
302 * next table element.
308 if (n->tok < ROFF_MAX) {
314 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
315 if (mans[n->tok].pre)
316 child = (*mans[n->tok].pre)(man, n, h);
318 /* Some block macros resume .nf in the body. */
319 if (save_fillmode && n->type == ROFFT_BODY)
320 want_fillmode = save_fillmode;
325 if (child && n->child)
326 print_man_nodelist(man, n->child, h);
328 /* This will automatically close out any font scope. */
331 if (fillmode(h, 0) == MAN_nf &&
332 n->next != NULL && n->next->flags & NODE_LINE)
337 * MAN_nf switches to no-fill mode, MAN_fi to fill mode.
338 * Other arguments do not switch.
339 * The old mode is returned.
342 fillmode(struct html *h, int want)
347 for (pre = h->tag; pre != NULL; pre = pre->next)
348 if (pre->tag == TAG_PRE)
351 had = pre == NULL ? MAN_fi : MAN_nf;
353 if (want && want != had) {
355 print_otag(h, TAG_PRE, "");
363 a2width(const struct roff_node *n, struct roffsu *su)
365 if (n->type != ROFFT_TEXT)
367 return a2roffsu(n->string, su, SCALE_EN) != NULL;
371 man_root_pre(MAN_ARGS)
378 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
380 t = print_otag(h, TAG_TABLE, "c", "head");
381 tt = print_otag(h, TAG_TR, "");
383 print_otag(h, TAG_TD, "c", "head-ltitle");
384 print_text(h, title);
387 print_otag(h, TAG_TD, "c", "head-vol");
388 if (NULL != man->vol)
389 print_text(h, man->vol);
392 print_otag(h, TAG_TD, "c", "head-rtitle");
393 print_text(h, title);
399 man_root_post(MAN_ARGS)
403 t = print_otag(h, TAG_TABLE, "c", "foot");
404 tt = print_otag(h, TAG_TR, "");
406 print_otag(h, TAG_TD, "c", "foot-date");
407 print_text(h, man->date);
410 print_otag(h, TAG_TD, "c", "foot-os");
412 print_text(h, man->os);
421 if (n->type == ROFFT_HEAD) {
422 id = html_make_id(n);
423 print_otag(h, TAG_H1, "cTi", "Sh", id);
425 print_otag(h, TAG_A, "chR", "selflink", 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);
493 print_otag(h, TAG_H2, "cTi", "Ss", id);
495 print_otag(h, TAG_A, "chR", "selflink", id);
505 if (n->type == ROFFT_HEAD)
507 else if (n->type == ROFFT_BLOCK)
516 const struct roff_node *nn;
518 if (n->type == ROFFT_BODY) {
519 print_otag(h, TAG_DD, "c", "It-tag");
521 } else if (n->type != ROFFT_HEAD) {
522 print_otag(h, TAG_DL, "c", "Bl-tag");
526 /* FIXME: width specification. */
528 print_otag(h, TAG_DT, "c", "It-tag");
530 /* For IP, only print the first header element. */
532 if (MAN_IP == n->tok && n->child)
533 print_man_node(man, n->child, h);
535 /* For TP, only print next-line header elements. */
537 if (MAN_TP == n->tok) {
539 while (NULL != nn && 0 == (NODE_LINE & nn->flags))
542 print_man_node(man, nn, h);
553 struct roffsu sum, sui;
554 const struct roff_node *np;
556 if (n->type == ROFFT_HEAD)
558 else if (n->type != ROFFT_BLOCK)
563 if (np == NULL || !a2width(np, &sum))
564 SCALE_HS_INIT(&sum, INDENT);
567 sui.scale = -sum.scale;
570 print_otag(h, TAG_DIV, "csului", "Pp", &sum, &sui);
580 h->flags |= HTML_NOSPACE;
581 tt = print_otag(h, TAG_SPAN, "c", "Op");
583 if (NULL != (n = n->child)) {
584 print_otag(h, TAG_B, "");
585 print_text(h, n->string);
590 if (NULL != n && NULL != n->next) {
591 print_otag(h, TAG_I, "");
592 print_text(h, n->next->string);
596 h->flags |= HTML_NOSPACE;
604 print_otag(h, TAG_B, "");
611 print_otag(h, TAG_I, "");
618 print_otag(h, TAG_BR, "");
623 man_ign_pre(MAN_ARGS)
634 if (n->type == ROFFT_HEAD)
636 else if (n->type == ROFFT_BODY)
639 SCALE_HS_INIT(&su, INDENT);
641 a2width(n->head->child, &su);
643 print_otag(h, TAG_DIV, "sul", &su);
652 assert(n->type == ROFFT_HEAD);
653 if (n->child != NULL) {
654 assert(n->child->type == ROFFT_TEXT);
655 if (n->tok == MAN_MT) {
656 mandoc_asprintf(&cp, "mailto:%s", n->child->string);
657 print_otag(h, TAG_A, "cTh", "Mt", cp);
660 print_otag(h, TAG_A, "cTh", "Lk", n->child->string);
663 assert(n->next->type == ROFFT_BODY);
664 if (n->next->child != NULL)
667 print_man_nodelist(man, n->child, h);