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

Annotation of mandoc/html.c, Revision 1.97

1.97    ! kristaps    1: /*     $Id: html.c,v 1.96 2010/02/17 19:48:33 kristaps Exp $ */
1.1       kristaps    2: /*
1.29      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.29      kristaps    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.
1.1       kristaps   16:  */
1.92      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.41      kristaps   21: #include <sys/types.h>
1.30      kristaps   22:
1.1       kristaps   23: #include <assert.h>
1.68      kristaps   24: #include <ctype.h>
1.76      kristaps   25: #include <stdarg.h>
1.29      kristaps   26: #include <stdio.h>
1.63      kristaps   27: #include <stdint.h>
1.1       kristaps   28: #include <stdlib.h>
1.33      kristaps   29: #include <string.h>
1.45      kristaps   30: #include <unistd.h>
1.1       kristaps   31:
1.58      kristaps   32: #include "out.h"
1.32      kristaps   33: #include "chars.h"
1.51      kristaps   34: #include "html.h"
1.64      kristaps   35: #include "main.h"
1.2       kristaps   36:
1.63      kristaps   37: #define        UNCONST(a)      ((void *)(uintptr_t)(const void *)(a))
                     38:
1.29      kristaps   39: struct htmldata {
1.63      kristaps   40:        const char       *name;
1.29      kristaps   41:        int               flags;
1.30      kristaps   42: #define        HTML_CLRLINE     (1 << 0)
                     43: #define        HTML_NOSTACK     (1 << 1)
1.93      kristaps   44: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.29      kristaps   45: };
1.7       kristaps   46:
1.29      kristaps   47: static const struct htmldata htmltags[TAG_MAX] = {
1.30      kristaps   48:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     49:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     50:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.93      kristaps   51:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.33      kristaps   52:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
1.30      kristaps   53:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
1.29      kristaps   54:        {"h1",          0}, /* TAG_H1 */
                     55:        {"h2",          0}, /* TAG_H2 */
                     56:        {"span",        0}, /* TAG_SPAN */
1.30      kristaps   57:        {"link",        HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
1.93      kristaps   58:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.30      kristaps   59:        {"a",           0}, /* TAG_A */
1.33      kristaps   60:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
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.29      kristaps   67: };
1.10      kristaps   68:
1.90      kristaps   69: static const char      *const htmlfonts[HTMLFONT_MAX] = {
                     70:        "roman",
                     71:        "bold",
                     72:        "italic"
                     73: };
                     74:
1.82      kristaps   75: static const char      *const htmlattrs[ATTR_MAX] = {
1.29      kristaps   76:        "http-equiv",
                     77:        "content",
                     78:        "name",
                     79:        "rel",
                     80:        "href",
                     81:        "type",
                     82:        "media",
1.33      kristaps   83:        "class",
                     84:        "style",
                     85:        "width",
                     86:        "valign",
1.54      kristaps   87:        "target",
1.57      kristaps   88:        "id",
1.67      kristaps   89:        "summary",
1.29      kristaps   90: };
1.10      kristaps   91:
1.83      kristaps   92: static void              print_spec(struct html *, const char *, size_t);
                     93: static void              print_res(struct html *, const char *, size_t);
1.82      kristaps   94: static void              print_ctag(struct html *, enum htmltag);
1.93      kristaps   95: static void              print_doctype(struct html *);
                     96: static void              print_xmltype(struct html *);
1.88      kristaps   97: static int               print_encode(struct html *, const char *, int);
                     98: static void              print_metaf(struct html *, enum roffdeco);
1.94      kristaps   99: static void              print_attr(struct html *,
                    100:                                const char *, const char *);
1.93      kristaps  101: static void             *ml_alloc(char *, enum htmltype);
1.82      kristaps  102:
                    103:
1.93      kristaps  104: static void *
                    105: ml_alloc(char *outopts, enum htmltype type)
1.10      kristaps  106: {
1.30      kristaps  107:        struct html     *h;
1.63      kristaps  108:        const char      *toks[4];
                    109:        char            *v;
1.43      kristaps  110:
                    111:        toks[0] = "style";
1.53      kristaps  112:        toks[1] = "man";
1.54      kristaps  113:        toks[2] = "includes";
                    114:        toks[3] = NULL;
1.30      kristaps  115:
1.72      kristaps  116:        h = calloc(1, sizeof(struct html));
                    117:        if (NULL == h) {
1.75      kristaps  118:                perror(NULL);
1.72      kristaps  119:                exit(EXIT_FAILURE);
                    120:        }
1.10      kristaps  121:
1.93      kristaps  122:        h->type = type;
1.66      kristaps  123:        h->tags.head = NULL;
                    124:        h->ords.head = NULL;
1.72      kristaps  125:        h->symtab = chars_init(CHARS_HTML);
1.41      kristaps  126:
1.47      kristaps  127:        while (outopts && *outopts)
1.63      kristaps  128:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43      kristaps  129:                case (0):
                    130:                        h->style = v;
                    131:                        break;
                    132:                case (1):
1.53      kristaps  133:                        h->base_man = v;
1.43      kristaps  134:                        break;
1.54      kristaps  135:                case (2):
                    136:                        h->base_includes = v;
                    137:                        break;
1.43      kristaps  138:                default:
                    139:                        break;
                    140:                }
                    141:
1.30      kristaps  142:        return(h);
1.29      kristaps  143: }
1.10      kristaps  144:
1.93      kristaps  145: void *
                    146: html_alloc(char *outopts)
                    147: {
                    148:
                    149:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    150: }
                    151:
                    152:
                    153: void *
                    154: xhtml_alloc(char *outopts)
                    155: {
                    156:
                    157:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    158: }
                    159:
1.33      kristaps  160:
1.29      kristaps  161: void
                    162: html_free(void *p)
                    163: {
1.30      kristaps  164:        struct tag      *tag;
1.37      kristaps  165:        struct ord      *ord;
1.30      kristaps  166:        struct html     *h;
                    167:
                    168:        h = (struct html *)p;
1.10      kristaps  169:
1.66      kristaps  170:        while ((ord = h->ords.head) != NULL) {
                    171:                h->ords.head = ord->next;
1.37      kristaps  172:                free(ord);
                    173:        }
                    174:
1.66      kristaps  175:        while ((tag = h->tags.head) != NULL) {
                    176:                h->tags.head = tag->next;
1.30      kristaps  177:                free(tag);
                    178:        }
1.36      kristaps  179:
                    180:        if (h->symtab)
                    181:                chars_free(h->symtab);
1.53      kristaps  182:
1.30      kristaps  183:        free(h);
1.10      kristaps  184: }
1.2       kristaps  185:
1.33      kristaps  186:
1.51      kristaps  187: void
1.29      kristaps  188: print_gen_head(struct html *h)
                    189: {
1.41      kristaps  190:        struct htmlpair  tag[4];
                    191:
                    192:        tag[0].key = ATTR_HTTPEQUIV;
                    193:        tag[0].val = "Content-Type";
                    194:        tag[1].key = ATTR_CONTENT;
                    195:        tag[1].val = "text/html; charset=utf-8";
                    196:        print_otag(h, TAG_META, 2, tag);
                    197:
                    198:        tag[0].key = ATTR_NAME;
                    199:        tag[0].val = "resource-type";
                    200:        tag[1].key = ATTR_CONTENT;
                    201:        tag[1].val = "document";
                    202:        print_otag(h, TAG_META, 2, tag);
                    203:
                    204:        if (h->style) {
                    205:                tag[0].key = ATTR_REL;
                    206:                tag[0].val = "stylesheet";
                    207:                tag[1].key = ATTR_HREF;
                    208:                tag[1].val = h->style;
                    209:                tag[2].key = ATTR_TYPE;
                    210:                tag[2].val = "text/css";
                    211:                tag[3].key = ATTR_MEDIA;
                    212:                tag[3].val = "all";
                    213:                print_otag(h, TAG_LINK, 4, tag);
                    214:        }
1.4       kristaps  215: }
                    216:
1.33      kristaps  217:
1.29      kristaps  218: static void
1.83      kristaps  219: print_spec(struct html *h, const char *p, size_t len)
1.32      kristaps  220: {
                    221:        const char      *rhs;
                    222:        size_t           sz;
                    223:
1.83      kristaps  224:        rhs = chars_a2ascii(h->symtab, p, len, &sz);
1.32      kristaps  225:
                    226:        if (NULL == rhs)
                    227:                return;
1.76      kristaps  228:        fwrite(rhs, 1, sz, stdout);
1.32      kristaps  229: }
                    230:
1.33      kristaps  231:
1.32      kristaps  232: static void
1.83      kristaps  233: print_res(struct html *h, const char *p, size_t len)
1.32      kristaps  234: {
                    235:        const char      *rhs;
                    236:        size_t           sz;
                    237:
1.83      kristaps  238:        rhs = chars_a2res(h->symtab, p, len, &sz);
1.32      kristaps  239:
                    240:        if (NULL == rhs)
                    241:                return;
1.76      kristaps  242:        fwrite(rhs, 1, sz, stdout);
1.32      kristaps  243: }
                    244:
1.33      kristaps  245:
1.90      kristaps  246: struct tag *
                    247: print_ofont(struct html *h, enum htmlfont font)
                    248: {
                    249:        struct htmlpair  tag;
                    250:
                    251:        h->metal = h->metac;
                    252:        h->metac = font;
                    253:
                    254:        /* FIXME: DECO_ROMAN should just close out preexisting. */
                    255:
1.91      kristaps  256:        if (h->metaf && h->tags.head == h->metaf)
1.90      kristaps  257:                print_tagq(h, h->metaf);
                    258:
                    259:        PAIR_CLASS_INIT(&tag, htmlfonts[font]);
                    260:        h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
                    261:        return(h->metaf);
                    262: }
                    263:
                    264:
1.88      kristaps  265: static void
                    266: print_metaf(struct html *h, enum roffdeco deco)
                    267: {
1.90      kristaps  268:        enum htmlfont    font;
1.88      kristaps  269:
                    270:        switch (deco) {
1.90      kristaps  271:        case (DECO_PREVIOUS):
                    272:                font = h->metal;
1.88      kristaps  273:                break;
                    274:        case (DECO_ITALIC):
1.90      kristaps  275:                font = HTMLFONT_ITALIC;
                    276:                break;
                    277:        case (DECO_BOLD):
                    278:                font = HTMLFONT_BOLD;
1.88      kristaps  279:                break;
                    280:        case (DECO_ROMAN):
1.90      kristaps  281:                font = HTMLFONT_NONE;
1.88      kristaps  282:                break;
                    283:        default:
                    284:                abort();
                    285:                /* NOTREACHED */
                    286:        }
                    287:
1.90      kristaps  288:        (void)print_ofont(h, font);
1.88      kristaps  289: }
                    290:
                    291:
1.85      kristaps  292: static int
1.88      kristaps  293: print_encode(struct html *h, const char *p, int norecurse)
1.29      kristaps  294: {
1.77      kristaps  295:        size_t           sz;
1.85      kristaps  296:        int              len, nospace;
1.82      kristaps  297:        const char      *seq;
                    298:        enum roffdeco    deco;
1.14      kristaps  299:
1.85      kristaps  300:        nospace = 0;
                    301:
1.32      kristaps  302:        for (; *p; p++) {
1.77      kristaps  303:                sz = strcspn(p, "\\<>&");
                    304:
                    305:                fwrite(p, 1, sz, stdout);
1.80      kristaps  306:                p += /* LINTED */
                    307:                        sz;
1.77      kristaps  308:
1.82      kristaps  309:                if ('<' == *p) {
                    310:                        printf("&lt;");
                    311:                        continue;
                    312:                } else if ('>' == *p) {
                    313:                        printf("&gt;");
                    314:                        continue;
                    315:                } else if ('&' == *p) {
                    316:                        printf("&amp;");
1.34      kristaps  317:                        continue;
1.77      kristaps  318:                } else if ('\0' == *p)
                    319:                        break;
                    320:
1.82      kristaps  321:                seq = ++p;
                    322:                len = a2roffdeco(&deco, &seq, &sz);
                    323:
                    324:                switch (deco) {
                    325:                case (DECO_RESERVED):
                    326:                        print_res(h, seq, sz);
                    327:                        break;
                    328:                case (DECO_SPECIAL):
                    329:                        print_spec(h, seq, sz);
                    330:                        break;
1.90      kristaps  331:                case (DECO_PREVIOUS):
                    332:                        /* FALLTHROUGH */
1.88      kristaps  333:                case (DECO_BOLD):
                    334:                        /* FALLTHROUGH */
                    335:                case (DECO_ITALIC):
                    336:                        /* FALLTHROUGH */
                    337:                case (DECO_ROMAN):
                    338:                        if (norecurse)
                    339:                                break;
                    340:                        print_metaf(h, deco);
                    341:                        break;
1.82      kristaps  342:                default:
                    343:                        break;
                    344:                }
                    345:
                    346:                p += len - 1;
1.84      kristaps  347:
                    348:                if (DECO_NOSPACE == deco && '\0' == *(p + 1))
1.85      kristaps  349:                        nospace = 1;
1.32      kristaps  350:        }
1.85      kristaps  351:
                    352:        return(nospace);
1.14      kristaps  353: }
                    354:
                    355:
1.94      kristaps  356: static void
                    357: print_attr(struct html *h, const char *key, const char *val)
                    358: {
                    359:        printf(" %s=\"", key);
                    360:        (void)print_encode(h, val, 1);
                    361:        putchar('\"');
                    362: }
                    363:
                    364:
1.51      kristaps  365: struct tag *
1.29      kristaps  366: print_otag(struct html *h, enum htmltag tag,
                    367:                int sz, const struct htmlpair *p)
1.14      kristaps  368: {
1.29      kristaps  369:        int              i;
1.30      kristaps  370:        struct tag      *t;
                    371:
1.94      kristaps  372:        /* Push this tags onto the stack of open scopes. */
                    373:
1.30      kristaps  374:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.72      kristaps  375:                t = malloc(sizeof(struct tag));
                    376:                if (NULL == t) {
1.75      kristaps  377:                        perror(NULL);
1.72      kristaps  378:                        exit(EXIT_FAILURE);
                    379:                }
1.30      kristaps  380:                t->tag = tag;
1.66      kristaps  381:                t->next = h->tags.head;
                    382:                h->tags.head = t;
1.30      kristaps  383:        } else
                    384:                t = NULL;
1.29      kristaps  385:
                    386:        if ( ! (HTML_NOSPACE & h->flags))
1.30      kristaps  387:                if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.78      kristaps  388:                        putchar(' ');
1.29      kristaps  389:
1.94      kristaps  390:        /* Print out the tag name and attributes. */
                    391:
1.29      kristaps  392:        printf("<%s", htmltags[tag].name);
1.94      kristaps  393:        for (i = 0; i < sz; i++)
                    394:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    395:
                    396:        /* Add non-overridable attributes. */
                    397:
                    398:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    399:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    400:                print_attr(h, "xml:lang", "en");
                    401:                print_attr(h, "lang", "en");
1.29      kristaps  402:        }
1.93      kristaps  403:
1.94      kristaps  404:        /* Accomodate for XML "well-formed" singleton escaping. */
                    405:
1.93      kristaps  406:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    407:                switch (h->type) {
                    408:                case (HTML_XHTML_1_0_STRICT):
                    409:                        putchar('/');
                    410:                        break;
                    411:                default:
                    412:                        break;
                    413:                }
                    414:
1.78      kristaps  415:        putchar('>');
1.14      kristaps  416:
1.29      kristaps  417:        h->flags |= HTML_NOSPACE;
1.30      kristaps  418:        return(t);
1.14      kristaps  419: }
                    420:
                    421:
1.29      kristaps  422: static void
                    423: print_ctag(struct html *h, enum htmltag tag)
1.14      kristaps  424: {
                    425:
1.29      kristaps  426:        printf("</%s>", htmltags[tag].name);
1.71      kristaps  427:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.29      kristaps  428:                h->flags |= HTML_NOSPACE;
1.78      kristaps  429:                putchar('\n');
1.87      kristaps  430:        }
1.14      kristaps  431: }
                    432:
                    433:
1.51      kristaps  434: void
1.93      kristaps  435: print_gen_decls(struct html *h)
1.1       kristaps  436: {
1.93      kristaps  437:
                    438:        print_xmltype(h);
                    439:        print_doctype(h);
                    440: }
                    441:
                    442:
                    443: static void
                    444: print_xmltype(struct html *h)
                    445: {
                    446:        const char      *decl;
                    447:
                    448:        switch (h->type) {
                    449:        case (HTML_XHTML_1_0_STRICT):
                    450:                decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    451:                break;
                    452:        default:
                    453:                decl = NULL;
                    454:                break;
                    455:        }
                    456:
                    457:        if (NULL == decl)
                    458:                return;
                    459:
                    460:        printf("%s\n", decl);
                    461: }
                    462:
                    463:
                    464: static void
                    465: print_doctype(struct html *h)
                    466: {
                    467:        const char      *doctype;
                    468:        const char      *dtd;
1.96      kristaps  469:        const char      *name;
1.93      kristaps  470:
                    471:        switch (h->type) {
                    472:        case (HTML_HTML_4_01_STRICT):
1.96      kristaps  473:                name = "HTML";
1.93      kristaps  474:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    475:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    476:                break;
                    477:        default:
1.96      kristaps  478:                name = "html";
1.93      kristaps  479:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    480:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    481:                break;
                    482:        }
                    483:
1.96      kristaps  484:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    485:                        name, doctype, dtd);
1.1       kristaps  486: }
                    487:
                    488:
1.51      kristaps  489: void
1.29      kristaps  490: print_text(struct html *h, const char *p)
1.1       kristaps  491: {
                    492:
1.29      kristaps  493:        if (*p && 0 == *(p + 1))
                    494:                switch (*p) {
                    495:                case('.'):
                    496:                        /* FALLTHROUGH */
                    497:                case(','):
                    498:                        /* FALLTHROUGH */
                    499:                case(';'):
                    500:                        /* FALLTHROUGH */
                    501:                case(':'):
                    502:                        /* FALLTHROUGH */
                    503:                case('?'):
                    504:                        /* FALLTHROUGH */
                    505:                case('!'):
                    506:                        /* FALLTHROUGH */
                    507:                case(')'):
                    508:                        /* FALLTHROUGH */
                    509:                case(']'):
1.52      kristaps  510:                        if ( ! (HTML_IGNDELIM & h->flags))
                    511:                                h->flags |= HTML_NOSPACE;
1.30      kristaps  512:                        break;
1.29      kristaps  513:                default:
                    514:                        break;
                    515:                }
1.1       kristaps  516:
1.29      kristaps  517:        if ( ! (h->flags & HTML_NOSPACE))
1.78      kristaps  518:                putchar(' ');
1.30      kristaps  519:
1.86      kristaps  520:        assert(p);
1.88      kristaps  521:        if ( ! print_encode(h, p, 0))
1.86      kristaps  522:                h->flags &= ~HTML_NOSPACE;
1.8       kristaps  523:
1.29      kristaps  524:        if (*p && 0 == *(p + 1))
                    525:                switch (*p) {
1.97    ! kristaps  526:                case('|'):
        !           527:                        /* FALLTHROUGH */
1.29      kristaps  528:                case('('):
                    529:                        /* FALLTHROUGH */
                    530:                case('['):
                    531:                        h->flags |= HTML_NOSPACE;
1.30      kristaps  532:                        break;
1.29      kristaps  533:                default:
                    534:                        break;
                    535:                }
1.1       kristaps  536: }
1.30      kristaps  537:
                    538:
1.51      kristaps  539: void
1.30      kristaps  540: print_tagq(struct html *h, const struct tag *until)
                    541: {
                    542:        struct tag      *tag;
                    543:
1.66      kristaps  544:        while ((tag = h->tags.head) != NULL) {
1.89      kristaps  545:                if (tag == h->metaf)
                    546:                        h->metaf = NULL;
1.30      kristaps  547:                print_ctag(h, tag->tag);
1.66      kristaps  548:                h->tags.head = tag->next;
1.30      kristaps  549:                free(tag);
                    550:                if (until && tag == until)
                    551:                        return;
                    552:        }
                    553: }
                    554:
                    555:
1.51      kristaps  556: void
1.30      kristaps  557: print_stagq(struct html *h, const struct tag *suntil)
                    558: {
                    559:        struct tag      *tag;
                    560:
1.66      kristaps  561:        while ((tag = h->tags.head) != NULL) {
1.30      kristaps  562:                if (suntil && tag == suntil)
                    563:                        return;
1.89      kristaps  564:                if (tag == h->metaf)
                    565:                        h->metaf = NULL;
1.30      kristaps  566:                print_ctag(h, tag->tag);
1.66      kristaps  567:                h->tags.head = tag->next;
1.30      kristaps  568:                free(tag);
                    569:        }
                    570: }
1.55      kristaps  571:
                    572:
                    573: void
                    574: bufinit(struct html *h)
                    575: {
                    576:
                    577:        h->buf[0] = '\0';
                    578:        h->buflen = 0;
                    579: }
                    580:
                    581:
                    582: void
1.58      kristaps  583: bufcat_style(struct html *h, const char *key, const char *val)
                    584: {
                    585:
                    586:        bufcat(h, key);
                    587:        bufncat(h, ":", 1);
                    588:        bufcat(h, val);
                    589:        bufncat(h, ";", 1);
                    590: }
                    591:
                    592:
                    593: void
1.55      kristaps  594: bufcat(struct html *h, const char *p)
                    595: {
                    596:
                    597:        bufncat(h, p, strlen(p));
                    598: }
                    599:
                    600:
                    601: void
                    602: buffmt(struct html *h, const char *fmt, ...)
                    603: {
                    604:        va_list          ap;
                    605:
                    606:        va_start(ap, fmt);
1.56      kristaps  607:        (void)vsnprintf(h->buf + (int)h->buflen,
1.55      kristaps  608:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    609:        va_end(ap);
                    610:        h->buflen = strlen(h->buf);
                    611: }
                    612:
                    613:
                    614: void
                    615: bufncat(struct html *h, const char *p, size_t sz)
                    616: {
                    617:
                    618:        if (h->buflen + sz > BUFSIZ - 1)
                    619:                sz = BUFSIZ - 1 - h->buflen;
                    620:
                    621:        (void)strncat(h->buf, p, sz);
                    622:        h->buflen += sz;
                    623: }
                    624:
                    625:
                    626: void
                    627: buffmt_includes(struct html *h, const char *name)
                    628: {
                    629:        const char      *p, *pp;
                    630:
                    631:        pp = h->base_includes;
1.61      kristaps  632:
                    633:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  634:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  635:                switch (*(p + 1)) {
                    636:                case('I'):
                    637:                        bufcat(h, name);
                    638:                        break;
                    639:                default:
                    640:                        bufncat(h, p, 2);
                    641:                        break;
                    642:                }
                    643:                pp = p + 2;
                    644:        }
                    645:        if (pp)
                    646:                bufcat(h, pp);
                    647: }
                    648:
                    649:
                    650: void
                    651: buffmt_man(struct html *h,
                    652:                const char *name, const char *sec)
                    653: {
                    654:        const char      *p, *pp;
                    655:
                    656:        pp = h->base_man;
1.61      kristaps  657:
                    658:        /* LINTED */
                    659:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  660:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  661:                switch (*(p + 1)) {
                    662:                case('S'):
1.58      kristaps  663:                        bufcat(h, sec ? sec : "1");
1.55      kristaps  664:                        break;
                    665:                case('N'):
1.58      kristaps  666:                        buffmt(h, name);
1.55      kristaps  667:                        break;
                    668:                default:
                    669:                        bufncat(h, p, 2);
                    670:                        break;
                    671:                }
                    672:                pp = p + 2;
                    673:        }
                    674:        if (pp)
                    675:                bufcat(h, pp);
                    676: }
