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