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