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

Annotation of mandoc/html.c, Revision 1.81

1.81    ! kristaps    1: /*     $Id: html.c,v 1.80 2009/11/02 06:22:44 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.41      kristaps   17: #include <sys/types.h>
1.30      kristaps   18:
1.1       kristaps   19: #include <assert.h>
1.68      kristaps   20: #include <ctype.h>
1.76      kristaps   21: #include <stdarg.h>
1.29      kristaps   22: #include <stdio.h>
1.63      kristaps   23: #include <stdint.h>
1.1       kristaps   24: #include <stdlib.h>
1.33      kristaps   25: #include <string.h>
1.45      kristaps   26: #include <unistd.h>
1.1       kristaps   27:
1.58      kristaps   28: #include "out.h"
1.32      kristaps   29: #include "chars.h"
1.51      kristaps   30: #include "html.h"
1.64      kristaps   31: #include "main.h"
1.2       kristaps   32:
1.63      kristaps   33: #define        UNCONST(a)      ((void *)(uintptr_t)(const void *)(a))
                     34:
1.29      kristaps   35: #define        DOCTYPE         "-//W3C//DTD HTML 4.01//EN"
                     36: #define        DTD             "http://www.w3.org/TR/html4/strict.dtd"
1.8       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.29      kristaps   43: };
1.7       kristaps   44:
1.29      kristaps   45: static const struct htmldata htmltags[TAG_MAX] = {
1.30      kristaps   46:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     47:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     48:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
                     49:        {"meta",        HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
1.33      kristaps   50:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
1.30      kristaps   51:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
1.29      kristaps   52:        {"h1",          0}, /* TAG_H1 */
                     53:        {"h2",          0}, /* TAG_H2 */
