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

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