1*95c635efSGarrett D'Amore /* $Id: tbl_html.c,v 1.9 2011/09/18 14:14:15 schwarze Exp $ */ 2*95c635efSGarrett D'Amore /* 3*95c635efSGarrett D'Amore * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4*95c635efSGarrett D'Amore * 5*95c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any 6*95c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above 7*95c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies. 8*95c635efSGarrett D'Amore * 9*95c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10*95c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*95c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12*95c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*95c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*95c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15*95c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16*95c635efSGarrett D'Amore */ 17*95c635efSGarrett D'Amore #ifdef HAVE_CONFIG_H 18*95c635efSGarrett D'Amore #include "config.h" 19*95c635efSGarrett D'Amore #endif 20*95c635efSGarrett D'Amore 21*95c635efSGarrett D'Amore #include <assert.h> 22*95c635efSGarrett D'Amore #include <stdio.h> 23*95c635efSGarrett D'Amore #include <stdlib.h> 24*95c635efSGarrett D'Amore #include <string.h> 25*95c635efSGarrett D'Amore 26*95c635efSGarrett D'Amore #include "mandoc.h" 27*95c635efSGarrett D'Amore #include "out.h" 28*95c635efSGarrett D'Amore #include "html.h" 29*95c635efSGarrett D'Amore 30*95c635efSGarrett D'Amore static void html_tblopen(struct html *, const struct tbl_span *); 31*95c635efSGarrett D'Amore static size_t html_tbl_len(size_t, void *); 32*95c635efSGarrett D'Amore static size_t html_tbl_strlen(const char *, void *); 33*95c635efSGarrett D'Amore 34*95c635efSGarrett D'Amore /* ARGSUSED */ 35*95c635efSGarrett D'Amore static size_t 36*95c635efSGarrett D'Amore html_tbl_len(size_t sz, void *arg) 37*95c635efSGarrett D'Amore { 38*95c635efSGarrett D'Amore 39*95c635efSGarrett D'Amore return(sz); 40*95c635efSGarrett D'Amore } 41*95c635efSGarrett D'Amore 42*95c635efSGarrett D'Amore /* ARGSUSED */ 43*95c635efSGarrett D'Amore static size_t 44*95c635efSGarrett D'Amore html_tbl_strlen(const char *p, void *arg) 45*95c635efSGarrett D'Amore { 46*95c635efSGarrett D'Amore 47*95c635efSGarrett D'Amore return(strlen(p)); 48*95c635efSGarrett D'Amore } 49*95c635efSGarrett D'Amore 50*95c635efSGarrett D'Amore static void 51*95c635efSGarrett D'Amore html_tblopen(struct html *h, const struct tbl_span *sp) 52*95c635efSGarrett D'Amore { 53*95c635efSGarrett D'Amore const struct tbl_head *hp; 54*95c635efSGarrett D'Amore struct htmlpair tag; 55*95c635efSGarrett D'Amore struct roffsu su; 56*95c635efSGarrett D'Amore struct roffcol *col; 57*95c635efSGarrett D'Amore 58*95c635efSGarrett D'Amore if (TBL_SPAN_FIRST & sp->flags) { 59*95c635efSGarrett D'Amore h->tbl.len = html_tbl_len; 60*95c635efSGarrett D'Amore h->tbl.slen = html_tbl_strlen; 61*95c635efSGarrett D'Amore tblcalc(&h->tbl, sp); 62*95c635efSGarrett D'Amore } 63*95c635efSGarrett D'Amore 64*95c635efSGarrett D'Amore assert(NULL == h->tblt); 65*95c635efSGarrett D'Amore PAIR_CLASS_INIT(&tag, "tbl"); 66*95c635efSGarrett D'Amore h->tblt = print_otag(h, TAG_TABLE, 1, &tag); 67*95c635efSGarrett D'Amore 68*95c635efSGarrett D'Amore for (hp = sp->head; hp; hp = hp->next) { 69*95c635efSGarrett D'Amore bufinit(h); 70*95c635efSGarrett D'Amore col = &h->tbl.cols[hp->ident]; 71*95c635efSGarrett D'Amore SCALE_HS_INIT(&su, col->width); 72*95c635efSGarrett D'Amore bufcat_su(h, "width", &su); 73*95c635efSGarrett D'Amore PAIR_STYLE_INIT(&tag, h); 74*95c635efSGarrett D'Amore print_otag(h, TAG_COL, 1, &tag); 75*95c635efSGarrett D'Amore } 76*95c635efSGarrett D'Amore 77*95c635efSGarrett D'Amore print_otag(h, TAG_TBODY, 0, NULL); 78*95c635efSGarrett D'Amore } 79*95c635efSGarrett D'Amore 80*95c635efSGarrett D'Amore void 81*95c635efSGarrett D'Amore print_tblclose(struct html *h) 82*95c635efSGarrett D'Amore { 83*95c635efSGarrett D'Amore 84*95c635efSGarrett D'Amore assert(h->tblt); 85*95c635efSGarrett D'Amore print_tagq(h, h->tblt); 86*95c635efSGarrett D'Amore h->tblt = NULL; 87*95c635efSGarrett D'Amore } 88*95c635efSGarrett D'Amore 89*95c635efSGarrett D'Amore void 90*95c635efSGarrett D'Amore print_tbl(struct html *h, const struct tbl_span *sp) 91*95c635efSGarrett D'Amore { 92*95c635efSGarrett D'Amore const struct tbl_head *hp; 93*95c635efSGarrett D'Amore const struct tbl_dat *dp; 94*95c635efSGarrett D'Amore struct htmlpair tag; 95*95c635efSGarrett D'Amore struct tag *tt; 96*95c635efSGarrett D'Amore 97*95c635efSGarrett D'Amore /* Inhibit printing of spaces: we do padding ourselves. */ 98*95c635efSGarrett D'Amore 99*95c635efSGarrett D'Amore if (NULL == h->tblt) 100*95c635efSGarrett D'Amore html_tblopen(h, sp); 101*95c635efSGarrett D'Amore 102*95c635efSGarrett D'Amore assert(h->tblt); 103*95c635efSGarrett D'Amore 104*95c635efSGarrett D'Amore h->flags |= HTML_NONOSPACE; 105*95c635efSGarrett D'Amore h->flags |= HTML_NOSPACE; 106*95c635efSGarrett D'Amore 107*95c635efSGarrett D'Amore tt = print_otag(h, TAG_TR, 0, NULL); 108*95c635efSGarrett D'Amore 109*95c635efSGarrett D'Amore switch (sp->pos) { 110*95c635efSGarrett D'Amore case (TBL_SPAN_HORIZ): 111*95c635efSGarrett D'Amore /* FALLTHROUGH */ 112*95c635efSGarrett D'Amore case (TBL_SPAN_DHORIZ): 113*95c635efSGarrett D'Amore PAIR_INIT(&tag, ATTR_COLSPAN, "0"); 114*95c635efSGarrett D'Amore print_otag(h, TAG_TD, 1, &tag); 115*95c635efSGarrett D'Amore break; 116*95c635efSGarrett D'Amore default: 117*95c635efSGarrett D'Amore dp = sp->first; 118*95c635efSGarrett D'Amore for (hp = sp->head; hp; hp = hp->next) { 119*95c635efSGarrett D'Amore print_stagq(h, tt); 120*95c635efSGarrett D'Amore print_otag(h, TAG_TD, 0, NULL); 121*95c635efSGarrett D'Amore 122*95c635efSGarrett D'Amore switch (hp->pos) { 123*95c635efSGarrett D'Amore case (TBL_HEAD_VERT): 124*95c635efSGarrett D'Amore /* FALLTHROUGH */ 125*95c635efSGarrett D'Amore case (TBL_HEAD_DVERT): 126*95c635efSGarrett D'Amore continue; 127*95c635efSGarrett D'Amore case (TBL_HEAD_DATA): 128*95c635efSGarrett D'Amore if (NULL == dp) 129*95c635efSGarrett D'Amore break; 130*95c635efSGarrett D'Amore if (TBL_CELL_DOWN != dp->layout->pos) 131*95c635efSGarrett D'Amore if (dp->string) 132*95c635efSGarrett D'Amore print_text(h, dp->string); 133*95c635efSGarrett D'Amore dp = dp->next; 134*95c635efSGarrett D'Amore break; 135*95c635efSGarrett D'Amore } 136*95c635efSGarrett D'Amore } 137*95c635efSGarrett D'Amore break; 138*95c635efSGarrett D'Amore } 139*95c635efSGarrett D'Amore 140*95c635efSGarrett D'Amore print_tagq(h, tt); 141*95c635efSGarrett D'Amore 142*95c635efSGarrett D'Amore h->flags &= ~HTML_NONOSPACE; 143*95c635efSGarrett D'Amore 144*95c635efSGarrett D'Amore if (TBL_SPAN_LAST & sp->flags) { 145*95c635efSGarrett D'Amore assert(h->tbl.cols); 146*95c635efSGarrett D'Amore free(h->tbl.cols); 147*95c635efSGarrett D'Amore h->tbl.cols = NULL; 148*95c635efSGarrett D'Amore print_tblclose(h); 149*95c635efSGarrett D'Amore } 150*95c635efSGarrett D'Amore 151*95c635efSGarrett D'Amore } 152