1.30      kristaps   54:        {"p",           HTML_CLRLINE}, /* TAG_P */
1.29      kristaps   55:        {"span",        0}, /* TAG_SPAN */
1.30      kristaps   56:        {"link",        HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
                     57:        {"br",          HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
                     58:        {"a",           0}, /* TAG_A */
1.33      kristaps   59:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
                     60:        {"col",         HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
                     61:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     62:        {"td",          HTML_CLRLINE}, /* TAG_TD */
1.34      kristaps   63:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     64:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     65:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.41      kristaps   66:        {"base",        HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
1.29      kristaps   67: };
1.10      kristaps   68:
1.29      kristaps   69: static const char       *const htmlattrs[ATTR_MAX] = {
                     70:        "http-equiv",
                     71:        "content",
                     72:        "name",
                     73:        "rel",
                     74:        "href",
                     75:        "type",
                     76:        "media",
1.33      kristaps   77:        "class",
                     78:        "style",
                     79:        "width",
                     80:        "valign",
1.54      kristaps   81:        "target",
1.57      kristaps   82:        "id",
1.67      kristaps   83:        "summary",
1.29      kristaps   84: };
1.10      kristaps   85:
1.33      kristaps   86: #ifdef __linux__
1.43      kristaps   87: extern int               getsubopt(char **, char * const *, char **);
1.33      kristaps   88: #endif
1.29      kristaps   89:
                     90: void *
1.43      kristaps   91: html_alloc(char *outopts)
1.10      kristaps   92: {
1.30      kristaps   93:        struct html     *h;
1.63      kristaps   94:        const char      *toks[4];
                     95:        char            *v;
1.43      kristaps   96:
                     97:        toks[0] = "style";
1.53      kristaps   98:        toks[1] = "man";
1.54      kristaps   99:        toks[2] = "includes";
                    100:        toks[3] = NULL;
1.30      kristaps  101:
1.72      kristaps  102:        h = calloc(1, sizeof(struct html));
                    103:        if (NULL == h) {
1.75      kristaps  104:                perror(NULL);
1.72      kristaps  105:                exit(EXIT_FAILURE);
                    106:        }
1.10      kristaps  107:
1.66      kristaps  108:        h->tags.head = NULL;
                    109:        h->ords.head = NULL;
1.72      kristaps  110:        h->symtab = chars_init(CHARS_HTML);
1.41      kristaps  111:
1.47      kristaps  112:        while (outopts && *outopts)
1.63      kristaps  113:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43      kristaps  114:                case (0):
                    115:                        h->style = v;
                    116:                        break;
                    117:                case (1):
1.53      kristaps  118:                        h->base_man = v;
1.43      kristaps  119:                        break;
1.54      kristaps  120:                case (2):
                    121:                        h->base_includes = v;
                    122:                        break;
1.43      kristaps  123:                default:
                    124:                        break;
                    125:                }
                    126:
1.30      kristaps  127:        return(h);
1.29      kristaps  128: }
1.10      kristaps  129:
1.33      kristaps  130:
1.29      kristaps  131: void
                    132: html_free(void *p)
                    133: {
1.30      kristaps  134:        struct tag      *tag;
1.37      kristaps  135:        struct ord      *ord;
1.30      kristaps  136:        struct html     *h;
                    137:
                    138:        h = (struct html *)p;
1.10      kristaps  139:
1.66      kristaps  140:        while ((ord = h->ords.head) != NULL) {
                    141:                h->ords.head = ord->next;
1.37      kristaps  142:                free(ord);
                    143:        }
                    144:
1.66      kristaps  145:        while ((tag = h->tags.head) != NULL) {
                    146:                h->tags.head = tag->next;
1.30      kristaps  147:                free(tag);
                    148:        }
1.36      kristaps  149:
                    150:        if (h->symtab)
                    151:                chars_free(h->symtab);
1.53      kristaps  152:
1.30      kristaps  153:        free(h);
1.10      kristaps  154: }
1.2       kristaps  155:
1.33      kristaps  156:
1.51      kristaps  157: void
1.29      kristaps  158: print_gen_head(struct html *h)
                    159: {
1.41      kristaps  160:        struct htmlpair  tag[4];
                    161:
                    162:        tag[0].key = ATTR_HTTPEQUIV;
                    163:        tag[0].val = "Content-Type";
                    164:        tag[1].key = ATTR_CONTENT;
                    165:        tag[1].val = "text/html; charset=utf-8";
                    166:        print_otag(h, TAG_META, 2, tag);
                    167:
                    168:        tag[0].key = ATTR_NAME;
                    169:        tag[0].val = "resource-type";
                    170:        tag[1].key = ATTR_CONTENT;
                    171:        tag[1].val = "document";
                    172:        print_otag(h, TAG_META, 2, tag);
                    173:
                    174:        if (h->style) {
                    175:                tag[0].key = ATTR_REL;
                    176:                tag[0].val = "stylesheet";
                    177:                tag[1].key = ATTR_HREF;
                    178:                tag[1].val = h->style;
                    179:                tag[2].key = ATTR_TYPE;
                    180:                tag[2].val = "text/css";
                    181:                tag[3].key = ATTR_MEDIA;
                    182:                tag[3].val = "all";
                    183:                print_otag(h, TAG_LINK, 4, tag);
                    184:        }
1.4       kristaps  185: }
                    186:
1.33      kristaps  187:
1.29      kristaps  188: static void
1.32      kristaps  189: print_spec(struct html *h, const char *p, int len)
                    190: {
                    191:        const char      *rhs;
                    192:        size_t           sz;
                    193:
                    194:        rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
                    195:
                    196:        if (NULL == rhs)
                    197:                return;
1.76      kristaps  198:        fwrite(rhs, 1, sz, stdout);
1.32      kristaps  199: }
                    200:
1.33      kristaps  201:
1.32      kristaps  202: static void
                    203: print_res(struct html *h, const char *p, int len)
                    204: {
                    205:        const char      *rhs;
                    206:        size_t           sz;
                    207:
                    208:        rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
                    209:
                    210:        if (NULL == rhs)
                    211:                return;
1.76      kristaps  212:        fwrite(rhs, 1, sz, stdout);
1.32      kristaps  213: }
                    214:
1.33      kristaps  215:
1.32      kristaps  216: static void
                    217: print_escape(struct html *h, const char **p)
                    218: {
                    219:        int              j, type;
                    220:        const char      *wp;
                    221:
                    222:        wp = *p;
                    223:        type = 1;
                    224:
                    225:        if (0 == *(++wp)) {
                    226:                *p = wp;
                    227:                return;
                    228:        }
                    229:
                    230:        if ('(' == *wp) {
                    231:                wp++;
                    232:                if (0 == *wp || 0 == *(wp + 1)) {
                    233:                        *p = 0 == *wp ? wp : wp + 1;
                    234:                        return;
                    235:                }
                    236:
                    237:                print_spec(h, wp, 2);
                    238:                *p = ++wp;
                    239:                return;
                    240:
                    241:        } else if ('*' == *wp) {
                    242:                if (0 == *(++wp)) {
                    243:                        *p = wp;
                    244:                        return;
                    245:                }
                    246:
                    247:                switch (*wp) {
                    248:                case ('('):
                    249:                        wp++;
                    250:                        if (0 == *wp || 0 == *(wp + 1)) {
                    251:                                *p = 0 == *wp ? wp : wp + 1;
                    252:                                return;
                    253:                        }
                    254:
                    255:                        print_res(h, wp, 2);
                    256:                        *p = ++wp;
                    257:                        return;
                    258:                case ('['):
                    259:                        type = 0;
                    260:                        break;
                    261:                default:
                    262:                        print_res(h, wp, 1);
                    263:                        *p = wp;
                    264:                        return;
                    265:                }
                    266:
                    267:        } else if ('f' == *wp) {
                    268:                if (0 == *(++wp)) {
                    269:                        *p = wp;
                    270:                        return;
                    271:                }
                    272:
1.81    ! kristaps  273:                /*
        !           274:                 * These aren't supported, as they're symmetry-breaking
        !           275:                 * constructs that don't play well with hierarchical
        !           276:                 * mark-up.  Consider:
        !           277:                 *
        !           278:                 * \fBHello.
        !           279:                 * .PP
        !           280:                 * World.
        !           281:                 *
        !           282:                 * The style started before "Hello" wouldn't be able to
        !           283:                 * propogate into the next `PP' because we'd exit the
        !           284:                 * current paragraph's scope.
        !           285:                 */
1.32      kristaps  286:
                    287:                *p = wp;
                    288:                return;
                    289:
                    290:        } else if ('[' != *wp) {
                    291:                print_spec(h, wp, 1);
                    292:                *p = wp;
                    293:                return;
                    294:        }
                    295:
                    296:        wp++;
                    297:        for (j = 0; *wp && ']' != *wp; wp++, j++)
                    298:                /* Loop... */ ;
                    299:
                    300:        if (0 == *wp) {
                    301:                *p = wp;
                    302:                return;
                    303:        }
                    304:
                    305:        if (type)
                    306:                print_spec(h, wp - j, j);
                    307:        else
                    308:                print_res(h, wp - j, j);
                    309:
                    310:        *p = wp;
                    311: }
                    312:
1.9       kristaps  313:
1.29      kristaps  314: static void
1.32      kristaps  315: print_encode(struct html *h, const char *p)
1.29      kristaps  316: {
1.77      kristaps  317:        size_t           sz;
1.14      kristaps  318:
1.32      kristaps  319:        for (; *p; p++) {
1.77      kristaps  320:                sz = strcspn(p, "\\<>&");
                    321:
                    322:                fwrite(p, 1, sz, stdout);
1.80      kristaps  323:                p += /* LINTED */
                    324:                        sz;
1.77      kristaps  325:
1.34      kristaps  326:                if ('\\' == *p) {
                    327:                        print_escape(h, &p);
                    328:                        continue;
1.77      kristaps  329:                } else if ('\0' == *p)
                    330:                        break;
                    331:
                    332:                if ('<' == *p)
1.34      kristaps  333:                        printf("&lt;");
1.77      kristaps  334:                else if ('>' == *p)
1.34      kristaps  335:                        printf("&gt;");
1.77      kristaps  336:                else if ('&' == *p)
1.34      kristaps  337:                        printf("&amp;");
1.32      kristaps  338:        }
1.14      kristaps  339: }
                    340:
                    341:
1.51      kristaps  342: struct tag *
1.29      kristaps  343: print_otag(struct html *h, enum htmltag tag,
                    344:                int sz, const struct htmlpair *p)
1.14      kristaps  345: {
1.29      kristaps  346:        int              i;
1.30      kristaps  347:        struct tag      *t;
                    348:
                    349:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.72      kristaps  350:                t = malloc(sizeof(struct tag));
                    351:                if (NULL == t) {
1.75      kristaps  352:                        perror(NULL);
1.72      kristaps  353:                        exit(EXIT_FAILURE);
                    354:                }
1.30      kristaps  355:                t->tag = tag;
1.66      kristaps  356:                t->next = h->tags.head;
                    357:                h->tags.head = t;
1.30      kristaps  358:        } else
                    359:                t = NULL;
1.29      kristaps  360:
                    361:        if ( ! (HTML_NOSPACE & h->flags))
1.30      kristaps  362:                if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.78      kristaps  363:                        putchar(' ');
1.29      kristaps  364:
                    365:        printf("<%s", htmltags[tag].name);
                    366:        for (i = 0; i < sz; i++) {
                    367:                printf(" %s=\"", htmlattrs[p[i].key]);
                    368:                assert(p->val);
1.32      kristaps  369:                print_encode(h, p[i].val);
1.78      kristaps  370:                putchar('\"');
1.29      kristaps  371:        }
1.78      kristaps  372:        putchar('>');
1.14      kristaps  373:
1.29      kristaps  374:        h->flags |= HTML_NOSPACE;
1.30      kristaps  375:        if (HTML_CLRLINE & htmltags[tag].flags)
                    376:                h->flags |= HTML_NEWLINE;
                    377:        else
                    378:                h->flags &= ~HTML_NEWLINE;
1.14      kristaps  379:
1.30      kristaps  380:        return(t);
1.14      kristaps  381: }
                    382:
                    383:
                    384: /* ARGSUSED */
1.29      kristaps  385: static void
                    386: print_ctag(struct html *h, enum htmltag tag)
1.14      kristaps  387: {
                    388:
1.29      kristaps  389:        printf("</%s>", htmltags[tag].name);
1.71      kristaps  390:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.29      kristaps  391:                h->flags |= HTML_NOSPACE;
1.30      kristaps  392:                h->flags |= HTML_NEWLINE;
1.78      kristaps  393:                putchar('\n');
1.71      kristaps  394:        } else
1.30      kristaps  395:                h->flags &= ~HTML_NEWLINE;
1.14      kristaps  396: }
                    397:
                    398:
