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