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

Annotation of mandoc/html.c, Revision 1.107

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

CVSweb