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

1.18    ! schwarze    1: /*     $Id: html.c,v 1.17 2010/10/01 21:38:26 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.18    ! schwarze   56:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.6       schwarze   57:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1       schwarze   58:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     59:        {"td",          HTML_CLRLINE}, /* TAG_TD */
                     60:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     61:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     62:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.18    ! schwarze   63:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
        !            64:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
        !            65:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
        !            66:        {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
        !            67:        {"p",           HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_P */
        !            68:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.1       schwarze   69: };
                     70:
1.5       schwarze   71: static const char      *const htmlfonts[HTMLFONT_MAX] = {
                     72:        "roman",
                     73:        "bold",
                     74:        "italic"
                     75: };
                     76:
                     77: static const char      *const htmlattrs[ATTR_MAX] = {
1.1       schwarze   78:        "http-equiv",
                     79:        "content",
                     80:        "name",
                     81:        "rel",
                     82:        "href",
                     83:        "type",
                     84:        "media",
                     85:        "class",
                     86:        "style",
                     87:        "width",
                     88:        "valign",
                     89:        "target",
                     90:        "id",
1.3       schwarze   91:        "summary",
1.1       schwarze   92: };
                     93:
1.13      schwarze   94: static void              print_spec(struct html *, enum roffdeco,
                     95:                                const char *, size_t);
1.5       schwarze   96: static void              print_res(struct html *, const char *, size_t);
                     97: static void              print_ctag(struct html *, enum htmltag);
1.6       schwarze   98: static void              print_doctype(struct html *);
                     99: static void              print_xmltype(struct html *);
1.5       schwarze  100: static int               print_encode(struct html *, const char *, int);
                    101: static void              print_metaf(struct html *, enum roffdeco);
1.6       schwarze  102: static void              print_attr(struct html *,
                    103:                                const char *, const char *);
                    104: static void             *ml_alloc(char *, enum htmltype);
1.5       schwarze  105:
                    106:
1.6       schwarze  107: static void *
                    108: ml_alloc(char *outopts, enum htmltype type)
1.1       schwarze  109: {
                    110:        struct html     *h;
                    111:        const char      *toks[4];
                    112:        char            *v;
                    113:
                    114:        toks[0] = "style";
                    115:        toks[1] = "man";
                    116:        toks[2] = "includes";
                    117:        toks[3] = NULL;
                    118:
1.3       schwarze  119:        h = calloc(1, sizeof(struct html));
                    120:        if (NULL == h) {
                    121:                perror(NULL);
1.16      schwarze  122:                exit((int)MANDOCLEVEL_SYSERR);
1.3       schwarze  123:        }
1.1       schwarze  124:
1.6       schwarze  125:        h->type = type;
1.2       schwarze  126:        h->tags.head = NULL;
1.3       schwarze  127:        h->symtab = chars_init(CHARS_HTML);
1.1       schwarze  128:
                    129:        while (outopts && *outopts)
                    130:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    131:                case (0):
                    132:                        h->style = v;
                    133:                        break;
                    134:                case (1):
                    135:                        h->base_man = v;
                    136:                        break;
                    137:                case (2):
                    138:                        h->base_includes = v;
                    139:                        break;
                    140:                default:
                    141:                        break;
                    142:                }
                    143:
                    144:        return(h);
                    145: }
                    146:
1.6       schwarze  147: void *
                    148: html_alloc(char *outopts)
                    149: {
                    150:
                    151:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    152: }
                    153:
                    154:
                    155: void *
                    156: xhtml_alloc(char *outopts)
                    157: {
                    158:
                    159:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    160: }
                    161:
1.1       schwarze  162:
                    163: void
                    164: html_free(void *p)
                    165: {
                    166:        struct tag      *tag;
                    167:        struct html     *h;
                    168:
                    169:        h = (struct html *)p;
                    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);
