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

1.21    ! schwarze    1: /*     $Id: html.c,v 1.20 2010/12/25 13:23:03 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.19      schwarze   69:        {"b",           0 }, /* TAG_B */
                     70:        {"i",           0 }, /* TAG_I */
1.20      schwarze   71:        {"code",        0 }, /* TAG_CODE */
                     72:        {"small",       0 }, /* TAG_SMALL */
1.5       schwarze   73: };
                     74:
                     75: static const char      *const htmlattrs[ATTR_MAX] = {
1.19      schwarze   76:        "http-equiv", /* ATTR_HTTPEQUIV */
                     77:        "content", /* ATTR_CONTENT */
                     78:        "name", /* ATTR_NAME */
                     79:        "rel", /* ATTR_REL */
                     80:        "href", /* ATTR_HREF */
                     81:        "type", /* ATTR_TYPE */
                     82:        "media", /* ATTR_MEDIA */
                     83:        "class", /* ATTR_CLASS */
                     84:        "style", /* ATTR_STYLE */
                     85:        "width", /* ATTR_WIDTH */
                     86:        "id", /* ATTR_ID */
                     87:        "summary", /* ATTR_SUMMARY */
                     88:        "align", /* ATTR_ALIGN */
1.1       schwarze   89: };
                     90:
1.13      schwarze   91: static void              print_spec(struct html *, enum roffdeco,
                     92:                                const char *, size_t);
1.5       schwarze   93: static void              print_res(struct html *, const char *, size_t);
                     94: static void              print_ctag(struct html *, enum htmltag);
1.6       schwarze   95: static void              print_doctype(struct html *);
                     96: static void              print_xmltype(struct html *);
1.5       schwarze   97: static int               print_encode(struct html *, const char *, int);
                     98: static void              print_metaf(struct html *, enum roffdeco);
1.6       schwarze   99: static void              print_attr(struct html *,
                    100:                                const char *, const char *);
                    101: static void             *ml_alloc(char *, enum htmltype);
1.5       schwarze  102:
                    103:
1.6       schwarze  104: static void *
                    105: ml_alloc(char *outopts, enum htmltype type)
1.1       schwarze  106: {
                    107:        struct html     *h;
                    108:        const char      *toks[4];
                    109:        char            *v;
                    110:
                    111:        toks[0] = "style";
                    112:        toks[1] = "man";
                    113:        toks[2] = "includes";
                    114:        toks[3] = NULL;
                    115:
1.3       schwarze  116:        h = calloc(1, sizeof(struct html));
                    117:        if (NULL == h) {
                    118:                perror(NULL);
1.16      schwarze  119:                exit((int)MANDOCLEVEL_SYSERR);
1.3       schwarze  120:        }
1.1       schwarze  121:
1.6       schwarze  122:        h->type = type;
1.2       schwarze  123:        h->tags.head = NULL;
1.3       schwarze  124:        h->symtab = chars_init(CHARS_HTML);
1.1       schwarze  125:
                    126:        while (outopts && *outopts)
                    127:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    128:                case (0):
                    129:                        h->style = v;
                    130:                        break;
                    131:                case (1):
                    132:                        h->base_man = v;
                    133:                        break;
                    134:                case (2):
                    135:                        h->base_includes = v;
                    136:                        break;
                    137:                default:
                    138:                        break;
                    139:                }
                    140:
                    141:        return(h);
                    142: }
                    143:
1.6       schwarze  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.1       schwarze  159:
                    160: void
                    161: html_free(void *p)
                    162: {
                    163:        struct tag      *tag;
                    164:        struct html     *h;
                    165:
                    166:        h = (struct html *)p;
                    167:
1.2       schwarze  168:        while ((tag = h->tags.head) != NULL) {
                    169:                h->tags.head = tag->next;
1.1       schwarze  170:                free(tag);
                    171:        }
                    172:
                    173:        if (h->symtab)
                    174:                chars_free(h->symtab);
                    175:
                    176:        free(h);
                    177: }
                    178:
                    179:
                    180: void
                    181: print_gen_head(struct html *h)
                    182: {
                    183:        struct htmlpair  tag[4];
                    184:
                    185:        tag[0].key = ATTR_HTTPEQUIV;
                    186:        tag[0].val = "Content-Type";
                    187:        tag[1].key = ATTR_CONTENT;
                    188:        tag[1].val = "text/html; charset=utf-8";
                    189:        print_otag(h, TAG_META, 2, tag);
                    190:
                    191:        tag[0].key = ATTR_NAME;
                    192:        tag[0].val = "resource-type";
                    193:        tag[1].key = ATTR_CONTENT;
                    194:        tag[1].val = "document";
                    195:        print_otag(h, TAG_META, 2, tag);
                    196:
                    197:        if (h->style) {
                    198:                tag[0].key = ATTR_REL;
                    199:                tag[0].val = "stylesheet";
                    200:                tag[1].key = ATTR_HREF;
                    201:                tag[1].val = h->style;
                    202:                tag[2].key = ATTR_TYPE;
                    203:                tag[2].val = "text/css";
                    204:                tag[3].key = ATTR_MEDIA;
                    205:                tag[3].val = "all";
                    206:                print_otag(h, TAG_LINK, 4, tag);
                    207:        }
                    208: }
                    209:
                    210:
                    211: static void
