1 /* $Id: man_html.c,v 1.129 2017/01/21 01:20:32 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"
35 /* TODO: preserve ident widths. */
36 /* FIXME: have PD set the default vspace width. */
40 #define MAN_ARGS const struct roff_meta *man, \
41 const struct roff_node *n, \
47 #define MANH_LITERAL (1 << 0) /* literal context */
52 int (*post)(MAN_ARGS);
55 static void print_bvspace(struct html *,
56 const struct roff_node *);
57 static void print_man_head(MAN_ARGS);
58 static void print_man_nodelist(MAN_ARGS);
59 static void print_man_node(MAN_ARGS);
60 static int a2width(const struct roff_node *,
62 static int man_B_pre(MAN_ARGS);
63 static int man_HP_pre(MAN_ARGS);
64 static int man_IP_pre(MAN_ARGS);
65 static int man_I_pre(MAN_ARGS);
66 static int man_OP_pre(MAN_ARGS);
67 static int man_PP_pre(MAN_ARGS);
68 static int man_RS_pre(MAN_ARGS);
69 static int man_SH_pre(MAN_ARGS);
70 static int man_SM_pre(MAN_ARGS);
71 static int man_SS_pre(MAN_ARGS);
72 static int man_UR_pre(MAN_ARGS);
73 static int man_alt_pre(MAN_ARGS);
74 static int man_br_pre(MAN_ARGS);
75 static int man_ign_pre(MAN_ARGS);
76 static int man_in_pre(MAN_ARGS);
77 static int man_literal_pre(MAN_ARGS);
78 static void man_root_post(MAN_ARGS);
79 static void man_root_pre(MAN_ARGS);
81 static const struct htmlman mans[MAN_MAX] = {
82 { man_br_pre, NULL }, /* br */
83 { NULL, NULL }, /* TH */
84 { man_SH_pre, NULL }, /* SH */
85 { man_SS_pre, NULL }, /* SS */
86 { man_IP_pre, NULL }, /* TP */
87 { man_PP_pre, NULL }, /* LP */
88 { man_PP_pre, NULL }, /* PP */
89 { man_PP_pre, NULL }, /* P */
90 { man_IP_pre, NULL }, /* IP */
91 { man_HP_pre, NULL }, /* HP */
92 { man_SM_pre, NULL }, /* SM */
93 { man_SM_pre, NULL }, /* SB */
94 { man_alt_pre, NULL }, /* BI */
95 { man_alt_pre, NULL }, /* IB */
96 { man_alt_pre, NULL }, /* BR */
97 { man_alt_pre, NULL }, /* RB */
98 { NULL, NULL }, /* R */
99 { man_B_pre, NULL }, /* B */
100 { man_I_pre, NULL }, /* I */
101 { man_alt_pre, NULL }, /* IR */
102 { man_alt_pre, NULL }, /* RI */
103 { man_br_pre, NULL }, /* sp */
104 { man_literal_pre, NULL }, /* nf */
105 { man_literal_pre, NULL }, /* fi */
106 { NULL, NULL }, /* RE */
107 { man_RS_pre, NULL }, /* RS */
108 { man_ign_pre, NULL }, /* DT */
109 { man_ign_pre, NULL }, /* UC */
110 { man_ign_pre, NULL }, /* PD */
111 { man_ign_pre, NULL }, /* AT */
112 { man_in_pre, NULL }, /* in */
113 { man_ign_pre, NULL }, /* ft */
114 { man_OP_pre, NULL }, /* OP */
115 { man_literal_pre, NULL }, /* EX */
116 { man_literal_pre, NULL }, /* EE */
117 { man_UR_pre, NULL }, /* UR */
118 { NULL, NULL }, /* UE */
119 { man_ign_pre, NULL }, /* ll */
124 * Printing leading vertical space before a block.
125 * This is used for the paragraph macros.
126 * The rules are pretty simple, since there's very little nesting going
127 * on here. Basically, if we're the first within another block (SS/SH),
128 * then don't emit vertical space. If we are (RS), then do. If not the
132 print_bvspace(struct html *h, const struct roff_node *n)
135 if (n->body && n->body->child)
136 if (n->body->child->type == ROFFT_TBL)
139 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
147 html_man(void *arg, const struct roff_man *man)
153 memset(&mh, 0, sizeof(mh));
154 h = (struct html *)arg;
156 if ((h->oflags & HTML_FRAGMENT) == 0) {
158 print_otag(h, TAG_HTML, "");
159 t = print_otag(h, TAG_HEAD, "");
160 print_man_head(&man->meta, man->first, &mh, h);
162 print_otag(h, TAG_BODY, "");
165 man_root_pre(&man->meta, man->first, &mh, h);
166 t = print_otag(h, TAG_DIV, "c", "manual-text");
167 print_man_nodelist(&man->meta, man->first->child, &mh, h);
169 man_root_post(&man->meta, man->first, &mh, h);
174 print_man_head(MAN_ARGS)
179 mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
180 print_otag(h, TAG_TITLE, "");
186 print_man_nodelist(MAN_ARGS)
190 print_man_node(man, n, mh, h);
196 print_man_node(MAN_ARGS)
206 if ('\0' == *n->string) {
210 if (n->flags & NODE_LINE && (*n->string == ' ' ||
211 (n->prev != NULL && mh->fl & MANH_LITERAL &&
212 ! (h->flags & HTML_NONEWLINE))))
213 print_otag(h, TAG_BR, "");
214 print_text(h, n->string);
217 print_eqn(h, n->eqn);
221 * This will take care of initialising all of the table
222 * state data for the first table, then tearing it down
225 print_tbl(h, n->span);
229 * Close out scope of font prior to opening a macro
232 if (HTMLFONT_NONE != h->metac) {
234 h->metac = HTMLFONT_NONE;
238 * Close out the current table, if it's open, and unset
239 * the "meta" table state. This will be reopened on the
240 * next table element.
246 if (mans[n->tok].pre)
247 child = (*mans[n->tok].pre)(man, n, mh, h);
251 if (child && n->child)
252 print_man_nodelist(man, n->child, mh, h);
254 /* This will automatically close out any font scope. */
261 if (mans[n->tok].post)
262 (*mans[n->tok].post)(man, n, mh, h);
268 a2width(const struct roff_node *n, struct roffsu *su)
271 if (n->type != ROFFT_TEXT)
273 if (a2roffsu(n->string, su, SCALE_EN))
280 man_root_pre(MAN_ARGS)
287 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
289 t = print_otag(h, TAG_TABLE, "c", "head");
290 print_otag(h, TAG_TBODY, "");
291 tt = print_otag(h, TAG_TR, "");
293 print_otag(h, TAG_TD, "c", "head-ltitle");
294 print_text(h, title);
297 print_otag(h, TAG_TD, "c", "head-vol");
298 if (NULL != man->vol)
299 print_text(h, man->vol);
302 print_otag(h, TAG_TD, "c", "head-rtitle");
303 print_text(h, title);
309 man_root_post(MAN_ARGS)
313 t = print_otag(h, TAG_TABLE, "c", "foot");
314 tt = print_otag(h, TAG_TR, "");
316 print_otag(h, TAG_TD, "c", "foot-date");
317 print_text(h, man->date);
320 print_otag(h, TAG_TD, "c", "foot-os");
322 print_text(h, man->os);
332 SCALE_VS_INIT(&su, 1);
334 if (MAN_sp == n->tok) {
335 if (NULL != (n = n->child))
336 if ( ! a2roffsu(n->string, &su, SCALE_VS))
341 print_otag(h, TAG_DIV, "suh", &su);
343 /* So the div isn't empty: */
344 print_text(h, "\\~");
352 if (n->type == ROFFT_BLOCK) {
353 mh->fl &= ~MANH_LITERAL;
355 } else if (n->type == ROFFT_BODY)
358 print_otag(h, TAG_H1, "c", "Sh");
363 man_alt_pre(MAN_ARGS)
365 const struct roff_node *nn;
370 if ((savelit = mh->fl & MANH_LITERAL))
371 print_otag(h, TAG_BR, "");
373 mh->fl &= ~MANH_LITERAL;
375 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
379 fp = i % 2 ? TAG_I : TAG_B;
382 fp = i % 2 ? TAG_B : TAG_I;
385 fp = i % 2 ? TAG_I : TAG_MAX;
388 fp = i % 2 ? TAG_MAX : TAG_I;
391 fp = i % 2 ? TAG_MAX : TAG_B;
394 fp = i % 2 ? TAG_B : TAG_MAX;
401 h->flags |= HTML_NOSPACE;
404 t = print_otag(h, fp, "");
406 print_man_node(man, nn, mh, h);
413 mh->fl |= MANH_LITERAL;
421 print_otag(h, TAG_SMALL, "");
422 if (MAN_SB == n->tok)
423 print_otag(h, TAG_B, "");
430 if (n->type == ROFFT_BLOCK) {
431 mh->fl &= ~MANH_LITERAL;
433 } else if (n->type == ROFFT_BODY)
436 print_otag(h, TAG_H2, "c", "Ss");
444 if (n->type == ROFFT_HEAD)
446 else if (n->type == ROFFT_BLOCK)
455 const struct roff_node *nn;
457 if (n->type == ROFFT_BODY) {
458 print_otag(h, TAG_DD, "c", "It-tag");
460 } else if (n->type != ROFFT_HEAD) {
461 print_otag(h, TAG_DL, "c", "Bl-tag");
465 /* FIXME: width specification. */
467 print_otag(h, TAG_DT, "c", "It-tag");
469 /* For IP, only print the first header element. */
471 if (MAN_IP == n->tok && n->child)
472 print_man_node(man, n->child, mh, h);
474 /* For TP, only print next-line header elements. */
476 if (MAN_TP == n->tok) {
478 while (NULL != nn && 0 == (NODE_LINE & nn->flags))
481 print_man_node(man, nn, mh, h);
492 struct roffsu sum, sui;
493 const struct roff_node *np;
495 if (n->type == ROFFT_HEAD)
497 else if (n->type != ROFFT_BLOCK)
502 if (np == NULL || !a2width(np, &sum))
503 SCALE_HS_INIT(&sum, INDENT);
506 sui.scale = -sum.scale;
509 print_otag(h, TAG_DIV, "csului", "Pp", &sum, &sui);
519 h->flags |= HTML_NOSPACE;
520 tt = print_otag(h, TAG_SPAN, "c", "Op");
522 if (NULL != (n = n->child)) {
523 print_otag(h, TAG_B, "");
524 print_text(h, n->string);
529 if (NULL != n && NULL != n->next) {
530 print_otag(h, TAG_I, "");
531 print_text(h, n->next->string);
535 h->flags |= HTML_NOSPACE;
543 print_otag(h, TAG_B, "");
550 print_otag(h, TAG_I, "");
555 man_literal_pre(MAN_ARGS)
558 if (MAN_fi == n->tok || MAN_EE == n->tok) {
559 print_otag(h, TAG_BR, "");
560 mh->fl &= ~MANH_LITERAL;
562 mh->fl |= MANH_LITERAL;
570 print_otag(h, TAG_BR, "");
575 man_ign_pre(MAN_ARGS)
586 if (n->type == ROFFT_HEAD)
588 else if (n->type == ROFFT_BODY)
591 SCALE_HS_INIT(&su, INDENT);
593 a2width(n->head->child, &su);
595 print_otag(h, TAG_DIV, "sul", &su);
603 assert(n->type == ROFFT_HEAD);
604 if (n->child != NULL) {
605 assert(n->child->type == ROFFT_TEXT);
606 print_otag(h, TAG_A, "ch", "Lk", n->child->string);
609 assert(n->next->type == ROFFT_BODY);
610 if (n->next->child != NULL)
613 print_man_nodelist(man, n->child, mh, h);