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