xref: /titanic_44/usr/src/cmd/mandoc/tbl_html.c (revision 698f87a48e2e945bfe5493ce168e0d0ae1cedd5c)
1*698f87a4SGarrett D'Amore /*	$Id: tbl_html.c,v 1.10 2012/05/27 17:54:54 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
495c635efSGarrett D'Amore  *
595c635efSGarrett D'Amore  * Permission to use, copy, modify, and distribute this software for any
695c635efSGarrett D'Amore  * purpose with or without fee is hereby granted, provided that the above
795c635efSGarrett D'Amore  * copyright notice and this permission notice appear in all copies.
895c635efSGarrett D'Amore  *
995c635efSGarrett D'Amore  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1095c635efSGarrett D'Amore  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1195c635efSGarrett D'Amore  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1295c635efSGarrett D'Amore  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1395c635efSGarrett D'Amore  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1495c635efSGarrett D'Amore  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1595c635efSGarrett D'Amore  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1695c635efSGarrett D'Amore  */
1795c635efSGarrett D'Amore #ifdef HAVE_CONFIG_H
1895c635efSGarrett D'Amore #include "config.h"
1995c635efSGarrett D'Amore #endif
2095c635efSGarrett D'Amore 
2195c635efSGarrett D'Amore #include <assert.h>
2295c635efSGarrett D'Amore #include <stdio.h>
2395c635efSGarrett D'Amore #include <stdlib.h>
2495c635efSGarrett D'Amore #include <string.h>
2595c635efSGarrett D'Amore 
2695c635efSGarrett D'Amore #include "mandoc.h"
2795c635efSGarrett D'Amore #include "out.h"
2895c635efSGarrett D'Amore #include "html.h"
2995c635efSGarrett D'Amore 
3095c635efSGarrett D'Amore static	void	 html_tblopen(struct html *, const struct tbl_span *);
3195c635efSGarrett D'Amore static	size_t	 html_tbl_len(size_t, void *);
3295c635efSGarrett D'Amore static	size_t	 html_tbl_strlen(const char *, void *);
3395c635efSGarrett D'Amore 
3495c635efSGarrett D'Amore /* ARGSUSED */
3595c635efSGarrett D'Amore static size_t
html_tbl_len(size_t sz,void * arg)3695c635efSGarrett D'Amore html_tbl_len(size_t sz, void *arg)
3795c635efSGarrett D'Amore {
3895c635efSGarrett D'Amore 
3995c635efSGarrett D'Amore 	return(sz);
4095c635efSGarrett D'Amore }
4195c635efSGarrett D'Amore 
4295c635efSGarrett D'Amore /* ARGSUSED */
4395c635efSGarrett D'Amore static size_t
html_tbl_strlen(const char * p,void * arg)4495c635efSGarrett D'Amore html_tbl_strlen(const char *p, void *arg)
4595c635efSGarrett D'Amore {
4695c635efSGarrett D'Amore 
4795c635efSGarrett D'Amore 	return(strlen(p));
4895c635efSGarrett D'Amore }
4995c635efSGarrett D'Amore 
5095c635efSGarrett D'Amore static void
html_tblopen(struct html * h,const struct tbl_span * sp)5195c635efSGarrett D'Amore html_tblopen(struct html *h, const struct tbl_span *sp)
5295c635efSGarrett D'Amore {
5395c635efSGarrett D'Amore 	const struct tbl_head *hp;
5495c635efSGarrett D'Amore 	struct htmlpair	 tag;
5595c635efSGarrett D'Amore 	struct roffsu	 su;
5695c635efSGarrett D'Amore 	struct roffcol	*col;
5795c635efSGarrett D'Amore 
5895c635efSGarrett D'Amore 	if (TBL_SPAN_FIRST & sp->flags) {
5995c635efSGarrett D'Amore 		h->tbl.len = html_tbl_len;
6095c635efSGarrett D'Amore 		h->tbl.slen = html_tbl_strlen;
6195c635efSGarrett D'Amore 		tblcalc(&h->tbl, sp);
6295c635efSGarrett D'Amore 	}
6395c635efSGarrett D'Amore 
6495c635efSGarrett D'Amore 	assert(NULL == h->tblt);
6595c635efSGarrett D'Amore 	PAIR_CLASS_INIT(&tag, "tbl");
6695c635efSGarrett D'Amore 	h->tblt = print_otag(h, TAG_TABLE, 1, &tag);
6795c635efSGarrett D'Amore 
6895c635efSGarrett D'Amore 	for (hp = sp->head; hp; hp = hp->next) {
6995c635efSGarrett D'Amore 		bufinit(h);
7095c635efSGarrett D'Amore 		col = &h->tbl.cols[hp->ident];
7195c635efSGarrett D'Amore 		SCALE_HS_INIT(&su, col->width);
7295c635efSGarrett D'Amore 		bufcat_su(h, "width", &su);
7395c635efSGarrett D'Amore 		PAIR_STYLE_INIT(&tag, h);
7495c635efSGarrett D'Amore 		print_otag(h, TAG_COL, 1, &tag);
7595c635efSGarrett D'Amore 	}
7695c635efSGarrett D'Amore 
7795c635efSGarrett D'Amore 	print_otag(h, TAG_TBODY, 0, NULL);
7895c635efSGarrett D'Amore }
7995c635efSGarrett D'Amore 
8095c635efSGarrett D'Amore void
print_tblclose(struct html * h)8195c635efSGarrett D'Amore print_tblclose(struct html *h)
8295c635efSGarrett D'Amore {
8395c635efSGarrett D'Amore 
8495c635efSGarrett D'Amore 	assert(h->tblt);
8595c635efSGarrett D'Amore 	print_tagq(h, h->tblt);
8695c635efSGarrett D'Amore 	h->tblt = NULL;
8795c635efSGarrett D'Amore }
8895c635efSGarrett D'Amore 
8995c635efSGarrett D'Amore void
print_tbl(struct html * h,const struct tbl_span * sp)9095c635efSGarrett D'Amore print_tbl(struct html *h, const struct tbl_span *sp)
9195c635efSGarrett D'Amore {
9295c635efSGarrett D'Amore 	const struct tbl_head *hp;
9395c635efSGarrett D'Amore 	const struct tbl_dat *dp;
9495c635efSGarrett D'Amore 	struct htmlpair	 tag;
9595c635efSGarrett D'Amore 	struct tag	*tt;
9695c635efSGarrett D'Amore 
9795c635efSGarrett D'Amore 	/* Inhibit printing of spaces: we do padding ourselves. */
9895c635efSGarrett D'Amore 
9995c635efSGarrett D'Amore 	if (NULL == h->tblt)
10095c635efSGarrett D'Amore 		html_tblopen(h, sp);
10195c635efSGarrett D'Amore 
10295c635efSGarrett D'Amore 	assert(h->tblt);
10395c635efSGarrett D'Amore 
10495c635efSGarrett D'Amore 	h->flags |= HTML_NONOSPACE;
10595c635efSGarrett D'Amore 	h->flags |= HTML_NOSPACE;
10695c635efSGarrett D'Amore 
10795c635efSGarrett D'Amore 	tt = print_otag(h, TAG_TR, 0, NULL);
10895c635efSGarrett D'Amore 
10995c635efSGarrett D'Amore 	switch (sp->pos) {
11095c635efSGarrett D'Amore 	case (TBL_SPAN_HORIZ):
11195c635efSGarrett D'Amore 		/* FALLTHROUGH */
11295c635efSGarrett D'Amore 	case (TBL_SPAN_DHORIZ):
11395c635efSGarrett D'Amore 		PAIR_INIT(&tag, ATTR_COLSPAN, "0");
11495c635efSGarrett D'Amore 		print_otag(h, TAG_TD, 1, &tag);
11595c635efSGarrett D'Amore 		break;
11695c635efSGarrett D'Amore 	default:
11795c635efSGarrett D'Amore 		dp = sp->first;
11895c635efSGarrett D'Amore 		for (hp = sp->head; hp; hp = hp->next) {
11995c635efSGarrett D'Amore 			print_stagq(h, tt);
12095c635efSGarrett D'Amore 			print_otag(h, TAG_TD, 0, NULL);
12195c635efSGarrett D'Amore 
12295c635efSGarrett D'Amore 			if (NULL == dp)
12395c635efSGarrett D'Amore 				break;
12495c635efSGarrett D'Amore 			if (TBL_CELL_DOWN != dp->layout->pos)
12595c635efSGarrett D'Amore 				if (dp->string)
12695c635efSGarrett D'Amore 					print_text(h, dp->string);
12795c635efSGarrett D'Amore 			dp = dp->next;
12895c635efSGarrett D'Amore 		}
12995c635efSGarrett D'Amore 		break;
13095c635efSGarrett D'Amore 	}
13195c635efSGarrett D'Amore 
13295c635efSGarrett D'Amore 	print_tagq(h, tt);
13395c635efSGarrett D'Amore 
13495c635efSGarrett D'Amore 	h->flags &= ~HTML_NONOSPACE;
13595c635efSGarrett D'Amore 
13695c635efSGarrett D'Amore 	if (TBL_SPAN_LAST & sp->flags) {
13795c635efSGarrett D'Amore 		assert(h->tbl.cols);
13895c635efSGarrett D'Amore 		free(h->tbl.cols);
13995c635efSGarrett D'Amore 		h->tbl.cols = NULL;
14095c635efSGarrett D'Amore 		print_tblclose(h);
14195c635efSGarrett D'Amore 	}
14295c635efSGarrett D'Amore 
14395c635efSGarrett D'Amore }
144