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

Annotation of mandoc/html.c, Revision 1.103

1.103   ! kristaps    1: /*     $Id: html.c,v 1.102 2010/06/19 20:46:27 kristaps Exp $ */
1.1       kristaps    2: /*
1.102     kristaps    3:  * Copyright (c) 2008, 2009 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.30      kristaps  396:                if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.78      kristaps  397:                        putchar(' ');
1.29      kristaps  398:
1.94      kristaps  399:        /* Print out the tag name and attributes. */
                    400:
1.29      kristaps  401:        printf("<%s", htmltags[tag].name);
1.94      kristaps  402:        for (i = 0; i < sz; i++)
                    403:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    404:
                    405:        /* Add non-overridable attributes. */
                    406:
                    407:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    408:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    409:                print_attr(h, "xml:lang", "en");
                    410:                print_attr(h, "lang", "en");
1.29      kristaps  411:        }
1.93      kristaps  412:
1.94      kristaps  413:        /* Accomodate for XML "well-formed" singleton escaping. */
                    414:
1.93      kristaps  415:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    416:                switch (h->type) {
                    417:                case (HTML_XHTML_1_0_STRICT):
                    418:                        putchar('/');
                    419:                        break;
                    420:                default:
                    421:                        break;
                    422:                }
                    423:
1.78      kristaps  424:        putchar('>');
1.14      kristaps  425:
1.29      kristaps  426:        h->flags |= HTML_NOSPACE;
1.30      kristaps  427:        return(t);
1.14      kristaps  428: }
                    429:
                    430:
1.29      kristaps  431: static void
                    432: print_ctag(struct html *h, enum htmltag tag)
1.14      kristaps  433: {
                    434:
1.29      kristaps  435:        printf("</%s>", htmltags[tag].name);
1.71      kristaps  436:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.29      kristaps  437:                h->flags |= HTML_NOSPACE;
1.78      kristaps  438:                putchar('\n');
1.87      kristaps  439:        }
1.14      kristaps  440: }
                    441:
                    442:
1.51      kristaps  443: void
1.93      kristaps  444: print_gen_decls(struct html *h)
1.1       kristaps  445: {
1.93      kristaps  446:
                    447:        print_xmltype(h);
                    448:        print_doctype(h);
                    449: }
                    450:
                    451:
                    452: static void
                    453: print_xmltype(struct html *h)
                    454: {
                    455:
1.100     kristaps  456:        if (HTML_XHTML_1_0_STRICT == h->type)
                    457:                printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.93      kristaps  458: }
                    459:
                    460:
                    461: static void
                    462: print_doctype(struct html *h)
                    463: {
                    464:        const char      *doctype;
                    465:        const char      *dtd;
1.96      kristaps  466:        const char      *name;
1.93      kristaps  467:
                    468:        switch (h->type) {
                    469:        case (HTML_HTML_4_01_STRICT):
1.96      kristaps  470:                name = "HTML";
1.93      kristaps  471:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    472:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    473:                break;
                    474:        default:
1.96      kristaps  475:                name = "html";
1.93      kristaps  476:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    477:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    478:                break;
                    479:        }
                    480:
1.96      kristaps  481:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    482:                        name, doctype, dtd);
1.1       kristaps  483: }
                    484:
                    485:
1.51      kristaps  486: void
1.29      kristaps  487: print_text(struct html *h, const char *p)
1.1       kristaps  488: {
                    489:
1.29      kristaps  490:        if (*p && 0 == *(p + 1))
                    491:                switch (*p) {
                    492:                case('.'):
                    493:                        /* FALLTHROUGH */
                    494:                case(','):
                    495:                        /* FALLTHROUGH */
                    496:                case(';'):
                    497:                        /* FALLTHROUGH */
                    498:                case(':'):
                    499:                        /* FALLTHROUGH */
                    500:                case('?'):
                    501:                        /* FALLTHROUGH */
                    502:                case('!'):
                    503:                        /* FALLTHROUGH */
                    504:                case(')'):
                    505:                        /* FALLTHROUGH */
                    506:                case(']'):
1.52      kristaps  507:                        if ( ! (HTML_IGNDELIM & h->flags))
                    508:                                h->flags |= HTML_NOSPACE;
1.30      kristaps  509:                        break;
1.29      kristaps  510:                default:
                    511:                        break;
                    512:                }
1.1       kristaps  513:
1.29      kristaps  514:        if ( ! (h->flags & HTML_NOSPACE))
1.78      kristaps  515:                putchar(' ');
1.30      kristaps  516:
1.86      kristaps  517:        assert(p);
1.88      kristaps  518:        if ( ! print_encode(h, p, 0))
1.86      kristaps  519:                h->flags &= ~HTML_NOSPACE;
1.8       kristaps  520:
1.98      kristaps  521:        /*
                    522:         * Note that we don't process the pipe: the parser sees it as
                    523:         * punctuation, but we don't in terms of typography.
                    524:         */
1.29      kristaps  525:        if (*p && 0 == *(p + 1))
                    526:                switch (*p) {
                    527:                case('('):
                    528:                        /* FALLTHROUGH */
                    529:                case('['):
                    530:                        h->flags |= HTML_NOSPACE;
1.30      kristaps  531:                        break;
1.29      kristaps  532:                default:
                    533:                        break;
                    534:                }
1.1       kristaps  535: }
1.30      kristaps  536:
                    537:
1.51      kristaps  538: void
1.30      kristaps  539: print_tagq(struct html *h, const struct tag *until)
                    540: {
                    541:        struct tag      *tag;
                    542:
1.66      kristaps  543:        while ((tag = h->tags.head) != NULL) {
1.89      kristaps  544:                if (tag == h->metaf)
                    545:                        h->metaf = NULL;
1.30      kristaps  546:                print_ctag(h, tag->tag);
1.66      kristaps  547:                h->tags.head = tag->next;
1.30      kristaps  548:                free(tag);
                    549:                if (until && tag == until)
                    550:                        return;
                    551:        }
                    552: }
                    553:
                    554:
1.51      kristaps  555: void
1.30      kristaps  556: print_stagq(struct html *h, const struct tag *suntil)
                    557: {
                    558:        struct tag      *tag;
                    559:
1.66      kristaps  560:        while ((tag = h->tags.head) != NULL) {
1.30      kristaps  561:                if (suntil && tag == suntil)
                    562:                        return;
1.89      kristaps  563:                if (tag == h->metaf)
                    564:                        h->metaf = NULL;
1.30      kristaps  565:                print_ctag(h, tag->tag);
1.66      kristaps  566:                h->tags.head = tag->next;
1.30      kristaps  567:                free(tag);
                    568:        }
                    569: }
1.55      kristaps  570:
                    571:
                    572: void
                    573: bufinit(struct html *h)
                    574: {
                    575:
                    576:        h->buf[0] = '\0';
                    577:        h->buflen = 0;
                    578: }
                    579:
                    580:
                    581: void
1.58      kristaps  582: bufcat_style(struct html *h, const char *key, const char *val)
                    583: {
                    584:
                    585:        bufcat(h, key);
                    586:        bufncat(h, ":", 1);
                    587:        bufcat(h, val);
                    588:        bufncat(h, ";", 1);
                    589: }
                    590:
                    591:
                    592: void
