]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
Consolidated some -man -Tascii functions.
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.8 2009/10/08 23:00:15 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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 #include <sys/types.h>
18 #include <sys/queue.h>
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <err.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "out.h"
28 #include "html.h"
29 #include "man.h"
30
31 /* TODO: preserve ident widths. */
32
33 #define INDENT 5
34 #define HALFINDENT 3
35
36 #define MAN_ARGS const struct man_meta *m, \
37 const struct man_node *n, \
38 struct html *h
39
40 struct htmlman {
41 int (*pre)(MAN_ARGS);
42 int (*post)(MAN_ARGS);
43 };
44
45 static void print_man(MAN_ARGS);
46 static void print_man_head(MAN_ARGS);
47 static void print_man_nodelist(MAN_ARGS);
48 static void print_man_node(MAN_ARGS);
49
50 static int a2width(const struct man_node *,
51 struct roffsu *);
52
53 static int man_alt_pre(MAN_ARGS);
54 static int man_br_pre(MAN_ARGS);
55 static int man_ign_pre(MAN_ARGS);
56 static void man_root_post(MAN_ARGS);
57 static int man_root_pre(MAN_ARGS);
58 static int man_B_pre(MAN_ARGS);
59 static int man_HP_pre(MAN_ARGS);
60 static int man_I_pre(MAN_ARGS);
61 static int man_IP_pre(MAN_ARGS);
62 static int man_PP_pre(MAN_ARGS);
63 static int man_SB_pre(MAN_ARGS);
64 static int man_SH_pre(MAN_ARGS);
65 static int man_SM_pre(MAN_ARGS);
66 static int man_SS_pre(MAN_ARGS);
67
68 #ifdef __linux__
69 extern size_t strlcpy(char *, const char *, size_t);
70 extern size_t strlcat(char *, const char *, size_t);
71 #endif
72
73 static const struct htmlman mans[MAN_MAX] = {
74 { man_br_pre, NULL }, /* br */
75 { NULL, NULL }, /* TH */
76 { man_SH_pre, NULL }, /* SH */
77 { man_SS_pre, NULL }, /* SS */
78 { man_IP_pre, NULL }, /* TP */
79 { man_PP_pre, NULL }, /* LP */
80 { man_PP_pre, NULL }, /* PP */
81 { man_PP_pre, NULL }, /* P */
82 { man_IP_pre, NULL }, /* IP */
83 { man_HP_pre, NULL }, /* HP */
84 { man_SM_pre, NULL }, /* SM */
85 { man_SB_pre, NULL }, /* SB */
86 { man_alt_pre, NULL }, /* BI */
87 { man_alt_pre, NULL }, /* IB */
88 { man_alt_pre, NULL }, /* BR */
89 { man_alt_pre, NULL }, /* RB */
90 { NULL, NULL }, /* R */
91 { man_B_pre, NULL }, /* B */
92 { man_I_pre, NULL }, /* I */
93 { man_alt_pre, NULL }, /* IR */
94 { man_alt_pre, NULL }, /* RI */
95 { NULL, NULL }, /* na */
96 { NULL, NULL }, /* i */
97 { man_br_pre, NULL }, /* sp */
98 { NULL, NULL }, /* nf */
99 { NULL, NULL }, /* fi */
100 { NULL, NULL }, /* r */
101 { NULL, NULL }, /* RE */
102 { NULL, NULL }, /* RS */
103 { man_ign_pre, NULL }, /* DT */
104 { man_ign_pre, NULL }, /* UC */
105 };
106
107
108 void
109 html_man(void *arg, const struct man *m)
110 {
111 struct html *h;
112 struct tag *t;
113
114 h = (struct html *)arg;
115
116 print_gen_doctype(h);
117
118 t = print_otag(h, TAG_HTML, 0, NULL);
119 print_man(man_meta(m), man_node(m), h);
120 print_tagq(h, t);
121
122 printf("\n");
123 }
124
125
126 static void
127 print_man(MAN_ARGS)
128 {
129 struct tag *t;
130 struct htmlpair tag;
131
132 t = print_otag(h, TAG_HEAD, 0, NULL);
133
134 print_man_head(m, n, h);
135 print_tagq(h, t);
136 t = print_otag(h, TAG_BODY, 0, NULL);
137
138 tag.key = ATTR_CLASS;
139 tag.val = "body";
140 print_otag(h, TAG_DIV, 1, &tag);
141
142 print_man_nodelist(m, n, h);
143
144 print_tagq(h, t);
145 }
146
147
148 /* ARGSUSED */
149 static void
150 print_man_head(MAN_ARGS)
151 {
152
153 print_gen_head(h);
154 bufinit(h);
155 buffmt(h, "%s(%d)", m->title, m->msec);
156
157 print_otag(h, TAG_TITLE, 0, NULL);
158 print_text(h, h->buf);
159 }
160
161
162 static void
163 print_man_nodelist(MAN_ARGS)
164 {
165
166 print_man_node(m, n, h);
167 if (n->next)
168 print_man_nodelist(m, n->next, h);
169 }
170
171
172 static void
173 print_man_node(MAN_ARGS)
174 {
175 int child;
176 struct tag *t;
177
178 child = 1;
179 t = SLIST_FIRST(&h->tags);
180
181 bufinit(h);
182
183 switch (n->type) {
184 case (MAN_ROOT):
185 child = man_root_pre(m, n, h);
186 break;
187 case (MAN_TEXT):
188 print_text(h, n->string);
189 break;
190 default:
191 if (mans[n->tok].pre)
192 child = (*mans[n->tok].pre)(m, n, h);
193 break;
194 }
195
196 if (child && n->child)
197 print_man_nodelist(m, n->child, h);
198
199 print_stagq(h, t);
200
201 bufinit(h);
202
203 switch (n->type) {
204 case (MAN_ROOT):
205 man_root_post(m, n, h);
206 break;
207 case (MAN_TEXT):
208 break;
209 default:
210 if (mans[n->tok].post)
211 (*mans[n->tok].post)(m, n, h);
212 break;
213 }
214 }
215
216
217 static int
218 a2width(const struct man_node *n, struct roffsu *su)
219 {
220
221 if (MAN_TEXT != n->type)
222 return(0);
223 if (a2roffsu(n->string, su))
224 return(1);
225
226 return(0);
227 }
228
229
230 /* ARGSUSED */
231 static int
232 man_root_pre(MAN_ARGS)
233 {
234 struct htmlpair tag[2];
235 struct tag *t, *tt;
236 char b[BUFSIZ], title[BUFSIZ];
237
238 b[0] = 0;
239 if (m->vol)
240 (void)strlcat(b, m->vol, BUFSIZ);
241
242 (void)snprintf(title, BUFSIZ - 1,
243 "%s(%d)", m->title, m->msec);
244
245 PAIR_CLASS_INIT(&tag[0], "header");
246 bufcat_style(h, "width", "100%");
247 PAIR_STYLE_INIT(&tag[1], h);
248 t = print_otag(h, TAG_TABLE, 2, tag);
249 tt = print_otag(h, TAG_TR, 0, NULL);
250
251 bufinit(h);
252 bufcat_style(h, "width", "10%");
253 PAIR_STYLE_INIT(&tag[0], h);
254 print_otag(h, TAG_TD, 1, tag);
255 print_text(h, title);
256 print_stagq(h, tt);
257
258 bufinit(h);
259 bufcat_style(h, "width", "80%");
260 bufcat_style(h, "white-space", "nowrap");
261 bufcat_style(h, "text-align", "center");
262 PAIR_STYLE_INIT(&tag[0], h);
263 print_otag(h, TAG_TD, 1, tag);
264 print_text(h, b);
265 print_stagq(h, tt);
266
267 bufinit(h);
268 bufcat_style(h, "width", "10%");
269 bufcat_style(h, "text-align", "right");
270 PAIR_STYLE_INIT(&tag[0], h);
271 print_otag(h, TAG_TD, 1, tag);
272 print_text(h, title);
273 print_tagq(h, t);
274 return(1);
275 }
276
277
278 /* ARGSUSED */
279 static void
280 man_root_post(MAN_ARGS)
281 {
282 struct tm tm;
283 struct htmlpair tag[2];
284 struct tag *t, *tt;
285 char b[BUFSIZ];
286
287 (void)localtime_r(&m->date, &tm);
288
289 if (0 == strftime(b, BUFSIZ - 1, "%B %e, %Y", &tm))
290 err(EXIT_FAILURE, "strftime");
291
292 PAIR_CLASS_INIT(&tag[0], "footer");
293 bufcat_style(h, "width", "100%");
294 PAIR_STYLE_INIT(&tag[1], h);
295 t = print_otag(h, TAG_TABLE, 2, tag);
296 tt = print_otag(h, TAG_TR, 0, NULL);
297
298 bufinit(h);
299 bufcat_style(h, "width", "50%");
300 PAIR_STYLE_INIT(&tag[0], h);
301 print_otag(h, TAG_TD, 1, tag);
302 print_text(h, b);
303 print_stagq(h, tt);
304
305 bufinit(h);
306 bufcat_style(h, "width", "50%");
307 bufcat_style(h, "text-align", "right");
308 PAIR_STYLE_INIT(&tag[0], h);
309 print_otag(h, TAG_TD, 1, tag);
310 if (m->source)
311 print_text(h, m->source);
312 print_tagq(h, t);
313 }
314
315
316
317 /* ARGSUSED */
318 static int
319 man_br_pre(MAN_ARGS)
320 {
321 struct roffsu su;
322 struct htmlpair tag;
323
324 SCALE_VS_INIT(&su, 1);
325
326 if (MAN_sp == n->tok) {
327 su.scale = 1;
328 if (n->child)
329 a2roffsu(n->child->string, &su);
330 } else if (MAN_br == n->tok)
331 su.scale = 0;
332
333 bufcat_su(h, "height", &su);
334 PAIR_STYLE_INIT(&tag, h);
335 print_otag(h, TAG_DIV, 1, &tag);
336 return(0);
337 }
338
339
340 /* ARGSUSED */
341 static int
342 man_SH_pre(MAN_ARGS)
343 {
344 struct htmlpair tag[2];
345 struct roffsu su;
346
347 if (MAN_BODY == n->type) {
348 SCALE_HS_INIT(&su, INDENT);
349 bufcat_su(h, "margin-left", &su);
350 PAIR_CLASS_INIT(&tag[0], "sec-body");
351 PAIR_STYLE_INIT(&tag[1], h);
352 print_otag(h, TAG_DIV, 2, tag);
353 return(1);
354 } else if (MAN_BLOCK == n->type) {
355 PAIR_CLASS_INIT(&tag[0], "sec-block");
356 if (n->prev && MAN_SH == n->prev->tok)
357 if (NULL == n->prev->body->child) {
358 print_otag(h, TAG_DIV, 1, tag);
359 return(1);
360 }
361
362 SCALE_VS_INIT(&su, 1);
363 bufcat_su(h, "margin-top", &su);
364 if (NULL == n->next)
365 bufcat_su(h, "margin-bottom", &su);
366 PAIR_STYLE_INIT(&tag[1], h);
367 print_otag(h, TAG_DIV, 2, tag);
368 return(1);
369 }
370
371 PAIR_CLASS_INIT(&tag[0], "sec-head");
372 print_otag(h, TAG_DIV, 1, tag);
373 return(1);
374 }
375
376
377 /* ARGSUSED */
378 static int
379 man_alt_pre(MAN_ARGS)
380 {
381 const struct man_node *nn;
382 struct tag *t;
383 int i;
384 struct htmlpair tagi, tagb, *tagp;
385
386 PAIR_CLASS_INIT(&tagi, "italic");
387 PAIR_CLASS_INIT(&tagb, "bold");
388
389 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
390 switch (n->tok) {
391 case (MAN_BI):
392 tagp = i % 2 ? &tagi : &tagb;
393 break;
394 case (MAN_IB):
395 tagp = i % 2 ? &tagb : &tagi;
396 break;
397 case (MAN_RI):
398 tagp = i % 2 ? &tagi : NULL;
399 break;
400 case (MAN_IR):
401 tagp = i % 2 ? NULL : &tagi;
402 break;
403 case (MAN_BR):
404 tagp = i % 2 ? NULL : &tagb;
405 break;
406 case (MAN_RB):
407 tagp = i % 2 ? &tagb : NULL;
408 break;
409 default:
410 abort();
411 /* NOTREACHED */
412 }
413
414 if (i)
415 h->flags |= HTML_NOSPACE;
416
417 if (tagp) {
418 t = print_otag(h, TAG_SPAN, 1, tagp);
419 print_man_node(m, nn, h);
420 print_tagq(h, t);
421 } else
422 print_man_node(m, nn, h);
423 }
424
425 return(0);
426 }
427
428
429 /* ARGSUSED */
430 static int
431 man_SB_pre(MAN_ARGS)
432 {
433 struct htmlpair tag;
434
435 PAIR_CLASS_INIT(&tag, "small bold");
436 print_otag(h, TAG_SPAN, 1, &tag);
437 return(1);
438 }
439
440
441 /* ARGSUSED */
442 static int
443 man_SM_pre(MAN_ARGS)
444 {
445 struct htmlpair tag;
446
447 PAIR_CLASS_INIT(&tag, "small");
448 print_otag(h, TAG_SPAN, 1, &tag);
449 return(1);
450 }
451
452
453 /* ARGSUSED */
454 static int
455 man_SS_pre(MAN_ARGS)
456 {
457 struct htmlpair tag[3];
458 struct roffsu su;
459
460 SCALE_VS_INIT(&su, 1);
461
462 if (MAN_BODY == n->type) {
463 PAIR_CLASS_INIT(&tag[0], "ssec-body");
464 if (n->parent->next && n->child) {
465 bufcat_su(h, "margin-bottom", &su);
466 PAIR_STYLE_INIT(&tag[1], h);
467 print_otag(h, TAG_DIV, 2, tag);
468 return(1);
469 }
470
471 print_otag(h, TAG_DIV, 1, tag);
472 return(1);
473 } else if (MAN_BLOCK == n->type) {
474 PAIR_CLASS_INIT(&tag[0], "ssec-block");
475 if (n->prev && MAN_SS == n->prev->tok)
476 if (n->prev->body->child) {
477 bufcat_su(h, "margin-top", &su);
478 PAIR_STYLE_INIT(&tag[1], h);
479 print_otag(h, TAG_DIV, 2, tag);
480 return(1);
481 }
482
483 print_otag(h, TAG_DIV, 1, tag);
484 return(1);
485 }
486
487 SCALE_HS_INIT(&su, INDENT - HALFINDENT);
488 bufcat_su(h, "margin-left", &su);
489 PAIR_CLASS_INIT(&tag[0], "ssec-head");
490 PAIR_STYLE_INIT(&tag[1], h);
491 print_otag(h, TAG_DIV, 2, tag);
492 return(1);
493 }
494
495
496 /* ARGSUSED */
497 static int
498 man_PP_pre(MAN_ARGS)
499 {
500 struct htmlpair tag;
501 struct roffsu su;
502 int i;
503
504 if (MAN_BLOCK != n->type)
505 return(1);
506
507 i = 0;
508
509 if (MAN_ROOT == n->parent->tok) {
510 SCALE_HS_INIT(&su, INDENT);
511 bufcat_su(h, "margin-left", &su);
512 i++;
513 }
514 if (n->next && n->next->child) {
515 SCALE_VS_INIT(&su, 1);
516 bufcat_su(h, "margin-bottom", &su);
517 i++;
518 }
519
520 PAIR_STYLE_INIT(&tag, h);
521 print_otag(h, TAG_DIV, i ? 1 : 0, &tag);
522 return(1);
523 }
524
525
526 /* ARGSUSED */
527 static int
528 man_IP_pre(MAN_ARGS)
529 {
530 struct roffsu su;
531 struct htmlpair tag;
532 const struct man_node *nn;
533 int width;
534
535 /*
536 * This scattering of 1-BU margins and pads is to make sure that
537 * when text overruns its box, the subsequent text isn't flush
538 * up against it. However, the rest of the right-hand box must
539 * also be adjusted in consideration of this 1-BU space.
540 */
541
542 if (MAN_BODY == n->type) {
543 SCALE_HS_INIT(&su, INDENT);
544 bufcat_su(h, "margin-left", &su);
545 PAIR_STYLE_INIT(&tag, h);
546 print_otag(h, TAG_DIV, 1, &tag);
547 return(1);
548 }
549
550 nn = MAN_BLOCK == n->type ?
551 n->head->child : n->parent->head->child;
552
553 SCALE_HS_INIT(&su, INDENT);
554 width = 0;
555
556 if (MAN_IP == n->tok && NULL != nn)
557 if (NULL != (nn = nn->next)) {
558 for ( ; nn->next; nn = nn->next)
559 /* Do nothing. */ ;
560 width = a2width(nn, &su);
561 }
562
563 if (MAN_TP == n->tok && NULL != nn)
564 width = a2width(nn, &su);
565
566 if (MAN_BLOCK == n->type) {
567 bufcat_su(h, "margin-left", &su);
568 bufcat_style(h, "clear", "both");
569 PAIR_STYLE_INIT(&tag, h);
570 print_otag(h, TAG_DIV, 1, &tag);
571 return(1);
572 }
573
574 bufcat_su(h, "min-width", &su);
575 SCALE_INVERT(&su);
576 bufcat_su(h, "margin-left", &su);
577 SCALE_HS_INIT(&su, 1);
578 bufcat_su(h, "margin-right", &su);
579 bufcat_style(h, "clear", "left");
580
581 if (n->next && n->next->child)
582 bufcat_style(h, "float", "left");
583
584 PAIR_STYLE_INIT(&tag, h);
585 print_otag(h, TAG_DIV, 1, &tag);
586
587 /* With a length string, manually omit the last child. */
588
589 if ( ! width)
590 return(1);
591
592 if (MAN_IP == n->tok)
593 for (nn = n->child; nn->next; nn = nn->next)
594 print_man_node(m, nn, h);
595 if (MAN_TP == n->tok)
596 for (nn = n->child->next; nn; nn = nn->next)
597 print_man_node(m, nn, h);
598
599 return(0);
600 }
601
602
603 /* ARGSUSED */
604 static int
605 man_HP_pre(MAN_ARGS)
606 {
607 const struct man_node *nn;
608 struct htmlpair tag;
609 struct roffsu su;
610
611 if (MAN_HEAD == n->type)
612 return(0);
613
614 nn = MAN_BLOCK == n->type ?
615 n->head->child : n->parent->head->child;
616
617 SCALE_HS_INIT(&su, INDENT);
618
619 if (NULL != nn)
620 (void)a2width(nn, &su);
621
622 if (MAN_BLOCK == n->type) {
623 bufcat_su(h, "margin-left", &su);
624 bufcat_style(h, "clear", "both");
625 PAIR_STYLE_INIT(&tag, h);
626 print_otag(h, TAG_DIV, 1, &tag);
627 return(1);
628 }
629
630 bufcat_su(h, "margin-left", &su);
631 SCALE_INVERT(&su);
632 bufcat_su(h, "text-indent", &su);
633
634 PAIR_STYLE_INIT(&tag, h);
635 print_otag(h, TAG_DIV, 1, &tag);
636 return(1);
637 }
638
639
640 /* ARGSUSED */
641 static int
642 man_B_pre(MAN_ARGS)
643 {
644 struct htmlpair tag;
645
646 PAIR_CLASS_INIT(&tag, "bold");
647 print_otag(h, TAG_SPAN, 1, &tag);
648 return(1);
649 }
650
651
652 /* ARGSUSED */
653 static int
654 man_I_pre(MAN_ARGS)
655 {
656 struct htmlpair tag;
657
658 PAIR_CLASS_INIT(&tag, "italic");
659 print_otag(h, TAG_SPAN, 1, &tag);
660 return(1);
661 }
662
663
664 /* ARGSUSED */
665 static int
666 man_ign_pre(MAN_ARGS)
667 {
668
669 return(0);
670 }