[BACK]Return to html.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/html.c, Revision 1.194

1.194   ! schwarze    1: /*     $Id: html.c,v 1.193 2017/01/08 16:38:26 schwarze Exp $ */
1.1       kristaps    2: /*
1.176     schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.194   ! schwarze    4:  * Copyright (c) 2011-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.186     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29      kristaps   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.
1.1       kristaps   17:  */
1.92      kristaps   18: #include "config.h"
                     19:
1.41      kristaps   20: #include <sys/types.h>
1.30      kristaps   21:
1.1       kristaps   22: #include <assert.h>
1.68      kristaps   23: #include <ctype.h>
1.76      kristaps   24: #include <stdarg.h>
1.29      kristaps   25: #include <stdio.h>
1.63      kristaps   26: #include <stdint.h>
1.1       kristaps   27: #include <stdlib.h>
1.33      kristaps   28: #include <string.h>
1.45      kristaps   29: #include <unistd.h>
1.1       kristaps   30:
1.100     kristaps   31: #include "mandoc.h"
1.155     schwarze   32: #include "mandoc_aux.h"
1.58      kristaps   33: #include "out.h"
1.51      kristaps   34: #include "html.h"
1.186     schwarze   35: #include "manconf.h"
1.64      kristaps   36: #include "main.h"
1.63      kristaps   37:
1.29      kristaps   38: struct htmldata {
1.63      kristaps   39:        const char       *name;
1.29      kristaps   40:        int               flags;
1.30      kristaps   41: #define        HTML_CLRLINE     (1 << 0)
                     42: #define        HTML_NOSTACK     (1 << 1)
1.93      kristaps   43: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.29      kristaps   44: };
1.7       kristaps   45:
1.29      kristaps   46: static const struct htmldata htmltags[TAG_MAX] = {
1.30      kristaps   47:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     48:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     49:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.93      kristaps   50:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.33      kristaps   51:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
1.30      kristaps   52:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
1.29      kristaps   53:        {"h1",          0}, /* TAG_H1 */
                     54:        {"h2",          0}, /* TAG_H2 */
                     55:        {"span",        0}, /* TAG_SPAN */
1.99      kristaps   56:        {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.93      kristaps   57:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.30      kristaps   58:        {"a",           0}, /* TAG_A */
1.33      kristaps   59:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
1.114     kristaps   60:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.93      kristaps   61:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.33      kristaps   62:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     63:        {"td",          HTML_CLRLINE}, /* TAG_TD */
1.34      kristaps   64:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     65:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     66:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.114     kristaps   67:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
                     68:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
                     69:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
1.115     kristaps   70:        {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
1.118     kristaps   71:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.119     kristaps   72:        {"b",           0 }, /* TAG_B */
1.120     kristaps   73:        {"i",           0 }, /* TAG_I */
1.121     kristaps   74:        {"code",        0 }, /* TAG_CODE */
1.122     kristaps   75:        {"small",       0 }, /* TAG_SMALL */
1.165     kristaps   76:        {"style",       HTML_CLRLINE}, /* TAG_STYLE */
1.173     kristaps   77:        {"math",        HTML_CLRLINE}, /* TAG_MATH */
                     78:        {"mrow",        0}, /* TAG_MROW */
                     79:        {"mi",          0}, /* TAG_MI */
                     80:        {"mo",          0}, /* TAG_MO */
                     81:        {"msup",        0}, /* TAG_MSUP */
                     82:        {"msub",        0}, /* TAG_MSUB */
                     83:        {"msubsup",     0}, /* TAG_MSUBSUP */
                     84:        {"mfrac",       0}, /* TAG_MFRAC */
                     85:        {"msqrt",       0}, /* TAG_MSQRT */
                     86:        {"mfenced",     0}, /* TAG_MFENCED */
                     87:        {"mtable",      0}, /* TAG_MTABLE */
                     88:        {"mtr",         0}, /* TAG_MTR */
                     89:        {"mtd",         0}, /* TAG_MTD */
1.174     kristaps   90:        {"munderover",  0}, /* TAG_MUNDEROVER */
                     91:        {"munder",      0}, /* TAG_MUNDER*/
                     92:        {"mover",       0}, /* TAG_MOVER*/
1.90      kristaps   93: };
                     94:
1.140     kristaps   95: static const char      *const roffscales[SCALE_MAX] = {
                     96:        "cm", /* SCALE_CM */
                     97:        "in", /* SCALE_IN */
                     98:        "pc", /* SCALE_PC */
                     99:        "pt", /* SCALE_PT */
                    100:        "em", /* SCALE_EM */
                    101:        "em", /* SCALE_MM */
                    102:        "ex", /* SCALE_EN */
                    103:        "ex", /* SCALE_BU */
                    104:        "em", /* SCALE_VS */
                    105:        "ex", /* SCALE_FS */
                    106: };
                    107:
1.194   ! schwarze  108: static void     a2width(const char *, struct roffsu *);
1.141     kristaps  109: static void     bufncat(struct html *, const char *, size_t);
1.184     schwarze  110: static void     print_ctag(struct html *, struct tag *);
1.159     schwarze  111: static int      print_escape(char);
1.141     kristaps  112: static int      print_encode(struct html *, const char *, int);
                    113: static void     print_metaf(struct html *, enum mandoc_esc);
                    114: static void     print_attr(struct html *, const char *, const char *);
1.82      kristaps  115:
1.156     schwarze  116:
1.180     schwarze  117: void *
1.191     schwarze  118: html_alloc(const struct manoutput *outopts)
1.10      kristaps  119: {
1.30      kristaps  120:        struct html     *h;
                    121:
1.128     kristaps  122:        h = mandoc_calloc(1, sizeof(struct html));
1.10      kristaps  123:
1.66      kristaps  124:        h->tags.head = NULL;
1.186     schwarze  125:        h->style = outopts->style;
                    126:        h->base_man = outopts->man;
                    127:        h->base_includes = outopts->includes;
                    128:        if (outopts->fragment)
                    129:                h->oflags |= HTML_FRAGMENT;
1.43      kristaps  130:
1.188     schwarze  131:        return h;
1.29      kristaps  132: }
1.10      kristaps  133:
1.29      kristaps  134: void
                    135: html_free(void *p)
                    136: {
1.30      kristaps  137:        struct tag      *tag;
                    138:        struct html     *h;
                    139:
                    140:        h = (struct html *)p;
1.37      kristaps  141:
1.66      kristaps  142:        while ((tag = h->tags.head) != NULL) {
1.156     schwarze  143:                h->tags.head = tag->next;
1.30      kristaps  144:                free(tag);
                    145:        }
1.53      kristaps  146:
1.30      kristaps  147:        free(h);
1.10      kristaps  148: }
1.2       kristaps  149:
1.51      kristaps  150: void
1.29      kristaps  151: print_gen_head(struct html *h)
                    152: {
1.165     kristaps  153:        struct tag      *t;
1.41      kristaps  154:
1.194   ! schwarze  155:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.165     kristaps  156:
1.168     kristaps  157:        /*
                    158:         * Print a default style-sheet.
                    159:         */
1.194   ! schwarze  160:        t = print_otag(h, TAG_STYLE, "");
1.168     kristaps  161:        print_text(h, "table.head, table.foot { width: 100%; }\n"
1.169     kristaps  162:              "td.head-rtitle, td.foot-os { text-align: right; }\n"
1.170     kristaps  163:              "td.head-vol { text-align: center; }\n"
                    164:              "table.foot td { width: 50%; }\n"
1.171     kristaps  165:              "table.head td { width: 33%; }\n"
                    166:              "div.spacer { margin: 1em 0; }\n");
1.165     kristaps  167:        print_tagq(h, t);
1.41      kristaps  168:
1.194   ! schwarze  169:        if (h->style)
        !           170:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
        !           171:                    h->style, "type", "text/css", "media", "all");
1.4       kristaps  172: }
                    173:
1.126     schwarze  174: static void
1.132     kristaps  175: print_metaf(struct html *h, enum mandoc_esc deco)
1.88      kristaps  176: {
1.90      kristaps  177:        enum htmlfont    font;
1.88      kristaps  178:
                    179:        switch (deco) {
1.156     schwarze  180:        case ESCAPE_FONTPREV:
1.90      kristaps  181:                font = h->metal;
1.88      kristaps  182:                break;
1.156     schwarze  183:        case ESCAPE_FONTITALIC:
1.90      kristaps  184:                font = HTMLFONT_ITALIC;
                    185:                break;
1.156     schwarze  186:        case ESCAPE_FONTBOLD:
1.90      kristaps  187:                font = HTMLFONT_BOLD;
1.88      kristaps  188:                break;
1.156     schwarze  189:        case ESCAPE_FONTBI:
1.152     schwarze  190:                font = HTMLFONT_BI;
                    191:                break;
1.156     schwarze  192:        case ESCAPE_FONT:
                    193:        case ESCAPE_FONTROMAN:
1.90      kristaps  194:                font = HTMLFONT_NONE;
1.88      kristaps  195:                break;
                    196:        default:
                    197:                abort();
                    198:        }
                    199:
1.122     kristaps  200:        if (h->metaf) {
                    201:                print_tagq(h, h->metaf);
                    202:                h->metaf = NULL;
                    203:        }
                    204:
                    205:        h->metal = h->metac;
                    206:        h->metac = font;
                    207:
1.152     schwarze  208:        switch (font) {
1.156     schwarze  209:        case HTMLFONT_ITALIC:
1.194   ! schwarze  210:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  211:                break;
1.156     schwarze  212:        case HTMLFONT_BOLD:
1.194   ! schwarze  213:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  214:                break;
1.156     schwarze  215:        case HTMLFONT_BI:
1.194   ! schwarze  216:                h->metaf = print_otag(h, TAG_B, "");
        !           217:                print_otag(h, TAG_I, "");
1.152     schwarze  218:                break;
                    219:        default:
                    220:                break;
                    221:        }
1.88      kristaps  222: }
                    223:
1.138     kristaps  224: int
                    225: html_strlen(const char *cp)
                    226: {
1.151     schwarze  227:        size_t           rsz;
                    228:        int              skip, sz;
1.138     kristaps  229:
                    230:        /*
                    231:         * Account for escaped sequences within string length
                    232:         * calculations.  This follows the logic in term_strlen() as we
                    233:         * must calculate the width of produced strings.
                    234:         * Assume that characters are always width of "1".  This is
                    235:         * hacky, but it gets the job done for approximation of widths.
                    236:         */
                    237:
                    238:        sz = 0;
1.151     schwarze  239:        skip = 0;
                    240:        while (1) {
                    241:                rsz = strcspn(cp, "\\");
                    242:                if (rsz) {
                    243:                        cp += rsz;
                    244:                        if (skip) {
                    245:                                skip = 0;
                    246:                                rsz--;
                    247:                        }
                    248:                        sz += rsz;
                    249:                }
                    250:                if ('\0' == *cp)
                    251:                        break;
                    252:                cp++;
                    253:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.156     schwarze  254:                case ESCAPE_ERROR:
1.188     schwarze  255:                        return sz;
1.156     schwarze  256:                case ESCAPE_UNICODE:
                    257:                case ESCAPE_NUMBERED:
                    258:                case ESCAPE_SPECIAL:
1.185     schwarze  259:                case ESCAPE_OVERSTRIKE:
1.151     schwarze  260:                        if (skip)
                    261:                                skip = 0;
                    262:                        else
                    263:                                sz++;
                    264:                        break;
1.156     schwarze  265:                case ESCAPE_SKIPCHAR:
1.151     schwarze  266:                        skip = 1;
1.138     kristaps  267:                        break;
                    268:                default:
                    269:                        break;
                    270:                }
                    271:        }
1.188     schwarze  272:        return sz;
1.138     kristaps  273: }
1.88      kristaps  274:
1.85      kristaps  275: static int
1.159     schwarze  276: print_escape(char c)
                    277: {
                    278:
                    279:        switch (c) {
                    280:        case '<':
                    281:                printf("&lt;");
                    282:                break;
                    283:        case '>':
                    284:                printf("&gt;");
                    285:                break;
                    286:        case '&':
                    287:                printf("&amp;");
                    288:                break;
                    289:        case '"':
                    290:                printf("&quot;");
                    291:                break;
                    292:        case ASCII_NBRSP:
1.190     schwarze  293:                printf("&nbsp;");
1.159     schwarze  294:                break;
                    295:        case ASCII_HYPH:
                    296:                putchar('-');
1.189     schwarze  297:                break;
1.159     schwarze  298:        case ASCII_BREAK:
                    299:                break;
                    300:        default:
1.188     schwarze  301:                return 0;
1.159     schwarze  302:        }
1.188     schwarze  303:        return 1;
1.159     schwarze  304: }
                    305:
                    306: static int
