xref: /dflybsd-src/contrib/mdocml/tbl_html.c (revision 16e9ff28733d8bd9941b9770d79be966ba221f5f)
1 /*	$Id: tbl_html.c,v 1.5 2011/01/06 12:31:39 kristaps Exp $ */
2 /*
3  * Copyright (c) 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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "mandoc.h"
27 #include "out.h"
28 #include "html.h"
29 
30 static	size_t	 html_tbl_len(size_t, void *);
31 static	size_t	 html_tbl_strlen(const char *, void *);
32 
33 /* ARGSUSED */
34 static size_t
35 html_tbl_len(size_t sz, void *arg)
36 {
37 
38 	return(sz);
39 }
40 
41 /* ARGSUSED */
42 static size_t
43 html_tbl_strlen(const char *p, void *arg)
44 {
45 
46 	return(strlen(p));
47 }
48 
49 void
50 print_tbl(struct html *h, const struct tbl_span *sp)
51 {
52 	const struct tbl_head *hp;
53 	const struct tbl_dat *dp;
54 	struct tag	*tt;
55 	struct htmlpair	 tag;
56 	struct roffsu	 su;
57 	struct roffcol	*col;
58 
59 	/* Inhibit printing of spaces: we do padding ourselves. */
60 
61 	h->flags |= HTML_NONOSPACE;
62 	h->flags |= HTML_NOSPACE;
63 
64 	/* First pass: calculate widths. */
65 
66 	if (TBL_SPAN_FIRST & sp->flags) {
67 		h->tbl.len = html_tbl_len;
68 		h->tbl.slen = html_tbl_strlen;
69 		tblcalc(&h->tbl, sp);
70 	}
71 
72 	switch (sp->pos) {
73 	case (TBL_SPAN_HORIZ):
74 		/* FALLTHROUGH */
75 	case (TBL_SPAN_DHORIZ):
76 		break;
77 	default:
78 		PAIR_CLASS_INIT(&tag, "tbl");
79 		print_otag(h, TAG_TABLE, 1, &tag);
80 		print_otag(h, TAG_TR, 0, NULL);
81 
82 		/* Iterate over template headers. */
83 
84 		dp = sp->first;
85 		for (hp = sp->head; hp; hp = hp->next) {
86 			switch (hp->pos) {
87 			case (TBL_HEAD_VERT):
88 				/* FALLTHROUGH */
89 			case (TBL_HEAD_DVERT):
90 				continue;
91 			case (TBL_HEAD_DATA):
92 				break;
93 			}
94 
95 			/*
96 			 * For the time being, use the simplest possible
97 			 * table styling: setting the widths of data
98 			 * columns.
99 			 */
100 
101 			col = &h->tbl.cols[hp->ident];
102 			SCALE_HS_INIT(&su, col->width);
103 			bufcat_su(h, "width", &su);
104 			PAIR_STYLE_INIT(&tag, h);
105 			tt = print_otag(h, TAG_TD, 1, &tag);
106 
107 			if (dp && dp->string)
108 				print_text(h, dp->string);
109 			if (dp)
110 				dp = dp->next;
111 
112 			print_tagq(h, tt);
113 		}
114 		break;
115 	}
116 
117 	h->flags &= ~HTML_NONOSPACE;
118 
119 	/* Close out column specifiers on the last span. */
120 
121 	if (TBL_SPAN_LAST & sp->flags) {
122 		assert(h->tbl.cols);
123 		free(h->tbl.cols);
124 		h->tbl.cols = NULL;
125 	}
126 }
127