Annotation of mandoc/tbl_html.c, Revision 1.42
1.42 ! schwarze 1: /* $Id: tbl_html.c,v 1.41 2022/04/23 14:02:17 schwarze Exp $ */
1.1 kristaps 2: /*
1.41 schwarze 3: * Copyright (c) 2014, 2015, 2017, 2018, 2021, 2022
4: * Ingo Schwarze <schwarze@openbsd.org>
1.9 schwarze 5: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include "config.h"
1.12 schwarze 20:
21: #include <sys/types.h>
1.1 kristaps 22:
23: #include <assert.h>
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27:
1.40 schwarze 28: #if DEBUG_MEMORY
29: #include "mandoc_dbg.h"
30: #endif
1.31 schwarze 31: #include "mandoc.h"
1.33 schwarze 32: #include "roff.h"
1.30 schwarze 33: #include "tbl.h"
1.1 kristaps 34: #include "out.h"
35: #include "html.h"
36:
1.7 kristaps 37: static void html_tblopen(struct html *, const struct tbl_span *);
1.4 kristaps 38: static size_t html_tbl_len(size_t, void *);
39: static size_t html_tbl_strlen(const char *, void *);
40:
1.11 schwarze 41:
1.4 kristaps 42: static size_t
43: html_tbl_len(size_t sz, void *arg)
44: {
1.17 schwarze 45: return sz;
1.4 kristaps 46: }
47:
48: static size_t
49: html_tbl_strlen(const char *p, void *arg)
50: {
1.21 schwarze 51: return strlen(p);
52: }
1.4 kristaps 53:
1.7 kristaps 54: static void
55: html_tblopen(struct html *h, const struct tbl_span *sp)
56: {
1.32 schwarze 57: html_close_paragraph(h);
1.16 schwarze 58: if (h->tbl.cols == NULL) {
1.7 kristaps 59: h->tbl.len = html_tbl_len;
60: h->tbl.slen = html_tbl_strlen;
1.22 schwarze 61: tblcalc(&h->tbl, sp, 0, 0);
1.7 kristaps 62: }
63: assert(NULL == h->tblt);
1.29 schwarze 64: h->tblt = print_otag(h, TAG_TABLE, "c?ss", "tbl",
65: "border",
66: sp->opts->opts & TBL_OPT_ALLBOX ? "1" : NULL,
67: "border-style",
68: sp->opts->opts & TBL_OPT_DBOX ? "double" :
69: sp->opts->opts & TBL_OPT_BOX ? "solid" : NULL,
70: "border-top-style",
71: sp->pos == TBL_SPAN_DHORIZ ? "double" :
72: sp->pos == TBL_SPAN_HORIZ ? "solid" : NULL);
1.7 kristaps 73: }
74:
75: void
76: print_tblclose(struct html *h)
77: {
78:
79: assert(h->tblt);
80: print_tagq(h, h->tblt);
81: h->tblt = NULL;
82: }
83:
1.1 kristaps 84: void
85: print_tbl(struct html *h, const struct tbl_span *sp)
86: {
1.26 schwarze 87: const struct tbl_dat *dp;
1.29 schwarze 88: const struct tbl_cell *cp;
89: const struct tbl_span *psp;
1.38 schwarze 90: const struct roffcol *col;
1.26 schwarze 91: struct tag *tt;
92: const char *hspans, *vspans, *halign, *valign;
1.29 schwarze 93: const char *bborder, *lborder, *rborder;
1.38 schwarze 94: const char *ccp;
1.26 schwarze 95: char hbuf[4], vbuf[4];
1.38 schwarze 96: size_t sz;
1.34 schwarze 97: enum mandoc_esc save_font;
1.29 schwarze 98: int i;
1.1 kristaps 99:
1.14 schwarze 100: if (h->tblt == NULL)
1.7 kristaps 101: html_tblopen(h, sp);
102:
1.29 schwarze 103: /*
104: * Horizontal lines spanning the whole table
105: * are handled by previous or following table rows.
106: */
107:
108: if (sp->pos != TBL_SPAN_DATA)
1.41 schwarze 109: goto out;
1.29 schwarze 110:
111: /* Inhibit printing of spaces: we do padding ourselves. */
1.7 kristaps 112:
1.1 kristaps 113: h->flags |= HTML_NONOSPACE;
114: h->flags |= HTML_NOSPACE;
115:
1.29 schwarze 116: /* Draw a vertical line left of this row? */
1.4 kristaps 117:
1.29 schwarze 118: switch (sp->layout->vert) {
119: case 2:
120: lborder = "double";
121: break;
122: case 1:
123: lborder = "solid";
1.5 kristaps 124: break;
125: default:
1.29 schwarze 126: lborder = NULL;
127: break;
128: }
1.27 schwarze 129:
1.29 schwarze 130: /* Draw a horizontal line below this row? */
131:
132: bborder = NULL;
133: if ((psp = sp->next) != NULL) {
134: switch (psp->pos) {
135: case TBL_SPAN_DHORIZ:
136: bborder = "double";
137: break;
138: case TBL_SPAN_HORIZ:
139: bborder = "solid";
140: break;
141: default:
142: break;
143: }
144: }
145:
146: tt = print_otag(h, TAG_TR, "ss",
147: "border-left-style", lborder,
148: "border-bottom-style", bborder);
149:
150: for (dp = sp->first; dp != NULL; dp = dp->next) {
151: print_stagq(h, tt);
152:
153: /*
154: * Do not generate <td> elements for continuations
155: * of spanned cells. Larger <td> elements covering
156: * this space were already generated earlier.
157: */
158:
159: cp = dp->layout;
160: if (cp->pos == TBL_CELL_SPAN || cp->pos == TBL_CELL_DOWN ||
161: (dp->string != NULL && strcmp(dp->string, "\\^") == 0))
162: continue;
163:
164: /* Determine the attribute values. */
165:
166: if (dp->hspans > 0) {
167: (void)snprintf(hbuf, sizeof(hbuf),
168: "%d", dp->hspans + 1);
169: hspans = hbuf;
170: } else
171: hspans = NULL;
172: if (dp->vspans > 0) {
173: (void)snprintf(vbuf, sizeof(vbuf),
174: "%d", dp->vspans + 1);
175: vspans = vbuf;
176: } else
177: vspans = NULL;
178:
179: switch (cp->pos) {
180: case TBL_CELL_CENTRE:
181: halign = "center";
182: break;
183: case TBL_CELL_RIGHT:
184: case TBL_CELL_NUMBER:
185: halign = "right";
186: break;
187: default:
188: halign = NULL;
189: break;
1.1 kristaps 190: }
1.29 schwarze 191: if (cp->flags & TBL_CELL_TALIGN)
192: valign = "top";
193: else if (cp->flags & TBL_CELL_BALIGN)
194: valign = "bottom";
195: else
196: valign = NULL;
197:
198: for (i = dp->hspans; i > 0; i--)
199: cp = cp->next;
200: switch (cp->vert) {
201: case 2:
202: rborder = "double";
203: break;
204: case 1:
205: rborder = "solid";
206: break;
207: default:
208: rborder = NULL;
209: break;
210: }
211:
212: /* Print the element and the attributes. */
213:
214: print_otag(h, TAG_TD, "??sss",
215: "colspan", hspans, "rowspan", vspans,
216: "vertical-align", valign,
217: "text-align", halign,
218: "border-right-style", rborder);
1.37 schwarze 219: if (dp->layout->pos == TBL_CELL_HORIZ ||
220: dp->layout->pos == TBL_CELL_DHORIZ ||
221: dp->pos == TBL_DATA_HORIZ ||
1.39 schwarze 222: dp->pos == TBL_DATA_NHORIZ ||
223: dp->pos == TBL_DATA_DHORIZ ||
224: dp->pos == TBL_DATA_NDHORIZ)
1.37 schwarze 225: print_otag(h, TAG_HR, "");
226: else if (dp->string != NULL) {
1.34 schwarze 227: save_font = h->metac;
1.36 schwarze 228: html_setfont(h, dp->layout->font);
1.35 schwarze 229: if (dp->layout->pos == TBL_CELL_LONG)
230: print_text(h, "\\[u2003]"); /* em space */
1.29 schwarze 231: print_text(h, dp->string);
1.38 schwarze 232: if (dp->layout->pos == TBL_CELL_NUMBER) {
233: col = h->tbl.cols + dp->layout->col;
234: if (col->decimal < col->nwidth) {
235: if ((ccp = strrchr(dp->string,
236: sp->opts->decimal)) == NULL) {
237: /* Punctuation space. */
238: print_text(h, "\\[u2008]");
239: ccp = strchr(dp->string, '\0');
240: } else
241: ccp++;
242: sz = col->nwidth - col->decimal;
243: while (--sz > 0) {
244: if (*ccp == '\0')
245: /* Figure space. */
246: print_text(h,
247: "\\[u2007]");
248: else
249: ccp++;
250: }
251: }
252: }
1.34 schwarze 253: html_setfont(h, save_font);
254: }
1.5 kristaps 255: }
1.4 kristaps 256:
1.7 kristaps 257: print_tagq(h, tt);
258:
1.1 kristaps 259: h->flags &= ~HTML_NONOSPACE;
1.4 kristaps 260:
1.41 schwarze 261: out:
1.16 schwarze 262: if (sp->next == NULL) {
1.4 kristaps 263: assert(h->tbl.cols);
264: free(h->tbl.cols);
265: h->tbl.cols = NULL;
1.7 kristaps 266: print_tblclose(h);
1.4 kristaps 267: }
1.1 kristaps 268: }
CVSweb