1.88      kristaps  307: print_encode(struct html *h, const char *p, int norecurse)
1.29      kristaps  308: {
1.77      kristaps  309:        size_t           sz;
1.141     kristaps  310:        int              c, len, nospace;
1.82      kristaps  311:        const char      *seq;
1.132     kristaps  312:        enum mandoc_esc  esc;
1.158     schwarze  313:        static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.154     schwarze  314:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  315:
1.85      kristaps  316:        nospace = 0;
                    317:
1.132     kristaps  318:        while ('\0' != *p) {
1.151     schwarze  319:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    320:                        h->flags &= ~HTML_SKIPCHAR;
                    321:                        p++;
                    322:                        continue;
                    323:                }
                    324:
1.100     kristaps  325:                sz = strcspn(p, rejs);
1.77      kristaps  326:
                    327:                fwrite(p, 1, sz, stdout);
1.132     kristaps  328:                p += (int)sz;
1.77      kristaps  329:
1.132     kristaps  330:                if ('\0' == *p)
                    331:                        break;
                    332:
1.159     schwarze  333:                if (print_escape(*p++))
1.154     schwarze  334:                        continue;
1.77      kristaps  335:
1.132     kristaps  336:                esc = mandoc_escape(&p, &seq, &len);
                    337:                if (ESCAPE_ERROR == esc)
                    338:                        break;
1.82      kristaps  339:
1.132     kristaps  340:                switch (esc) {
1.156     schwarze  341:                case ESCAPE_FONT:
                    342:                case ESCAPE_FONTPREV:
                    343:                case ESCAPE_FONTBOLD:
                    344:                case ESCAPE_FONTITALIC:
                    345:                case ESCAPE_FONTBI:
                    346:                case ESCAPE_FONTROMAN:
1.151     schwarze  347:                        if (0 == norecurse)
                    348:                                print_metaf(h, esc);
                    349:                        continue;
1.156     schwarze  350:                case ESCAPE_SKIPCHAR:
1.151     schwarze  351:                        h->flags |= HTML_SKIPCHAR;
                    352:                        continue;
                    353:                default:
                    354:                        break;
                    355:                }
                    356:
                    357:                if (h->flags & HTML_SKIPCHAR) {
                    358:                        h->flags &= ~HTML_SKIPCHAR;
                    359:                        continue;
                    360:                }
                    361:
                    362:                switch (esc) {
1.156     schwarze  363:                case ESCAPE_UNICODE:
1.159     schwarze  364:                        /* Skip past "u" header. */
1.144     kristaps  365:                        c = mchars_num2uc(seq + 1, len - 1);
                    366:                        break;
1.156     schwarze  367:                case ESCAPE_NUMBERED:
1.141     kristaps  368:                        c = mchars_num2char(seq, len);
1.181     schwarze  369:                        if (c < 0)
                    370:                                continue;
1.82      kristaps  371:                        break;
1.156     schwarze  372:                case ESCAPE_SPECIAL:
1.191     schwarze  373:                        c = mchars_spec2cp(seq, len);
1.181     schwarze  374:                        if (c <= 0)
                    375:                                continue;
1.132     kristaps  376:                        break;
1.156     schwarze  377:                case ESCAPE_NOSPACE:
1.132     kristaps  378:                        if ('\0' == *p)
                    379:                                nospace = 1;
1.179     schwarze  380:                        continue;
1.185     schwarze  381:                case ESCAPE_OVERSTRIKE:
                    382:                        if (len == 0)
                    383:                                continue;
                    384:                        c = seq[len - 1];
                    385:                        break;
1.82      kristaps  386:                default:
1.179     schwarze  387:                        continue;
1.82      kristaps  388:                }
1.181     schwarze  389:                if ((c < 0x20 && c != 0x09) ||
                    390:                    (c > 0x7E && c < 0xA0))
1.179     schwarze  391:                        c = 0xFFFD;
                    392:                if (c > 0x7E)
                    393:                        printf("&#%d;", c);
                    394:                else if ( ! print_escape(c))
                    395:                        putchar(c);
1.32      kristaps  396:        }
1.85      kristaps  397:
1.188     schwarze  398:        return nospace;
1.14      kristaps  399: }
                    400:
