xref: /titanic_52/usr/src/cmd/mandoc/tbl_html.c (revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f)
1*260e9a87SYuri Pankov /*	$Id: tbl_html.c,v 1.16 2015/01/30 17:32:16 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 #include "config.h"
18*260e9a87SYuri Pankov 
19*260e9a87SYuri Pankov #include <sys/types.h>
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 
34*260e9a87SYuri Pankov 
3595c635efSGarrett D'Amore static size_t
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 static size_t
4395c635efSGarrett D'Amore html_tbl_strlen(const char *p, void *arg)
4495c635efSGarrett D'Amore {
4595c635efSGarrett D'Amore 
4695c635efSGarrett D'Amore 	return(strlen(p));
4795c635efSGarrett D'Amore }
4895c635efSGarrett D'Amore 
4995c635efSGarrett D'Amore static void
5095c635efSGarrett D'Amore html_tblopen(struct html *h, const struct tbl_span *sp)
5195c635efSGarrett D'Amore {
5295c635efSGarrett D'Amore 	struct htmlpair	 tag;
5395c635efSGarrett D'Amore 	struct roffsu	 su;
5495c635efSGarrett D'Amore 	struct roffcol	*col;
55*260e9a87SYuri Pankov 	int		 ic;
5695c635efSGarrett D'Amore 
57*260e9a87SYuri Pankov 	if (h->tbl.cols == NULL) {
5895c635efSGarrett D'Amore 		h->tbl.len = html_tbl_len;
5995c635efSGarrett D'Amore 		h->tbl.slen = html_tbl_strlen;
60*260e9a87SYuri Pankov 		tblcalc(&h->tbl, sp, 0);
6195c635efSGarrett D'Amore 	}
6295c635efSGarrett D'Amore 
6395c635efSGarrett D'Amore 	assert(NULL == h->tblt);
6495c635efSGarrett D'Amore 	PAIR_CLASS_INIT(&tag, "tbl");
6595c635efSGarrett D'Amore 	h->tblt = print_otag(h, TAG_TABLE, 1, &tag);
6695c635efSGarrett D'Amore 
67*260e9a87SYuri Pankov 	for (ic = 0; ic < sp->opts->cols; ic++) {
6895c635efSGarrett D'Amore 		bufinit(h);
69*260e9a87SYuri Pankov 		col = h->tbl.cols + ic;
7095c635efSGarrett D'Amore 		SCALE_HS_INIT(&su, col->width);
7195c635efSGarrett D'Amore 		bufcat_su(h, "width", &su);
7295c635efSGarrett D'Amore 		PAIR_STYLE_INIT(&tag, h);
7395c635efSGarrett D'Amore 		print_otag(h, TAG_COL, 1, &tag);
7495c635efSGarrett D'Amore 	}
7595c635efSGarrett D'Amore 
7695c635efSGarrett D'Amore 	print_otag(h, TAG_TBODY, 0, NULL);
7795c635efSGarrett D'Amore }
7895c635efSGarrett D'Amore 
7995c635efSGarrett D'Amore void
8095c635efSGarrett D'Amore print_tblclose(struct html *h)
8195c635efSGarrett D'Amore {
8295c635efSGarrett D'Amore 
8395c635efSGarrett D'Amore 	assert(h->tblt);
8495c635efSGarrett D'Amore 	print_tagq(h, h->tblt);
8595c635efSGarrett D'Amore 	h->tblt = NULL;
8695c635efSGarrett D'Amore }
8795c635efSGarrett D'Amore 
8895c635efSGarrett D'Amore void
8995c635efSGarrett D'Amore print_tbl(struct html *h, const struct tbl_span *sp)
9095c635efSGarrett D'Amore {
9195c635efSGarrett D'Amore 	const struct tbl_dat *dp;
9295c635efSGarrett D'Amore 	struct htmlpair	 tag;
9395c635efSGarrett D'Amore 	struct tag	*tt;
94*260e9a87SYuri Pankov 	int		 ic;
9595c635efSGarrett D'Amore 
9695c635efSGarrett D'Amore 	/* Inhibit printing of spaces: we do padding ourselves. */
9795c635efSGarrett D'Amore 
98*260e9a87SYuri Pankov 	if (h->tblt == NULL)
9995c635efSGarrett D'Amore 		html_tblopen(h, sp);
10095c635efSGarrett D'Amore 
10195c635efSGarrett D'Amore 	assert(h->tblt);
10295c635efSGarrett D'Amore 
10395c635efSGarrett D'Amore 	h->flags |= HTML_NONOSPACE;
10495c635efSGarrett D'Amore 	h->flags |= HTML_NOSPACE;
10595c635efSGarrett D'Amore 
10695c635efSGarrett D'Amore 	tt = print_otag(h, TAG_TR, 0, NULL);
10795c635efSGarrett D'Amore 
10895c635efSGarrett D'Amore 	switch (sp->pos) {
109*260e9a87SYuri Pankov 	case TBL_SPAN_HORIZ:
11095c635efSGarrett D'Amore 		/* FALLTHROUGH */
111*260e9a87SYuri Pankov 	case TBL_SPAN_DHORIZ:
11295c635efSGarrett D'Amore 		PAIR_INIT(&tag, ATTR_COLSPAN, "0");
11395c635efSGarrett D'Amore 		print_otag(h, TAG_TD, 1, &tag);
11495c635efSGarrett D'Amore 		break;
11595c635efSGarrett D'Amore 	default:
11695c635efSGarrett D'Amore 		dp = sp->first;
117*260e9a87SYuri Pankov 		for (ic = 0; ic < sp->opts->cols; ic++) {
11895c635efSGarrett D'Amore 			print_stagq(h, tt);
11995c635efSGarrett D'Amore 			print_otag(h, TAG_TD, 0, NULL);
12095c635efSGarrett D'Amore 
121*260e9a87SYuri Pankov 			if (dp == NULL || dp->layout->col > ic)
122*260e9a87SYuri Pankov 				continue;
123*260e9a87SYuri Pankov 			if (dp->layout->pos != TBL_CELL_DOWN)
124*260e9a87SYuri Pankov 				if (dp->string != NULL)
12595c635efSGarrett D'Amore 					print_text(h, dp->string);
12695c635efSGarrett D'Amore 			dp = dp->next;
12795c635efSGarrett D'Amore 		}
12895c635efSGarrett D'Amore 		break;
12995c635efSGarrett D'Amore 	}
13095c635efSGarrett D'Amore 
13195c635efSGarrett D'Amore 	print_tagq(h, tt);
13295c635efSGarrett D'Amore 
13395c635efSGarrett D'Amore 	h->flags &= ~HTML_NONOSPACE;
13495c635efSGarrett D'Amore 
135*260e9a87SYuri Pankov 	if (sp->next == NULL) {
13695c635efSGarrett D'Amore 		assert(h->tbl.cols);
13795c635efSGarrett D'Amore 		free(h->tbl.cols);
13895c635efSGarrett D'Amore 		h->tbl.cols = NULL;
13995c635efSGarrett D'Amore 		print_tblclose(h);
14095c635efSGarrett D'Amore 	}
14195c635efSGarrett D'Amore 
14295c635efSGarrett D'Amore }
143