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