1.94      kristaps  401: static void
                    402: print_attr(struct html *h, const char *key, const char *val)
                    403: {
                    404:        printf(" %s=\"", key);
                    405:        (void)print_encode(h, val, 1);
                    406:        putchar('\"');
                    407: }
                    408:
1.51      kristaps  409: struct tag *
1.194   ! schwarze  410: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14      kristaps  411: {
1.194   ! schwarze  412:        va_list          ap;
        !           413:        struct roffsu    mysu, *su;
1.30      kristaps  414:        struct tag      *t;
1.194   ! schwarze  415:        char            *s;
        !           416:        int              i, have_style;
1.30      kristaps  417:
1.94      kristaps  418:        /* Push this tags onto the stack of open scopes. */
                    419:
1.30      kristaps  420:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.128     kristaps  421:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  422:                t->tag = tag;
1.66      kristaps  423:                t->next = h->tags.head;
                    424:                h->tags.head = t;
1.30      kristaps  425:        } else
                    426:                t = NULL;
1.29      kristaps  427:
                    428:        if ( ! (HTML_NOSPACE & h->flags))
1.105     kristaps  429:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    430:                        /* Manage keeps! */
                    431:                        if ( ! (HTML_KEEP & h->flags)) {
                    432:                                if (HTML_PREKEEP & h->flags)
                    433:                                        h->flags |= HTML_KEEP;
                    434:                                putchar(' ');
                    435:                        } else
                    436:                                printf("&#160;");
                    437:                }