1.58      kristaps  677:
                    678:
                    679: void
                    680: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    681: {
1.62      kristaps  682:        double           v;
1.63      kristaps  683:        const char      *u;
1.58      kristaps  684:
                    685:        v = su->scale;
                    686:
                    687:        switch (su->unit) {
                    688:        case (SCALE_CM):
                    689:                u = "cm";
                    690:                break;
                    691:        case (SCALE_IN):
                    692:                u = "in";
                    693:                break;
                    694:        case (SCALE_PC):
                    695:                u = "pc";
                    696:                break;
                    697:        case (SCALE_PT):
                    698:                u = "pt";
                    699:                break;
1.59      kristaps  700:        case (SCALE_EM):
                    701:                u = "em";
                    702:                break;
1.58      kristaps  703:        case (SCALE_MM):
                    704:                if (0 == (v /= 100))
                    705:                        v = 1;
                    706:                u = "em";
                    707:                break;
1.59      kristaps  708:        case (SCALE_EN):
                    709:                u = "ex";
                    710:                break;
                    711:        case (SCALE_BU):
                    712:                u = "ex";
                    713:                break;
1.58      kristaps  714:        case (SCALE_VS):
                    715:                u = "em";
                    716:                break;
                    717:        default:
                    718:                u = "ex";
                    719:                break;
                    720:        }
                    721:
1.62      kristaps  722:        if (su->pt)
                    723:                buffmt(h, "%s: %f%s;", p, v, u);
                    724:        else
                    725:                /* LINTED */
                    726:                buffmt(h, "%s: %d%s;", p, (int)v, u);
1.58      kristaps  727: }
1.65      kristaps  728:
1.68      kristaps  729:
                    730: void
1.70      kristaps  731: html_idcat(char *dst, const char *src, int sz)
1.68      kristaps  732: {
1.70      kristaps  733:        int              ssz;
1.68      kristaps  734:
                    735:        assert(sz);
                    736:
                    737:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    738:
1.70      kristaps  739:        for ( ; *dst != '\0' && sz; dst++, sz--)
1.68      kristaps  740:                /* Jump to end. */ ;
                    741:
1.70      kristaps  742:        assert(sz > 2);
1.68      kristaps  743:
1.70      kristaps  744:        /* We can't start with a number (bah). */
1.68      kristaps  745:
1.70      kristaps  746:        *dst++ = 'x';
1.68      kristaps  747:        *dst = '\0';
1.70      kristaps  748:        sz--;
                    749:
                    750:        for ( ; *src != '\0' && sz > 1; src++) {
1.73      kristaps  751:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70      kristaps  752:                sz -= ssz;
                    753:                dst += ssz;
                    754:        }
1.68      kristaps  755: }

CVSweb