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

Annotation of mandoc/html.c, Revision 1.100

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

CVSweb