1.29      kristaps  438:
1.109     kristaps  439:        if ( ! (h->flags & HTML_NONOSPACE))
                    440:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  441:        else
                    442:                h->flags |= HTML_NOSPACE;
1.109     kristaps  443:
1.94      kristaps  444:        /* Print out the tag name and attributes. */
                    445:
1.29      kristaps  446:        printf("<%s", htmltags[tag].name);
1.194   ! schwarze  447:
        !           448:        va_start(ap, fmt);
        !           449:
        !           450:        have_style = 0;
        !           451:        while (*fmt != '\0') {
        !           452:                if (*fmt == 's') {
        !           453:                        printf(" style=\"");
        !           454:                        have_style = 1;
        !           455:                        fmt++;
        !           456:                        break;
        !           457:                }
        !           458:                s = va_arg(ap, char *);
        !           459:                switch (*fmt++) {
        !           460:                case 'c':
        !           461:                        print_attr(h, "class", s);
        !           462:                        break;
        !           463:                case 'h':
        !           464:                        print_attr(h, "href", s);
        !           465:                        break;
        !           466:                case 'i':
        !           467:                        print_attr(h, "id", s);
        !           468:                        break;
        !           469:                case '?':
        !           470:                        print_attr(h, s, va_arg(ap, char *));
        !           471:                        break;
        !           472:                default:
        !           473:                        abort();
        !           474:                }
        !           475:        }
        !           476:
        !           477:        /* Print out styles. */
        !           478:
        !           479:        s = NULL;
        !           480:        su = &mysu;
        !           481:        while (*fmt != '\0') {
        !           482:
        !           483:                /* First letter: input argument type. */
        !           484:
        !           485:                switch (*fmt++) {
        !           486:                case 'h':
        !           487:                        i = va_arg(ap, int);
        !           488:                        SCALE_HS_INIT(su, i);
        !           489:                        break;
        !           490:                case 's':
        !           491:                        s = va_arg(ap, char *);
        !           492:                        break;
        !           493:                case 'u':
        !           494:                        su = va_arg(ap, struct roffsu *);
        !           495:                        break;
        !           496:                case 'v':
        !           497:                        i = va_arg(ap, int);
        !           498:                        SCALE_VS_INIT(su, i);
        !           499:                        break;
        !           500:                case 'w':
        !           501:                        s = va_arg(ap, char *);
        !           502:                        a2width(s, su);
        !           503:                        break;
        !           504:                default:
        !           505:                        abort();
        !           506:                }
        !           507:
        !           508:                /* Second letter: style name. */
        !           509:
        !           510:                bufinit(h);
        !           511:                switch (*fmt++) {
        !           512:                case 'b':
        !           513:                        bufcat_su(h, "margin-bottom", su);
        !           514:                        break;
        !           515:                case 'h':
        !           516:                        bufcat_su(h, "height", su);
        !           517:                        break;
        !           518:                case 'i':
        !           519:                        bufcat_su(h, "text-indent", su);
        !           520:                        break;
        !           521:                case 'l':
        !           522:                        bufcat_su(h, "margin-left", su);
        !           523:                        break;
        !           524:                case 't':
        !           525:                        bufcat_su(h, "margin-top", su);
        !           526:                        break;
        !           527:                case 'w':
        !           528:                        bufcat_su(h, "width", su);
        !           529:                        break;
        !           530:                case 'W':
        !           531:                        bufcat_su(h, "min-width", su);
        !           532:                        break;
        !           533:                case '?':
        !           534:                        bufcat_style(h, s, va_arg(ap, char *));
        !           535:                        break;
        !           536:                default:
        !           537:                        abort();
        !           538:                }
        !           539:                printf("%s", h->buf);
        !           540:        }
        !           541:        if (have_style)
        !           542:                putchar('"');
        !           543:
        !           544:        va_end(ap);
