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