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

Annotation of mandoc/html.c, Revision 1.106

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

CVSweb