Annotation of src/usr.bin/mandoc/tbl_html.c, Revision 1.8
1.8 ! schwarze 1: /* $OpenBSD$ */
1.1 schwarze 2: /*
1.5 schwarze 3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 schwarze 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: */
1.8 ! schwarze 17: #include <sys/types.h>
! 18:
1.1 schwarze 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:
1.4 schwarze 28: static void html_tblopen(struct html *, const struct tbl_span *);
1.2 schwarze 29: static size_t html_tbl_len(size_t, void *);
30: static size_t html_tbl_strlen(const char *, void *);
31:
1.7 schwarze 32:
1.2 schwarze 33: static size_t
34: html_tbl_len(size_t sz, void *arg)
35: {
1.7 schwarze 36:
1.2 schwarze 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:
1.4 schwarze 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;
1.8 ! schwarze 58: tblcalc(&h->tbl, sp, 0);
1.4 schwarze 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:
1.1 schwarze 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;
1.4 schwarze 91: struct htmlpair tag;
1.2 schwarze 92: struct tag *tt;
93:
94: /* Inhibit printing of spaces: we do padding ourselves. */
95:
1.4 schwarze 96: if (NULL == h->tblt)
97: html_tblopen(h, sp);
98:
99: assert(h->tblt);
100:
1.2 schwarze 101: h->flags |= HTML_NONOSPACE;
102: h->flags |= HTML_NOSPACE;
103:
1.4 schwarze 104: tt = print_otag(h, TAG_TR, 0, NULL);
1.1 schwarze 105:
106: switch (sp->pos) {
1.7 schwarze 107: case TBL_SPAN_HORIZ:
1.1 schwarze 108: /* FALLTHROUGH */
1.7 schwarze 109: case TBL_SPAN_DHORIZ:
1.4 schwarze 110: PAIR_INIT(&tag, ATTR_COLSPAN, "0");
111: print_otag(h, TAG_TD, 1, &tag);
1.2 schwarze 112: break;
1.1 schwarze 113: default:
1.2 schwarze 114: dp = sp->first;
115: for (hp = sp->head; hp; hp = hp->next) {
1.4 schwarze 116: print_stagq(h, tt);
117: print_otag(h, TAG_TD, 0, NULL);
118:
1.6 schwarze 119: if (NULL == dp)
1.4 schwarze 120: break;
1.6 schwarze 121: if (TBL_CELL_DOWN != dp->layout->pos)
122: if (dp->string)
123: print_text(h, dp->string);
124: dp = dp->next;
1.2 schwarze 125: }
1.1 schwarze 126: break;
127: }
128:
1.4 schwarze 129: print_tagq(h, tt);
130:
1.2 schwarze 131: h->flags &= ~HTML_NONOSPACE;
1.1 schwarze 132:
1.2 schwarze 133: if (TBL_SPAN_LAST & sp->flags) {
134: assert(h->tbl.cols);
135: free(h->tbl.cols);
136: h->tbl.cols = NULL;
1.4 schwarze 137: print_tblclose(h);
1.1 schwarze 138: }
1.4 schwarze 139:
1.1 schwarze 140: }