xref: /openbsd-src/usr.bin/mandoc/tbl_html.c (revision d59bb9942320b767f2a19aaa7690c8c6e30b724c)
1 /*	$OpenBSD: tbl_html.c,v 1.15 2017/02/05 18:13:28 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014, 2015, 2017 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 tag	*t;
52 	int		 ic;
53 
54 	if (h->tbl.cols == NULL) {
55 		h->tbl.len = html_tbl_len;
56 		h->tbl.slen = html_tbl_strlen;
57 		tblcalc(&h->tbl, sp, 0);
58 	}
59 
60 	assert(NULL == h->tblt);
61 	h->tblt = print_otag(h, TAG_TABLE, "c", "tbl");
62 
63 	t = print_otag(h, TAG_COLGROUP, "");
64 	for (ic = 0; ic < sp->opts->cols; ic++)
65 		print_otag(h, TAG_COL, "shw", h->tbl.cols[ic].width);
66 	print_tagq(h, t);
67 }
68 
69 void
70 print_tblclose(struct html *h)
71 {
72 
73 	assert(h->tblt);
74 	print_tagq(h, h->tblt);
75 	h->tblt = NULL;
76 }
77 
78 void
79 print_tbl(struct html *h, const struct tbl_span *sp)
80 {
81 	const struct tbl_dat *dp;
82 	struct tag	*tt;
83 	int		 ic;
84 
85 	/* Inhibit printing of spaces: we do padding ourselves. */
86 
87 	if (h->tblt == NULL)
88 		html_tblopen(h, sp);
89 
90 	assert(h->tblt);
91 
92 	h->flags |= HTML_NONOSPACE;
93 	h->flags |= HTML_NOSPACE;
94 
95 	tt = print_otag(h, TAG_TR, "");
96 
97 	switch (sp->pos) {
98 	case TBL_SPAN_HORIZ:
99 	case TBL_SPAN_DHORIZ:
100 		print_otag(h, TAG_TD, "?", "colspan", "0");
101 		break;
102 	default:
103 		dp = sp->first;
104 		for (ic = 0; ic < sp->opts->cols; ic++) {
105 			print_stagq(h, tt);
106 			print_otag(h, TAG_TD, "");
107 
108 			if (dp == NULL || dp->layout->col > ic)
109 				continue;
110 			if (dp->layout->pos != TBL_CELL_DOWN)
111 				if (dp->string != NULL)
112 					print_text(h, dp->string);
113 			dp = dp->next;
114 		}
115 		break;
116 	}
117 
118 	print_tagq(h, tt);
119 
120 	h->flags &= ~HTML_NONOSPACE;
121 
122 	if (sp->next == NULL) {
123 		assert(h->tbl.cols);
124 		free(h->tbl.cols);
125 		h->tbl.cols = NULL;
126 		print_tblclose(h);
127 	}
128 
129 }
130