[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.9

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