]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
Now that the NODE_NOFILL flag in the syntax tree is accurate,
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.163 2019/01/05 09:14:44 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
5 *
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.
9 *
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.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "mandoc_aux.h"
29 #include "mandoc.h"
30 #include "roff.h"
31 #include "man.h"
32 #include "out.h"
33 #include "html.h"
34 #include "main.h"
35
36 /* FIXME: have PD set the default vspace width. */
37
38 #define MAN_ARGS const struct roff_meta *man, \
39 const struct roff_node *n, \
40 struct html *h
41
42 struct man_html_act {
43 int (*pre)(MAN_ARGS);
44 int (*post)(MAN_ARGS);
45 };
46
47 static void print_bvspace(struct html *,
48 const struct roff_node *);
49 static void print_man_head(const struct roff_meta *,
50 struct html *);
51 static void print_man_nodelist(MAN_ARGS);
52 static void print_man_node(MAN_ARGS);
53 static int man_B_pre(MAN_ARGS);
54 static int man_HP_pre(MAN_ARGS);
55 static int man_IP_pre(MAN_ARGS);
56 static int man_I_pre(MAN_ARGS);
57 static int man_OP_pre(MAN_ARGS);
58 static int man_PP_pre(MAN_ARGS);
59 static int man_RS_pre(MAN_ARGS);
60 static int man_SH_pre(MAN_ARGS);
61 static int man_SM_pre(MAN_ARGS);
62 static int man_SS_pre(MAN_ARGS);
63 static int man_SY_pre(MAN_ARGS);
64 static int man_UR_pre(MAN_ARGS);
65 static int man_abort_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 *,
70 struct html *);
71 static void man_root_pre(const struct roff_meta *,
72 struct html *);
73
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_abort_pre, NULL }, /* LP */
81 { man_PP_pre, NULL }, /* PP */
82 { man_abort_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 }, /* 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_SY_pre, NULL }, /* SY */
104 { NULL, NULL }, /* YS */
105 { man_OP_pre, NULL }, /* OP */
106 { NULL, NULL }, /* EX */
107 { NULL, NULL }, /* EE */
108 { man_UR_pre, NULL }, /* UR */
109 { NULL, NULL }, /* UE */
110 { man_UR_pre, NULL }, /* MT */
111 { NULL, NULL }, /* ME */
112 };
113
114
115 /*
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
121 * first, print it.
122 */
123 static void
124 print_bvspace(struct html *h, const struct roff_node *n)
125 {
126
127 if (n->body && n->body->child)
128 if (n->body->child->type == ROFFT_TBL)
129 return;
130
131 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
132 if (NULL == n->prev)
133 return;
134
135 print_paragraph(h);
136 }
137
138 void
139 html_man(void *arg, const struct roff_meta *man)
140 {
141 struct html *h;
142 struct roff_node *n;
143 struct tag *t;
144
145 h = (struct html *)arg;
146 n = man->first->child;
147
148 if ((h->oflags & HTML_FRAGMENT) == 0) {
149 print_gen_decls(h);
150 print_otag(h, TAG_HTML, "");
151 if (n->type == ROFFT_COMMENT)
152 print_gen_comment(h, n);
153 t = print_otag(h, TAG_HEAD, "");
154 print_man_head(man, h);
155 print_tagq(h, t);
156 print_otag(h, TAG_BODY, "");
157 }
158
159 man_root_pre(man, h);
160 t = print_otag(h, TAG_DIV, "c", "manual-text");
161 print_man_nodelist(man, n, h);
162 print_tagq(h, t);
163 man_root_post(man, h);
164 print_tagq(h, NULL);
165 }
166
167 static void
168 print_man_head(const struct roff_meta *man, struct html *h)
169 {
170 char *cp;
171
172 print_gen_head(h);
173 mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
174 print_otag(h, TAG_TITLE, "");
175 print_text(h, cp);
176 free(cp);
177 }
178
179 static void
180 print_man_nodelist(MAN_ARGS)
181 {
182
183 while (n != NULL) {
184 print_man_node(man, n, h);
185 n = n->next;
186 }
187 }
188
189 static void
190 print_man_node(MAN_ARGS)
191 {
192 struct tag *t;
193 int child;
194
195 html_fillmode(h, n->flags & NODE_NOFILL ? ROFF_nf : ROFF_fi);
196
197 child = 1;
198 switch (n->type) {
199 case ROFFT_TEXT:
200 if (*n->string == '\0') {
201 print_endline(h);
202 return;
203 }
204 t = h->tag;
205 if (*n->string == ' ' && n->flags & NODE_LINE &&
206 (h->flags & HTML_NONEWLINE) == 0)
207 print_endline(h);
208 else if (n->flags & NODE_DELIMC)
209 h->flags |= HTML_NOSPACE;
210 print_text(h, n->string);
211 break;
212 case ROFFT_COMMENT:
213 return;
214 case ROFFT_EQN:
215 t = h->tag;
216 print_eqn(h, n->eqn);
217 break;
218 case ROFFT_TBL:
219 /*
220 * This will take care of initialising all of the table
221 * state data for the first table, then tearing it down
222 * for the last one.
223 */
224 print_tbl(h, n->span);
225 return;
226 default:
227 /*
228 * Close out scope of font prior to opening a macro
229 * scope.
230 */
231 if (HTMLFONT_NONE != h->metac) {
232 h->metal = h->metac;
233 h->metac = HTMLFONT_NONE;
234 }
235
236 /*
237 * Close out the current table, if it's open, and unset
238 * the "meta" table state. This will be reopened on the
239 * next table element.
240 */
241 if (h->tblt)
242 print_tblclose(h);
243
244 t = h->tag;
245 if (n->tok < ROFF_MAX) {
246 roff_html_pre(h, n);
247 print_stagq(h, t);
248 return;
249 }
250
251 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
252 if (man_html_acts[n->tok - MAN_TH].pre != NULL)
253 child = (*man_html_acts[n->tok - MAN_TH].pre)(man,
254 n, h);
255 break;
256 }
257
258 if (child && n->child)
259 print_man_nodelist(man, n->child, h);
260
261 /* This will automatically close out any font scope. */
262 print_stagq(h, t);
263
264 if (n->flags & NODE_NOFILL &&
265 (n->next == NULL || n->next->flags & NODE_LINE)) {
266 /* In .nf = <pre>, print even empty lines. */
267 h->col++;
268 print_endline(h);
269 }
270 }
271
272 static void
273 man_root_pre(const struct roff_meta *man, struct html *h)
274 {
275 struct tag *t, *tt;
276 char *title;
277
278 assert(man->title);
279 assert(man->msec);
280 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
281
282 t = print_otag(h, TAG_TABLE, "c", "head");
283 tt = print_otag(h, TAG_TR, "");
284
285 print_otag(h, TAG_TD, "c", "head-ltitle");
286 print_text(h, title);
287 print_stagq(h, tt);
288
289 print_otag(h, TAG_TD, "c", "head-vol");
290 if (NULL != man->vol)
291 print_text(h, man->vol);
292 print_stagq(h, tt);
293
294 print_otag(h, TAG_TD, "c", "head-rtitle");
295 print_text(h, title);
296 print_tagq(h, t);
297 free(title);
298 }
299
300 static void
301 man_root_post(const struct roff_meta *man, struct html *h)
302 {
303 struct tag *t, *tt;
304
305 t = print_otag(h, TAG_TABLE, "c", "foot");
306 tt = print_otag(h, TAG_TR, "");
307
308 print_otag(h, TAG_TD, "c", "foot-date");
309 print_text(h, man->date);
310 print_stagq(h, tt);
311
312 print_otag(h, TAG_TD, "c", "foot-os");
313 if (man->os)
314 print_text(h, man->os);
315 print_tagq(h, t);
316 }
317
318 static int
319 man_SH_pre(MAN_ARGS)
320 {
321 char *id;
322
323 if (n->type == ROFFT_HEAD) {
324 id = html_make_id(n, 1);
325 print_otag(h, TAG_H1, "cTi", "Sh", id);
326 if (id != NULL)
327 print_otag(h, TAG_A, "chR", "permalink", id);
328 }
329 return 1;
330 }
331
332 static int
333 man_alt_pre(MAN_ARGS)
334 {
335 const struct roff_node *nn;
336 int i;
337 enum htmltag fp;
338 struct tag *t;
339
340 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
341 switch (n->tok) {
342 case MAN_BI:
343 fp = i % 2 ? TAG_I : TAG_B;
344 break;
345 case MAN_IB:
346 fp = i % 2 ? TAG_B : TAG_I;
347 break;
348 case MAN_RI:
349 fp = i % 2 ? TAG_I : TAG_MAX;
350 break;
351 case MAN_IR:
352 fp = i % 2 ? TAG_MAX : TAG_I;
353 break;
354 case MAN_BR:
355 fp = i % 2 ? TAG_MAX : TAG_B;
356 break;
357 case MAN_RB:
358 fp = i % 2 ? TAG_B : TAG_MAX;
359 break;
360 default:
361 abort();
362 }
363
364 if (i)
365 h->flags |= HTML_NOSPACE;
366
367 if (fp != TAG_MAX)
368 t = print_otag(h, fp, "");
369
370 print_text(h, nn->string);
371
372 if (fp != TAG_MAX)
373 print_tagq(h, t);
374 }
375 return 0;
376 }
377
378 static int
379 man_SM_pre(MAN_ARGS)
380 {
381 print_otag(h, TAG_SMALL, "");
382 if (MAN_SB == n->tok)
383 print_otag(h, TAG_B, "");
384 return 1;
385 }
386
387 static int
388 man_SS_pre(MAN_ARGS)
389 {
390 char *id;
391
392 if (n->type == ROFFT_HEAD) {
393 id = html_make_id(n, 1);
394 print_otag(h, TAG_H2, "cTi", "Ss", id);
395 if (id != NULL)
396 print_otag(h, TAG_A, "chR", "permalink", id);
397 }
398 return 1;
399 }
400
401 static int
402 man_PP_pre(MAN_ARGS)
403 {
404
405 if (n->type == ROFFT_HEAD)
406 return 0;
407 else if (n->type == ROFFT_BLOCK)
408 print_bvspace(h, n);
409
410 return 1;
411 }
412
413 static int
414 man_IP_pre(MAN_ARGS)
415 {
416 const struct roff_node *nn;
417
418 if (n->type == ROFFT_BODY) {
419 print_otag(h, TAG_DD, "");
420 return 1;
421 } else if (n->type != ROFFT_HEAD) {
422 print_otag(h, TAG_DL, "c", "Bl-tag");
423 return 1;
424 }
425
426 print_otag(h, TAG_DT, "");
427
428 switch(n->tok) {
429 case MAN_IP: /* Only print the first header element. */
430 if (n->child != NULL)
431 print_man_node(man, n->child, h);
432 break;
433 case MAN_TP: /* Only print next-line header elements. */
434 case MAN_TQ:
435 nn = n->child;
436 while (nn != NULL && (NODE_LINE & nn->flags) == 0)
437 nn = nn->next;
438 while (nn != NULL) {
439 print_man_node(man, nn, h);
440 nn = nn->next;
441 }
442 break;
443 default:
444 abort();
445 }
446
447 return 0;
448 }
449
450 static int
451 man_HP_pre(MAN_ARGS)
452 {
453 if (n->type == ROFFT_HEAD)
454 return 0;
455
456 if (n->type == ROFFT_BLOCK) {
457 print_bvspace(h, n);
458 print_otag(h, TAG_DIV, "c", "HP");
459 }
460 return 1;
461 }
462
463 static int
464 man_OP_pre(MAN_ARGS)
465 {
466 struct tag *tt;
467
468 print_text(h, "[");
469 h->flags |= HTML_NOSPACE;
470 tt = print_otag(h, TAG_SPAN, "c", "Op");
471
472 if (NULL != (n = n->child)) {
473 print_otag(h, TAG_B, "");
474 print_text(h, n->string);
475 }
476
477 print_stagq(h, tt);
478
479 if (NULL != n && NULL != n->next) {
480 print_otag(h, TAG_I, "");
481 print_text(h, n->next->string);
482 }
483
484 print_stagq(h, tt);
485 h->flags |= HTML_NOSPACE;
486 print_text(h, "]");
487 return 0;
488 }
489
490 static int
491 man_B_pre(MAN_ARGS)
492 {
493 print_otag(h, TAG_B, "");
494 return 1;
495 }
496
497 static int
498 man_I_pre(MAN_ARGS)
499 {
500 print_otag(h, TAG_I, "");
501 return 1;
502 }
503
504 static int
505 man_in_pre(MAN_ARGS)
506 {
507 print_otag(h, TAG_BR, "");
508 return 0;
509 }
510
511 static int
512 man_ign_pre(MAN_ARGS)
513 {
514
515 return 0;
516 }
517
518 static int
519 man_RS_pre(MAN_ARGS)
520 {
521 if (n->type == ROFFT_HEAD)
522 return 0;
523 if (n->type == ROFFT_BLOCK)
524 print_otag(h, TAG_DIV, "c", "Bd-indent");
525 return 1;
526 }
527
528 static int
529 man_SY_pre(MAN_ARGS)
530 {
531 switch (n->type) {
532 case ROFFT_BLOCK:
533 print_otag(h, TAG_TABLE, "c", "Nm");
534 print_otag(h, TAG_TR, "");
535 break;
536 case ROFFT_HEAD:
537 print_otag(h, TAG_TD, "");
538 print_otag(h, TAG_CODE, "cT", "Nm");
539 break;
540 case ROFFT_BODY:
541 print_otag(h, TAG_TD, "");
542 break;
543 default:
544 abort();
545 }
546 return 1;
547 }
548
549 static int
550 man_UR_pre(MAN_ARGS)
551 {
552 char *cp;
553 n = n->child;
554 assert(n->type == ROFFT_HEAD);
555 if (n->child != NULL) {
556 assert(n->child->type == ROFFT_TEXT);
557 if (n->tok == MAN_MT) {
558 mandoc_asprintf(&cp, "mailto:%s", n->child->string);
559 print_otag(h, TAG_A, "cTh", "Mt", cp);
560 free(cp);
561 } else
562 print_otag(h, TAG_A, "cTh", "Lk", n->child->string);
563 }
564
565 assert(n->next->type == ROFFT_BODY);
566 if (n->next->child != NULL)
567 n = n->next;
568
569 print_man_nodelist(man, n->child, h);
570
571 return 0;
572 }
573
574 static int
575 man_abort_pre(MAN_ARGS)
576 {
577 abort();
578 }