]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
Sync section titles with OpenBSD.
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.97 2014/08/10 23:54:41 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 #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.h"
29 #include "mandoc_aux.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
40 #define MAN_ARGS const struct man_meta *man, \
41 const struct man_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 man_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 man_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_ign_pre, NULL }, /* na */
105 { man_br_pre, NULL }, /* sp */
106 { man_literal_pre, NULL }, /* nf */
107 { man_literal_pre, NULL }, /* fi */
108 { NULL, NULL }, /* RE */
109 { man_RS_pre, NULL }, /* RS */
110 { man_ign_pre, NULL }, /* DT */
111 { man_ign_pre, NULL }, /* UC */
112 { man_ign_pre, NULL }, /* PD */
113 { man_ign_pre, NULL }, /* AT */
114 { man_in_pre, NULL }, /* in */
115 { man_ign_pre, NULL }, /* ft */
116 { man_OP_pre, NULL }, /* OP */
117 { man_literal_pre, NULL }, /* EX */
118 { man_literal_pre, NULL }, /* EE */
119 { man_UR_pre, NULL }, /* UR */
120 { NULL, NULL }, /* UE */
121 { man_ign_pre, NULL }, /* ll */
122 };
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 static void
182 print_man_head(MAN_ARGS)
183 {
184
185 print_gen_head(h);
186 assert(man->title);
187 assert(man->msec);
188 bufcat_fmt(h, "%s(%s)", man->title, man->msec);
189 print_otag(h, TAG_TITLE, 0, NULL);
190 print_text(h, h->buf);
191 }
192
193 static void
194 print_man_nodelist(MAN_ARGS)
195 {
196
197 print_man_node(man, n, mh, h);
198 if (n->next)
199 print_man_nodelist(man, n->next, mh, h);
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 MAN_ROOT:
213 man_root_pre(man, n, mh, h);
214 break;
215 case MAN_TEXT:
216 /*
217 * If we have a blank line, output a vertical space.
218 * If we have a space as the first character, break
219 * before printing the line's data.
220 */
221 if ('\0' == *n->string) {
222 print_otag(h, TAG_P, 0, NULL);
223 return;
224 }
225
226 if (' ' == *n->string && MAN_LINE & n->flags)
227 print_otag(h, TAG_BR, 0, NULL);
228 else if (MANH_LITERAL & mh->fl && n->prev)
229 print_otag(h, TAG_BR, 0, NULL);
230
231 print_text(h, n->string);
232 return;
233 case MAN_EQN:
234 print_eqn(h, n->eqn);
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)(man, n, mh, h);
265 break;
266 }
267
268 if (child && n->child)
269 print_man_nodelist(man, 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(man, n, mh, h);
277 break;
278 case MAN_EQN:
279 break;
280 default:
281 if (mans[n->tok].post)
282 (*mans[n->tok].post)(man, n, mh, h);
283 break;
284 }
285 }
286
287 static int
288 a2width(const struct man_node *n, struct roffsu *su)
289 {
290
291 if (MAN_TEXT != n->type)
292 return(0);
293 if (a2roffsu(n->string, su, SCALE_BU))
294 return(1);
295
296 return(0);
297 }
298
299 static void
300 man_root_pre(MAN_ARGS)
301 {
302 struct htmlpair tag[3];
303 struct tag *t, *tt;
304 char *title;
305
306 assert(man->title);
307 assert(man->msec);
308 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
309
310 PAIR_SUMMARY_INIT(&tag[0], "Document Header");
311 PAIR_CLASS_INIT(&tag[1], "head");
312 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
313 t = print_otag(h, TAG_TABLE, 3, tag);
314 PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
315 print_otag(h, TAG_COL, 1, tag);
316 print_otag(h, TAG_COL, 1, tag);
317 print_otag(h, TAG_COL, 1, tag);
318
319 print_otag(h, TAG_TBODY, 0, NULL);
320
321 tt = print_otag(h, TAG_TR, 0, NULL);
322
323 PAIR_CLASS_INIT(&tag[0], "head-ltitle");
324 print_otag(h, TAG_TD, 1, tag);
325 print_text(h, title);
326 print_stagq(h, tt);
327
328 PAIR_CLASS_INIT(&tag[0], "head-vol");
329 PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
330 print_otag(h, TAG_TD, 2, tag);
331 if (NULL != man->vol)
332 print_text(h, man->vol);
333 print_stagq(h, tt);
334
335 PAIR_CLASS_INIT(&tag[0], "head-rtitle");
336 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
337 print_otag(h, TAG_TD, 2, tag);
338 print_text(h, title);
339 print_tagq(h, t);
340 free(title);
341 }
342
343 static void
344 man_root_post(MAN_ARGS)
345 {
346 struct htmlpair tag[3];
347 struct tag *t, *tt;
348
349 PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
350 PAIR_CLASS_INIT(&tag[1], "foot");
351 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
352 t = print_otag(h, TAG_TABLE, 3, tag);
353 PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
354 print_otag(h, TAG_COL, 1, tag);
355 print_otag(h, TAG_COL, 1, tag);
356
357 tt = print_otag(h, TAG_TR, 0, NULL);
358
359 PAIR_CLASS_INIT(&tag[0], "foot-date");
360 print_otag(h, TAG_TD, 1, tag);
361
362 assert(man->date);
363 print_text(h, man->date);
364 print_stagq(h, tt);
365
366 PAIR_CLASS_INIT(&tag[0], "foot-os");
367 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
368 print_otag(h, TAG_TD, 2, tag);
369
370 if (man->source)
371 print_text(h, man->source);
372 print_tagq(h, t);
373 }
374
375
376 static int
377 man_br_pre(MAN_ARGS)
378 {
379 struct roffsu su;
380 struct htmlpair tag;
381
382 SCALE_VS_INIT(&su, 1);
383
384 if (MAN_sp == n->tok) {
385 if (NULL != (n = n->child))
386 if ( ! a2roffsu(n->string, &su, SCALE_VS))
387 SCALE_VS_INIT(&su, atoi(n->string));
388 } else
389 su.scale = 0.0;
390
391 bufinit(h);
392 bufcat_su(h, "height", &su);
393 PAIR_STYLE_INIT(&tag, h);
394 print_otag(h, TAG_DIV, 1, &tag);
395
396 /* So the div isn't empty: */
397 print_text(h, "\\~");
398
399 return(0);
400 }
401
402 static int
403 man_SH_pre(MAN_ARGS)
404 {
405 struct htmlpair tag;
406
407 if (MAN_BLOCK == n->type) {
408 mh->fl &= ~MANH_LITERAL;
409 PAIR_CLASS_INIT(&tag, "section");
410 print_otag(h, TAG_DIV, 1, &tag);
411 return(1);
412 } else if (MAN_BODY == n->type)
413 return(1);
414
415 print_otag(h, TAG_H1, 0, NULL);
416 return(1);
417 }
418
419 static int
420 man_alt_pre(MAN_ARGS)
421 {
422 const struct man_node *nn;
423 int i, savelit;
424 enum htmltag fp;
425 struct tag *t;
426
427 if ((savelit = mh->fl & MANH_LITERAL))
428 print_otag(h, TAG_BR, 0, NULL);
429
430 mh->fl &= ~MANH_LITERAL;
431
432 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
433 t = NULL;
434 switch (n->tok) {
435 case MAN_BI:
436 fp = i % 2 ? TAG_I : TAG_B;
437 break;
438 case MAN_IB:
439 fp = i % 2 ? TAG_B : TAG_I;
440 break;
441 case MAN_RI:
442 fp = i % 2 ? TAG_I : TAG_MAX;
443 break;
444 case MAN_IR:
445 fp = i % 2 ? TAG_MAX : TAG_I;
446 break;
447 case MAN_BR:
448 fp = i % 2 ? TAG_MAX : TAG_B;
449 break;
450 case MAN_RB:
451 fp = i % 2 ? TAG_B : TAG_MAX;
452 break;
453 default:
454 abort();
455 /* NOTREACHED */
456 }
457
458 if (i)
459 h->flags |= HTML_NOSPACE;
460
461 if (TAG_MAX != fp)
462 t = print_otag(h, fp, 0, NULL);
463
464 print_man_node(man, nn, mh, h);
465
466 if (t)
467 print_tagq(h, t);
468 }
469
470 if (savelit)
471 mh->fl |= MANH_LITERAL;
472
473 return(0);
474 }
475
476 static int
477 man_SM_pre(MAN_ARGS)
478 {
479
480 print_otag(h, TAG_SMALL, 0, NULL);
481 if (MAN_SB == n->tok)
482 print_otag(h, TAG_B, 0, NULL);
483 return(1);
484 }
485
486 static int
487 man_SS_pre(MAN_ARGS)
488 {
489 struct htmlpair tag;
490
491 if (MAN_BLOCK == n->type) {
492 mh->fl &= ~MANH_LITERAL;
493 PAIR_CLASS_INIT(&tag, "subsection");
494 print_otag(h, TAG_DIV, 1, &tag);
495 return(1);
496 } else if (MAN_BODY == n->type)
497 return(1);
498
499 print_otag(h, TAG_H2, 0, NULL);
500 return(1);
501 }
502
503 static int
504 man_PP_pre(MAN_ARGS)
505 {
506
507 if (MAN_HEAD == n->type)
508 return(0);
509 else if (MAN_BLOCK == n->type)
510 print_bvspace(h, n);
511
512 return(1);
513 }
514
515 static int
516 man_IP_pre(MAN_ARGS)
517 {
518 const struct man_node *nn;
519
520 if (MAN_BODY == n->type) {
521 print_otag(h, TAG_DD, 0, NULL);
522 return(1);
523 } else if (MAN_HEAD != n->type) {
524 print_otag(h, TAG_DL, 0, NULL);
525 return(1);
526 }
527
528 /* FIXME: width specification. */
529
530 print_otag(h, TAG_DT, 0, NULL);
531
532 /* For IP, only print the first header element. */
533
534 if (MAN_IP == n->tok && n->child)
535 print_man_node(man, n->child, mh, h);
536
537 /* For TP, only print next-line header elements. */
538
539 if (MAN_TP == n->tok) {
540 nn = n->child;
541 while (NULL != nn && 0 == (MAN_LINE & nn->flags))
542 nn = nn->next;
543 while (NULL != nn) {
544 print_man_node(man, nn, mh, h);
545 nn = nn->next;
546 }
547 }
548
549 return(0);
550 }
551
552 static int
553 man_HP_pre(MAN_ARGS)
554 {
555 struct htmlpair tag;
556 struct roffsu su;
557 const struct man_node *np;
558
559 if (MAN_HEAD == n->type)
560 return(0);
561 else if (MAN_BLOCK != n->type)
562 return(1);
563
564 np = n->head->child;
565
566 if (NULL == np || ! a2width(np, &su))
567 SCALE_HS_INIT(&su, INDENT);
568
569 bufinit(h);
570
571 print_bvspace(h, n);
572 bufcat_su(h, "margin-left", &su);
573 su.scale = -su.scale;
574 bufcat_su(h, "text-indent", &su);
575 PAIR_STYLE_INIT(&tag, h);
576 print_otag(h, TAG_P, 1, &tag);
577 return(1);
578 }
579
580 static int
581 man_OP_pre(MAN_ARGS)
582 {
583 struct tag *tt;
584 struct htmlpair tag;
585
586 print_text(h, "[");
587 h->flags |= HTML_NOSPACE;
588 PAIR_CLASS_INIT(&tag, "opt");
589 tt = print_otag(h, TAG_SPAN, 1, &tag);
590
591 if (NULL != (n = n->child)) {
592 print_otag(h, TAG_B, 0, NULL);
593 print_text(h, n->string);
594 }
595
596 print_stagq(h, tt);
597
598 if (NULL != n && NULL != n->next) {
599 print_otag(h, TAG_I, 0, NULL);
600 print_text(h, n->next->string);
601 }
602
603 print_stagq(h, tt);
604 h->flags |= HTML_NOSPACE;
605 print_text(h, "]");
606 return(0);
607 }
608
609 static int
610 man_B_pre(MAN_ARGS)
611 {
612
613 print_otag(h, TAG_B, 0, NULL);
614 return(1);
615 }
616
617 static int
618 man_I_pre(MAN_ARGS)
619 {
620
621 print_otag(h, TAG_I, 0, NULL);
622 return(1);
623 }
624
625 static int
626 man_literal_pre(MAN_ARGS)
627 {
628
629 if (MAN_fi == n->tok || MAN_EE == n->tok) {
630 print_otag(h, TAG_BR, 0, NULL);
631 mh->fl &= ~MANH_LITERAL;
632 } else
633 mh->fl |= MANH_LITERAL;
634
635 return(0);
636 }
637
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 static int
647 man_ign_pre(MAN_ARGS)
648 {
649
650 return(0);
651 }
652
653 static int
654 man_RS_pre(MAN_ARGS)
655 {
656 struct htmlpair tag;
657 struct roffsu su;
658
659 if (MAN_HEAD == n->type)
660 return(0);
661 else if (MAN_BODY == n->type)
662 return(1);
663
664 SCALE_HS_INIT(&su, INDENT);
665 if (n->head->child)
666 a2width(n->head->child, &su);
667
668 bufinit(h);
669 bufcat_su(h, "margin-left", &su);
670 PAIR_STYLE_INIT(&tag, h);
671 print_otag(h, TAG_DIV, 1, &tag);
672 return(1);
673 }
674
675 static int
676 man_UR_pre(MAN_ARGS)
677 {
678 struct htmlpair tag[2];
679
680 n = n->child;
681 assert(MAN_HEAD == n->type);
682 if (n->nchild) {
683 assert(MAN_TEXT == n->child->type);
684 PAIR_CLASS_INIT(&tag[0], "link-ext");
685 PAIR_HREF_INIT(&tag[1], n->child->string);
686 print_otag(h, TAG_A, 2, tag);
687 }
688
689 assert(MAN_BODY == n->next->type);
690 if (n->next->nchild)
691 n = n->next;
692
693 print_man_nodelist(man, n->child, mh, h);
694
695 return(0);
696 }