1.16      schwarze  397:                        exit((int)MANDOCLEVEL_SYSERR);
1.3       schwarze  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;
1.14      schwarze  418:        else
                    419:                h->flags |= HTML_NOSPACE;
1.13      schwarze  420:
1.6       schwarze  421:        /* Print out the tag name and attributes. */
                    422:
1.1       schwarze  423:        printf("<%s", htmltags[tag].name);
1.6       schwarze  424:        for (i = 0; i < sz; i++)
                    425:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    426:
                    427:        /* Add non-overridable attributes. */
                    428:
                    429:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    430:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    431:                print_attr(h, "xml:lang", "en");
                    432:                print_attr(h, "lang", "en");
1.1       schwarze  433:        }
1.6       schwarze  434:
                    435:        /* Accomodate for XML "well-formed" singleton escaping. */
                    436:
                    437:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    438:                switch (h->type) {
                    439:                case (HTML_XHTML_1_0_STRICT):
                    440:                        putchar('/');
                    441:                        break;
                    442:                default:
                    443:                        break;
                    444:                }
                    445:
1.4       schwarze  446:        putchar('>');
1.1       schwarze  447:
                    448:        h->flags |= HTML_NOSPACE;
1.18    ! schwarze  449:
        !           450:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
        !           451:                putchar('\n');
        !           452:
1.1       schwarze  453:        return(t);
                    454: }
                    455:
                    456:
                    457: static void
                    458: print_ctag(struct html *h, enum htmltag tag)
                    459: {
                    460:
                    461:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  462:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  463:                h->flags |= HTML_NOSPACE;
1.4       schwarze  464:                putchar('\n');
1.5       schwarze  465:        }
1.1       schwarze  466: }
                    467:
                    468:
                    469: void
1.6       schwarze  470: print_gen_decls(struct html *h)
                    471: {
                    472:
                    473:        print_xmltype(h);
                    474:        print_doctype(h);
                    475: }
                    476:
                    477:
                    478: static void
                    479: print_xmltype(struct html *h)
                    480: {
                    481:
1.9       schwarze  482:        if (HTML_XHTML_1_0_STRICT == h->type)
                    483:                printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.6       schwarze  484: }
                    485:
                    486:
                    487: static void
                    488: print_doctype(struct html *h)
1.1       schwarze  489: {
1.6       schwarze  490:        const char      *doctype;
                    491:        const char      *dtd;
                    492:        const char      *name;
                    493:
                    494:        switch (h->type) {
                    495:        case (HTML_HTML_4_01_STRICT):
                    496:                name = "HTML";
                    497:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    498:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    499:                break;
                    500:        default:
                    501:                name = "html";
                    502:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    503:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    504:                break;
                    505:        }
                    506:
                    507:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    508:                        name, doctype, dtd);
1.1       schwarze  509: }
                    510:
                    511:
                    512: void
