xref: /illumos-gate/usr/src/cmd/mandoc/tbl_html.c (revision ad42deeadc90fc89c86ce30c488a778e2a0b5d68)
1 /*	$Id: tbl_html.c,v 1.18 2015/10/12 00:08:16 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014, 2015 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "mandoc.h"
28 #include "out.h"
29 #include "html.h"
30 
31 static	void	 html_tblopen(struct html *, const struct tbl_span *);
32 static	size_t	 html_tbl_len(size_t, void *);
33 static	size_t	 html_tbl_strlen(const char *, void *);
34 
35 
36 static size_t
37 html_tbl_len(size_t sz, void *arg)
38 {
39 
40 	return sz;
41 }
42 
43 static size_t
44 html_tbl_strlen(const char *p, void *arg)
45 {
46 
47 	return strlen(p);
48 }
49 
50 static void
51 html_tblopen(struct html *h, const struct tbl_span *sp)
52 {
53 	struct htmlpair	 tag;
54 	struct roffsu	 su;
55 	struct roffcol	*col;
56 	int		 ic;
57 
58 	if (h->tbl.cols == NULL) {
59 		h->tbl.len = html_tbl_len;
60 		h->tbl.slen = html_tbl_strlen;
61 		tblcalc(&h->tbl, sp, 0);
62 	}
63 
64 	assert(NULL == h->tblt);
65 	PAIR_CLASS_INIT(&tag, "tbl");
66 	h->tblt = print_otag(h, TAG_TABLE, 1, &tag);
67 
68 	for (ic = 0; ic < sp->opts->cols; ic++) {
69 		bufinit(h);
70 		col = h->tbl.cols + ic;
71 		SCALE_HS_INIT(&su, col->width);
72 		bufcat_su(h, "width", &su);
73 		PAIR_STYLE_INIT(&tag, h);
74 		print_otag(h, TAG_COL, 1, &tag);
75 	}
76 
77 	print_otag(h, TAG_TBODY, 0, NULL);
78 }
79 
80 void
81 print_tblclose(struct html *h)
82 {
83 
84 	assert(h->tblt);
85 	print_tagq(h, h->tblt);
86 	h->tblt = NULL;
87 }
88 
89 void
90 print_tbl(struct html *h, const struct tbl_span *sp)
91 {
92 	const struct tbl_dat *dp;
93 	struct htmlpair	 tag;
94 	struct tag	*tt;
95 	int		 ic;
96 
97 	/* Inhibit printing of spaces: we do padding ourselves. */
98 
99 	if (h->tblt == NULL)
100 		html_tblopen(h, sp);
101 
102 	assert(h->tblt);
103 
104 	h->flags |= HTML_NONOSPACE;
105 	h->flags |= HTML_NOSPACE;
106 
107 	tt = print_otag(h, TAG_TR, 0, NULL);
108 
109 	switch (sp->pos) {
110 	case TBL_SPAN_HORIZ:
111 	case TBL_SPAN_DHORIZ:
112 		PAIR_INIT(&tag, ATTR_COLSPAN, "0");
113 		print_otag(h, TAG_TD, 1, &tag);
114 		break;
115 	default:
116 		dp = sp->first;
117 		for (ic = 0; ic < sp->opts->cols; ic++) {
118 			print_stagq(h, tt);
119 			print_otag(h, TAG_TD, 0, NULL);
120 
121 			if (dp == NULL || dp->layout->col > ic)
122 				continue;
123 			if (dp->layout->pos != TBL_CELL_DOWN)
124 				if (dp->string != NULL)
125 					print_text(h, dp->string);
126 			dp = dp->next;
127 		}
128 		break;
129 	}
130 
131 	print_tagq(h, tt);
132 
133 	h->flags &= ~HTML_NONOSPACE;
134 
135 	if (sp->next == NULL) {
136 		assert(h->tbl.cols);
137 		free(h->tbl.cols);
138 		h->tbl.cols = NULL;
139 		print_tblclose(h);
140 	}
141 
142 }
143