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