xref: /minix3/external/bsd/mdocml/dist/tbl_html.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	Id: tbl_html.c,v 1.10 2012/05/27 17:54:54 schwarze Exp  */
2d65f6f70SBen Gras /*
392395e9cSLionel Sambuc  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4d65f6f70SBen Gras  *
5d65f6f70SBen Gras  * Permission to use, copy, modify, and distribute this software for any
6d65f6f70SBen Gras  * purpose with or without fee is hereby granted, provided that the above
7d65f6f70SBen Gras  * copyright notice and this permission notice appear in all copies.
8d65f6f70SBen Gras  *
9d65f6f70SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10d65f6f70SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11d65f6f70SBen Gras  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12d65f6f70SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13d65f6f70SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14d65f6f70SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15d65f6f70SBen Gras  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16d65f6f70SBen Gras  */
17d65f6f70SBen Gras #ifdef HAVE_CONFIG_H
18d65f6f70SBen Gras #include "config.h"
19d65f6f70SBen Gras #endif
20d65f6f70SBen Gras 
21d65f6f70SBen Gras #include <assert.h>
22d65f6f70SBen Gras #include <stdio.h>
23d65f6f70SBen Gras #include <stdlib.h>
24d65f6f70SBen Gras #include <string.h>
25d65f6f70SBen Gras 
26d65f6f70SBen Gras #include "mandoc.h"
27d65f6f70SBen Gras #include "out.h"
28d65f6f70SBen Gras #include "html.h"
29d65f6f70SBen Gras 
3092395e9cSLionel Sambuc static	void	 html_tblopen(struct html *, const struct tbl_span *);
31d65f6f70SBen Gras static	size_t	 html_tbl_len(size_t, void *);
32d65f6f70SBen Gras static	size_t	 html_tbl_strlen(const char *, void *);
33d65f6f70SBen Gras 
34d65f6f70SBen Gras /* ARGSUSED */
35d65f6f70SBen Gras static size_t
html_tbl_len(size_t sz,void * arg)36d65f6f70SBen Gras html_tbl_len(size_t sz, void *arg)
37d65f6f70SBen Gras {
38d65f6f70SBen Gras 
39d65f6f70SBen Gras 	return(sz);
40d65f6f70SBen Gras }
41d65f6f70SBen Gras 
42d65f6f70SBen Gras /* ARGSUSED */
43d65f6f70SBen Gras static size_t
html_tbl_strlen(const char * p,void * arg)44d65f6f70SBen Gras html_tbl_strlen(const char *p, void *arg)
45d65f6f70SBen Gras {
46d65f6f70SBen Gras 
47d65f6f70SBen Gras 	return(strlen(p));
48d65f6f70SBen Gras }
49d65f6f70SBen Gras 
5092395e9cSLionel Sambuc static void
html_tblopen(struct html * h,const struct tbl_span * sp)5192395e9cSLionel Sambuc html_tblopen(struct html *h, const struct tbl_span *sp)
52d65f6f70SBen Gras {
53d65f6f70SBen Gras 	const struct tbl_head *hp;
54d65f6f70SBen Gras 	struct htmlpair	 tag;
55d65f6f70SBen Gras 	struct roffsu	 su;
56d65f6f70SBen Gras 	struct roffcol	*col;
57d65f6f70SBen Gras 
58d65f6f70SBen Gras 	if (TBL_SPAN_FIRST & sp->flags) {
59d65f6f70SBen Gras 		h->tbl.len = html_tbl_len;
60d65f6f70SBen Gras 		h->tbl.slen = html_tbl_strlen;
61d65f6f70SBen Gras 		tblcalc(&h->tbl, sp);
62d65f6f70SBen Gras 	}
63d65f6f70SBen Gras 
6492395e9cSLionel Sambuc 	assert(NULL == h->tblt);
6592395e9cSLionel Sambuc 	PAIR_CLASS_INIT(&tag, "tbl");
6692395e9cSLionel Sambuc 	h->tblt = print_otag(h, TAG_TABLE, 1, &tag);
6792395e9cSLionel Sambuc 
6892395e9cSLionel Sambuc 	for (hp = sp->head; hp; hp = hp->next) {
6992395e9cSLionel Sambuc 		bufinit(h);
7092395e9cSLionel Sambuc 		col = &h->tbl.cols[hp->ident];
7192395e9cSLionel Sambuc 		SCALE_HS_INIT(&su, col->width);
7292395e9cSLionel Sambuc 		bufcat_su(h, "width", &su);
7392395e9cSLionel Sambuc 		PAIR_STYLE_INIT(&tag, h);
7492395e9cSLionel Sambuc 		print_otag(h, TAG_COL, 1, &tag);
7592395e9cSLionel Sambuc 	}
7692395e9cSLionel Sambuc 
7792395e9cSLionel Sambuc 	print_otag(h, TAG_TBODY, 0, NULL);
7892395e9cSLionel Sambuc }
7992395e9cSLionel Sambuc 
8092395e9cSLionel Sambuc void
print_tblclose(struct html * h)8192395e9cSLionel Sambuc print_tblclose(struct html *h)
8292395e9cSLionel Sambuc {
8392395e9cSLionel Sambuc 
8492395e9cSLionel Sambuc 	assert(h->tblt);
8592395e9cSLionel Sambuc 	print_tagq(h, h->tblt);
8692395e9cSLionel Sambuc 	h->tblt = NULL;
8792395e9cSLionel Sambuc }
8892395e9cSLionel Sambuc 
8992395e9cSLionel Sambuc void
print_tbl(struct html * h,const struct tbl_span * sp)9092395e9cSLionel Sambuc print_tbl(struct html *h, const struct tbl_span *sp)
9192395e9cSLionel Sambuc {
9292395e9cSLionel Sambuc 	const struct tbl_head *hp;
9392395e9cSLionel Sambuc 	const struct tbl_dat *dp;
9492395e9cSLionel Sambuc 	struct htmlpair	 tag;
9592395e9cSLionel Sambuc 	struct tag	*tt;
9692395e9cSLionel Sambuc 
9792395e9cSLionel Sambuc 	/* Inhibit printing of spaces: we do padding ourselves. */
9892395e9cSLionel Sambuc 
9992395e9cSLionel Sambuc 	if (NULL == h->tblt)
10092395e9cSLionel Sambuc 		html_tblopen(h, sp);
10192395e9cSLionel Sambuc 
10292395e9cSLionel Sambuc 	assert(h->tblt);
10392395e9cSLionel Sambuc 
10492395e9cSLionel Sambuc 	h->flags |= HTML_NONOSPACE;
10592395e9cSLionel Sambuc 	h->flags |= HTML_NOSPACE;
10692395e9cSLionel Sambuc 
10792395e9cSLionel Sambuc 	tt = print_otag(h, TAG_TR, 0, NULL);
10892395e9cSLionel Sambuc 
109d65f6f70SBen Gras 	switch (sp->pos) {
110d65f6f70SBen Gras 	case (TBL_SPAN_HORIZ):
111d65f6f70SBen Gras 		/* FALLTHROUGH */
112d65f6f70SBen Gras 	case (TBL_SPAN_DHORIZ):
11392395e9cSLionel Sambuc 		PAIR_INIT(&tag, ATTR_COLSPAN, "0");
11492395e9cSLionel Sambuc 		print_otag(h, TAG_TD, 1, &tag);
115d65f6f70SBen Gras 		break;
116d65f6f70SBen Gras 	default:
117d65f6f70SBen Gras 		dp = sp->first;
118d65f6f70SBen Gras 		for (hp = sp->head; hp; hp = hp->next) {
11992395e9cSLionel Sambuc 			print_stagq(h, tt);
12092395e9cSLionel Sambuc 			print_otag(h, TAG_TD, 0, NULL);
12192395e9cSLionel Sambuc 
12292395e9cSLionel Sambuc 			if (NULL == dp)
12392395e9cSLionel Sambuc 				break;
12492395e9cSLionel Sambuc 			if (TBL_CELL_DOWN != dp->layout->pos)
12592395e9cSLionel Sambuc 				if (dp->string)
12692395e9cSLionel Sambuc 					print_text(h, dp->string);
12792395e9cSLionel Sambuc 			dp = dp->next;
12892395e9cSLionel Sambuc 		}
12992395e9cSLionel Sambuc 		break;
13092395e9cSLionel Sambuc 	}
131d65f6f70SBen Gras 
132d65f6f70SBen Gras 	print_tagq(h, tt);
133d65f6f70SBen Gras 
134d65f6f70SBen Gras 	h->flags &= ~HTML_NONOSPACE;
135d65f6f70SBen Gras 
136d65f6f70SBen Gras 	if (TBL_SPAN_LAST & sp->flags) {
137d65f6f70SBen Gras 		assert(h->tbl.cols);
138d65f6f70SBen Gras 		free(h->tbl.cols);
139d65f6f70SBen Gras 		h->tbl.cols = NULL;
14092395e9cSLionel Sambuc 		print_tblclose(h);
141d65f6f70SBen Gras 	}
14292395e9cSLionel Sambuc 
143d65f6f70SBen Gras }
144