1.13      schwarze  212: print_spec(struct html *h, enum roffdeco d, const char *p, size_t len)
1.1       schwarze  213: {
1.13      schwarze  214:        int              cp;
1.1       schwarze  215:        const char      *rhs;
                    216:        size_t           sz;
                    217:
1.13      schwarze  218:        if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
                    219:                printf("&#%d;", cp);
                    220:                return;
                    221:        } else if (-1 == cp && DECO_SSPECIAL == d) {
                    222:                fwrite(p, 1, len, stdout);
                    223:                return;
                    224:        } else if (-1 == cp)
                    225:                return;
1.1       schwarze  226:
1.13      schwarze  227:        if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
                    228:                fwrite(rhs, 1, sz, stdout);
1.1       schwarze  229: }
                    230:
                    231:
                    232: static void
1.5       schwarze  233: print_res(struct html *h, const char *p, size_t len)
1.1       schwarze  234: {
1.13      schwarze  235:        int              cp;
1.1       schwarze  236:        const char      *rhs;
                    237:        size_t           sz;
                    238:
1.13      schwarze  239:        if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
                    240:                printf("&#%d;", cp);
                    241:                return;
                    242:        } else if (-1 == cp)
                    243:                return;
1.1       schwarze  244:
1.13      schwarze  245:        if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
                    246:                fwrite(rhs, 1, sz, stdout);
1.1       schwarze  247: }
                    248:
                    249:
1.5       schwarze  250: static void
                    251: print_metaf(struct html *h, enum roffdeco deco)
                    252: {
                    253:        enum htmlfont    font;
1.1       schwarze  254:
1.5       schwarze  255:        switch (deco) {
                    256:        case (DECO_PREVIOUS):
                    257:                font = h->metal;
                    258:                break;
                    259:        case (DECO_ITALIC):
                    260:                font = HTMLFONT_ITALIC;
                    261:                break;
                    262:        case (DECO_BOLD):
                    263:                font = HTMLFONT_BOLD;
                    264:                break;
                    265:        case (DECO_ROMAN):
                    266:                font = HTMLFONT_NONE;
                    267:                break;
                    268:        default:
                    269:                abort();
                    270:                /* NOTREACHED */
1.1       schwarze  271:        }
                    272:
1.20      schwarze  273:        if (h->metaf) {
                    274:                print_tagq(h, h->metaf);
                    275:                h->metaf = NULL;
                    276:        }
                    277:
                    278:        h->metal = h->metac;
                    279:        h->metac = font;
                    280:
                    281:        if (HTMLFONT_NONE != font)
                    282:                h->metaf = HTMLFONT_BOLD == font ?
                    283:                        print_otag(h, TAG_B, 0, NULL) :
                    284:                        print_otag(h, TAG_I, 0, NULL);
1.1       schwarze  285: }
                    286:
                    287:
1.5       schwarze  288: static int
                    289: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  290: {
1.4       schwarze  291:        size_t           sz;
1.5       schwarze  292:        int              len, nospace;
                    293:        const char      *seq;
                    294:        enum roffdeco    deco;
1.9       schwarze  295:        static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.5       schwarze  296:
                    297:        nospace = 0;
1.1       schwarze  298:
                    299:        for (; *p; p++) {
1.9       schwarze  300:                sz = strcspn(p, rejs);
1.4       schwarze  301:
                    302:                fwrite(p, 1, sz, stdout);
                    303:                p += /* LINTED */
                    304:                        sz;
                    305:
1.5       schwarze  306:                if ('<' == *p) {
                    307:                        printf("&lt;");
                    308:                        continue;
                    309:                } else if ('>' == *p) {
                    310:                        printf("&gt;");
                    311:                        continue;
                    312:                } else if ('&' == *p) {
                    313:                        printf("&amp;");
1.1       schwarze  314:                        continue;
1.9       schwarze  315:                } else if (ASCII_HYPH == *p) {
                    316:                        /*
                    317:                         * Note: "soft hyphens" aren't graphically
                    318:                         * displayed when not breaking the text; we want
                    319:                         * them to be displayed.
                    320:                         */
                    321:                        /*printf("&#173;");*/
                    322:                        putchar('-');
                    323:                        continue;
1.4       schwarze  324:                } else if ('\0' == *p)
                    325:                        break;
                    326:
1.5       schwarze  327:                seq = ++p;
                    328:                len = a2roffdeco(&deco, &seq, &sz);
                    329:
                    330:                switch (deco) {
                    331:                case (DECO_RESERVED):
                    332:                        print_res(h, seq, sz);
                    333:                        break;
1.13      schwarze  334:                case (DECO_SSPECIAL):
                    335:                        /* FALLTHROUGH */
1.5       schwarze  336:                case (DECO_SPECIAL):
1.13      schwarze  337:                        print_spec(h, deco, seq, sz);
1.5       schwarze  338:                        break;
                    339:                case (DECO_PREVIOUS):
                    340:                        /* FALLTHROUGH */
                    341:                case (DECO_BOLD):
                    342:                        /* FALLTHROUGH */
                    343:                case (DECO_ITALIC):
                    344:                        /* FALLTHROUGH */
                    345:                case (DECO_ROMAN):
                    346:                        if (norecurse)
                    347:                                break;
                    348:                        print_metaf(h, deco);
                    349:                        break;
                    350:                default:
                    351:                        break;
                    352:                }
                    353:
                    354:                p += len - 1;
                    355:
                    356:                if (DECO_NOSPACE == deco && '\0' == *(p + 1))
                    357:                        nospace = 1;
1.1       schwarze  358:        }
1.5       schwarze  359:
                    360:        return(nospace);
1.1       schwarze  361: }
                    362:
                    363:
1.6       schwarze  364: static void
                    365: print_attr(struct html *h, const char *key, const char *val)
                    366: {
                    367:        printf(" %s=\"", key);
                    368:        (void)print_encode(h, val, 1);
                    369:        putchar('\"');
                    370: }
                    371:
                    372:
1.1       schwarze  373: struct tag *
                    374: print_otag(struct html *h, enum htmltag tag,
                    375:                int sz, const struct htmlpair *p)
                    376: {
                    377:        int              i;
                    378:        struct tag      *t;
                    379:
1.6       schwarze  380:        /* Push this tags onto the stack of open scopes. */
                    381:
1.1       schwarze  382:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.3       schwarze  383:                t = malloc(sizeof(struct tag));
                    384:                if (NULL == t) {
                    385:                        perror(NULL);
1.16      schwarze  386:                        exit((int)MANDOCLEVEL_SYSERR);
1.3       schwarze  387:                }
1.1       schwarze  388:                t->tag = tag;
1.2       schwarze  389:                t->next = h->tags.head;
                    390:                h->tags.head = t;
1.1       schwarze  391:        } else
                    392:                t = NULL;
                    393:
                    394:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  395:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    396:                        /* Manage keeps! */
                    397:                        if ( ! (HTML_KEEP & h->flags)) {
                    398:                                if (HTML_PREKEEP & h->flags)
                    399:                                        h->flags |= HTML_KEEP;
                    400:                                putchar(' ');
                    401:                        } else
                    402:                                printf("&#160;");
                    403:                }
1.1       schwarze  404:
1.13      schwarze  405:        if ( ! (h->flags & HTML_NONOSPACE))
                    406:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  407:        else
                    408:                h->flags |= HTML_NOSPACE;
1.13      schwarze  409:
1.6       schwarze  410:        /* Print out the tag name and attributes. */
                    411:
1.1       schwarze  412:        printf("<%s", htmltags[tag].name);
1.6       schwarze  413:        for (i = 0; i < sz; i++)
                    414:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    415:
                    416:        /* Add non-overridable attributes. */
                    417:
                    418:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    419:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    420:                print_attr(h, "xml:lang", "en");
                    421:                print_attr(h, "lang", "en");
