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

Annotation of mandoc/html.c, Revision 1.121

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

CVSweb