xref: /titanic_51/usr/src/cmd/mandoc/tbl_html.c (revision 7f11fd00fc23e2af7ae21cc8837a2b86380dcfa7)
1 /*	$Id: tbl_html.c,v 1.16 2015/01/30 17:32:16 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 #include "config.h"
18 
19 #include <sys/types.h>
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	void	 html_tblopen(struct html *, const struct tbl_span *);
31 static	size_t	 html_tbl_len(size_t, void *);
32 static	size_t	 html_tbl_strlen(const char *, void *);
33 
34 
35 static size_t
36 html_tbl_len(size_t sz, void *arg)
37 {
38 
39 	return(sz);
40 }
41 
42 static size_t
43 html_tbl_strlen(const char *p, void *arg)
44 {
45 
46 	return(strlen(p));
47 }
48 
49 static void
50 html_tblopen(struct html *h, const struct tbl_span *sp)
51 {
52 	struct htmlpair	 tag;
53 	struct roffsu	 su;
54 	struct roffcol	*col;
55 	int		 ic;
56 
57 	if (h->tbl.cols == NULL) {
58 		h->tbl.len = html_tbl_len;
59 		h->tbl.slen = html_tbl_strlen;
60 		tblcalc(&h->tbl, sp, 0);
61 	}
62 
63 	assert(NULL == h->tblt);
64 	PAIR_CLASS_INIT(&tag, "tbl");
65 	h->tblt = print_otag(h, TAG_TABLE, 1, &tag);
66 
67 	for (ic = 0; ic < sp->opts->cols; ic++) {
68 		bufinit(h);
69 		col = h->tbl.cols + ic;
70 		SCALE_HS_INIT(&su, col->width);
71 		bufcat_su(h, "width", &su);
72 		PAIR_STYLE_INIT(&tag, h);
73 		print_otag(h, TAG_COL, 1, &tag);
74 	}
75 
76 	print_otag(h, TAG_TBODY, 0, NULL);
77 }
78 
79 void
80 print_tblclose(struct html *h)
81 {
82 
83 	assert(h->tblt);
84 	print_tagq(h, h->tblt);
85 	h->tblt = NULL;
86 }
87 
88 void
89 print_tbl(struct html *h, const struct tbl_span *sp)
90 {
91 	const struct tbl_dat *dp;
92 	struct htmlpair	 tag;
93 	struct tag	*tt;
94 	int		 ic;
95 
96 	/* Inhibit printing of spaces: we do padding ourselves. */
97 
98 	if (h->tblt == NULL)
99 		html_tblopen(h, sp);
100 
101 	assert(h->tblt);
102 
103 	h->flags |= HTML_NONOSPACE;
104 	h->flags |= HTML_NOSPACE;
105 
106 	tt = print_otag(h, TAG_TR, 0, NULL);
107 
108 	switch (sp->pos) {
109 	case TBL_SPAN_HORIZ:
110 		/* FALLTHROUGH */
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