1.94      kristaps  545:
1.172     kristaps  546:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  547:
1.93      kristaps  548:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.172     kristaps  549:                putchar('/');
1.93      kristaps  550:
1.78      kristaps  551:        putchar('>');
1.14      kristaps  552:
1.29      kristaps  553:        h->flags |= HTML_NOSPACE;
1.117     kristaps  554:
                    555:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    556:                putchar('\n');
                    557:
1.188     schwarze  558:        return t;
1.14      kristaps  559: }
                    560:
1.29      kristaps  561: static void
1.184     schwarze  562: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  563: {
1.156     schwarze  564:
1.184     schwarze  565:        /*
                    566:         * Remember to close out and nullify the current
                    567:         * meta-font and table, if applicable.
                    568:         */
                    569:        if (tag == h->metaf)
                    570:                h->metaf = NULL;
                    571:        if (tag == h->tblt)
                    572:                h->tblt = NULL;
                    573:
                    574:        printf("</%s>", htmltags[tag->tag].name);
                    575:        if (HTML_CLRLINE & htmltags[tag->tag].flags) {
1.29      kristaps  576:                h->flags |= HTML_NOSPACE;
1.78      kristaps  577:                putchar('\n');
1.156     schwarze  578:        }
1.184     schwarze  579:
                    580:        h->tags.head = tag->next;
                    581:        free(tag);
1.14      kristaps  582: }
                    583:
1.51      kristaps  584: void
1.93      kristaps  585: print_gen_decls(struct html *h)
1.1       kristaps  586: {
1.93      kristaps  587:
1.164     kristaps  588:        puts("<!DOCTYPE html>");
1.1       kristaps  589: }
                    590:
1.51      kristaps  591: void
1.104     kristaps  592: print_text(struct html *h, const char *word)
1.1       kristaps  593: {
                    594:
1.105     kristaps  595:        if ( ! (HTML_NOSPACE & h->flags)) {
                    596:                /* Manage keeps! */
                    597:                if ( ! (HTML_KEEP & h->flags)) {
                    598:                        if (HTML_PREKEEP & h->flags)
                    599:                                h->flags |= HTML_KEEP;
                    600:                        putchar(' ');
                    601:                } else
                    602:                        printf("&#160;");
                    603:        }
1.30      kristaps  604:
1.122     kristaps  605:        assert(NULL == h->metaf);
1.152     schwarze  606:        switch (h->metac) {
1.156     schwarze  607:        case HTMLFONT_ITALIC:
1.194   ! schwarze  608:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  609:                break;
1.156     schwarze  610:        case HTMLFONT_BOLD:
1.194   ! schwarze  611:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  612:                break;
1.156     schwarze  613:        case HTMLFONT_BI:
1.194   ! schwarze  614:                h->metaf = print_otag(h, TAG_B, "");
        !           615:                print_otag(h, TAG_I, "");
1.152     schwarze  616:                break;
                    617:        default:
                    618:                break;
                    619:        }
1.122     kristaps  620:
1.104     kristaps  621:        assert(word);
1.149     kristaps  622:        if ( ! print_encode(h, word, 0)) {
1.109     kristaps  623:                if ( ! (h->flags & HTML_NONOSPACE))
                    624:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  625:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  626:        } else
1.183     schwarze  627:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  628:
                    629:        if (h->metaf) {
                    630:                print_tagq(h, h->metaf);
                    631:                h->metaf = NULL;
                    632:        }
1.113     schwarze  633:
                    634:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  635: }
