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