[BACK]Return to html.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/html.c, Revision 1.11

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