1.55      kristaps  593: bufcat(struct html *h, const char *p)
                    594: {
                    595:
                    596:        bufncat(h, p, strlen(p));
                    597: }
                    598:
                    599:
                    600: void
                    601: buffmt(struct html *h, const char *fmt, ...)
                    602: {
                    603:        va_list          ap;
                    604:
                    605:        va_start(ap, fmt);
1.56      kristaps  606:        (void)vsnprintf(h->buf + (int)h->buflen,
1.55      kristaps  607:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    608:        va_end(ap);
                    609:        h->buflen = strlen(h->buf);
                    610: }
                    611:
                    612:
                    613: void
                    614: bufncat(struct html *h, const char *p, size_t sz)
                    615: {
                    616:
                    617:        if (h->buflen + sz > BUFSIZ - 1)
                    618:                sz = BUFSIZ - 1 - h->buflen;
                    619:
                    620:        (void)strncat(h->buf, p, sz);
                    621:        h->buflen += sz;
                    622: }
                    623:
                    624:
                    625: void
                    626: buffmt_includes(struct html *h, const char *name)
                    627: {
                    628:        const char      *p, *pp;
                    629:
                    630:        pp = h->base_includes;
1.61      kristaps  631:
                    632:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  633:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  634:                switch (*(p + 1)) {
                    635:                case('I'):
                    636:                        bufcat(h, name);
                    637:                        break;
                    638:                default:
                    639:                        bufncat(h, p, 2);
                    640:                        break;
                    641:                }
                    642:                pp = p + 2;
                    643:        }
                    644:        if (pp)
                    645:                bufcat(h, pp);
                    646: }
                    647:
                    648:
                    649: void
                    650: buffmt_man(struct html *h,
                    651:                const char *name, const char *sec)
                    652: {
                    653:        const char      *p, *pp;
                    654:
                    655:        pp = h->base_man;
1.61      kristaps  656:
                    657:        /* LINTED */
                    658:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  659:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  660:                switch (*(p + 1)) {
                    661:                case('S'):
1.58      kristaps  662:                        bufcat(h, sec ? sec : "1");
1.55      kristaps  663:                        break;
                    664:                case('N'):
1.58      kristaps  665:                        buffmt(h, name);
1.55      kristaps  666:                        break;
                    667:                default:
                    668:                        bufncat(h, p, 2);
                    669:                        break;
                    670:                }
                    671:                pp = p + 2;
                    672:        }
                    673:        if (pp)
                    674:                bufcat(h, pp);
                    675: }
1.58      kristaps  676:
                    677:
                    678: void
                    679: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    680: {
1.62      kristaps  681:        double           v;
1.63      kristaps  682:        const char      *u;
1.58      kristaps  683:
                    684:        v = su->scale;
                    685:
                    686:        switch (su->unit) {
                    687:        case (SCALE_CM):
                    688:                u = "cm";
                    689:                break;
                    690:        case (SCALE_IN):
                    691:                u = "in";
                    692:                break;
                    693:        case (SCALE_PC):
                    694:                u = "pc";
                    695:                break;
                    696:        case (SCALE_PT):
                    697:                u = "pt";
                    698:                break;
1.59      kristaps  699:        case (SCALE_EM):
                    700:                u = "em";
                    701:                break;
1.58      kristaps  702:        case (SCALE_MM):
                    703:                if (0 == (v /= 100))
                    704:                        v = 1;
                    705:                u = "em";
                    706:                break;
1.59      kristaps  707:        case (SCALE_EN):
                    708:                u = "ex";
                    709:                break;
                    710:        case (SCALE_BU):
                    711:                u = "ex";
                    712:                break;
1.58      kristaps  713:        case (SCALE_VS):
                    714:                u = "em";
                    715:                break;
                    716:        default:
                    717:                u = "ex";
                    718:                break;
                    719:        }
                    720:
1.103   ! kristaps  721:        /*
        !           722:         * XXX: the CSS spec isn't clear as to which types accept
        !           723:         * integer or real numbers, so we just make them all decimals.
        !           724:         */
        !           725:        buffmt(h, "%s: %.2f%s;", p, v, u);
1.58      kristaps  726: }
1.65      kristaps  727:
1.68      kristaps  728:
                    729: void
1.70      kristaps  730: html_idcat(char *dst, const char *src, int sz)
1.68      kristaps  731: {
1.70      kristaps  732:        int              ssz;
1.68      kristaps  733:
                    734:        assert(sz);
                    735:
                    736:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    737:
1.70      kristaps  738:        for ( ; *dst != '\0' && sz; dst++, sz--)
1.68      kristaps  739:                /* Jump to end. */ ;
                    740:
1.70      kristaps  741:        assert(sz > 2);
1.68      kristaps  742:
1.70      kristaps  743:        /* We can't start with a number (bah). */
1.68      kristaps  744:
1.70      kristaps  745:        *dst++ = 'x';
1.68      kristaps  746:        *dst = '\0';
1.70      kristaps  747:        sz--;
                    748:
                    749:        for ( ; *src != '\0' && sz > 1; src++) {
1.73      kristaps  750:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70      kristaps  751:                sz -= ssz;
                    752:                dst += ssz;
                    753:        }
1.68      kristaps  754: }

CVSweb