]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
integrate preconv(1) into mandoc(1);
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.104 2014/09/27 11:17:19 kristaps 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.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_paragraph(h);
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_paragraph(h);
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;
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_CLASS_INIT(&tag, "head");
311 t = print_otag(h, TAG_TABLE, 1, &tag);
312
313 print_otag(h, TAG_TBODY, 0, NULL);
314
315 tt = print_otag(h, TAG_TR, 0, NULL);
316
317 PAIR_CLASS_INIT(&tag, "head-ltitle");
318 print_otag(h, TAG_TD, 1, &tag);
319 print_text(h, title);
320 print_stagq(h, tt);
321
322 PAIR_CLASS_INIT(&tag, "head-vol");
323 print_otag(h, TAG_TD, 1, &tag);
324 if (NULL != man->vol)
325 print_text(h, man->vol);
326 print_stagq(h, tt);
327
328 PAIR_CLASS_INIT(&tag, "head-rtitle");
329 print_otag(h, TAG_TD, 1, &tag);
330 print_text(h, title);
331 print_tagq(h, t);
332 free(title);
333 }
334
335 static void
336 man_root_post(MAN_ARGS)
337 {
338 struct htmlpair tag;
339 struct tag *t, *tt;
340
341 PAIR_CLASS_INIT(&tag, "foot");
342 t = print_otag(h, TAG_TABLE, 1, &tag);
343
344 tt = print_otag(h, TAG_TR, 0, NULL);
345
346 PAIR_CLASS_INIT(&tag, "foot-date");
347 print_otag(h, TAG_TD, 1, &tag);
348
349 assert(man->date);
350 print_text(h, man->date);
351 print_stagq(h, tt);
352
353 PAIR_CLASS_INIT(&tag, "foot-os");
354 print_otag(h, TAG_TD, 1, &tag);
355
356 if (man->source)
357 print_text(h, man->source);
358 print_tagq(h, t);
359 }
360
361
362 static int
363 man_br_pre(MAN_ARGS)
364 {
365 struct roffsu su;
366 struct htmlpair tag;
367
368 SCALE_VS_INIT(&su, 1);
369
370 if (MAN_sp == n->tok) {
371 if (NULL != (n = n->child))
372 if ( ! a2roffsu(n->string, &su, SCALE_VS))
373 SCALE_VS_INIT(&su, atoi(n->string));
374 } else
375 su.scale = 0.0;
376
377 bufinit(h);
378 bufcat_su(h, "height", &su);
379 PAIR_STYLE_INIT(&tag, h);
380 print_otag(h, TAG_DIV, 1, &tag);
381
382 /* So the div isn't empty: */
383 print_text(h, "\\~");
384
385 return(0);
386 }
387
388 static int
389 man_SH_pre(MAN_ARGS)
390 {
391 struct htmlpair tag;
392
393 if (MAN_BLOCK == n->type) {
394 mh->fl &= ~MANH_LITERAL;
395 PAIR_CLASS_INIT(&tag, "section");
396 print_otag(h, TAG_DIV, 1, &tag);
397 return(1);
398 } else if (MAN_BODY == n->type)
399 return(1);
400
401 print_otag(h, TAG_H1, 0, NULL);
402 return(1);
403 }
404
405 static int
406 man_alt_pre(MAN_ARGS)
407 {
408 const struct man_node *nn;
409 int i, savelit;
410 enum htmltag fp;
411 struct tag *t;
412
413 if ((savelit = mh->fl & MANH_LITERAL))
414 print_otag(h, TAG_BR, 0, NULL);
415
416 mh->fl &= ~MANH_LITERAL;
417
418 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
419 t = NULL;
420 switch (n->tok) {
421 case MAN_BI:
422 fp = i % 2 ? TAG_I : TAG_B;
423 break;
424 case MAN_IB:
425 fp = i % 2 ? TAG_B : TAG_I;
426 break;
427 case MAN_RI:
428 fp = i % 2 ? TAG_I : TAG_MAX;
429 break;
430 case MAN_IR:
431 fp = i % 2 ? TAG_MAX : TAG_I;
432 break;
433 case MAN_BR:
434 fp = i % 2 ? TAG_MAX : TAG_B;
435 break;
436 case MAN_RB:
437 fp = i % 2 ? TAG_B : TAG_MAX;
438 break;
439 default:
440 abort();
441 /* NOTREACHED */
442 }
443
444 if (i)
445 h->flags |= HTML_NOSPACE;
446
447 if (TAG_MAX != fp)
448 t = print_otag(h, fp, 0, NULL);
449
450 print_man_node(man, nn, mh, h);
451
452 if (t)
453 print_tagq(h, t);
454 }
455
456 if (savelit)
457 mh->fl |= MANH_LITERAL;
458
459 return(0);
460 }
461
462 static int
463 man_SM_pre(MAN_ARGS)
464 {
465
466 print_otag(h, TAG_SMALL, 0, NULL);
467 if (MAN_SB == n->tok)
468 print_otag(h, TAG_B, 0, NULL);
469 return(1);
470 }
471
472 static int
473 man_SS_pre(MAN_ARGS)
474 {
475 struct htmlpair tag;
476
477 if (MAN_BLOCK == n->type) {
478 mh->fl &= ~MANH_LITERAL;
479 PAIR_CLASS_INIT(&tag, "subsection");
480 print_otag(h, TAG_DIV, 1, &tag);
481 return(1);
482 } else if (MAN_BODY == n->type)
483 return(1);
484
485 print_otag(h, TAG_H2, 0, NULL);
486 return(1);
487 }
488
489 static int
490 man_PP_pre(MAN_ARGS)
491 {
492
493 if (MAN_HEAD == n->type)
494 return(0);
495 else if (MAN_BLOCK == n->type)
496 print_bvspace(h, n);
497
498 return(1);
499 }
500
501 static int
502 man_IP_pre(MAN_ARGS)
503 {
504 const struct man_node *nn;
505
506 if (MAN_BODY == n->type) {
507 print_otag(h, TAG_DD, 0, NULL);
508 return(1);
509 } else if (MAN_HEAD != n->type) {
510 print_otag(h, TAG_DL, 0, NULL);
511 return(1);
512 }
513
514 /* FIXME: width specification. */
515
516 print_otag(h, TAG_DT, 0, NULL);
517
518 /* For IP, only print the first header element. */
519
520 if (MAN_IP == n->tok && n->child)
521 print_man_node(man, n->child, mh, h);
522
523 /* For TP, only print next-line header elements. */
524
525 if (MAN_TP == n->tok) {
526 nn = n->child;
527 while (NULL != nn && 0 == (MAN_LINE & nn->flags))
528 nn = nn->next;
529 while (NULL != nn) {
530 print_man_node(man, nn, mh, h);
531 nn = nn->next;
532 }
533 }
534
535 return(0);
536 }
537
538 static int
539 man_HP_pre(MAN_ARGS)
540 {
541 struct htmlpair tag[2];
542 struct roffsu su;
543 const struct man_node *np;
544
545 if (MAN_HEAD == n->type)
546 return(0);
547 else if (MAN_BLOCK != n->type)
548 return(1);
549
550 np = n->head->child;
551
552 if (NULL == np || ! a2width(np, &su))
553 SCALE_HS_INIT(&su, INDENT);
554
555 bufinit(h);
556
557 print_bvspace(h, n);
558 bufcat_su(h, "margin-left", &su);
559 su.scale = -su.scale;
560 bufcat_su(h, "text-indent", &su);
561 PAIR_STYLE_INIT(&tag[0], h);
562 PAIR_CLASS_INIT(&tag[1], "spacer");
563 print_otag(h, TAG_DIV, 2, tag);
564 return(1);
565 }
566
567 static int
568 man_OP_pre(MAN_ARGS)
569 {
570 struct tag *tt;
571 struct htmlpair tag;
572
573 print_text(h, "[");
574 h->flags |= HTML_NOSPACE;
575 PAIR_CLASS_INIT(&tag, "opt");
576 tt = print_otag(h, TAG_SPAN, 1, &tag);
577
578 if (NULL != (n = n->child)) {
579 print_otag(h, TAG_B, 0, NULL);
580 print_text(h, n->string);
581 }
582
583 print_stagq(h, tt);
584
585 if (NULL != n && NULL != n->next) {
586 print_otag(h, TAG_I, 0, NULL);
587 print_text(h, n->next->string);
588 }
589
590 print_stagq(h, tt);
591 h->flags |= HTML_NOSPACE;
592 print_text(h, "]");
593 return(0);
594 }
595
596 static int
597 man_B_pre(MAN_ARGS)
598 {
599
600 print_otag(h, TAG_B, 0, NULL);
601 return(1);
602 }
603
604 static int
605 man_I_pre(MAN_ARGS)
606 {
607
608 print_otag(h, TAG_I, 0, NULL);
609 return(1);
610 }
611
612 static int
613 man_literal_pre(MAN_ARGS)
614 {
615
616 if (MAN_fi == n->tok || MAN_EE == n->tok) {
617 print_otag(h, TAG_BR, 0, NULL);
618 mh->fl &= ~MANH_LITERAL;
619 } else
620 mh->fl |= MANH_LITERAL;
621
622 return(0);
623 }
624
625 static int
626 man_in_pre(MAN_ARGS)
627 {
628
629 print_otag(h, TAG_BR, 0, NULL);
630 return(0);
631 }
632
633 static int
634 man_ign_pre(MAN_ARGS)
635 {
636
637 return(0);
638 }
639
640 static int
641 man_RS_pre(MAN_ARGS)
642 {
643 struct htmlpair tag;
644 struct roffsu su;
645
646 if (MAN_HEAD == n->type)
647 return(0);
648 else if (MAN_BODY == n->type)
649 return(1);
650
651 SCALE_HS_INIT(&su, INDENT);
652 if (n->head->child)
653 a2width(n->head->child, &su);
654
655 bufinit(h);
656 bufcat_su(h, "margin-left", &su);
657 PAIR_STYLE_INIT(&tag, h);
658 print_otag(h, TAG_DIV, 1, &tag);
659 return(1);
660 }
661
662 static int
663 man_UR_pre(MAN_ARGS)
664 {
665 struct htmlpair tag[2];
666
667 n = n->child;
668 assert(MAN_HEAD == n->type);
669 if (n->nchild) {
670 assert(MAN_TEXT == n->child->type);
671 PAIR_CLASS_INIT(&tag[0], "link-ext");
672 PAIR_HREF_INIT(&tag[1], n->child->string);
673 print_otag(h, TAG_A, 2, tag);
674 }
675
676 assert(MAN_BODY == n->next->type);
677 if (n->next->nchild)
678 n = n->next;
679
680 print_man_nodelist(man, n->child, mh, h);
681
682 return(0);
683 }