1.30      kristaps  636:
1.51      kristaps  637: void
1.30      kristaps  638: print_tagq(struct html *h, const struct tag *until)
                    639: {
                    640:        struct tag      *tag;
                    641:
1.66      kristaps  642:        while ((tag = h->tags.head) != NULL) {
1.184     schwarze  643:                print_ctag(h, tag);
1.30      kristaps  644:                if (until && tag == until)
                    645:                        return;
                    646:        }
                    647: }
                    648:
1.51      kristaps  649: void
1.30      kristaps  650: print_stagq(struct html *h, const struct tag *suntil)
                    651: {
                    652:        struct tag      *tag;
                    653:
1.66      kristaps  654:        while ((tag = h->tags.head) != NULL) {
1.30      kristaps  655:                if (suntil && tag == suntil)
                    656:                        return;
1.184     schwarze  657:                print_ctag(h, tag);
1.30      kristaps  658:        }
                    659: }
1.171     kristaps  660:
                    661: void
                    662: print_paragraph(struct html *h)
                    663: {
                    664:        struct tag      *t;
                    665:
1.194   ! schwarze  666:        t = print_otag(h, TAG_DIV, "c", "spacer");
1.171     kristaps  667:        print_tagq(h, t);
                    668: }
                    669:
1.194   ! schwarze  670:
        !           671: /*
        !           672:  * Calculate the scaling unit passed in a `-width' argument.  This uses
        !           673:  * either a native scaling unit (e.g., 1i, 2m) or the string length of
        !           674:  * the value.
        !           675:  */
        !           676: static void
        !           677: a2width(const char *p, struct roffsu *su)
        !           678: {
        !           679:        if (a2roffsu(p, su, SCALE_MAX) < 2) {
        !           680:                su->unit = SCALE_EN;
        !           681:                su->scale = html_strlen(p);
        !           682:        } else if (su->scale < 0.0)
        !           683:                su->scale = 0.0;
        !           684: }
1.55      kristaps  685:
                    686: void
                    687: bufinit(struct html *h)
                    688: {
                    689:
                    690:        h->buf[0] = '\0';
                    691:        h->buflen = 0;
                    692: }
                    693:
                    694: void
1.58      kristaps  695: bufcat_style(struct html *h, const char *key, const char *val)
                    696: {
                    697:
                    698:        bufcat(h, key);
1.141     kristaps  699:        bufcat(h, ":");
1.58      kristaps  700:        bufcat(h, val);
1.141     kristaps  701:        bufcat(h, ";");
1.58      kristaps  702: }
                    703:
                    704: void
