]> git.cameronkatri.com Git - mandoc.git/blob - tbl_html.c
Improve HTML formatting of .Bl -tag.
[mandoc.git] / tbl_html.c
1 /* $Id: tbl_html.c,v 1.19 2017/01/17 01:47:51 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "mandoc.h"
28 #include "out.h"
29 #include "html.h"
30
31 static void html_tblopen(struct html *, const struct tbl_span *);
32 static size_t html_tbl_len(size_t, void *);
33 static size_t html_tbl_strlen(const char *, void *);
34
35
36 static size_t
37 html_tbl_len(size_t sz, void *arg)
38 {
39
40 return sz;
41 }
42
43 static size_t
44 html_tbl_strlen(const char *p, void *arg)
45 {
46
47 return strlen(p);
48 }
49
50 static void
51 html_tblopen(struct html *h, const struct tbl_span *sp)
52 {
53 int ic;
54
55 if (h->tbl.cols == NULL) {
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 h->tblt = print_otag(h, TAG_TABLE, "c", "tbl");
63
64 for (ic = 0; ic < sp->opts->cols; ic++)
65 print_otag(h, TAG_COL, "shw", h->tbl.cols[ic].width);
66
67 print_otag(h, TAG_TBODY, "");
68 }
69
70 void
71 print_tblclose(struct html *h)
72 {
73
74 assert(h->tblt);
75 print_tagq(h, h->tblt);
76 h->tblt = NULL;
77 }
78
79 void
80 print_tbl(struct html *h, const struct tbl_span *sp)
81 {
82 const struct tbl_dat *dp;
83 struct tag *tt;
84 int ic;
85
86 /* Inhibit printing of spaces: we do padding ourselves. */
87
88 if (h->tblt == NULL)
89 html_tblopen(h, sp);
90
91 assert(h->tblt);
92
93 h->flags |= HTML_NONOSPACE;
94 h->flags |= HTML_NOSPACE;
95
96 tt = print_otag(h, TAG_TR, "");
97
98 switch (sp->pos) {
99 case TBL_SPAN_HORIZ:
100 case TBL_SPAN_DHORIZ:
101 print_otag(h, TAG_TD, "?", "colspan", "0");
102 break;
103 default:
104 dp = sp->first;
105 for (ic = 0; ic < sp->opts->cols; ic++) {
106 print_stagq(h, tt);
107 print_otag(h, TAG_TD, "");
108
109 if (dp == NULL || dp->layout->col > ic)
110 continue;
111 if (dp->layout->pos != TBL_CELL_DOWN)
112 if (dp->string != NULL)
113 print_text(h, dp->string);
114 dp = dp->next;
115 }
116 break;
117 }
118
119 print_tagq(h, tt);
120
121 h->flags &= ~HTML_NONOSPACE;
122
123 if (sp->next == NULL) {
124 assert(h->tbl.cols);
125 free(h->tbl.cols);
126 h->tbl.cols = NULL;
127 print_tblclose(h);
128 }
129
130 }