1.29      kristaps  399: /* ARGSUSED */
1.51      kristaps  400: void
1.29      kristaps  401: print_gen_doctype(struct html *h)
1.1       kristaps  402: {
1.29      kristaps  403:
1.46      kristaps  404:        printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
1.1       kristaps  405: }
                    406:
                    407:
1.51      kristaps  408: void
1.29      kristaps  409: print_text(struct html *h, const char *p)
1.1       kristaps  410: {
                    411:
1.29      kristaps  412:        if (*p && 0 == *(p + 1))
                    413:                switch (*p) {
                    414:                case('.'):
                    415:                        /* FALLTHROUGH */
                    416:                case(','):
                    417:                        /* FALLTHROUGH */
                    418:                case(';'):
                    419:                        /* FALLTHROUGH */
                    420:                case(':'):
                    421:                        /* FALLTHROUGH */
                    422:                case('?'):
                    423:                        /* FALLTHROUGH */
                    424:                case('!'):
                    425:                        /* FALLTHROUGH */
                    426:                case(')'):
                    427:                        /* FALLTHROUGH */
                    428:                case(']'):
                    429:                        /* FALLTHROUGH */
                    430:                case('}'):
1.52      kristaps  431:                        if ( ! (HTML_IGNDELIM & h->flags))
                    432:                                h->flags |= HTML_NOSPACE;
1.30      kristaps  433:                        break;
1.29      kristaps  434:                default:
                    435:                        break;
                    436:                }
1.1       kristaps  437:
1.29      kristaps  438:        if ( ! (h->flags & HTML_NOSPACE))
1.78      kristaps  439:                putchar(' ');
1.30      kristaps  440:
1.29      kristaps  441:        h->flags &= ~HTML_NOSPACE;
1.30      kristaps  442:        h->flags &= ~HTML_NEWLINE;
1.1       kristaps  443:
1.29      kristaps  444:        if (p)
1.32      kristaps  445:                print_encode(h, p);
1.8       kristaps  446:
1.29      kristaps  447:        if (*p && 0 == *(p + 1))
                    448:                switch (*p) {
                    449:                case('('):
                    450:                        /* FALLTHROUGH */
                    451:                case('['):
                    452:                        /* FALLTHROUGH */
                    453:                case('{'):
                    454:                        h->flags |= HTML_NOSPACE;
1.30      kristaps  455:                        break;
1.29      kristaps  456:                default:
                    457:                        break;
                    458:                }
1.1       kristaps  459: }
1.30      kristaps  460:
                    461:
1.51      kristaps  462: void
1.30      kristaps  463: print_tagq(struct html *h, const struct tag *until)
                    464: {
                    465:        struct tag      *tag;
                    466:
1.66      kristaps  467:        while ((tag = h->tags.head) != NULL) {
1.30      kristaps  468:                print_ctag(h, tag->tag);
1.66      kristaps  469:                h->tags.head = tag->next;
1.30      kristaps  470:                free(tag);
                    471:                if (until && tag == until)
                    472:                        return;
                    473:        }
                    474: }
                    475:
                    476:
1.51      kristaps  477: void
1.30      kristaps  478: print_stagq(struct html *h, const struct tag *suntil)
                    479: {
                    480:        struct tag      *tag;
                    481:
1.66      kristaps  482:        while ((tag = h->tags.head) != NULL) {
1.30      kristaps  483:                if (suntil && tag == suntil)
                    484:                        return;
                    485:                print_ctag(h, tag->tag);
1.66      kristaps  486:                h->tags.head = tag->next;
1.30      kristaps  487:                free(tag);
                    488:        }
                    489: }
1.55      kristaps  490:
                    491:
                    492: void
                    493: bufinit(struct html *h)
                    494: {
                    495:
                    496:        h->buf[0] = '\0';
                    497:        h->buflen = 0;
                    498: }
                    499:
                    500:
                    501: void