1.55      kristaps  705: bufcat(struct html *h, const char *p)
                    706: {
1.157     schwarze  707:
                    708:        /*
                    709:         * XXX This is broken and not easy to fix.
                    710:         * When using the -Oincludes option, buffmt_includes()
                    711:         * may pass in strings overrunning BUFSIZ, causing a crash.
                    712:         */
1.55      kristaps  713:
1.141     kristaps  714:        h->buflen = strlcat(h->buf, p, BUFSIZ);
                    715:        assert(h->buflen < BUFSIZ);
1.55      kristaps  716: }
                    717:
                    718: void
1.141     kristaps  719: bufcat_fmt(struct html *h, const char *fmt, ...)
1.55      kristaps  720: {
                    721:        va_list          ap;
                    722:
                    723:        va_start(ap, fmt);
1.156     schwarze  724:        (void)vsnprintf(h->buf + (int)h->buflen,
                    725:            BUFSIZ - h->buflen - 1, fmt, ap);
1.55      kristaps  726:        va_end(ap);
                    727:        h->buflen = strlen(h->buf);
                    728: }
                    729:
1.141     kristaps  730: static void
1.55      kristaps  731: bufncat(struct html *h, const char *p, size_t sz)
                    732: {
                    733:
1.141     kristaps  734:        assert(h->buflen + sz + 1 < BUFSIZ);
                    735:        strncat(h->buf, p, sz);
1.55      kristaps  736:        h->buflen += sz;
                    737: }
                    738:
                    739: void
                    740: buffmt_includes(struct html *h, const char *name)
                    741: {
                    742:        const char      *p, *pp;
                    743:
                    744:        pp = h->base_includes;
1.156     schwarze  745:
1.143     kristaps  746:        bufinit(h);
1.61      kristaps  747:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  748:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  749:                switch (*(p + 1)) {
1.193     schwarze  750:                case 'I':
1.55      kristaps  751:                        bufcat(h, name);
                    752:                        break;
                    753:                default:
                    754:                        bufncat(h, p, 2);
                    755:                        break;
                    756:                }
                    757:                pp = p + 2;
                    758:        }
                    759:        if (pp)
                    760:                bufcat(h, pp);
                    761: }
                    762:
                    763: void
1.156     schwarze  764: buffmt_man(struct html *h, const char *name, const char *sec)
1.55      kristaps  765: {
                    766:        const char      *p, *pp;
                    767:
                    768:        pp = h->base_man;
1.156     schwarze  769:
1.143     kristaps  770:        bufinit(h);
1.61      kristaps  771:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  772:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  773:                switch (*(p + 1)) {
1.156     schwarze  774:                case 'S':
1.58      kristaps  775:                        bufcat(h, sec ? sec : "1");
1.55      kristaps  776:                        break;
1.156     schwarze  777:                case 'N':
1.153     joerg     778:                        bufcat_fmt(h, "%s", name);
1.55      kristaps  779:                        break;
                    780:                default:
                    781:                        bufncat(h, p, 2);
                    782:                        break;
                    783:                }
                    784:                pp = p + 2;
                    785:        }
                    786:        if (pp)
                    787:                bufcat(h, pp);
                    788: }
1.58      kristaps  789:
                    790: void
                    791: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    792: {
1.62      kristaps  793:        double           v;
1.58      kristaps  794:
                    795:        v = su->scale;
1.140     kristaps  796:        if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                    797:                v = 1.0;
1.162     kristaps  798:        else if (SCALE_BU == su->unit)
                    799:                v /= 24.0;
1.58      kristaps  800:
1.141     kristaps  801:        bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.58      kristaps  802: }
1.68      kristaps  803:
                    804: void
1.142     kristaps  805: bufcat_id(struct html *h, const char *src)
1.68      kristaps  806: {
                    807:
1.192     schwarze  808:        /* Cf. <http://www.w3.org/TR/html5/dom.html#the-id-attribute>. */
1.68      kristaps  809:
1.192     schwarze  810:        for (; '\0' != *src; src++)
                    811:                bufncat(h, *src == ' ' ? "_" : src, 1);
1.68      kristaps  812: }

CVSweb