1.12      schwarze  513: print_text(struct html *h, const char *word)
1.1       schwarze  514: {
                    515:
1.12      schwarze  516:        if (word[0] && '\0' == word[1])
                    517:                switch (word[0]) {
1.1       schwarze  518:                case('.'):
                    519:                        /* FALLTHROUGH */
                    520:                case(','):
                    521:                        /* FALLTHROUGH */
                    522:                case(';'):
                    523:                        /* FALLTHROUGH */
                    524:                case(':'):
                    525:                        /* FALLTHROUGH */
                    526:                case('?'):
                    527:                        /* FALLTHROUGH */
                    528:                case('!'):
                    529:                        /* FALLTHROUGH */
                    530:                case(')'):
                    531:                        /* FALLTHROUGH */
                    532:                case(']'):
                    533:                        if ( ! (HTML_IGNDELIM & h->flags))
                    534:                                h->flags |= HTML_NOSPACE;
                    535:                        break;
                    536:                default:
                    537:                        break;
                    538:                }
                    539:
1.12      schwarze  540:        if ( ! (HTML_NOSPACE & h->flags)) {
                    541:                /* Manage keeps! */
                    542:                if ( ! (HTML_KEEP & h->flags)) {
                    543:                        if (HTML_PREKEEP & h->flags)
                    544:                                h->flags |= HTML_KEEP;
                    545:                        putchar(' ');
                    546:                } else
                    547:                        printf("&#160;");
                    548:        }
1.1       schwarze  549:
1.12      schwarze  550:        assert(word);
                    551:        if ( ! print_encode(h, word, 0))
1.13      schwarze  552:                if ( ! (h->flags & HTML_NONOSPACE))
                    553:                        h->flags &= ~HTML_NOSPACE;
1.17      schwarze  554:
                    555:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  556:
1.8       schwarze  557:        /*
                    558:         * Note that we don't process the pipe: the parser sees it as
                    559:         * punctuation, but we don't in terms of typography.
                    560:         */
1.12      schwarze  561:        if (word[0] && '\0' == word[1])
                    562:                switch (word[0]) {
1.1       schwarze  563:                case('('):
                    564:                        /* FALLTHROUGH */
                    565:                case('['):
                    566:                        h->flags |= HTML_NOSPACE;
                    567:                        break;
                    568:                default:
                    569:                        break;
                    570:                }
                    571: }
                    572:
                    573:
                    574: void
                    575: print_tagq(struct html *h, const struct tag *until)
                    576: {
                    577:        struct tag      *tag;
                    578:
1.2       schwarze  579:        while ((tag = h->tags.head) != NULL) {
1.5       schwarze  580:                if (tag == h->metaf)
                    581:                        h->metaf = NULL;
1.1       schwarze  582:                print_ctag(h, tag->tag);
1.2       schwarze  583:                h->tags.head = tag->next;
1.1       schwarze  584:                free(tag);
                    585:                if (until && tag == until)
                    586:                        return;
                    587:        }
                    588: }
                    589:
                    590:
                    591: void
                    592: print_stagq(struct html *h, const struct tag *suntil)
                    593: {
                    594:        struct tag      *tag;
                    595:
1.2       schwarze  596:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  597:                if (suntil && tag == suntil)
                    598:                        return;
1.5       schwarze  599:                if (tag == h->metaf)
                    600:                        h->metaf = NULL;
1.1       schwarze  601:                print_ctag(h, tag->tag);
1.2       schwarze  602:                h->tags.head = tag->next;
1.1       schwarze  603:                free(tag);
                    604:        }
                    605: }
                    606:
                    607:
                    608: void
                    609: bufinit(struct html *h)
                    610: {
                    611:
                    612:        h->buf[0] = '\0';
                    613:        h->buflen = 0;
                    614: }
                    615:
                    616:
                    617: void
                    618: bufcat_style(struct html *h, const char *key, const char *val)
                    619: {
                    620:
                    621:        bufcat(h, key);
                    622:        bufncat(h, ":", 1);
                    623:        bufcat(h, val);
                    624:        bufncat(h, ";", 1);
                    625: }
                    626:
                    627:
                    628: void
                    629: bufcat(struct html *h, const char *p)
                    630: {
                    631:
                    632:        bufncat(h, p, strlen(p));
                    633: }
                    634:
                    635:
                    636: void
                    637: buffmt(struct html *h, const char *fmt, ...)
                    638: {
                    639:        va_list          ap;
                    640:
                    641:        va_start(ap, fmt);
                    642:        (void)vsnprintf(h->buf + (int)h->buflen,
                    643:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    644:        va_end(ap);
                    645:        h->buflen = strlen(h->buf);
                    646: }
                    647:
                    648:
                    649: void
                    650: bufncat(struct html *h, const char *p, size_t sz)
                    651: {
                    652:
                    653:        if (h->buflen + sz > BUFSIZ - 1)
                    654:                sz = BUFSIZ - 1 - h->buflen;
                    655:
                    656:        (void)strncat(h->buf, p, sz);
                    657:        h->buflen += sz;
                    658: }
                    659:
                    660:
                    661: void
                    662: buffmt_includes(struct html *h, const char *name)
                    663: {
                    664:        const char      *p, *pp;
                    665:
                    666:        pp = h->base_includes;
                    667:
                    668:        while (NULL != (p = strchr(pp, '%'))) {
                    669:                bufncat(h, pp, (size_t)(p - pp));
                    670:                switch (*(p + 1)) {
                    671:                case('I'):
                    672:                        bufcat(h, name);
                    673:                        break;
                    674:                default:
                    675:                        bufncat(h, p, 2);
                    676:                        break;
                    677:                }
                    678:                pp = p + 2;
                    679:        }
                    680:        if (pp)
                    681:                bufcat(h, pp);
                    682: }
                    683:
                    684:
                    685: void
                    686: buffmt_man(struct html *h,
                    687:                const char *name, const char *sec)
                    688: {
                    689:        const char      *p, *pp;
                    690:
                    691:        pp = h->base_man;
                    692:
                    693:        /* LINTED */
                    694:        while (NULL != (p = strchr(pp, '%'))) {
                    695:                bufncat(h, pp, (size_t)(p - pp));
                    696:                switch (*(p + 1)) {
                    697:                case('S'):
                    698:                        bufcat(h, sec ? sec : "1");
                    699:                        break;
                    700:                case('N'):
                    701:                        buffmt(h, name);
                    702:                        break;
                    703:                default:
                    704:                        bufncat(h, p, 2);
                    705:                        break;
                    706:                }
                    707:                pp = p + 2;
                    708:        }
                    709:        if (pp)
                    710:                bufcat(h, pp);
                    711: }
                    712:
                    713:
                    714: void
                    715: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    716: {
                    717:        double           v;
                    718:        const char      *u;
                    719:
                    720:        v = su->scale;
                    721:
                    722:        switch (su->unit) {
                    723:        case (SCALE_CM):
                    724:                u = "cm";
                    725:                break;
                    726:        case (SCALE_IN):
                    727:                u = "in";
                    728:                break;
                    729:        case (SCALE_PC):
                    730:                u = "pc";
                    731:                break;
                    732:        case (SCALE_PT):
                    733:                u = "pt";
                    734:                break;
                    735:        case (SCALE_EM):
                    736:                u = "em";
                    737:                break;
                    738:        case (SCALE_MM):
                    739:                if (0 == (v /= 100))
                    740:                        v = 1;
                    741:                u = "em";
                    742:                break;
                    743:        case (SCALE_EN):
                    744:                u = "ex";
                    745:                break;
                    746:        case (SCALE_BU):
                    747:                u = "ex";
                    748:                break;
                    749:        case (SCALE_VS):
                    750:                u = "em";
                    751:                break;
                    752:        default:
                    753:                u = "ex";
                    754:                break;
                    755:        }
                    756:
1.11      schwarze  757:        /*
                    758:         * XXX: the CSS spec isn't clear as to which types accept
                    759:         * integer or real numbers, so we just make them all decimals.
                    760:         */
                    761:        buffmt(h, "%s: %.2f%s;", p, v, u);
1.1       schwarze  762: }
                    763:
1.3       schwarze  764:
                    765: void
                    766: html_idcat(char *dst, const char *src, int sz)
                    767: {
                    768:        int              ssz;
                    769:
                    770:        assert(sz);
                    771:
                    772:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    773:
                    774:        for ( ; *dst != '\0' && sz; dst++, sz--)
                    775:                /* Jump to end. */ ;
                    776:
                    777:        assert(sz > 2);
                    778:
                    779:        /* We can't start with a number (bah). */
                    780:
                    781:        *dst++ = 'x';
                    782:        *dst = '\0';
                    783:        sz--;
                    784:
                    785:        for ( ; *src != '\0' && sz > 1; src++) {
                    786:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
                    787:                sz -= ssz;
                    788:                dst += ssz;
                    789:        }
                    790: }