]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
look at COHERENT troff
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.115 2015/04/02 23:48:19 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015 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 "roff.h"
30 #include "man.h"
31 #include "out.h"
32 #include "html.h"
33 #include "main.h"
34
35 /* TODO: preserve ident widths. */
36 /* FIXME: have PD set the default vspace width. */
37
38 #define INDENT 5
39
40 #define MAN_ARGS const struct roff_meta *man, \
41 const struct roff_node *n, \
42 struct mhtml *mh, \
43 struct html *h
44
45 struct mhtml {
46 int fl;
47 #define MANH_LITERAL (1 << 0) /* literal context */
48 };
49
50 struct htmlman {
51 int (*pre)(MAN_ARGS);
52 int (*post)(MAN_ARGS);
53 };
54
55 static void print_bvspace(struct html *,
56 const struct roff_node *);
57 static void print_man(MAN_ARGS);
58 static void print_man_head(MAN_ARGS);
59 static void print_man_nodelist(MAN_ARGS);
60 static void print_man_node(MAN_ARGS);
61 static int a2width(const struct roff_node *,
62 struct roffsu *);
63 static int man_B_pre(MAN_ARGS);
64 static int man_HP_pre(MAN_ARGS);
65 static int man_IP_pre(MAN_ARGS);
66 static int man_I_pre(MAN_ARGS);
67 static int man_OP_pre(MAN_ARGS);
68 static int man_PP_pre(MAN_ARGS);
69 static int man_RS_pre(MAN_ARGS);
70 static int man_SH_pre(MAN_ARGS);
71 static int man_SM_pre(MAN_ARGS);
72 static int man_SS_pre(MAN_ARGS);
73 static int man_UR_pre(MAN_ARGS);
74 static int man_alt_pre(MAN_ARGS);
75 static int man_br_pre(MAN_ARGS);
76 static int man_ign_pre(MAN_ARGS);
77 static int man_in_pre(MAN_ARGS);
78 static int man_literal_pre(MAN_ARGS);
79 static void man_root_post(MAN_ARGS);
80 static void man_root_pre(MAN_ARGS);
81
82 static const struct htmlman mans[MAN_MAX] = {
83 { man_br_pre, NULL }, /* br */
84 { NULL, NULL }, /* TH */
85 { man_SH_pre, NULL }, /* SH */
86 { man_SS_pre, NULL }, /* SS */
87 { man_IP_pre, NULL }, /* TP */
88 { man_PP_pre, NULL }, /* LP */
89 { man_PP_pre, NULL }, /* PP */
90 { man_PP_pre, NULL }, /* P */
91 { man_IP_pre, NULL }, /* IP */
92 { man_HP_pre, NULL }, /* HP */
93 { man_SM_pre, NULL }, /* SM */
94 { man_SM_pre, NULL }, /* SB */
95 { man_alt_pre, NULL }, /* BI */
96 { man_alt_pre, NULL }, /* IB */
97 { man_alt_pre, NULL }, /* BR */
98 { man_alt_pre, NULL }, /* RB */
99 { NULL, NULL }, /* R */
100 { man_B_pre, NULL }, /* B */
101 { man_I_pre, NULL }, /* I */
102 { man_alt_pre, NULL }, /* IR */
103 { man_alt_pre, NULL }, /* RI */
104 { man_br_pre, NULL }, /* sp */
105 { man_literal_pre, NULL }, /* nf */
106 { man_literal_pre, NULL }, /* fi */
107 { NULL, NULL }, /* RE */
108 { man_RS_pre, NULL }, /* RS */
109 { man_ign_pre, NULL }, /* DT */
110 { man_ign_pre, NULL }, /* UC */
111 { man_ign_pre, NULL }, /* PD */
112 { man_ign_pre, NULL }, /* AT */
113 { man_in_pre, NULL }, /* in */
114 { man_ign_pre, NULL }, /* ft */
115 { man_OP_pre, NULL }, /* OP */
116 { man_literal_pre, NULL }, /* EX */
117 { man_literal_pre, NULL }, /* EE */
118 { man_UR_pre, NULL }, /* UR */
119 { NULL, NULL }, /* UE */
120 { man_ign_pre, NULL }, /* ll */
121 };
122
123
124 /*
125 * Printing leading vertical space before a block.
126 * This is used for the paragraph macros.
127 * The rules are pretty simple, since there's very little nesting going
128 * on here. Basically, if we're the first within another block (SS/SH),
129 * then don't emit vertical space. If we are (RS), then do. If not the
130 * first, print it.
131 */
132 static void
133 print_bvspace(struct html *h, const struct roff_node *n)
134 {
135
136 if (n->body && n->body->child)
137 if (n->body->child->type == ROFFT_TBL)
138 return;
139
140 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
141 if (NULL == n->prev)
142 return;
143
144 print_paragraph(h);
145 }
146
147 void
148 html_man(void *arg, const struct man *man)
149 {
150 struct mhtml mh;
151
152 memset(&mh, 0, sizeof(struct mhtml));
153 print_man(man_meta(man), man_node(man), &mh, (struct html *)arg);
154 putchar('\n');
155 }
156
157 static void
158 print_man(MAN_ARGS)
159 {
160 struct tag *t, *tt;
161 struct htmlpair tag;
162
163 PAIR_CLASS_INIT(&tag, "mandoc");
164
165 if ( ! (HTML_FRAGMENT & h->oflags)) {
166 print_gen_decls(h);
167 t = print_otag(h, TAG_HTML, 0, NULL);
168 tt = print_otag(h, TAG_HEAD, 0, NULL);
169 print_man_head(man, n, mh, h);
170 print_tagq(h, tt);
171 print_otag(h, TAG_BODY, 0, NULL);
172 print_otag(h, TAG_DIV, 1, &tag);
173 } else
174 t = print_otag(h, TAG_DIV, 1, &tag);
175
176 print_man_nodelist(man, n, mh, h);
177 print_tagq(h, t);
178 }
179
180 static void
181 print_man_head(MAN_ARGS)
182 {
183
184 print_gen_head(h);
185 assert(man->title);
186 assert(man->msec);
187 bufcat_fmt(h, "%s(%s)", man->title, man->msec);
188 print_otag(h, TAG_TITLE, 0, NULL);
189 print_text(h, h->buf);
190 }
191
192 static void
193 print_man_nodelist(MAN_ARGS)
194 {
195
196 while (n != NULL) {
197 print_man_node(man, n, mh, h);
198 n = n->next;
199 }
200 }
201
202 static void
203 print_man_node(MAN_ARGS)
204 {
205 int child;
206 struct tag *t;
207
208 child = 1;
209 t = h->tags.head;
210
211 switch (n->type) {
212 case ROFFT_ROOT:
213 man_root_pre(man, n, mh, h);
214 break;
215 case ROFFT_TEXT:
216 if ('\0' == *n->string) {
217 print_paragraph(h);
218 return;
219 }
220 if (n->flags & MAN_LINE && (*n->string == ' ' ||
221 (n->prev != NULL && mh->fl & MANH_LITERAL &&
222 ! (h->flags & HTML_NONEWLINE))))
223 print_otag(h, TAG_BR, 0, NULL);
224 print_text(h, n->string);
225 return;
226 case ROFFT_EQN:
227 if (n->flags & MAN_LINE)
228 putchar('\n');
229 print_eqn(h, n->eqn);
230 break;
231 case ROFFT_TBL:
232 /*
233 * This will take care of initialising all of the table
234 * state data for the first table, then tearing it down
235 * for the last one.
236 */
237 print_tbl(h, n->span);
238 return;
239 default:
240 /*
241 * Close out scope of font prior to opening a macro
242 * scope.
243 */
244 if (HTMLFONT_NONE != h->metac) {
245 h->metal = h->metac;
246 h->metac = HTMLFONT_NONE;
247 }
248
249 /*
250 * Close out the current table, if it's open, and unset
251 * the "meta" table state. This will be reopened on the
252 * next table element.
253 */
254 if (h->tblt) {
255 print_tblclose(h);
256 t = h->tags.head;
257 }
258 if (mans[n->tok].pre)
259 child = (*mans[n->tok].pre)(man, n, mh, h);
260 break;
261 }
262
263 if (child && n->child)
264 print_man_nodelist(man, n->child, mh, h);
265
266 /* This will automatically close out any font scope. */
267 print_stagq(h, t);
268
269 switch (n->type) {
270 case ROFFT_ROOT:
271 man_root_post(man, n, mh, h);
272 break;
273 case ROFFT_EQN:
274 break;
275 default:
276 if (mans[n->tok].post)
277 (*mans[n->tok].post)(man, n, mh, h);
278 break;
279 }
280 }
281
282 static int
283 a2width(const struct roff_node *n, struct roffsu *su)
284 {
285
286 if (n->type != ROFFT_TEXT)
287 return(0);
288 if (a2roffsu(n->string, su, SCALE_EN))
289 return(1);
290
291 return(0);
292 }
293
294 static void
295 man_root_pre(MAN_ARGS)
296 {
297 struct htmlpair tag;
298 struct tag *t, *tt;
299 char *title;
300
301 assert(man->title);
302 assert(man->msec);
303 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
304
305 PAIR_CLASS_INIT(&tag, "head");
306 t = print_otag(h, TAG_TABLE, 1, &tag);
307
308 print_otag(h, TAG_TBODY, 0, NULL);
309
310 tt = print_otag(h, TAG_TR, 0, NULL);
311
312 PAIR_CLASS_INIT(&tag, "head-ltitle");
313 print_otag(h, TAG_TD, 1, &tag);
314 print_text(h, title);
315 print_stagq(h, tt);
316
317 PAIR_CLASS_INIT(&tag, "head-vol");
318 print_otag(h, TAG_TD, 1, &tag);
319 if (NULL != man->vol)
320 print_text(h, man->vol);
321 print_stagq(h, tt);
322
323 PAIR_CLASS_INIT(&tag, "head-rtitle");
324 print_otag(h, TAG_TD, 1, &tag);
325 print_text(h, title);
326 print_tagq(h, t);
327 free(title);
328 }
329
330 static void
331 man_root_post(MAN_ARGS)
332 {
333 struct htmlpair tag;
334 struct tag *t, *tt;
335
336 PAIR_CLASS_INIT(&tag, "foot");
337 t = print_otag(h, TAG_TABLE, 1, &tag);
338
339 tt = print_otag(h, TAG_TR, 0, NULL);
340
341 PAIR_CLASS_INIT(&tag, "foot-date");
342 print_otag(h, TAG_TD, 1, &tag);
343
344 assert(man->date);
345 print_text(h, man->date);
346 print_stagq(h, tt);
347
348 PAIR_CLASS_INIT(&tag, "foot-os");
349 print_otag(h, TAG_TD, 1, &tag);
350
351 if (man->os)
352 print_text(h, man->os);
353 print_tagq(h, t);
354 }
355
356
357 static int
358 man_br_pre(MAN_ARGS)
359 {
360 struct roffsu su;
361 struct htmlpair tag;
362
363 SCALE_VS_INIT(&su, 1);
364
365 if (MAN_sp == n->tok) {
366 if (NULL != (n = n->child))
367 if ( ! a2roffsu(n->string, &su, SCALE_VS))
368 su.scale = 1.0;
369 } else
370 su.scale = 0.0;
371
372 bufinit(h);
373 bufcat_su(h, "height", &su);
374 PAIR_STYLE_INIT(&tag, h);
375 print_otag(h, TAG_DIV, 1, &tag);
376
377 /* So the div isn't empty: */
378 print_text(h, "\\~");
379
380 return(0);
381 }
382
383 static int
384 man_SH_pre(MAN_ARGS)
385 {
386 struct htmlpair tag;
387
388 if (n->type == ROFFT_BLOCK) {
389 mh->fl &= ~MANH_LITERAL;
390 PAIR_CLASS_INIT(&tag, "section");
391 print_otag(h, TAG_DIV, 1, &tag);
392 return(1);
393 } else if (n->type == ROFFT_BODY)
394 return(1);
395
396 print_otag(h, TAG_H1, 0, NULL);
397 return(1);
398 }
399
400 static int
401 man_alt_pre(MAN_ARGS)
402 {
403 const struct roff_node *nn;
404 int i, savelit;
405 enum htmltag fp;
406 struct tag *t;
407
408 if ((savelit = mh->fl & MANH_LITERAL))
409 print_otag(h, TAG_BR, 0, NULL);
410
411 mh->fl &= ~MANH_LITERAL;
412
413 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
414 t = NULL;
415 switch (n->tok) {
416 case MAN_BI:
417 fp = i % 2 ? TAG_I : TAG_B;
418 break;
419 case MAN_IB:
420 fp = i % 2 ? TAG_B : TAG_I;
421 break;
422 case MAN_RI:
423 fp = i % 2 ? TAG_I : TAG_MAX;
424 break;
425 case MAN_IR:
426 fp = i % 2 ? TAG_MAX : TAG_I;
427 break;
428 case MAN_BR:
429 fp = i % 2 ? TAG_MAX : TAG_B;
430 break;
431 case MAN_RB:
432 fp = i % 2 ? TAG_B : TAG_MAX;
433 break;
434 default:
435 abort();
436 /* NOTREACHED */
437 }
438
439 if (i)
440 h->flags |= HTML_NOSPACE;
441
442 if (TAG_MAX != fp)
443 t = print_otag(h, fp, 0, NULL);
444
445 print_man_node(man, nn, mh, h);
446
447 if (t)
448 print_tagq(h, t);
449 }
450
451 if (savelit)
452 mh->fl |= MANH_LITERAL;
453
454 return(0);
455 }
456
457 static int
458 man_SM_pre(MAN_ARGS)
459 {
460
461 print_otag(h, TAG_SMALL, 0, NULL);
462 if (MAN_SB == n->tok)
463 print_otag(h, TAG_B, 0, NULL);
464 return(1);
465 }
466
467 static int
468 man_SS_pre(MAN_ARGS)
469 {
470 struct htmlpair tag;
471
472 if (n->type == ROFFT_BLOCK) {
473 mh->fl &= ~MANH_LITERAL;
474 PAIR_CLASS_INIT(&tag, "subsection");
475 print_otag(h, TAG_DIV, 1, &tag);
476 return(1);
477 } else if (n->type == ROFFT_BODY)
478 return(1);
479
480 print_otag(h, TAG_H2, 0, NULL);
481 return(1);
482 }
483
484 static int
485 man_PP_pre(MAN_ARGS)
486 {
487
488 if (n->type == ROFFT_HEAD)
489 return(0);
490 else if (n->type == ROFFT_BLOCK)
491 print_bvspace(h, n);
492
493 return(1);
494 }
495
496 static int
497 man_IP_pre(MAN_ARGS)
498 {
499 const struct roff_node *nn;
500
501 if (n->type == ROFFT_BODY) {
502 print_otag(h, TAG_DD, 0, NULL);
503 return(1);
504 } else if (n->type != ROFFT_HEAD) {
505 print_otag(h, TAG_DL, 0, NULL);
506 return(1);
507 }
508
509 /* FIXME: width specification. */
510
511 print_otag(h, TAG_DT, 0, NULL);
512
513 /* For IP, only print the first header element. */
514
515 if (MAN_IP == n->tok && n->child)
516 print_man_node(man, n->child, mh, h);
517
518 /* For TP, only print next-line header elements. */
519
520 if (MAN_TP == n->tok) {
521 nn = n->child;
522 while (NULL != nn && 0 == (MAN_LINE & nn->flags))
523 nn = nn->next;
524 while (NULL != nn) {
525 print_man_node(man, nn, mh, h);
526 nn = nn->next;
527 }
528 }
529
530 return(0);
531 }
532
533 static int
534 man_HP_pre(MAN_ARGS)
535 {
536 struct htmlpair tag[2];
537 struct roffsu su;
538 const struct roff_node *np;
539
540 if (n->type == ROFFT_HEAD)
541 return(0);
542 else if (n->type != ROFFT_BLOCK)
543 return(1);
544
545 np = n->head->child;
546
547 if (NULL == np || ! a2width(np, &su))
548 SCALE_HS_INIT(&su, INDENT);
549
550 bufinit(h);
551
552 print_bvspace(h, n);
553 bufcat_su(h, "margin-left", &su);
554 su.scale = -su.scale;
555 bufcat_su(h, "text-indent", &su);
556 PAIR_STYLE_INIT(&tag[0], h);
557 PAIR_CLASS_INIT(&tag[1], "spacer");
558 print_otag(h, TAG_DIV, 2, tag);
559 return(1);
560 }
561
562 static int
563 man_OP_pre(MAN_ARGS)
564 {
565 struct tag *tt;
566 struct htmlpair tag;
567
568 print_text(h, "[");
569 h->flags |= HTML_NOSPACE;
570 PAIR_CLASS_INIT(&tag, "opt");
571 tt = print_otag(h, TAG_SPAN, 1, &tag);
572
573 if (NULL != (n = n->child)) {
574 print_otag(h, TAG_B, 0, NULL);
575 print_text(h, n->string);
576 }
577
578 print_stagq(h, tt);
579
580 if (NULL != n && NULL != n->next) {
581 print_otag(h, TAG_I, 0, NULL);
582 print_text(h, n->next->string);
583 }
584
585 print_stagq(h, tt);
586 h->flags |= HTML_NOSPACE;
587 print_text(h, "]");
588 return(0);
589 }
590
591 static int
592 man_B_pre(MAN_ARGS)
593 {
594
595 print_otag(h, TAG_B, 0, NULL);
596 return(1);
597 }
598
599 static int
600 man_I_pre(MAN_ARGS)
601 {
602
603 print_otag(h, TAG_I, 0, NULL);
604 return(1);
605 }
606
607 static int
608 man_literal_pre(MAN_ARGS)
609 {
610
611 if (MAN_fi == n->tok || MAN_EE == n->tok) {
612 print_otag(h, TAG_BR, 0, NULL);
613 mh->fl &= ~MANH_LITERAL;
614 } else
615 mh->fl |= MANH_LITERAL;
616
617 return(0);
618 }
619
620 static int
621 man_in_pre(MAN_ARGS)
622 {
623
624 print_otag(h, TAG_BR, 0, NULL);
625 return(0);
626 }
627
628 static int
629 man_ign_pre(MAN_ARGS)
630 {
631
632 return(0);
633 }
634
635 static int
636 man_RS_pre(MAN_ARGS)
637 {
638 struct htmlpair tag;
639 struct roffsu su;
640
641 if (n->type == ROFFT_HEAD)
642 return(0);
643 else if (n->type == ROFFT_BODY)
644 return(1);
645
646 SCALE_HS_INIT(&su, INDENT);
647 if (n->head->child)
648 a2width(n->head->child, &su);
649
650 bufinit(h);
651 bufcat_su(h, "margin-left", &su);
652 PAIR_STYLE_INIT(&tag, h);
653 print_otag(h, TAG_DIV, 1, &tag);
654 return(1);
655 }
656
657 static int
658 man_UR_pre(MAN_ARGS)
659 {
660 struct htmlpair tag[2];
661
662 n = n->child;
663 assert(n->type == ROFFT_HEAD);
664 if (n->nchild) {
665 assert(n->child->type == ROFFT_TEXT);
666 PAIR_CLASS_INIT(&tag[0], "link-ext");
667 PAIR_HREF_INIT(&tag[1], n->child->string);
668 print_otag(h, TAG_A, 2, tag);
669 }
670
671 assert(n->next->type == ROFFT_BODY);
672 if (n->next->nchild)
673 n = n->next;
674
675 print_man_nodelist(man, n->child, mh, h);
676
677 return(0);
678 }