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