1.1       schwarze  422:        }
1.6       schwarze  423:
                    424:        /* Accomodate for XML "well-formed" singleton escaping. */
                    425:
                    426:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    427:                switch (h->type) {
                    428:                case (HTML_XHTML_1_0_STRICT):
                    429:                        putchar('/');
                    430:                        break;
                    431:                default:
                    432:                        break;
                    433:                }
                    434:
1.4       schwarze  435:        putchar('>');
1.1       schwarze  436:
                    437:        h->flags |= HTML_NOSPACE;
1.18      schwarze  438:
                    439:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    440:                putchar('\n');
                    441:
1.1       schwarze  442:        return(t);
                    443: }
                    444:
                    445:
                    446: static void
                    447: print_ctag(struct html *h, enum htmltag tag)
                    448: {
                    449:
                    450:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  451:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  452:                h->flags |= HTML_NOSPACE;
1.4       schwarze  453:                putchar('\n');
1.5       schwarze  454:        }
1.1       schwarze  455: }
                    456:
                    457:
                    458: void
1.6       schwarze  459: print_gen_decls(struct html *h)
                    460: {
                    461:
                    462:        print_xmltype(h);
                    463:        print_doctype(h);
                    464: }
                    465:
                    466:
                    467: static void
                    468: print_xmltype(struct html *h)
                    469: {
                    470:
1.9       schwarze  471:        if (HTML_XHTML_1_0_STRICT == h->type)
1.20      schwarze  472:                puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.6       schwarze  473: }
                    474:
                    475:
                    476: static void
                    477: print_doctype(struct html *h)
1.1       schwarze  478: {
1.6       schwarze  479:        const char      *doctype;
                    480:        const char      *dtd;
                    481:        const char      *name;
                    482:
                    483:        switch (h->type) {
                    484:        case (HTML_HTML_4_01_STRICT):
                    485:                name = "HTML";
                    486:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    487:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    488:                break;
                    489:        default:
                    490:                name = "html";
                    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:
                    496:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    497:                        name, doctype, dtd);
1.1       schwarze  498: }
                    499:
                    500:
                    501: void
1.12      schwarze  502: print_text(struct html *h, const char *word)
1.1       schwarze  503: {
                    504:
1.12      schwarze  505:        if (word[0] && '\0' == word[1])
                    506:                switch (word[0]) {
1.1       schwarze  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(']'):
                    522:                        if ( ! (HTML_IGNDELIM & h->flags))
                    523:                                h->flags |= HTML_NOSPACE;
                    524:                        break;
                    525:                default:
                    526:                        break;
                    527:                }
                    528:
1.12      schwarze  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.1       schwarze  538:
1.20      schwarze  539:        assert(NULL == h->metaf);
                    540:        if (HTMLFONT_NONE != h->metac)
                    541:                h->metaf = HTMLFONT_BOLD == h->metac ?
                    542:                        print_otag(h, TAG_B, 0, NULL) :
                    543:                        print_otag(h, TAG_I, 0, NULL);
                    544:
1.12      schwarze  545:        assert(word);
                    546:        if ( ! print_encode(h, word, 0))
1.13      schwarze  547:                if ( ! (h->flags & HTML_NONOSPACE))
                    548:                        h->flags &= ~HTML_NOSPACE;
1.20      schwarze  549:
                    550:        if (h->metaf) {
                    551:                print_tagq(h, h->metaf);
                    552:                h->metaf = NULL;
                    553:        }
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:
1.21    ! schwarze  770:        assert(sz > 2);
1.3       schwarze  771:
                    772:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    773:
1.21    ! schwarze  774:        /* We can't start with a number (bah). */
        !           775:
        !           776:        if ('#' == *dst) {
        !           777:                dst++;
        !           778:                sz--;
        !           779:        }
        !           780:        if ('\0' == *dst) {
        !           781:                *dst++ = 'x';
        !           782:                *dst = '\0';
        !           783:                sz--;
        !           784:        }
        !           785:
1.3       schwarze  786:        for ( ; *dst != '\0' && sz; dst++, sz--)
                    787:                /* Jump to end. */ ;
                    788:
                    789:        for ( ; *src != '\0' && sz > 1; src++) {
                    790:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
                    791:                sz -= ssz;
                    792:                dst += ssz;
                    793:        }
                    794: }