1.58      kristaps  502: bufcat_style(struct html *h, const char *key, const char *val)
                    503: {
                    504:
                    505:        bufcat(h, key);
                    506:        bufncat(h, ":", 1);
                    507:        bufcat(h, val);
                    508:        bufncat(h, ";", 1);
                    509: }
                    510:
                    511:
                    512: void
1.55      kristaps  513: bufcat(struct html *h, const char *p)
                    514: {
                    515:
                    516:        bufncat(h, p, strlen(p));
                    517: }
                    518:
                    519:
                    520: void
                    521: buffmt(struct html *h, const char *fmt, ...)
                    522: {
                    523:        va_list          ap;
                    524:
                    525:        va_start(ap, fmt);
1.56      kristaps  526:        (void)vsnprintf(h->buf + (int)h->buflen,
1.55      kristaps  527:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    528:        va_end(ap);
                    529:        h->buflen = strlen(h->buf);
                    530: }
                    531:
                    532:
                    533: void
                    534: bufncat(struct html *h, const char *p, size_t sz)
                    535: {
                    536:
                    537:        if (h->buflen + sz > BUFSIZ - 1)
                    538:                sz = BUFSIZ - 1 - h->buflen;
                    539:
                    540:        (void)strncat(h->buf, p, sz);
                    541:        h->buflen += sz;
                    542: }
                    543:
                    544:
                    545: void
                    546: buffmt_includes(struct html *h, const char *name)
                    547: {
                    548:        const char      *p, *pp;
                    549:
                    550:        pp = h->base_includes;
1.61      kristaps  551:
                    552:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  553:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  554:                switch (*(p + 1)) {
                    555:                case('I'):
                    556:                        bufcat(h, name);
                    557:                        break;
                    558:                default:
                    559:                        bufncat(h, p, 2);
                    560:                        break;
                    561:                }
                    562:                pp = p + 2;
                    563:        }
                    564:        if (pp)
                    565:                bufcat(h, pp);
                    566: }
                    567:
                    568:
                    569: void
                    570: buffmt_man(struct html *h,
                    571:                const char *name, const char *sec)
                    572: {
                    573:        const char      *p, *pp;
                    574:
                    575:        pp = h->base_man;
1.61      kristaps  576:
                    577:        /* LINTED */
                    578:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  579:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  580:                switch (*(p + 1)) {
                    581:                case('S'):
1.58      kristaps  582:                        bufcat(h, sec ? sec : "1");
1.55      kristaps  583:                        break;
                    584:                case('N'):
1.58      kristaps  585:                        buffmt(h, name);
1.55      kristaps  586:                        break;
                    587:                default:
                    588:                        bufncat(h, p, 2);
                    589:                        break;
                    590:                }
                    591:                pp = p + 2;
                    592:        }
                    593:        if (pp)
                    594:                bufcat(h, pp);
                    595: }
1.58      kristaps  596:
                    597:
                    598: void
                    599: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    600: {
1.62      kristaps  601:        double           v;
1.63      kristaps  602:        const char      *u;
1.58      kristaps  603:
                    604:        v = su->scale;
                    605:
                    606:        switch (su->unit) {
                    607:        case (SCALE_CM):
                    608:                u = "cm";
                    609:                break;
                    610:        case (SCALE_IN):
                    611:                u = "in";
                    612:                break;
                    613:        case (SCALE_PC):
                    614:                u = "pc";
                    615:                break;
                    616:        case (SCALE_PT):
                    617:                u = "pt";
                    618:                break;
1.59      kristaps  619:        case (SCALE_EM):
                    620:                u = "em";
                    621:                break;
1.58      kristaps  622:        case (SCALE_MM):
                    623:                if (0 == (v /= 100))
                    624:                        v = 1;
                    625:                u = "em";
                    626:                break;
1.59      kristaps  627:        case (SCALE_EN):
                    628:                u = "ex";
                    629:                break;
                    630:        case (SCALE_BU):
                    631:                u = "ex";
                    632:                break;
1.58      kristaps  633:        case (SCALE_VS):
                    634:                u = "em";
                    635:                break;
                    636:        default:
                    637:                u = "ex";
                    638:                break;
                    639:        }
                    640:
1.62      kristaps  641:        if (su->pt)
                    642:                buffmt(h, "%s: %f%s;", p, v, u);
                    643:        else
                    644:                /* LINTED */
                    645:                buffmt(h, "%s: %d%s;", p, (int)v, u);
1.58      kristaps  646: }
1.65      kristaps  647:
1.68      kristaps  648:
                    649: void
1.70      kristaps  650: html_idcat(char *dst, const char *src, int sz)
1.68      kristaps  651: {
1.70      kristaps  652:        int              ssz;
1.68      kristaps  653:
                    654:        assert(sz);
                    655:
                    656:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    657:
1.70      kristaps  658:        for ( ; *dst != '\0' && sz; dst++, sz--)
1.68      kristaps  659:                /* Jump to end. */ ;
                    660:
1.70      kristaps  661:        assert(sz > 2);
1.68      kristaps  662:
1.70      kristaps  663:        /* We can't start with a number (bah). */
1.68      kristaps  664:
1.70      kristaps  665:        *dst++ = 'x';
1.68      kristaps  666:        *dst = '\0';
1.70      kristaps  667:        sz--;
                    668:
                    669:        for ( ; *src != '\0' && sz > 1; src++) {
1.73      kristaps  670:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70      kristaps  671:                sz -= ssz;
                    672:                dst += ssz;
                    673:        }
1.68      kristaps  674: }

CVSweb